Python jieba 中文分词与词频统计的操作

这篇文章主要介绍了Python jieba 中文分词与词频统计的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

我就废话不多说了,大家还是直接看代码吧~

 #! python3 # -*- coding: utf-8 -*- import os, codecs import jieba from collections import Counter def get_words(txt): seg_list = jieba.cut(txt) c = Counter() for x in seg_list: if len(x)>1 and x != '\r\n': c[x] += 1 print('常用词频度统计结果') for (k,v) in c.most_common(100): print('%s%s %s %d' % (' '*(5-len(k)), k, '*'*int(v/3), v)) if __name__ == '__main__': with codecs.open('19d.txt', 'r', 'utf8') as f: txt = f.read() get_words(txt)

样本:十九大报告全文

 常用词频度统计结果 发展 ********************************************************************** 212 中国 ******************************************************** 168 人民 **************************************************** 157 建设 ************************************************* 148 社会主义 ************************************************ 146 坚持 ******************************************* 130 国家 ****************************** 90 全面 ***************************** 88 制度 *************************** 83 实现 *************************** 83 推进 *************************** 81 政治 ************************** 80 社会 ************************** 80 特色 ************************** 79 加强 *********************** 71 体系 ********************** 68 文化 ********************** 66 我们 ********************* 64 时代 ********************* 63 必须 ******************** 61 经济 ******************* 59 伟大 ******************* 58 完善 ***************** 51 我国 **************** 50 推动 *************** 47 现代化 *************** 47 安全 *************** 46 更加 ************** 44 民主 ************** 44 

补充:jieba读取txt文档并进行分词、词频统计,输出词云图

代码实现

 # 库的引用 import jieba import matplotlib as mpl import matplotlib.pyplot as plt from wordcloud import WordCloud #定义一个空字符串 final = "" #文件夹位置 filename = r"D:\python\pra\推荐系统1-500.txt" #打开文件夹,读取内容,并进行分词 with open(filename,'r',encoding = 'utf-8') as f: for line in f.readlines(): word = jieba.cut(line) for i in word: final = final + i +" " 

运行结果

 # 图云打印 word_pic = WordCloud(font_path = r'C:\Windows\Fonts\simkai.ttf',width = 2000,height = 1000).generate(final) plt.imshow(word_pic) #去掉坐标轴 plt.axis('off') #保存图片到相应文件夹 plt.savefig(r'D:\python\pra\6.png-600')

图云输出图

以上为个人经验,希望能给大家一个参考,也希望大家多多支持html中文网。如有错误或未考虑完全的地方,望不吝赐教。

以上就是Python jieba 中文分词与词频统计的操作的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python