利用结巴分词的Python版本实现分词并统计词频
来源:互联网 发布:扬州网络电视台节目单 编辑:程序博客网 时间:2024/06/04 17:43
最近在做文本处理,统计词频这个东西大家都熟悉了,直接贴代码方便以后找吧
Python 版本
Python 3.5.2 |Anaconda 4.2.0 (64-bit)|
#!/usr/bin/python# -*- coding:utf-8 -*-import sysimport importlibimportlib.reload(sys)import jiebaimport jieba.analyseimport xlwt # 写入Excel表的库if __name__ == "__main__": wbk = xlwt.Workbook(encoding='ascii') sheet = wbk.add_sheet("wordCount") # Excel单元格名字 word_lst = [] key_list = [] for line in open('1.txt',encoding= 'utf-8'): # 1.txt是需要分词统计的文档 item = line.strip('\n\r').split('\t') # 制表格切分 # print item tags = jieba.analyse.extract_tags(item[0]) # jieba分词 for t in tags: word_lst.append(t) word_dict = {} with open("wordCount.txt", 'w') as wf2: # 打开文件 for item in word_lst: if item not in word_dict: # 统计数量 word_dict[item] = 1 else: word_dict[item] += 1 orderList = list(word_dict.values()) orderList.sort(reverse=True) # print orderList for i in range(len(orderList)): for key in word_dict: if word_dict[key] == orderList[i]: wf2.write(key + ' ' + str(word_dict[key]) + '\n') # 写入txt文档 key_list.append(key) word_dict[key] = 0 for i in range(len(key_list)): sheet.write(i, 1, label=orderList[i]) sheet.write(i, 0, label=key_list[i]) wbk.save('wordCount.xls') # 保存为 wordCount.xls文件
需要在python程序同级目录新建三个文件
- 1.txt
- wordCount.txt
- wordCount.xls
1.txt存放待处理文本,其余两个空白即可
阅读全文
0 0
- 利用结巴分词的Python版本实现分词并统计词频
- python3结巴分词分行拆分统计词频
- Python分词并进行词频统计
- Python利用结巴分词进行中文分词
- Python分词:结巴分词的安装使用
- python中文分词:结巴分词
- IKanalyzer分词实例并统计词频
- Python jieba 中文分词与词频统计
- python结巴分词
- python 结巴分词
- Python--结巴分词
- python结巴分词
- Python---结巴分词介绍
- 分词统计词频
- python jieba分词(结巴分词)、提取词,加载词,修改词频,定义词库
- python 结巴分词的具体应用
- python结巴分词器的使用说明
- “结巴”分词:做最好的Python分词组件
- 《招聘一个靠谱的iOS》面试题及参考答案(上)--转
- display:none与visible:hidden的区别
- FZU 1046 Tempter of the Bone
- Linux环境下Strom单机安装详解
- 数组归并排序
- 利用结巴分词的Python版本实现分词并统计词频
- Android Glide图片加载框架详解
- stm32 rtc 误差实验
- Codeforces Round #423 C. String Reconstruction
- IIS应用程序池自动回收问题的有效解决办法
- Java 常用的时间算法 返回时间集合 时间差 返回加N天后的日期 判断是否同一天
- Java开发中的23种设计模式详解
- 【Deep Learning】相关杂文
- Linux命令行——free命令详解