Skip to main content

Chinese word segmentation toolkit for spaCy (fork of pkuseg-python)

Project description

spacy-pkuseg: Chinese word segmentation toolkit for spaCy

This package is a fork of pkuseg-python that simplifies installation and serialization for use with spaCy. The underlying segmentation tools remain unmodified.


pkuseg:一个多领域中文分词工具包 (English Version)

pkuseg 是基于论文[Luo et. al, 2019]的工具包。其简单易用,支持细分领域分词,有效提升了分词准确度。

目录

主要亮点

pkuseg具有如下几个特点:

  1. 多领域分词。不同于以往的通用中文分词工具,此工具包同时致力于为不同领域的数据提供个性化的预训练模型。根据待分词文本的领域特点,用户可以自由地选择不同的模型。 我们目前支持了新闻领域,网络领域,医药领域,旅游领域,以及混合领域的分词预训练模型。在使用中,如果用户明确待分词的领域,可加载对应的模型进行分词。如果用户无法确定具体领域,推荐使用在混合领域上训练的通用模型。各领域分词样例可参考 example.txt
  2. 更高的分词准确率。相比于其他的分词工具包,当使用相同的训练数据和测试数据,pkuseg可以取得更高的分词准确率。
  3. 支持用户自训练模型。支持用户使用全新的标注数据进行训练。
  4. 支持词性标注。

编译和安装

  • 目前仅支持python3
  • 为了获得好的效果和速度,强烈建议大家通过pip install更新到目前的最新版本
  1. 通过PyPI安装(自带模型文件):

    pip3 install pkuseg
    之后通过import pkuseg来引用
    

    建议更新到最新版本以获得更好的开箱体验:

    pip3 install -U pkuseg
    
  2. 如果PyPI官方源下载速度不理想,建议使用镜像源,比如:
    初次安装:

    pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pkuseg
    

    更新:

    pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -U pkuseg
    
  3. 如果不使用pip安装方式,选择从GitHub下载,可运行以下命令安装:

    python setup.py build_ext -i
    

    GitHub的代码并不包括预训练模型,因此需要用户自行下载或训练模型,预训练模型可详见release。使用时需设定"model_name"为模型文件。

注意:安装方式1和2目前仅支持linux(ubuntu)、mac、windows 64 位的python3版本。如果非以上系统,请使用安装方式3进行本地编译安装。

各类分词工具包的性能对比

我们选择jieba、THULAC等国内代表分词工具包与pkuseg做性能比较,详细设置可参考实验环境

细领域训练及测试结果

以下是在不同数据集上的对比结果:

MSRA Precision Recall F-score
jieba 87.01 89.88 88.42
THULAC 95.60 95.91 95.71
pkuseg 96.94 96.81 96.88
WEIBO Precision Recall F-score
jieba 87.79 87.54 87.66
THULAC 93.40 92.40 92.87
pkuseg 93.78 94.65 94.21

默认模型在不同领域的测试效果

考虑到很多用户在尝试分词工具的时候,大多数时候会使用工具包自带模型测试。为了直接对比“初始”性能,我们也比较了各个工具包的默认模型在不同领域的测试效果。请注意,这样的比较只是为了说明默认情况下的效果,并不一定是公平的。

Default MSRA CTB8 PKU WEIBO All Average
jieba 81.45 79.58 81.83 83.56 81.61
THULAC 85.55 87.84 92.29 86.65 88.08
pkuseg 87.29 91.77 92.68 93.43 91.29

其中,All Average显示的是在所有测试集上F-score的平均。

更多详细比较可参见和现有工具包的比较

使用方式

代码示例

以下代码示例适用于python交互式环境。

