中文詞頻統計

#源代碼
import jieba
# 加載分析的文本三國演義
file=open('sanguo.txt','r',encoding='utf-8')
test=file.read()
# 加載要過濾的詞語
fileStops=open('stops_chinese1.txt','r',encoding='utf-8')
testStops=fileStops.read()
wordStops=jieba.lcut(testStops)
print("要過濾的詞語:")
print(wordStops)
# 加載詞庫
jieba.load_userdict('san.txt')
words=jieba.lcut(test)
wcdict={}
for word in words:
    if len(word)==1:
        continue
    else:
        wcdict[word]=wcdict.get(word,0)+1
wcls=list(wcdict.items())
wcls.sort(key=lambda  x:x[1],reverse=True)
i=0
while i< len(wcls):
    if wcls[i][0] in wordStops: #查找單詞列表是否在要過濾的單詞表中,有的,從單詞列表中去掉該單詞
        wcls.remove(wcls[i])
        if i!=0: #由於去掉單詞後,列表內容會前移一位,因此索引要減一
            i=i-1
    else:
        i=i+1


print("三國演義關鍵詞top25:")
for i in range(25):
    print(wcls[i])

 

 
 
 

 如下是三國演義的詞庫,將其引入:spa

要分析的文本,《三國演義》:3d

要過濾的詞語:code

運行結果圖:blog

由於運行wordcloud總是出現下面錯誤,就用網上的在線詞雲。索引

相關文章
相關標籤/搜索