jieba README

The project's README from github.com/fxsjy/jieba. As one markdown file: README.md.

jieba

“结巴”中文分词:做最好的 Python 中文分词组件

"Jieba" (Chinese for "to stutter") Chinese text segmentation: built to be the best Python Chinese word segmentation module.

特点

安装说明

代码对 Python 2/3 均兼容

算法

主要功能

  1. 分词

代码示例

# encoding=utf-8
import jieba

jieba.enable_paddle()# 启动paddle模式。 0.40版之后开始支持,早期版本不支持
strs=["我来到北京清华大学","乒乓球拍卖完了","中国科学技术大学"]
for str in strs:
    seg_list = jieba.cut(str,use_paddle=True) # 使用paddle模式
    print("Paddle Mode: " + '/'.join(list(seg_list)))

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))

输出:

【全模式】: 我/ 来到/ 北京/ 清华/ 清华大学/ 华大/ 大学

【精确模式】: 我/ 来到/ 北京/ 清华大学

【新词识别】:他, 来到, 了, 网易, 杭研, 大厦    (此处,“杭研”并没有在词典中,但是也被Viterbi算法识别出来了)

【搜索引擎模式】: 小明, 硕士, 毕业, 于, 中国, 科学, 学院, 科学院, 中国科学院, 计算, 计算所, 后, 在, 日本, 京都, 大学, 日本京都大学, 深造
  1. 添加自定义词典

载入词典

例如:

创新办 3 i
云计算 5
凱特琳 nz
台中

调整词典

代码示例:

>>> print('/'.join(jieba.cut('如果放到post中将出错。', HMM=False)))
如果/放到/post/中将/出错/。
>>> jieba.suggest_freq(('中', '将'), True)
494
>>> print('/'.join(jieba.cut('如果放到post中将出错。', HMM=False)))
如果/放到/post/中/将/出错/。
>>> print('/'.join(jieba.cut('「台中」正确应该不会被切开', HMM=False)))
「/台/中/」/正确/应该/不会/被/切开
>>> jieba.suggest_freq('台中', True)
69
>>> print('/'.join(jieba.cut('「台中」正确应该不会被切开', HMM=False)))
「/台中/」/正确/应该/不会/被/切开
  1. 关键词提取

基于 TF-IDF 算法的关键词抽取

import jieba.analyse

代码示例 (关键词提取)

https://github.com/fxsjy/jieba/blob/master/test/extract_tags.py

关键词提取所使用逆向文件频率(IDF)文本语料库可以切换成自定义语料库的路径

关键词提取所使用停止词(Stop Words)文本语料库可以切换成自定义语料库的路径

关键词一并返回关键词权重值示例

基于 TextRank 算法的关键词抽取

算法论文: TextRank: Bringing Order into Texts

基本思想:

  1. 将待抽取关键词的文本进行分词
  2. 以固定窗口大小(默认为5,通过span属性调整),词之间的共现关系,构建图
  3. 计算图中节点的PageRank,注意是无向带权图

使用示例:

见 test/demo.py

  1. 词性标注

>>> import jieba
>>> import jieba.posseg as pseg
>>> words = pseg.cut("我爱北京天安门") #jieba默认模式
>>> jieba.enable_paddle() #启动paddle模式。 0.40版之后开始支持,早期版本不支持
>>> words = pseg.cut("我爱北京天安门",use_paddle=True) #paddle模式
>>> for word, flag in words:
...    print('%s %s' % (word, flag))
...
我 r
爱 v
北京 ns
天安门 ns

paddle模式词性标注对应表如下:

paddle模式词性和专名类别标签集合如下表,其中词性标签 24 个(小写字母),专名类别标签 4 个(大写字母)。

标签 含义 标签 含义 标签 含义 标签 含义
n 普通名词 f 方位名词 s 处所名词 t 时间
nr 人名 ns 地名 nt 机构名 nw 作品名
nz 其他专名 v 普通动词 vd 动副词 vn 名动词
a 形容词 ad 副形词 an 名形词 d 副词
m 数量词 q 量词 r 代词 p 介词
c 连词 u 助词 xc 其他虚词 w 标点符号
PER 人名 LOC 地名 ORG 机构名 TIME 时间
  1. 并行分词

  1. Tokenize:返回词语在原文的起止位置

