def getText("红楼梦.txt"):
f = open("红楼梦.txt","r",encoding="utf-8")
text = f.read()
f.close()
return text
import jieba
def wordFreq(红楼梦.txt,text,200):
words = jieba.lcut(text.strip())
counts = {}
for word in words:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key = lambda x:x[1],reverse = true)
f = open(filepath[:-4]+'_词频.txt',"w")
for i in range(topn):
word, count = items[i]
f.writelines("{}\t{}\n".format(word,count))
f.ciose()
有我在,不要哭 嗯,你把文件发给我,我看一下