Skip to main content

A micro tokenizer for Chinese

Project description


微型中文分词器
==============

一个微型的中文分词器,目前提供了七种分词算法:


#. 按照词语的频率(概率)来利用构建 DAG(有向无环图)来分词,使用 ``Trie Tree`` 构建前缀字典树
#. 使用隐马尔可夫模型(Hidden Markov Model,HMM)来分词
#. 融合 DAG 和 HMM 两种分词模型的结果,按照分词粒度最大化的原则进行融合得到的模型
#. 正向最大匹配法
#. 反向最大匹配法
#. 双向最大匹配法
#. 基于 CRF (Conditional Random Field, 条件随机场) 的分词方法

特点 / 特色
===========


* 面向教育:可以导出 ``graphml`` 格式的图结构文件,辅助学习者理解算法过程
* 良好的分词性能:由于使用类似 ``结巴分词`` 的算法,具有良好的分词性能
* 具有良好的扩展性:使用和 ``结巴分词`` 一样的字典文件,可以轻松添加自定义字典
* 自定义能力强
* 提供工具和脚本帮助用户训练自己的分词模型而不是使用内建的模型

演示
====

在线演示
--------

在线的 Jupyter Notebook 在

.. image:: https://mybinder.org/badge.svg
:target: https://mybinder.org/v2/gh/howl-anderson/MicroTokenizer/master?filepath=.notebooks%2FMicroTokenizer.ipynb
:alt: Binder


离线演示
--------

DAG 分词
^^^^^^^^

代码:

.. code-block:: python

import MicroTokenizer

tokens = MicroTokenizer.cut("知识就是力量")
print(tokens)

输出:

.. code-block:: python

['知识', '就是', '力量']

有向无环图效果演示
^^^^^^^^^^^^^^^^^^


.. image:: .images/DAG_of_knowledge_is_power.png
:target: .images/DAG_of_knowledge_is_power.png
:alt: DAG of 'knowledge is power'


备注
~~~~


* ``<s>`` 和 ``</s>`` 是图的起始和结束节点,不是实际要分词的文本
* 图中 Edge 上标注的是 ``log(下一个节点的概率的倒数)``
* 最短路径已经用 ``深绿色`` 作了标记

更多 DAG 分词的演示
^^^^^^^^^^^^^^^^^^^

"王小明在北京的清华大学读书"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~


.. image:: .images/DAG_of_xiaomin.png
:target: .images/DAG_of_xiaomin.png
:alt: DAG of xiaomin


HMM 分词
^^^^^^^^

因 HMM 模型单独分词性能不佳, 一般情况下只用于和其他模型的融合, 故不在此提供示例, 需要演示者,可在 在线的 Jupyter Notebook 找到使用的例子.

DAG+HMM 分词
^^^^^^^^^^^^

将前两个模型的结果融合,融合了 DAG 稳定的构词能力和 HMM 的新词发现能力,缺点是速度较慢

正向最大匹配法
^^^^^^^^^^^^^^

具体介绍,请阅读博文 `构建中文分词器 - 正向最大匹配法 <http://blog.xiaoquankong.ai/%E6%9E%84%E5%BB%BA%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%99%A8-%E6%AD%A3%E5%90%91%E6%9C%80%E5%A4%A7%E5%8C%B9%E9%85%8D%E6%B3%95/>`_

反向最大匹配法
^^^^^^^^^^^^^^

具体介绍,请阅读博文 `构建中文分词器 - 反向最大匹配法 <http://blog.xiaoquankong.ai/%E6%9E%84%E5%BB%BA%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%99%A8-%E5%8F%8D%E5%90%91%E6%9C%80%E5%A4%A7%E5%8C%B9%E9%85%8D%E6%B3%95/>`_

双向最大匹配法
^^^^^^^^^^^^^^