result = jieba.tokenize(u'永和服装饰品有限公司')
for tk in result:
    print("word %s\t\t start: %d \t\t end:%d" % (tk[0],tk[1],tk[2]))
word 永和                start: 0                end:2
word 服装                start: 2                end:4
word 饰品                start: 4                end:6
word 有限公司            start: 6                end:10
result = jieba.tokenize(u'永和服装饰品有限公司', mode='search')
for tk in result:
    print("word %s\t\t start: %d \t\t end:%d" % (tk[0],tk[1],tk[2]))
word 永和                start: 0                end:2
word 服装                start: 2                end:4
word 饰品                start: 4                end:6
word 有限                start: 6                end:8
word 公司                start: 8                end:10
word 有限公司            start: 6                end:10
  1. ChineseAnalyzer for Whoosh 搜索引擎

  1. 命令行分词

使用示例:python -m jieba news.txt > cut_result.txt

命令行选项(翻译):

使用: python -m jieba [options] filename

结巴命令行界面。

固定参数:
  filename              输入文件

可选参数:
  -h, --help            显示此帮助信息并退出
  -d [DELIM], --delimiter [DELIM]
                        使用 DELIM 分隔词语,而不是用默认的' / '。
                        若不指定 DELIM,则使用一个空格分隔。
  -p [DELIM], --pos [DELIM]
                        启用词性标注;如果指定 DELIM,词语和词性之间
                        用它分隔,否则用 _ 分隔
  -D DICT, --dict DICT  使用 DICT 代替默认词典
  -u USER_DICT, --user-dict USER_DICT
                        使用 USER_DICT 作为附加词典,与默认词典或自定义词典配合使用
  -a, --cut-all         全模式分词(不支持词性标注)
  -n, --no-hmm          不使用隐含马尔可夫模型
  -q, --quiet           不输出载入信息到 STDERR
  -V, --version         显示版本信息并退出

如果没有指定文件名,则使用标准输入。

--help 选项输出:

$> python -m jieba --help
Jieba command line interface.

positional arguments:
  filename              input file

optional arguments:
  -h, --help            show this help message and exit
  -d [DELIM], --delimiter [DELIM]
                        use DELIM instead of ' / ' for word delimiter; or a
                        space if it is used without DELIM
  -p [DELIM], --pos [DELIM]
                        enable POS tagging; if DELIM is specified, use DELIM
                        instead of '_' for POS delimiter
  -D DICT, --dict DICT  use DICT as dictionary
  -u USER_DICT, --user-dict USER_DICT
                        use USER_DICT together with the default dictionary or
                        DICT (if specified)
  -a, --cut-all         full pattern cutting (ignored with POS tagging)
  -n, --no-hmm          don't use the Hidden Markov Model
  -q, --quiet           don't print loading messages to stderr
  -V, --version         show program's version number and exit

If no filename specified, use STDIN instead.

延迟加载机制

jieba 采用延迟加载,import jieba 和 jieba.Tokenizer() 不会立即触发词典的加载,一旦有必要才开始加载词典构建前缀字典。如果你想手工初始 jieba,也可以手动初始化。

import jieba
jieba.initialize()  # 手动初始化(可选)

在 0.28 之前的版本是不能指定主词典的路径的,有了延迟加载机制后,你可以改变主词典的路径:

jieba.set_dictionary('data/dict.txt.big')

例子: https://github.com/fxsjy/jieba/blob/master/test/test_change_dictpath.py

其他词典

  1. 占用内存较小的词典文件 https://github.com/fxsjy/jieba/raw/master/extra_dict/dict.txt.small

  2. 支持繁体分词更好的词典文件 https://github.com/fxsjy/jieba/raw/master/extra_dict/dict.txt.big

下载你所需要的词典,然后覆盖 jieba/dict.txt 即可;或者用 jieba.set_dictionary('data/dict.txt.big')

其他语言实现

结巴分词 Java 版本

作者:piaolingxue 地址:https://github.com/huaban/jieba-analysis

结巴分词 C++ 版本

作者:yanyiwu 地址:https://github.com/yanyiwu/cppjieba

结巴分词 Rust 版本

作者:messense, MnO2 地址:https://github.com/messense/jieba-rs

结巴分词 Node.js 版本

作者:yanyiwu 地址:https://github.com/yanyiwu/nodejieba

