天天看點

中文詞頻統計

作業要求來源: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. 生成詞雲:

中文詞頻統計