首页 > 其他 > 详细

使用jieba库寻找相同词

时间:2019-04-03 23:43:34      阅读:138      评论:0      收藏:0      [点我收藏+]

一、工具

先安装好jieba库

技术分享图片

下载好你需要的txt文件

技术分享图片

(注:文件必须改为utf-8编码)

二、代码

 

 

import jieba
txt = open(r‘D:\\三国演义 (2).txt‘, "r", encoding=‘utf-8‘).read()
words  = jieba.lcut(txt)
counts = {}
for word in words:
    if len(word) == 1:  #排除单个字符的分词结果
        continue
    else:
        counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True) 
for i in range(10):
    word, count = items[i]
    print ("{0:<10}{1:>5}".format(word, count))

三、运行结果技术分享图片

 

使用jieba库寻找相同词

原文:https://www.cnblogs.com/shinawear/p/10652400.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!