结巴分词 Erlang 版本

作者:falood 地址:https://github.com/falood/exjieba

结巴分词 R 版本

作者:qinwf 地址:https://github.com/qinwf/jiebaR

结巴分词 iOS 版本

作者:yanyiwu 地址:https://github.com/yanyiwu/iosjieba

结巴分词 PHP 版本

作者:fukuball 地址:https://github.com/fukuball/jieba-php

结巴分词 .NET(C#) 版本

作者:anderscui 地址:https://github.com/anderscui/jieba.NET/

结巴分词 Go 版本

结巴分词Android版本

友情链接

系统集成

  1. Solr: https://github.com/sing1ee/jieba-solr

分词速度

常见问题

1. 模型的数据是如何生成的?

详见: https://github.com/fxsjy/jieba/issues/7

2. “台中”总是被切成“台 中”?(以及类似情况)

P(台中) < P(台)×P(中),“台中”词频不够导致其成词概率较低

解决方法:强制调高词频

jieba.add_word('台中') 或者 jieba.suggest_freq('台中', True)

3. “今天天气 不错”应该被切成“今天 天气 不错”?(以及类似情况)

解决方法:强制调低词频

jieba.suggest_freq(('今天', '天气'), True)

或者直接删除该词 jieba.del_word('今天天气')

4. 切出了词典中没有的词语,效果不理想?

解决方法:关闭新词发现

jieba.cut('丰田太省了', HMM=False) jieba.cut('我们中出了一个叛徒', HMM=False)

更多问题请点击:https://github.com/fxsjy/jieba/issues?sort=updated&state=closed

修订历史

https://github.com/fxsjy/jieba/blob/master/Changelog


jieba

"Jieba" (Chinese for "to stutter") Chinese text segmentation: built to be the best Python Chinese word segmentation module.

Features

  1. Accurate Mode attempts to cut the sentence into the most accurate segmentations, which is suitable for text analysis.
  2. Full Mode gets all the possible words from the sentence. Fast but not accurate.
  3. Search Engine Mode, based on the Accurate Mode, attempts to cut long words into several short words, which can raise the recall rate. Suitable for search engines.

Online demo

http://jie

jieba (fxsjy/jieba)

Searching Database…

35.2K ·GitHub
这是什么?Jieba 是一个流行的开源 Python 库,用于中文分词:它把中文文本切分成词语,同时支持简体和繁体中文。“Jieba”(结巴)在中文里的意思是“说话结巴”。
它是我导入的库,还是我运行的程序?是一个库:你在 Python 代码里导入 jieba。它还附带一个命令行工具,可以切分文本文件:python -m jieba news.txt > cutresult.txt。
要用起来还需要装别的东西吗?基本使用什么都不需要——它是纯 Python 实现的。只有可选的 Paddle 深度学习模式需要额外安装一个包:pip install paddlepaddle-tiny==1.6.1。
有什么坑需要提前知道吗?并行分词(jieba.enable_parallel,一种多进程加速方式)在 Windows 上不能用。另外,不建议直接输入 GBK 编码的文本——请使用 UTF-8 或 unicode 字符串。
它有哪几种分词模式?内置三种:精确模式(切分最准确,适合做文本分析)、全模式(把所有可能的词都切出来,速度最快)和搜索引擎模式(对长词再次切分,提高召回率)。另外还有一个可选的 Paddle 模式,提供基于深度学习的分词和词性标注。
除了把文本切成词,它还能做什么?可以:基于 TF-IDF 和 TextRank 的关键词提取(jieba.analyse)、词性标注(jieba.posseg)、返回词首词尾位置的分词,以及为 Whoosh 搜索引擎提供的 ChineseAnalyzer。
遇到词典里没有的词怎么办?它用基于 Viterbi 算法的 HMM 模型,根据字符规律识别新词。你也可以通过 jieba.load_userdict 添加自定义词典,或者用 jieba.add_word 或 jieba.suggest_freq 调整词频来修正切错的地方。
它的分词速度有多快?在 Intel Core i7-2600 CPU 上切分一整本小说时,全模式大约 1.5 MB/s,默认模式大约 400 KB/s。
Creator
fxsjy
Size
54.7 MB
Stars
35.2K
Age
14y
Commits · 90 Days