import jieba
txt = open("聊斋志异白话简写版.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt) # 使用精确模式对文本进行分词
counts = {} # 通过键值对的形式存储词语及其出现的次数
for word in words:
if len(word) == 1:
continue
elif word == "小倩" or word == "鬼妻":
rword = "聂小倩"
elif word == "采臣":
rword = "唐僧"
elif word == "黑山" or word=="万妖群魔之首":
rword = "黑山老妖"
elif word=="十四娘":
rword="辛十四娘"
elif word == "子楚":
rword = "孙子楚"
elif word=="赵阿宝":
rword="阿宝"
else:
rword = word
counts[rword] = counts.get(rword,0) + 1
items = list(counts.items()) #将键值对转换成列表
items.sort(key=lambda x: x[1], reverse=True) # 根据词语出现的次数进行从大到小排序
for i in range(20):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))