中文词频统计
1. 下载一长篇中文小说。
2. 从文件读取待分析文本。
3. 安装并使用jieba进行中文分词。
pip install jieba
import jieba
ljieba.lcut(text)
4. 更新词库,加入所分析对象的专业词汇。
jieba.add_word('天罡北斗阵') #逐个添加
jieba.load_userdict(word_dict) #词库文本文件
参考词库下载地址:https://pinyin.sogou.com/dict/
转换代码:scel_to_text
5. 生成词频统计
from wordcloud import WordCloud import matplotlib.pyplot as plt import jieba txt=open(r'hhh.txt','r',encoding='utf-8').read() s=[line.strip() for line in open('stops_chinese.txt',encoding='utf-8').readlines()] jieba.load_userdict(s) wordcut = jieba.lcut(txt) wdict = {} for word in wordcut: if word not in s: if len(word)==1: continue else: wdict[word]=wdict.get(word,0)+1 wc=list(wdict.items()) wc.sort(key=lambda x:x[1], reverse=True) for i in range(25): print(wc[i]) cut_text = " ".join(wordcut) 'print(cut_text)' mywc = WordCloud().generate(cut_text) plt.imshow(mywc) plt.axis("off") plt.show()