具体介绍,请阅读博文 `构建中文分词器 - 双向最大匹配法 <http://blog.xiaoquankong.ai/%E6%9E%84%E5%BB%BA%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D%E5%99%A8-%E5%8F%8C%E5%90%91%E6%9C%80%E5%A4%A7%E5%8C%B9%E9%85%8D%E6%B3%95/>`_

基于 CRF 的分词
^^^^^^^^^^^^^^^

TODO

依赖
====

只在 python 3.5+ 环境测试过,其他环境不做兼容性保障。

安装
====

pip
---

.. code-block:: bash

pip install MicroTokenizer

source
------

.. code-block:: console

pip install git+https://github.com/howl-anderson/MicroTokenizer.git

如何使用
========

分词
----

见上文

导出 GraphML 文件
-----------------

.. code-block:: python

import MicroTokenizer

MicroTokenizer.initialize()

from MicroTokenizer import default_tokenizer

dag_tokenizer = default_tokenizer.dag_tokenizer
dag_tokenizer.build_graph("知识就是力量")
dag_tokenizer.write_graphml("output.graphml")

NOTE: 导出后的 ``graphml`` 文件可以使用 `Cytoscape <http://www.cytoscape.org/>`_ 进行浏览和渲染

Roadmap
=======


* [DONE] DAG 模型融合 HMM 模型 以处理 OOV 以及提高 Performance
* [DONE] 和主流分词模型做一个分词能力的测试 @ `中文分词软件基准测试 | Chinese tokenizer benchmark <https://github.com/howl-anderson/Chinese_tokenizer_benchmark>`_
* [DONE] 使用 ``Trie Tree`` 来压缩运行时内存和改善前缀查找速度
* [TODO] 允许添加自定义 DAG 词典
* [DONE] 开发自定义 DAG 字典构造 Feature, 允许用户构建自己的 DAG 字典
* [DONE] 开发自定义 HMM 参数构建 Feature, 允许用户训练自己的 HMM 模型
* [DONE] 引入 CRF 分词模型,使用 python-crfsuite
* [TODO] 模型系统分成代码和模型两个部分,用户可以选择性的下载和安装模型以及让用户训练和安装定制的模型
* [TODO] 引入 char-level word embedding + Bi-LSTM + CRF 分词模型,参考 `FoolNLTK <https://github.com/rockyzhengwu/FoolNLTK>`_

Credits
=======


* 目前 DAG 算法所用的字典文件来自 `jieba <https://github.com/fxsjy/jieba>`_ 项目


=======
History
=======

0.1.0 (2018-06-12)
------------------

* First release on PyPI.


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

MicroTokenizer-0.7.6.tar.gz (12.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

MicroTokenizer-0.7.6-py2.py3-none-any.whl (24.4 MB view details)

Uploaded Python 2Python 3

File details

Details for the file MicroTokenizer-0.7.6.tar.gz.

File metadata

  • Download URL: MicroTokenizer-0.7.6.tar.gz
  • Upload date:
  • Size: 12.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.10.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for MicroTokenizer-0.7.6.tar.gz
Algorithm Hash digest
SHA256 b671b7c0c66625825ed9e84f1c9210c331d3d0223ded3c05ab2ab9af34a6bc25
MD5 f448e78cfb714e8db45b1a5b0223704b
BLAKE2b-256 d863d577516db2c87cc843d721dcd60d1ba5014d9c58c524e02e84e786dbeab3

See more details on using hashes here.

File details

Details for the file MicroTokenizer-0.7.6-py2.py3-none-any.whl.

File metadata

  • Download URL: MicroTokenizer-0.7.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 24.4 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.10.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for MicroTokenizer-0.7.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6be28a8868672b330e6cf378647b21ab973f812337fb1056b72b1851be30d974
MD5 dec37a388646d8e27c671256beed2373
BLAKE2b-256 28a9d80ee7582841c672b918598e8d13c177eb7c89fb1824266ee1b6a42f2f65

See more details on using hashes here.

Supported by

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