中文詞頻統計

做業要求來源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2773

1. 下載一長篇中文小說。

2. 從文件讀取待分析文本。

3. 安裝並使用jieba進行中文分詞。

4. 更新詞庫,加入所分析對象的專業詞彙。

5. 生成詞頻統計、排序、排除語法型詞彙,代詞、冠詞、連詞等停用詞、輸出詞頻最大TOP20,把結果存放到文件裏

源代碼:

import jieba
fo = open(r"d:/三體.txt",encoding="utf-8")
santi_txt = fo.read()
jieba.load_userdict(r"d:/stWord.txt") #添加專業詞彙詞庫
fo2 = open(r"d:/stops_chinese1.txt",encoding="utf-8")
cnStops = fo2.read()
cnStops = cnStops.split("\n")
st=[]
ss=set(cnStops)

for d in jieba.cut_for_search(santi_txt):
    st.append(d)

for n in a:
    g.update(n)
    g[n]=santi_txt.count(n)
    print("{}  :  {}".format(n,santi_txt.count(n)))


for w in st:
    isStop = False
    for a in cnStops:
        if w==a:
            isStop=True
    if isStop==True:
        st.remove(w)
santi_Txt =' '.join(wordlist)  
wordCount = WordCloud().generate(wl_split)
santi_txt.sort(key=lambda x: x[1], reverse=True)  # 列表排序
print(santi_txt[0:20])  #輸出top20
plt.imshow(wordCount)
plt.axis("off")
plt.show()

結果:

6. 生成詞雲:

相關文章
相關標籤/搜索