python中文分词,使用结巴分词对python进行分词
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中文分词,使用结巴分词对python进行分词相关的知识,希望对你有一定的参考价值。
在采集美女站时,需要对关键词进行分词,最终采用的是python的结巴分词方法.
中文分词是中文文本处理的一个基础性工作,结巴分词利用进行中文分词。其基本实现原理有三点:
基于Trie树结构实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图(DAG)
采用了动态规划查找最大概率路径, 找出基于词频的最大切分组合
对于未登录词,采用了基于汉字成词能力的HMM模型,使用了Viterbi算法
安装(Linux环境)
下载工具包,解压后进入目录下,运行:python setup.py install
模式
默认模式,试图将句子最精确地切开,适合文本分析
全模式,把句子中所有的可以成词的词语都扫描出来,适合搜索引擎
接口
组件只提供jieba.cut 方法用于分词
cut方法接受两个输入参数:
第一个参数为需要分词的字符串
cut_all参数用来控制分词模式
待分词的字符串可以是gbk字符串、utf-8字符串或者unicode
jieba.cut返回的结构是一个可迭代的generator,可以使用for循环来获得分词后得到的每一个词语(unicode),也可以用list(jieba.cut(...))转化为list
seg=jieba.cut("http://www.gg4493.cn/"):
实例
<span style="margin: 0px; padding: 0px;">#! -*- coding:utf-<span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102);">8</span> -*-
<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">import</span> jieba
seg_list = jieba.cut(<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"我来到北京清华大学"</span>, cut_all = <span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">True</span>)
<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">print</span> <span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"Full Mode:"</span>, <span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">‘ ‘</span>.<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">join</span>(seg_list)
seg_list = jieba.cut(<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"我来到北京清华大学"</span>)
<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">print</span> <span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"Default Mode:"</span>, <span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">‘ ‘</span>.<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">join</span>(seg_list)</span>
以上是关于python中文分词,使用结巴分词对python进行分词的主要内容,如果未能解决你的问题,请参考以下文章