CalThreeKingdomsV1.py:
#CalThreeKingdomsV1.py
import jieba
txt = open("threekingdoms.txt", "r", encoding = "utf-8").read()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word, 0) + 1
items = list(counts.items())
items.sort(key = lambda x:x[1], reverse = True)
for i in range(15):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
运行结果:
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\ZHUYUA~1\AppData\Local\Temp\jieba.cache
Loading model cost 1.086 seconds.
Prefix dict has been built successfully.
曹操 953
孔明 836
将军 772
却说 656