fenci
中文分词模块
本分词器采用基于词典的最大正向匹配算法为主,辅以HMM(隐马尔可夫模型)进行未登录词识别。在 SIGHAN Bakeoff 2005 数据集上的评测结果为:Precision 83.59%、Recall 84.47%、F1 84.03%,处理速度达 1145.6 KB/s。结果表明,该分词器在准确性与处理效率之间取得了良好平衡,适用于对实时性要求较高的通用文本分词场景。
重要提示
- 模型文件默认是
\AppData\Local\Temp里面的fenci_model,该模型实际就是一个json文件。后续你可以继续训练该模型,也可以回滚该模型 (seg.reset_model())。 - 推荐将
seg = Segment()放在一个更全局的位置,而不要频繁创建它。
安装
pip install fenci
使用
lcut or cut
from fenci.segment import Segment
seg = Segment()
res = seg.lcut("这是一段测试文字。")
加载自定义词库
from fenci.segment import Segment
s = Segment()
s.load_userdict('tests/test_dict.txt')
训练模型
指定root和regexp来搜索指定文件夹下的文本,其中的文本格式如下:
’ 我 扔 了 两颗 手榴弹 , 他 一下子 出 溜 下去 。
即该分词的地方空格即可。
from fenci import Segment
seg = Segment()
seg.training('../icwb2-data/training', 'msr_training.utf8', with_hmm=True)
seg.save_model(save_hmm=True)
注意training之后词典库还只是on-fly模式,要保存到模型需要调用方法save_model
只训练HMM模型
from fenci import Segment
seg = Segment()
seg.hmm_segment.traning('../icwb2-data/training', 'msr_training.utf8')
seg.hmm_segment.save_model()
只训练词库
from fenci import Segment
seg = Segment()
seg.traning('../icwb2-data/training', 'msr_training.utf8', with_hmm=False)
seg.save_model(save_hmm=False)
回滚模型
回滚到默认模型
from fenci import Segment
s = Segment()
s.reset_model(model='default')
评估
评测使用 SIGHAN Bakeoff 2005 金标准文件 :
=== 分词评测结果 ===
总词数(金标准): 106873
总词数(预测): 107996
正确词数: 90279
Precision: 83.59%
Recall: 84.47%
F1: 84.03%
=== 速度测试 ===
文本大小: 539.3 KB
重复次数: 3
平均耗时: 0.471 s
速度: 1145.6 KB/s
Release files for fenci 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| fenci-0.4.0.tar.gz | 1.9 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fenci-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 3.8 MB
Release files / fenci-0.4.0.tar.gz
| Download URL | fenci-0.4.0.tar.gz |
|---|---|
| Size | 1.9 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d4f1f29987277844059f83f8a16eafaaec53ff52f05f32225b321e7b28fafa81
|
|
BLAKE2b-256 checksum How to use checksums |
3d620600ae95f2ae18e6d916e0eb9f8526459e54e4e7bae7c1acdb412290569f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 29, 2026.
Transparency logRelease files / fenci-0.4.0-py3-none-any.whl
| Download URL | fenci-0.4.0-py3-none-any.whl |
|---|---|
| Size | 1.9 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8fb8bdc5db0b752d44489a120920000be03a4fb0545159070c188bc8ff082351
|
|
BLAKE2b-256 checksum How to use checksums |
6f5f37e76117e90fdd263860d2245c5175559c46c74c3fe0d77ccc8635d2dee2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 29, 2026.
Transparency log