#源代碼
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])
以下是三國演義的詞庫,将其引入:

要分析的文本,《三國演義》:
要過濾的詞語:
運作結果圖:
因為運作wordcloud老是出現下面錯誤,就用網上的線上詞雲。