jieba分词
import jieba
with open ("D:\红楼梦.txt",encoding="ANSI") as file:
f = file.read()
del_list ={"什么","一个","我们","那里","如今","你们","说道","起来","姑娘","这里","出来","他们","众人","自己","一面","只见","两个","怎么","没有","不是","不知",
"听见","这个","这样","进来","咱们","就是","东西","告诉","回来","袭人","只是","大家","只得","丫头","这些","不敢","出去","所以","不过","不好","一时","鸳鸯","姐姐",
"过来","不能","心里","银子","如此","答应","今日","几个","二人","还有","只管","一回","说话","这么","那边","这话","外头","打发","自然","今儿","罢了","那些","小丫头",
"妹妹","屋里","如何","听说","问道","知道","太太","的话","人家","媳妇","看见","家里","不用","家里","一声","不得","一句","原来","这会子","到底","进去","姊妹","过去",
"方才","回去","别人","连忙","心中","哥哥","婆子","不成","丫鬟","里头","还是","小厮","只有","一件","明白","身上","有人","起身","已经","一日","那个","于是","果然"}
hlm_word_ls = jieba.lcut(f)
cnts = {}
for word in hlm_word_ls:
if len(word) == 1:
continue
elif word =="林黛玉" or word =="黛玉道":
rword ="黛玉"
elif word == "凤姐儿":
rword = "凤姐"
else:
rword = word
cnts[rword]= cnts.get(rword,0)+1
for i in del_list:
del cnts[i]
items = list(cnts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(20):
word,cnt = items[i]
print("{0:<10}{1:>5}".format(word,cnt))