看悬疑推理小说,比较重要的就是理清人物关系。所以在练习networkx库时,我选择了之前看的《清明上河图密码》。然后在练习过程中,遇到了中文标签无法显示的问题(在此记录解决方法)。
创建词频文件,筛选主要人物
import os
import jieba
os.chdir("F:\\book")
def get_text(filepath):
f = open(filepath,'r',encoding="utf-16")
text = f.read()
f.close()
return text
def word_freq(filepath,text,topn):
words = jieba.lcut(text.strip())
counts = {
}
for word in words:
if len(word) ==1:
continue
counts[word] = counts.get(word,0)+1
items = list(counts.items())
items.sort(key = lambda x:x[1],reverse=True )
f = open(filepath[:-4]