python从入门到大神---Python的jieba模块简介
一、总结
一句话总结:
jieba包是分词技术,也就是将一句话分成多个词,有多种分词模型可选
1、分词模块包一般有哪些分词模式(比如python的jieba包分'我想和女朋友一起去北京天安门闲逛。。')?
精确模式:jieba.cut(s):词只分一次:我,想,和,女朋友,一起,去,北京,天安门,闲逛,。,。
全模式:jieba.cut(s,cut_all = True):尽量将所有的词拿出来:我,想,和,女朋友,朋友,一起,去,北京,天安,天安门,闲逛,,,
搜索引擎模式:jieba.cut_for_search(s):我,想,和,朋友,女朋友,一起,去,北京,天安,天安门,闲逛,。,。
2、分词模块包比如jieba,可以查看词的词性么?
可以:x.flag:print([(x.word,x.flag) for x in psg.cut(s)])
二、Python的jieba模块简介
转自或参考:Python的jieba模块简介
https://www.cnblogs.com/yuxuanlian/p/9781792.html
现如今,词云技术遍地都是,分词模块除了jieba也有很多,主要介绍一下jieba的基本使用
importjiebaimportjieba.posseg as psgfrom os importpathfrom collections importCounter
s=u'我想和女朋友一起去北京天安门闲逛。。'cut=jieba.cut(s)print('精确模式')print(cut)print(','.join(cut))print ('全模式')print(','.join(jieba.cut(s,cut_all =True)))print('搜索引擎模式')print(','.join(jieba.cut_for_search(s)))print('词性')print([(x.word,x.flag) for x inpsg.cut(s)])print([(x.word,x.flag) for x in psg.cut(s) if x.flag.startswith('n')])print('--*--'*10)
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))
d=path.dirname(__file__)
sanguo_text=open(path.join(d,"data//sanguo.txt"),encoding='utf-8').read()print(len(sanguo_text))
sanguo_words= [x for x in jieba.cut(sanguo_text) if len(x) >= 2]
c= Counter(sanguo_words).most_common(20)print(c)
运行结果
其中精确模式比较好用,全模式就是尽量将所有的词拿出来
D:softwarecodingpythonpython.exe E:/Coding_Folder/python/first_test/jieba_tset.py
精确模式Building prefix dictfromthe default dictionary ...
Loading modelfrom cache C:UsersFANREN~1AppDataLocalTempjieba.cache
Loading model cost0.706seconds.
Prefix dict has been built succesfully.
我,想,和,女朋友,一起,去,北京,天安门,闲逛,。,。
全模式
我,想,和,女朋友,朋友,一起,去,北京,天安,天安门,闲逛,,,
搜索引擎模式
我,想,和,朋友,女朋友,一起,去,北京,天安,天安门,闲逛,。,。
词性
[('我', 'r'), ('想', 'v'), ('和', 'c'), ('女朋友', 'n'), ('一起', 'm'), ('去', 'v'), ('北京', 'ns'), ('天安门', 'ns'), ('闲逛', 'v'), ('。', 'x'), ('。', 'x')]
[('女朋友', 'n'), ('北京', 'ns'), ('天安门', 'ns')]--*----*----*----*----*----*----*----*----*----*--Full Mode: 我/ 来到/ 北京/ 清华/ 清华大学/ 华大/大学
Default Mode: 我/ 来到/ 北京/清华大学
他, 来到, 了, 网易, 杭研, 大厦
小明, 硕士, 毕业, 于, 中国, 科学, 学院, 科学院, 中国科学院, 计算, 计算所, ,, 后, 在, 日本, 京都, 大学, 日本京都大学, 深造