聊斋
作者:互联网
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))
标签:elif,rword,word,items,聊斋,counts,txt 来源: https://www.cnblogs.com/TanRuiQuan/p/15678445.html