代码示例1:使用默认配置进行分词(如果用户无法确定分词领域,推荐使用默认模型分词

import pkuseg

seg = pkuseg.pkuseg()           # 以默认配置加载模型
text = seg.cut('我爱北京天安门')  # 进行分词
print(text)

代码示例2:细领域分词(如果用户明确分词领域,推荐使用细领域模型分词

import pkuseg

seg = pkuseg.pkuseg(model_name='medicine')  # 程序会自动下载所对应的细领域模型
text = seg.cut('我爱北京天安门')              # 进行分词
print(text)

代码示例3:分词同时进行词性标注,各词性标签的详细含义可参考 tags.txt

import pkuseg

seg = pkuseg.pkuseg(postag=True)  # 开启词性标注功能
text = seg.cut('我爱北京天安门')    # 进行分词和词性标注
print(text)

代码示例4:对文件分词

import pkuseg

# 对input.txt的文件分词输出到output.txt中
# 开20个进程
pkuseg.test('input.txt', 'output.txt', nthread=20)     

其他使用示例可参见详细代码示例

参数说明

模型配置

pkuseg.pkuseg(model_name = "default", user_dict = "default", postag = False)
	model_name		模型路径。
			        "default",默认参数,表示使用我们预训练好的混合领域模型(仅对pip下载的用户)。
				"news", 使用新闻领域模型。
				"web", 使用网络领域模型。
				"medicine", 使用医药领域模型。
				"tourism", 使用旅游领域模型。
			        model_path, 从用户指定路径加载模型。
	user_dict		设置用户词典。
				"default", 默认参数,使用我们提供的词典。
				None, 不使用词典。
				dict_path, 在使用默认词典的同时会额外使用用户自定义词典,可以填自己的用户词典的路径,词典格式为一行一个词(如果选择进行词性标注并且已知该词的词性,则在该行写下词和词性,中间用tab字符隔开)。
	postag		        是否进行词性分析。
				False, 默认参数,只进行分词,不进行词性标注。
				True, 会在分词的同时进行词性标注。

对文件进行分词

pkuseg.test(readFile, outputFile, model_name = "default", user_dict = "default", postag = False, nthread = 10)
	readFile		输入文件路径。
	outputFile		输出文件路径。
	model_name		模型路径。同pkuseg.pkuseg
	user_dict		设置用户词典。同pkuseg.pkuseg
	postag			设置是否开启词性分析功能。同pkuseg.pkuseg
	nthread			测试时开的进程数。

模型训练

pkuseg.train(trainFile, testFile, savedir, train_iter = 20, init_model = None)
	trainFile		训练文件路径。
	testFile		测试文件路径。
	savedir			训练模型的保存路径。
	train_iter		训练轮数。
	init_model		初始化模型,默认为None表示使用默认初始化,用户可以填自己想要初始化的模型的路径如init_model='./models/'。

多进程分词

当将以上代码示例置于文件中运行时,如涉及多进程功能,请务必使用if __name__ == '__main__'保护全局语句,详见多进程分词

预训练模型

从pip安装的用户在使用细领域分词功能时,只需要设置model_name字段为对应的领域即可,会自动下载对应的细领域模型。

从github下载的用户则需要自己下载对应的预训练模型,并设置model_name字段为预训练模型路径。预训练模型可以在release部分下载。以下是对预训练模型的说明:

  • news: 在MSRA(新闻语料)上训练的模型。

  • web: 在微博(网络文本语料)上训练的模型。

  • medicine: 在医药领域上训练的模型。

  • tourism: 在旅游领域上训练的模型。

  • mixed: 混合数据集训练的通用模型。随pip包附带的是此模型。

欢迎更多用户可以分享自己训练好的细分领域模型。

版本历史

详见版本历史

开源协议

  1. 本代码采用MIT许可证。
  2. 欢迎对该工具包提出任何宝贵意见和建议,请发邮件至jingjingxu@pku.edu.cn

论文引用

该代码包主要基于以下科研论文,如使用了本工具,请引用以下论文:


@article{pkuseg,
  author = {Luo, Ruixuan and Xu, Jingjing and Zhang, Yi and Ren, Xuancheng and Sun, Xu},
  journal = {CoRR},
  title = {PKUSEG: A Toolkit for Multi-Domain Chinese Word Segmentation.},
  url = {https://arxiv.org/abs/1906.11455},
  volume = {abs/1906.11455},
  year = 2019
}

其他相关论文

  • Xu Sun, Houfeng Wang, Wenjie Li. Fast Online Training with Frequency-Adaptive Learning Rates for Chinese Word Segmentation and New Word Detection. ACL. 2012.
  • Jingjing Xu and Xu Sun. Dependency-based gated recursive neural network for chinese word segmentation. ACL. 2016.
  • Jingjing Xu and Xu Sun. Transfer learning for low-resource chinese word segmentation with a novel neural network. NLPCC. 2017.

常见问题及解答

  1. 为什么要发布pkuseg?
  2. pkuseg使用了哪些技术?
  3. 无法使用多进程分词和训练功能,提示RuntimeError和BrokenPipeError。
  4. 是如何跟其它工具包在细领域数据上进行比较的?
  5. 在黑盒测试集上进行比较的话,效果如何?
  6. 如果我不了解待分词语料的所属领域呢?
  7. 如何看待在一些特定样例上的分词结果?
  8. 关于运行速度问题?
  9. 关于多进程速度问题?

致谢

感谢俞士汶教授(北京大学计算语言所)与邱立坤博士提供的训练数据集!

作者

Ruixuan Luo (罗睿轩), Jingjing Xu(许晶晶), Xuancheng Ren(任宣丞), Yi Zhang(张艺), Bingzhen Wei(位冰镇), Xu Sun (孙栩)

北京大学 语言计算与机器学习研究组

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spacy_pkuseg-1.0.0.tar.gz (2.1 MB view details)

Uploaded Source

Built Distributions

spacy_pkuseg-1.0.0-cp312-cp312-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.12 Windows x86-64

spacy_pkuseg-1.0.0-cp312-cp312-musllinux_1_2_x86_64.whl (5.2 MB view details)

Uploaded CPython 3.12 musllinux: musl 1.2+ x86-64

spacy_pkuseg-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

spacy_pkuseg-1.0.0-cp312-cp312-macosx_10_9_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.12 macOS 10.9+ x86-64

spacy_pkuseg-1.0.0-cp311-cp311-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.11 Windows x86-64

spacy_pkuseg-1.0.0-cp311-cp311-musllinux_1_2_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.11 musllinux: musl 1.2+ x86-64

spacy_pkuseg-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.2 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

spacy_pkuseg-1.0.0-cp311-cp311-macosx_10_9_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

spacy_pkuseg-1.0.0-cp310-cp310-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.10 Windows x86-64

spacy_pkuseg-1.0.0-cp310-cp310-musllinux_1_2_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.2+ x86-64

spacy_pkuseg-1.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

spacy_pkuseg-1.0.0-cp310-cp310-macosx_10_9_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

spacy_pkuseg-1.0.0-cp39-cp39-win_amd64.whl (2.4 MB view details)

Uploaded CPython 3.9 Windows x86-64

spacy_pkuseg-1.0.0-cp39-cp39-musllinux_1_2_x86_64.whl (5.1 MB view details)

Uploaded CPython 3.9 musllinux: musl 1.2+ x86-64

spacy_pkuseg-1.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

spacy_pkuseg-1.0.0-cp39-cp39-macosx_10_9_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

File details

Details for the file spacy_pkuseg-1.0.0.tar.gz.

File metadata

  • Download URL: spacy_pkuseg-1.0.0.tar.gz
  • Upload date:
  • Size: 2.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for spacy_pkuseg-1.0.0.tar.gz
Algorithm Hash digest
SHA256 33531ea8e13fc09ebe3b40bd97e84d07ccd5a1fe67fa8e84173769a25ac03158
MD5 80d20ba10209017bcf70471c2e3eb9a7
BLAKE2b-256 a733c2370bbe09daf655332a34b263a0e6279e630b30b57364438381a511f964

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 2f3932c65b5dbbbdd23f6332e13102bd7a00a1563fad6893be73e32a876cd4cc
MD5 8b66ac35c1261d91ad2ef04ed3906cd0
BLAKE2b-256 b1a7a6c5ba96e22d3609d23610514667d9de8838885698e00f26c15859b8a43d

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 703a429559583e8b9819836aefb24c01c0b86e757d1cc6838c7fcc11ef3b6f28
MD5 f30480001f8817b1cc6abf8eba98badf
BLAKE2b-256 dff68596a741994fb3ef8d7fe93a04aca741d9bbcc0da78169292284daa04d29

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1c1e12acb4135b22d459bc24a63ffe979cdd8570df42d164323ab8a93dd7125b
MD5 20c59a93c6c24bd1123733234b9cdde0
BLAKE2b-256 4178cf266d0ea4dee349ca3c6bce86e6323e83fc4da711c69d89d0db9062ed54

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp312-cp312-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp312-cp312-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5dfd5a9ed53bacb84e1e4fad16c532c965b49632dcea4dee390dc2bb59bc00d6
MD5 c960a96f04fa94a883d2f79d30d4a444
BLAKE2b-256 7b24b21cd80975b5ca7241e5f6b7fbefbf76ec80822e35aa864a14857331b47e

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4317e33656b9d65fe19687ec9a7b978c1a1a7be5813e6bec722eeba1084f2744
MD5 3c4922afe9d3b76454c13b5d993dd864
BLAKE2b-256 a0b707c159266ced53b7e56d823aa3c2e304ab9726f56c214a901600c9ec94d2

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ec1e87c4d3cca440b354632364e571617916a8d94b1fc06d7c2edb92c1dc12cb
MD5 4deb2881da15074870b70a796809c0d2
BLAKE2b-256 7678326ffe802d97cc218aaf0348ddebf4077d31447aa212c20374d162f53fed

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ecfd222c3a2f97724336a0fd6635315853697fd4c32b005facea6b58f305d961
MD5 b7aec4b8a58a0650d5d617a395b8c655
BLAKE2b-256 1b3979742bf28c3119a66f42c3be0ff8dac84c3178e5f2dfc78a7ab5f784953a

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e8a80894b6faf8cb73bec19918fb8b2cd4e5eb54a5c1e75ddd285a9d629a1953
MD5 c79e930dddc314d18d88a6cc0c64e668
BLAKE2b-256 0474908e883df7cbfa288b9df47e396dee0b66e459dcecd1de087b29c812ba11

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a32760d9df0412ac11fe01b2b0aea57c6fd32b9ba081a06b4f3841e6c360ae93
MD5 8f1e92c802c4c9ac984676db5e2410a8
BLAKE2b-256 8d4e38c257cd9d59a55e75a3857301fb3e4c29b136daaa45bdd3ac9df65faa8b

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d5a088602217a7e68ec3a98d73082315c3161f7e48bf7fd4295b4f5e22bbd7a8
MD5 b012847430fa52b1440f3c0e3140af00
BLAKE2b-256 6d4f1ad1c9607f82e5e08df1b873b70f2a235045f9b43845312e87fc6369b5af

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7108075c345faa6cc7f18628cdd89df78850c2fc850b4c2ef23a324ea437dea3
MD5 6b6f7a5fd55accb95a982194ba891abd
BLAKE2b-256 6b037742adf7fd9e74e0040b4f606b5f8c7fcf8b8f53a475e25c2391b81bbafa

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9463788ef0c906bcbc587c379a35fd86a93e9634a49059e60e0a9314537a0364
MD5 1f55b35a3aa3f4a9c9a899ee71d5f075
BLAKE2b-256 e9ea6850962f1eef56ef45226f665fca3e94441a955a93beeed61a0ebe342396

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c31741ad2627b6fa9938765ab6b260e7b4aa97f075e6e000797070f838f508f9
MD5 33b8cecfcedbc881d21e59d6c8fbf3fb
BLAKE2b-256 02d0ad01cc6cca5aaf5637d517fa09de278ace874f9f4abf7ac394f3d18dc137

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 ca18420c370f768ed71c39f4d76db6c33fb224ddb7dcfc0378076e1bdb6e8d17
MD5 86448f15fdb104add1cb72868596da3c
BLAKE2b-256 8d6bbffc0094afbf1ca0d1604542e90c3625608aaf273576d1d502d30893552a

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6a4c69766ef4604d63bbca4088c2e3c94be3f19c5edd3766603b146c03028c99
MD5 9a5e0df79906f79658bcbb7d438a4f93
BLAKE2b-256 921cecf544a3a7aeee45c35bd18df8a5609c4065883a2fb9276ab6e64561d16c

See more details on using hashes here.

File details

Details for the file spacy_pkuseg-1.0.0-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for spacy_pkuseg-1.0.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d9f02792bc91806aeeca855f1e38f621a4b5f0b03dcf110999ae118b571ae111
MD5 f7246f5f9e4692c6d3e3a2e85714720d
BLAKE2b-256 d45b8fefee884280cf17b4c9ee1cab1f099cc5eb5bcc0229e4459a5982648d0c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page