{"id":21259607,"url":"https://github.com/howl-anderson/microtokenizer","last_synced_at":"2025-04-12T16:47:55.646Z","repository":{"id":48637632,"uuid":"137062055","full_name":"howl-anderson/MicroTokenizer","owner":"howl-anderson","description":"一个轻量且功能全面的中文分词器，帮助学生了解分词器的工作原理。MicroTokenizer: A lightweight Chinese tokenizer designed for educational and research purposes. Provides a practical, hands-on approach to understanding NLP concepts, featuring multiple tokenization algorithms and customizable models. Ideal for students, researchers, and NLP enthusiasts..","archived":false,"fork":false,"pushed_at":"2024-10-18T05:19:26.000Z","size":182407,"stargazers_count":150,"open_issues_count":2,"forks_count":22,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-03T19:13:17.392Z","etag":null,"topics":["chinese-nlp","chinese-tokenizer","chinese-word-segmentation","dag-network","educational-project","nlp-machine-learning","tokenizer"],"latest_commit_sha":null,"homepage":"https://nlp.xiaoquankong.ai","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/howl-anderson.png","metadata":{"files":{"readme":"README-ZH.md","changelog":"HISTORY.rst","contributing":"CONTRIBUTING.rst","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.rst","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-06-12T11:28:38.000Z","updated_at":"2025-03-24T08:52:28.000Z","dependencies_parsed_at":"2024-12-09T00:15:42.521Z","dependency_job_id":null,"html_url":"https://github.com/howl-anderson/MicroTokenizer","commit_stats":{"total_commits":383,"total_committers":1,"mean_commits":383.0,"dds":0.0,"last_synced_commit":"f0ad19ee42fc947f432dfcbe687a474ccc78c7c8"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howl-anderson%2FMicroTokenizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howl-anderson%2FMicroTokenizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howl-anderson%2FMicroTokenizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/howl-anderson%2FMicroTokenizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/howl-anderson","download_url":"https://codeload.github.com/howl-anderson/MicroTokenizer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248601064,"owners_count":21131607,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chinese-nlp","chinese-tokenizer","chinese-word-segmentation","dag-network","educational-project","nlp-machine-learning","tokenizer"],"created_at":"2024-11-21T04:14:38.873Z","updated_at":"2025-04-12T16:47:55.602Z","avatar_url":"https://github.com/howl-anderson.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 微型中文分词器\n\n[![Python package](https://github.com/howl-anderson/MicroTokenizer/actions/workflows/python-package.yml/badge.svg)](https://github.com/howl-anderson/MicroTokenizer/actions/workflows/python-package.yml)\n[![PyPI Status](https://img.shields.io/pypi/v/MicroTokenizer.svg)](https://pypi.python.org/pypi/MicroTokenizer)\n\n一个用于教学目的的微型的中文分词器，目前提供了以下几种分词算法:\n\n1. 按照词语的频率（概率）来利用构建 DAG（有向无环图）来分词，使用 `Trie Tree` 构建前缀字典树\n2. 使用隐马尔可夫模型（Hidden Markov Model，HMM）来分词\n3. 融合 DAG 和 HMM 两种分词模型的结果，按照分词粒度最大化的原则进行融合得到的模型\n4. 正向最大匹配法\n5. 反向最大匹配法\n6. 双向最大匹配法\n7. 基于 CRF (Conditional Random Field, 条件随机场) 的分词方法\n8. UnicodeScriptTokenizer: 当 Unicode Script 发生变化时拆分\n9. EmsembelTokenizer: 按照 Unicode Script 切分文本，然后按照每个文本段的 Unicode Script 调用不同的分词器进行处理。\n\n# 特点 / 特色\n\n* 教学目的：可以导出 `graphml` 格式的图结构文件，辅助学习者理解算法过程\n* 良好的分词性能：由于使用类似 `结巴分词` 的算法，具有良好的分词性能\n* 具有良好的扩展性：使用和 `结巴分词` 一样的字典文件，可以轻松添加自定义字典\n* 自定义能力强\n* 提供工具和脚本帮助用户训练自己的分词模型而不是使用内建的模型\n\n# 演示\n\n## 在线演示\n\n### 在线的 Jupyter Notebook\n点击右侧图标，即可访问 [![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/howl-anderson/MicroTokenizer/master?filepath=.notebooks%2FMicroTokenizer.ipynb)\n\n### Online web demo\n@ [http://nlp_demo.xiaoquankong.ai/](http://nlp_demo.xiaoquankong.ai/)\n\n## 离线演示\n### DAG 分词\n代码：\n```python\nimport MicroTokenizer\n\ntokens = MicroTokenizer.cut(\"知识就是力量\")\nprint(tokens)\n\n```\n输出：\n```python\n['知识', '就是', '力量']\n```\n\n#### 加载用户字典\n```python\nimport MicroTokenizer\n\ntokens = MicroTokenizer.cut(\"「杭研」正确应该不会被切开\", HMM=False)\nprint(tokens)\n\n# loading user's custom dictionary file\nMicroTokenizer.load_userdict('user_dict.txt')\n\ntokens = MicroTokenizer.cut(\"「杭研」正确应该不会被切开\", HMM=False)\nprint(tokens)\n\n```\n\n`user_dict.txt` 的内容为：\n```text\n杭研 10\n\n```\n\n输出:\n```text\n['「', '杭', '研', '」', '正确', '应该', '不会', '被', '切开']\n['「', '杭研', '」', '正确', '应该', '不会', '被', '切开']\n\n```\n\n### 有向无环图效果演示\n![DAG of 'knowledge is power'](.images/DAG_of_knowledge_is_power.png)\n\n#### 备注\n* `\u003cs\u003e` 和 `\u003c/s\u003e` 是图的起始和结束节点，不是实际要分词的文本\n* 图中 Edge 上标注的是 `log(下一个节点的概率的倒数)`\n* 最短路径已经用 `深绿色` 作了标记\n\n### 更多 DAG 分词的演示\n#### \"王小明在北京的清华大学读书\"\n![DAG of xiaomin](.images/DAG_of_xiaomin.png)\n\n### HMM 分词\n因 HMM 模型单独分词性能不佳, 一般情况下只用于和其他模型的融合, 故不在此提供示例, 需要演示者,可在 在线的 Jupyter Notebook 找到使用的例子.\n\n### DAG+HMM 分词\n将前两个模型的结果融合，融合了 DAG 稳定的构词能力和 HMM 的新词发现能力，缺点是速度较慢\n\n### 正向最大匹配法\n\n具体介绍，请阅读博文 [构建中文分词器 - 正向最大匹配法](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/)\n\n### 反向最大匹配法\n\n具体介绍，请阅读博文 [构建中文分词器 - 反向最大匹配法](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/)\n\n### 双向最大匹配法\n\n具体介绍，请阅读博文 [构建中文分词器 - 双向最大匹配法](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/)\n\n### 基于 CRF 的分词\n\nTODO\n\n# 依赖\n只在 python 3.6+ 环境测试过，其他环境不做兼容性保障。\n\n# 安装\n\n```bash\npip install MicroTokenizer\n```\n\n# 如何使用\n\n## 分词\n\n```python\nfrom MicroTokenizer import (\n    hmm_tokenizer,\n    crf_tokenizer,\n    dag_tokenizer,\n    max_match_forward_tokenizer,\n    max_match_backward_tokenizer,\n    max_match_bidirectional_tokenizer,\n)\n\ninput_text = \"王小明在北京的清华大学读书。\"\n\n# 使用相关的算法来分词。\n\nresult = hmm_tokenizer.segment(input_text)\nprint(result)\n\nresult = crf_tokenizer.segment(input_text)\nprint(result)\n\nresult = max_match_forward_tokenizer.segment(input_text)\nprint(result)\n\nresult = max_match_backward_tokenizer.segment(input_text)\nprint(result)\n\nresult = max_match_bidirectional_tokenizer.segment(input_text)\nprint(result)\n\nresult = dag_tokenizer.segment(input_text)\nprint(result)\n\n```\n\n输出：\n\n```python\n['小', '明', '在', '北京', '的', '清华大学', '读书', '。']\n\n```\n\n## Unicode Script 分词\n\n```python\nfrom MicroTokenizer.tokenizers.unicode_script.tokenizer import UnicodeScriptTokenizer\n\n\ntokenizer = UnicodeScriptTokenizer()\ntokens = tokenizer.segment(\"2021年时我在Korea的汉城听了이효리的にほんご这首歌。\")\nprint([(token.text, token.script) for token in tokens])\n\n```\n\n输出：\n\n```python\n[('2021', 'Common'), ('年时我在', 'Han'), ('Korea', 'Latin'), ('的汉城听了', 'Han'), ('이효리', 'Hangul'), ('的', 'Han'), ('にほんご', 'Hiragana'), ('这首歌', 'Han'), ('。', 'Common')]\n```\n\n## Ensemble 分词\n\n### 区分语言进行处理\n\n```python\nfrom MicroTokenizer.tokenizers.ensemble.tokenizer import EnsembleTokenizer\nfrom MicroTokenizer import dag_tokenizer\n\n\ntokenizer = EnsembleTokenizer({\"Han\": dag_tokenizer})\ntokens = tokenizer.segment(\"2021年时我在Korea的汉城听了이효리的にほんご这首歌。\")\nprint(tokens)\n\n```\n\n输出：\n\n```python\n['2021', '年', '时', '我', '在', 'Korea', '的', '汉城', '听', '了', '이효리', '的', 'にほんご', '这', '首', '歌', '。']\n```\n\n### [实验性质] 基于流水线的分词方案\n\n可以稳定的提取数字和电子邮箱地址。可以区分中文英文采用不同的分词方案（英文默认按照空格切分）。\n\n```python\nfrom MicroTokenizer.experimental import dag_tokenizer\n\ntokens = dag_tokenizer.segment(\"我的电话是15555555555，邮箱是xxx@yy.com,工作单位是 Tokyo University。\")\nprint(tokens)\n```\n\n输出：\n\n```python\n['我', '的', '电话', '是', '15555555555', '，', '邮箱', '是', 'xxx@yy.com', ',', '工作', '单位', '是', 'Tokyo', 'University', '。']\n```\n\n## 导出 GraphML 文件\n\n针对基于 DAG 的算法，用户可以导出 GraphML 文件，研究其工作原理。\n\n```python\nfrom MicroTokenizer import dag_tokenizer\n\ndag_tokenizer.graph_builder.build_graph(\"知识就是力量\")\ndag_tokenizer.graph_builder.write_graphml(\"output.graphml\")\n\n```\n\nNOTE: 导出后的 `graphml` 文件可以使用 [Cytoscape](http://www.cytoscape.org/) 进行浏览和渲染\n\n## 如何训练自己的模型\nMicroTokenizer 也提供了工具帮助你训练模型。\n\n### 代码训练\n\n```python\nfrom MicroTokenizer.training.train import train\n\n# you can use multiple files as training data, it is a list\ntrain([\"./corpus.txt\"], \"./model_data\")\n\n```\n\n## 如何使用自己的模型\n\n```python\nfrom MicroTokenizer import MaxMatchBackwardTokenizer\nfrom MicroTokenizer import MaxMatchForwardTokenizer\nfrom MicroTokenizer import MaxMatchBidirectionalTokenizer\nfrom MicroTokenizer import DAGTokenizer\nfrom MicroTokenizer import HMMTokenizer\nfrom MicroTokenizer import CRFTokenizer\n\nmodel_dir = \"path/to/your/model\"\ninput_text = \"你的待分词文本\"\n\nmax_match_backward_tokenizer = MaxMatchBackwardTokenizer.load(model_dir)\ntokens = max_match_backward_tokenizer.segment(input_text)\n\nmax_match_forward_tokenizer = MaxMatchForwardTokenizer.load(model_dir)\ntokens = max_match_forward_tokenizer.segment(input_text)\n\nmax_match_bidirectional_tokenizer = MaxMatchBidirectionalTokenizer.load(model_dir)\ntokens = max_match_bidirectional_tokenizer.segment(input_text)\n\ndag_tokenizer = DAGTokenizer.load(model_dir)\ntokens = dag_tokenizer.segment(input_text)\n\nhmm_tokenizer = HMMTokenizer.load(model_dir)\ntokens = hmm_tokenizer.segment(input_text)\n\ncrf_tokenizer = CRFTokenizer.load(model_dir)\ntokens = crf_tokenizer.segment(input_text)\n\n```\n\n\n# Roadmap\n* [DONE] DAG 模型融合 HMM 模型 以处理 OOV 以及提高 Performance\n* [DONE] 和主流分词模型做一个分词能力的测试 @ [中文分词软件基准测试 | Chinese tokenizer benchmark](https://github.com/howl-anderson/Chinese_tokenizer_benchmark)\n* [DONE] 使用 `Trie Tree` 来压缩运行时内存和改善前缀查找速度\n* [DONE] 允许添加自定义 DAG 词典\n* [DONE] 开发自定义 DAG 字典构造 Feature, 允许用户构建自己的 DAG 字典\n* [DONE] 开发自定义 HMM 参数构建 Feature, 允许用户训练自己的 HMM 模型\n* [DONE] 引入 CRF 分词模型，使用 python-crfsuite\n* [DOING] 模型系统分成代码和模型两个部分，用户可以选择性的下载和安装模型以及让用户训练和安装定制的模型\n* [TODO] 引入 char-level word embedding + Bi-LSTM + CRF 分词模型，参考 [FoolNLTK](https://github.com/rockyzhengwu/FoolNLTK)\n* [TODO] 将预测和训练代码分成不同的模块\n* [DOING] 添加测试代码，确保功能正常和 Python 2/3 的兼容性\n* [TODO] 增加并发处理的能力\n* [TODO] 允许用户非常方便的训练自己的模型包\n* [DONE] 使用人明日报字典替换 jieba 提供的字典\n* [TODO] 添加 jieba 兼容的 banana peel 接口\n* [TODO] 使用 scikit-crfsuite 替代 python-crfsuite\n* [TODO] 移除对于模型的下载、安装、连接等支持（资源有限，无力支持）","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhowl-anderson%2Fmicrotokenizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhowl-anderson%2Fmicrotokenizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhowl-anderson%2Fmicrotokenizer/lists"}