如何在python中使用jieba库

发布时间:2021-03-05 17:35:46 作者:Leah
来源:亿速云 阅读:441

这篇文章将为大家详细讲解有关如何在python中使用jieba库,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

python可以做什么

Python是一种编程语言,内置了许多有效的工具,Python几乎无所不能,该语言通俗易懂、容易入门、功能强大,在许多领域中都有广泛的应用,例如最热门的大数据分析,人工智能,Web开发等。

一、jieba库概述

jieba是优秀的中文分词第三方库

二、jieba库安装

pip install jieba

三、jieba分词的原理

jieba分词依靠中文词库

四、jieba分词的3种模式

五、jieba库常用函数

函数描述
jieba.lcut(s)精确模式,返回一个列表类型的分词结果
jieba.lcut(s,cut_all=True)全模式,返回一个列表类型的分词结果,存在冗余
jieba.lcut_for_search(s)搜索引擎模式,返回一个列表类型的分词结果,存在冗余
jieba.lcut(s)精确模式,返回一个列表类型的分词结果
jieba.add_word(s)向分词词典增加新词w

例子:

>>> jieba.lcut("中国是一个伟大的国家")
['中国', '是', '一个', '伟大', '的', '国家']

>>> jieba.lcut("中国是一个伟大的国家", cut_all=True)
['中国', '国是', '一个', '伟大', '的', '国家']

>>> jieba.lcut_for_search("中华人民共和国是伟大的")
['中华', '华人', '人民', '共和', '共和国', '中华人民共和国', '是', '伟大', '的']

六、文本词频示例

问题分析

https://python123.io/resources/pye/hamlet.txt

https://python123.io/resources/pye/threekingdoms.txt

代码如下:

def getText():
 # 打开 hamlet.txt 这个文件
 txt = open("hamlet.txt", "r").read()
 # 避免大小写对词频统计的干扰,将所有单词转换为小写
 txt = txt.lower()
 # 将文中出现的所有特殊字符替换为空格
 for ch in '|"#$%^&*()_+-=\\`~{}[];:<>?/':
 txt = txt.replace(ch, " ")
 # 返回一个所以后单词都是小写的,单词间以空格间隔的文本
 return txt

hamletTxt = getText()
# split() 默认使用空格作为分隔符
words = hamletTxt.split()
counts = {}
for word in words:
 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))

上面代码中的

items.sort(key=lambda x:x[1], reverse=True)

是根据单词出现的次数进行排序,其中使用了 lambda 函数。更多解释请看:
https://www.runoob.com/python/att-list-sort.html

下面使用 jieba 库来统计《三国演义》中任务出场的次数:

import jieba
txt = open("threekingdoms.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(15):
 word, count = items[i]
 print("{0:<10}{1:>5}".format(word,count))

运行结果:

曹操  953
孔明  836
将军  772
却说  656
玄德  585
关公  510
丞相  491
二人  469
不可  440
荆州  425
玄德曰  390
孔明曰  390
不能  384
如此  378
张飞  358

我们可以看到得出的结果与我们想象的有些差异,比如

所以我们需要对上面代码进行优化,在词频统计的基础上,面向问题改造我们的程序。

下面是《三国演义》人物数量统计代码的升级版,升级版中对于某些确定不是人名的词,即使做了词频统计,也要将它删除掉。使用寄一个集合excludes来接收一些确定不是人名但是又排序比较靠前的单词列进去。

import jieba
txt = open("threekingdoms.txt","r",encoding="utf-8").read()
excludes = {"将军","却说","荆州","二人","不可","不能","如此"}
words = jieba.lcut(txt)
counts = {}
for word in words:
 if len(word) == 1:
 continue
 elif word == "诸葛亮" or word == "孔明曰":
 rword == "孔明"
 elif word == "关公" or word == "云长":
 rword == "关羽"
 elif word == "玄德" or word == "玄德曰":
 rword == "刘备"
 elif word == "孟德" or 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(15):
 word, count = items[i]
 print("{0:<10}{1:>5}".format(word,count))

运行结果:

曹操  963
孔明  847
张飞  366
商议  359
如何  352
主公  340
军士  320
吕布  303
左右  298
军马  297
赵云  283
刘备  282
引兵  279
次日  278
大喜  274

可以看出还是有像“商议”、“如何”等不是人物的词出现在统计结果,我们将这些词加入到 excludes 中,多次运行程序后最后得到《三国演义》任务出场顺序前20:

关于如何在python中使用jieba库就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. jieba库
  2. 如何在python中安装jieba库

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python jieba

上一篇:使用Pandas怎么筛选特定的字符

下一篇:使用Python怎么批量获取基金数据

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》