当前位置:   article > 正文

[Python自然语言处理] 入门 —— jieba库入门_python jieba csdn

python jieba csdn

作者链接:zhaozhengcoder

1. 什么是jiaba库

jieba 是一个python实现的分词库,对中文有着很强大的分词能力。
git链接:https://github.com/fxsjy/jieba

2. jieba库的优点

1 支持三种分词模式:
a. 精确模式,试图将句子最精确地切开,适合文本分析;
b. 全模式,把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不能解决歧义;
c. 搜索引擎模式,在精确模式的基础上,对长词再次切分,提高召回率,适合用于搜索引擎分词。

2 支持自定义词典

3. 关于jiaba库的基本功能

demo1:分词

#coding:utf-8
import jieba

words=jieba.cut("他来到了网易杭研大厦")
print "/".join(words)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

执行

demo2 : 加入自定义字典

#coding:utf-8
import jieba

jieba.load_userdict("dict.txt")
words=jieba.cut("他来到了网易杭研大厦")
print "/".join(words)
print type(words)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

自定义的词典 dict.txt

杭研大厦 100 n

  
  
   
   
  
  
  • 1

自定义的词典一行上面有三列,第二个是指出现的次数,第三个是词性

运行结果:


也就是说杭研大厦被看作为了一个整体。

demo3:允许程序在运行的时候,动态的修改词典

#coding:utf-8
import jieba
words =jieba.cut("我们中出了一个叛徒",HMM=False)
#jieba.suggest_freq(('中出'),True)
print '/'.join(words)
  • 1
  • 2
  • 3
  • 4
  • 5

使用 suggest_freq(segment, tune=True) 可调节单个词语的词频,使其(或不能)被分出来。
注意:自动计算的词频在使用 HMM 新词发现功能时可能无效。

#coding:utf-8
import jieba
words =jieba.cut("我们中出了一个叛徒",HMM=False)
jieba.suggest_freq(('中出'),True)
#jieba.suggest_freq(('中','出'),True)
print '/'.join(words)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

demo4:词性标注

#coding:utf-8
import jieba.posseg as pseg 

words=pseg.cut("我爱北京天安门")
for word ,flag in words:
    print ('%s %s' %(word,flag))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

demo5:三种模式的分词

# encoding=utf-8
import jieba

seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
print("Full Mode: " + "/ ".join(seg_list))  # 全模式

seg_list = jieba.cut("我来到北京清华大学", cut_all=False)
print("Default Mode: " + "/ ".join(seg_list))  # 精确模式

seg_list = jieba.cut("他来到了网易杭研大厦")  # 默认是精确模式
print(", ".join(seg_list))

seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所,后在日本京都大学深造")  # 搜索引擎模式
print(", ".join(seg_list))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

运行结果:


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/344052
推荐阅读
相关标签
  

闽ICP备14008679号