Python分词统计
2016-01-25 22:38
791 查看
利用Python切片处理文本非常方便,下面是一个简单的例子,进行分词统计
(需要读取的文件为utf-8编码,运行环境为Windows,版本为python3)
(需要读取的文件为utf-8编码,运行环境为Windows,版本为python3)
# -*- coding: utf-8 -*- import re import os Total = 0; #总字母数 words = [] #获取所有单词 readfile = open('Data.txt', encoding = 'utf-8') for line in readfile.readlines(): lineArr = line.strip().split() for word in lineArr: data = re.findall(r'[a-zA-Z]*', word) for w in data: if w != '': words.append(w.lower()) readfile.close() #进行统计 def MySta(n): Dic = {} total_num = 0 for word in words: for i in range(len(word) - n + 1): letter = word[i:i+n] total_num = total_num + 1 if letter in Dic.keys(): Dic[letter] = Dic[letter] + 1 else: Dic[letter] = 1 return Dic,total_num #输出前n个和后n个高频字母、统计频数、统计频率 def PrintSta(Dic, total_num, n): if n > len(Dic): print('n超出索引范围') return word_lst = [] for word, freq in Dic.items(): word_lst.append((freq, word)) word_lst.sort(reverse = True) print('*-----------------------------------------*') print('字母组合\t频数\t频率') for freq, word in word_lst[:n]: print('{0}\t{1}\t{2:.5}'.format(word, freq, freq/total_num)) for freq, word in word_lst[-n:]: print('{0}\t{1}\t{2:.5}'.format(word, freq, freq/total_num)) os.system("pause")
相关文章推荐
- Python遥感数据主成分分析
- python打印等腰三角形
- 让python在hadoop上跑起来
- python下文件的批量复制
- Python基本代码
- Python -- 装饰器
- python小白-day4迭代器和生成器
- 《笨办法学Python》 第23课手记
- 【python脚本收集】全角半角转换
- python,numpy matplotlib WIN7 64位 安装教程
- Cisco Syslog 处理
- 【Python】抓取拉勾网全国Python的招聘信息
- python调试
- python 条件判断与循环
- python数字图像处理(15):霍夫线变换
- python 文件操作
- Python学习之--python概要
- python collections 系列
- Python、PIP环境变量的配置
- 让python在hadoop上跑起来