python中文分词,使用结巴分词对python进行分词

在采集美女站时,需要对关键词进行分词,最终采用的是python的结巴分词方法.

中文分词是中文文本处理的一个基础性工作,结巴分词利用进行中文分词。其基本实现原理有三点:

  1. 基于Trie树结构实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图(DAG)
  2. 采用了动态规划查找最大概率路径, 找出基于词频的最大切分组合
  3. 对于未登录词,采用了基于汉字成词能力的HMM模型,使用了Viterbi算法

安装(Linux环境)

下载工具包,解压后进入目录下,运行:python setup.py install

模式

  1. 默认模式,试图将句子最精确地切开,适合文本分析
  2. 全模式,把句子中所有的可以成词的词语都扫描出来,适合搜索引擎

接口

  • 组件只提供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/"):

实例

Hljs gradle代码

  1. <span style="margin: 0px; padding: 0px;">#! -*- coding:utf-<span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102);">8</span> -*-
  2. <span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">import</span> jieba
  3. 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>)
  4. <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)
  5. seg_list = jieba.cut(<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"我来到北京清华大学"</span>)
  6. <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>
时间: 2024-07-30 12:21:24

python中文分词,使用结巴分词对python进行分词的相关文章

汉语分词软件的使用 (python底下)

目前我常常使用的分词有结巴分词.NLPIR分词等等 最近是在使用结巴分词,稍微做一下推荐,还是蛮好用的. 一.结巴分词简介 利用结巴分词进行中文分词,基本实现原理有三: 基于Trie树结构实现高效的词图扫描,生成句子中汉字所有可能成词情况所构成的有向无环图(DAG) 采用了动态规划查找最大概率路径, 找出基于词频的最大切分组合 对于未登录词,采用了基于汉字成词能力的HMM模型,使用了Viterbi算法 二.安装及使用(Linux) 1.下载工具包,解压后进入目录下,运行:python setup

中文分词之结巴分词~~~附使用场景+demo

常用技能(更新ing):http://www.cnblogs.com/dunitian/p/4822808.html#skill 技能总纲(更新ing):http://www.cnblogs.com/dunitian/p/5493793.html 在线演示:http://cppjieba-webdemo.herokuapp.com 完整demo:https://github.com/dunitian/TempCode/tree/master/2016-09-05 先说下注意点,结巴分词他没有对分

centos7 python 中文 “UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte...”解决方法

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128) 1. 开始以为是自己写的python有问题,但后来发现同样的代码在其它电脑上运行正常... 2. 按网上说的方法什么load(sys),或者加# -*- coding: utf-8 -*-的方式,虽说都知道是python2年代的东西.但就当病急乱投医了.结果依旧无效. 3. 怀疑CentOS7 的LAN

python中文注释报错问题

错误信息: SyntaxError: Non-ASCII character '\xe4' in file... 解决办法: 在文件第一行或第二行添加:# -*- coding: utf-8 -*- 参考:http://blog.csdn.net/chen861201/article/details/7706535 python中文注释报错问题,布布扣,bubuko.com

python中文字符串前加u

我明明在编码前就加上了# -*- coding: UTF-8 -*-可是运行时还是出错了, # -*- coding: UTF-8 -*- 这句是告诉python程序中的文本是utf-8编码,让python可以按照utf-8读取程中文前加u就是告诉python后面的是个unicode编码,存储时按unicode格式存储. 参考 http://www.cnblogs.com/ymy124/archive/2012/06/23/2559282.html

python 中文转码

最近在玩一些爬虫,python中文转码刚接触是一件麻烦的事(转换的原理省略) 一般来说有encode('gbk'),edcode('utf8'),decode('gbk'),decode('utf8')可以解决很大一部问题 但是今天遇到了'\\u6210\\u529f'这种格式,上面的三板斧就搞不定了 经过万能的度娘,发现了第一个贴子,叹为观止 http://bbs.chinaunix.net/thread-3674073-1-1.html 代码如下 #!/usr/bin/env python

Python中文全攻略

Python中文全攻略[转] 这几天一直纠结月python的中文编码问题,发现一篇不错的文章,转过来留个念想. 原文链接:http://blog.csdn.net/mayflowers/archive/2007/04/18/1568852.aspx 1.        在Python中使用中文 在Python中有两种默认的字符串:str和unicode.在Python中一定要注意区分“Unicode字符串”和“unicode对象”的区别.后面所有的“unicode字符串”指的都是python里的

Python中文乱码问题

1:源代码 #!/usr/bin/env python name = raw_input('请输入您的姓名:') print 'Hello,', name 运行时出错: [email protected]:~/my_note/my_python$ python 5.py File "5.py", line 3 SyntaxError: Non-ASCII character '\xe8' in file 5.py on line 3, but no encoding declared;

Python中文显示问题

默认pyhon使用ASCII码来解释程序的,默认不支持中文,需要在程序的第一行或者第二行声明编码. 官方解决方案:https://www.python.org/dev/peps/pep-0263/ To define a source code encoding, a magic comment must be placed into the source files either as first or second line in the file, such as: # coding=<e