import jieba
txt=open("D:/系统文件/桌面/FangYY/Pytho/新建文件夹/红楼梦.txt","rt",encoding="utf-8").read()
excludes={"什么","一个","我们","那里","你们","如今","说道","不过","二人","还有","只管","这么","说话","心里","如此","今日", "银子","几个","答应","不能","过来","一时","回来","姐姐","不好","的话","老爷","只得","老爷","丫头","这些","不敢","出去", "所以","知道","起来","奶奶","这里","出来","他们","众人","自己","一面","大家","告诉","只是","东西","就是","一回","自然", "今儿","罢了","屋里","那边","这话","外头","打发","只见","怎么","两个","没有","不是","那些","听说","小丫头","不知","这个","如何","媳妇","人家","不用","问道","听见","看见","这样","妹妹","进来","咱们"}
words=jieba.lcut(txt)
counts={}
for word in words:
if len(word)==1:
continue
elif word=="林姑娘" or word=="林妹妹" or word=="林丫头" or word=="大姑娘" or word=="玉儿" or word=="你妹妹" or word=="姑娘" or word=="黛玉":
rword="林黛玉"
counts[rword] = counts.get(rword, 0) + 1
elif word=="宝姑娘" or word=="宝姐姐" or word=="宝丫头" or word=="薛姑娘" or word=="宝钗":
rword="薛宝钗"
counts[rword] = counts.get(rword, 0) + 1
elif word=="宝玉":
rword="贾宝玉"
counts[rword] = counts.get(rword, 0) + 1
elif word=="凤姐" or word=="凤姐儿":
rword="王熙凤"
counts[rword] = counts.get(rword, 0) + 1
elif word=="老太太" :
rword="贾母"
counts[rword] = counts.get(rword, 0) + 1
elif word=="太太" :
rword="王夫人"
counts[rword] = counts.get(rword, 0) + 1
elif word=="二爷" :
rword="贾琏"
counts[rword] = counts.get(rword, 0) + 1
else:
rword=word
counts[rword]=counts.get(rword,0)+1
for word in excludes:
del(counts[word])
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(20):
word,count=items[i]
print(word,count)
jieba 分词(红楼梦相关的分词,出现次数最高的20个)
最新推荐文章于 2024-08-14 14:19:06 发布