python 分词库jieba
2017-09-07 19:09
232 查看
算法实现:
基于Trie树结构实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图(DAG)
采用了动态规划查找最大概率路径, 找出基于词频的最大切分组合
对于未登录词,采用了基于汉字成词能力的HMM模型,使用了Viterbi算法
支持三种分词模式:
a,精确模式,试图将句子最精确地切开,适合文本分析;
b,全模式,把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不能解决歧义;
c,搜索引擎模式,在精确模式的基础上,对长词再次切分,提高召回率,适合用于搜索引擎分词
#!/usr/bin/python #coding=utf-8 #__author__='dahu' #data=2017- # 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 "Search Mode:","/ ".join(seg_list)
/usr/bin/python2.7 /home/dahu/myfile/jieba.test/t1.py Full Mode:Building prefix dict from the default dictionary ... Loading model from cache /tmp/jieba.cache 我/ 来到/ 北京/ 清华/ 清华大学/ 华大/ 大学 Loading model cost 0.186 seconds. Default Mode: 我/ 来到/ 北京/ 清华大学 Prefix dict has been built succesfully. 他, 来到, 了, 网易, 杭研, 大厦 Search Mode: 小明/ 硕士/ 毕业/ 于/ 中国/ 科学/ 学院/ 科学院/ 中国科学院/ 计算/ 计算所/ ,/ 后/ 在/ 日本/ 京都/ 大学/ 日本京都大学/ 深造 Process finished with exit code 0
添加自定义词典
开发者可以指定自己自定义的词典,以便包含jieba词库里没有的词。虽然jieba有新词识别能力,但是自行添加新词可以保证更高的正确率
用法:
jieba.load_userdict(file_name) # file_name为自定义词典的路径
#!/usr/bin/python #coding=utf-8 #__author__='dahu' #data=2017- # import time import sys reload(sys) sys.setdefaultencoding("utf-8") import jieba jieba.enable_parallel(4) # content = open('file',"r").read() content = open('tmp',"r").read() t1 = time.time() words = list(jieba.cut(content)) t2 = time.time() tm_cost = t2-t1 with open("4.log","w") as f: for w in words: f.write(w.encode("utf-8")+ " ") print 'speed',len(content)/tm_cost, "bytes/second"View Code
4进程和单进程的速度差:
speed 1472737.19137 bytes/second
speed 431273.869046 bytes/second
其他词典
占用内存较小的词典文件 https://github.com/fxsjy/jieba/raw/master/extra_dict/dict.txt.small
支持繁体分词更好的词典文件 https://github.com/fxsjy/jieba/raw/master/extra_dict/dict.txt.big
下载你所需要的词典,然后覆盖jieba/dict.txt 即可或者用jieba.set_dictionary('data/dict.txt.big')
初始化
模块初始化机制的改变:lazy load (从0.28版本开始) jieba采用延迟加载,"import jieba"不会立即触发词典的加载,一旦有必要才开始加载词典构建trie。如果你想手工初始jieba,也可以手动初始化。
import jieba jieba.initialize() # 手动初始化(可选)在0.28之前的版本是不能指定主词典的路径的,有了延迟加载机制后,你可以改变主词典的路径: jieba.set_dictionary('data/dict.txt.big')
例子:
#encoding=utf-8 import sys sys.path.append("../") import jieba def cuttest(test_sent): result = jieba.cut(test_sent) print " ".join(result) def testcase(): cuttest("这是一个伸手不见五指的黑夜。我叫孙悟空,我爱北京,我爱Python和C++。") cuttest("我不喜欢日本和服。") cuttest("雷猴回归人间。") cuttest("工信处女干事每月经过下属科室都要亲口交代24口交换机等技术性器件的安装工作") cuttest("我需要廉租房") cuttest("永和服装饰品有限公司") cuttest("我爱北京天安门") cuttest("abc") cuttest("隐马尔可夫") cuttest("雷猴是个好网站") if __name__ == "__main__": testcase() jieba.set_dictionary("foobar.txt") print "================================" testcase()
相关文章推荐
- Python学习——jieba分词库和time模块
- python运用jieba和wordcloud库简单实现对QQ聊天记录制作词云
- 【记录】python中,两种读取txt的方式;并结合jieba找出词频位置分布
- python+gensim︱jieba分词、词袋doc2bow、TFIDF文本挖掘
- Python分词模块推荐:jieba中文分词
- 在Python里安装Jieba中文分词组件
- python下结巴分词(jieba)学习笔记
- python:jieba:当你看完个这句子之后就会现发,里这词的字序是乱的
- Python中文分词组件 jieba
- 解析搜狗词库(python)
- paip.输入法编程---词库多意义条目分割 python实现.
- python+gensim︱jieba分词、词袋doc2bow、TFIDF文本挖掘
- python库之jieba小试牛刀 2
- jieba分词python建立倒排索引
- Python + worldcloud + jieba 十分钟学会用任意中文文本生成词云
- Python安装jieba包,进行分词
- [置顶] 【python 走进NLP】 NLP 使用jieba分词处理文本
- 贝叶斯分类方法学习三 python+jieba+mongodb实现朴素贝叶斯新闻文本自动分类
- error: could not create '/usr/local/lib/python2.7/dist-packages/jieba': Permission denied
- paip.输入法编程---词库多意义条目分割 python实现.