首页 > 其他分享 >jieba分词

jieba分词

时间:2023-12-20 11:22:59浏览次数:28  
标签:elif rword word items jieba counts 分词

 

import jieba

txt = open("D:\\python\\西游记.txt", "r", encoding='ansi').read()
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 == "齐天大圣" or word == "猴子":
rword = "孙悟空"
elif word == "师父" or word == "三藏" or word == "圣僧":
rword = "唐僧"
elif word == "呆子" or word == "八戒" or word == "老猪":
rword = "猪八戒"
elif word == "沙和尚":
rword = "沙僧"
elif word == "妖精" or word == "妖魔" or 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,jieba,counts,分词
From: https://www.cnblogs.com/Housesnurse/p/17916123.html

相关文章

  • jieba分词《聊斋》
    importjiebatxt=open("聊斋志异白话简写版.txt","r",encoding='utf-8').read()words=jieba.lcut(txt)#使用精确模式对文本进行分词counts={}#通过键值对的形式存储词语及其出现的次数forwordinwords:iflen(word)==1:continueelif......
  • jieba分词——西游记相关的分词,出现次数最高的20个
    1importjieba23txt=open("D:\Pythonproject\Python123作业\西游记.txt","r",encoding='utf-8').read()4words=jieba.lcut(txt)#使用精确模式对文本进行分词5counts={}#通过键值对的形式存储词语及其出现的次数67forwordinwords:......
  • jieba 分词
    西游记相关的分词,出现次数最高的20个输入:1importjieba2excludes={"一个","我们","怎么","那里","不知","不是","只见","两个","不敢","这个","如何","原来","甚......
  • jieba分词
    尾号为1,2,3的同学做,西游记相关的分词,出现次数最高的20个。```importjieba#读取文本文件path="西游记.txt"file=open(path,"r",encoding="utf-8")text=file.read()file.close()#使用jieba分词words=jieba.lcut(text)#统计词频counts={}forwordin......
  • jieba 分词
    描述尾号为1,2,3的同学做,西游记相关的分词,出现次数最高的20个。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪......
  • jieba 分词红楼梦相关的分词,出现次数最高的20个
    点击查看代码importjiebaimportwordclouddeftakeSecond(elem):returnelem[1]defcreateWordCloud(text):#生成词云函数w=wordcloud.WordCloud(font_path="STZHONGS.TTF",width=1000,height=500,background_color="white")w.g......
  • jieba 分词西游记
    importjiebatxt=open("西游记.txt","r",encoding='utf-8').read()words=jieba.lcut(txt)counts={}forwordinwords:iflen(word)==1:continueelifword=="大圣"orword=="老孙"or......
  • jieba分词
    jieba分词‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬描述‪‬‪‬‪‬‪‬‪‬‮‬‪‬......
  • jieba分词之聊斋
    importjiebaexcludes={"不知","不可","一日","不敢","数日","以为","不能","可以","不得","如此","------------","三日","而已","明日","其中&qu......
  • jieba分词
    importjiebapath="all.txt"#读取文本文件file=open(path,"r",encoding="utf-8")text=file.read()file.close()words=jieba.lcut(text)#使用jieba分词counts={}#统计词频forwordinwords:iflen(word)==1:#过滤掉长度为1的词语......