Skip to main content

fenci

中文分词模块:继承了jieba分词的基本算法逻辑,进行了全方位的代码优化,还额外提供了HMM算法的训练功能支持。

本分词器采用基于词典的最大正向匹配算法为主,辅以HMM(隐马尔可夫模型)进行未登录词识别。在 SIGHAN Bakeoff 2005 数据集上的评测结果为:Precision 81.33%、Recall 83.21%、F1 82.26%,处理速度达 1157.2 KB/s。结果表明,该分词器在准确性与处理效率之间取得了良好平衡,适用于对实时性要求较高的通用文本分词场景。

设计

数据存储格式

不使用marshal,这并不规范,也不使用pickle,在某些情况下确实使用pickle是必要的,但至少在这里数据格式还没必要上pickle。而是使用更通用和更安全的json数据存储格式。

模型数据就存放在 \AppData\Local\Temp 里面的 fenci.cache ,其就是一个json文件。

读写速度模型文件未建立需要1秒多,模型文件建立正常读写文件需要0.3秒多,值得一提的是本程序经过优化只要你一直调用 s=Segment() 同一对象,则读取模型只会读取一次,也就是后面多次cut则前面的0.3秒加载时间几乎可以忽略不计。

USAGE

lcut or cut

from fenci.segment import Segment
segment = Segment()
res = segment.lcut("这是一段测试文字。")

load_userdict

from fenci.segment import Segment
s = Segment()
s.load_userdict('tests/test_dict.txt')

training

指定root和regexp来搜索指定文件夹下的文本,其中的文本格式如下:

’  我  扔  了  两颗  手榴弹  ,  他  一下子  出  溜  下去  。

即该分词的地方空格即可。

    def training(self, root=None, regexp=None):
        """
        根据已经分好词的内容来训练
        :param root:
        :param regexp:
        :return:
        """

注意training之后词典库还只是on-fly模式,要保存到模型需要调用方法save_model

training_hmm

训练HMM模型,如果设置update_dict=True,则语料库的词语数据也会刷入进来。

    def training_hmm(self, root=None, regexp=None, update_dict=False):

save_model

所有on-fly的词库都导入到模型里面

    def save_model(self, save_hmm=False):

add_word

    def add_word(self, word, freq=1):

tokenize 和 lcut

给nltk调用提供的接口

hmm_segment

默认内部构建的hmm分词器

    self.hmm_segment = HMMSegment(traning_root=traning_root,
                                  traning_regexp=traning_regexp,
                                  cache_file=self.cache_file)

HMMSegment

training

指定root和regexp来搜索指定文件夹下的文本,其中的文本格式如下:

’  我  扔  了  两颗  手榴弹  ,  他  一下子  出  溜  下去  。

即该分词的地方空格即可。

    def training(self, root=None, regexp=None, training_mode='update'):

提供了两种训练模式 update 和 replace 。

update模式将在原有HMM训练数据基础上继续训练,注意训练之后的模型数据仍是on-fly的。保存需要调用save_model方法。

save_model

将hmm_segment分词器的模型保存下来。

self.hmm_segment.save_model()

评估

评测使用 SIGHAN Bakeoff 2005 金标准文件 :

=== 分词评测结果 ===
总词数(金标准): 106873
总词数(预测):   109343
正确词数:       88926
Precision:      81.33%
Recall:         83.21%
F1:             82.26%
结果已写入 my_result.txt

=== 速度测试 ===
文本大小:       539.3 KB
重复次数:       3
平均耗时:       0.470 s
速度:           1146.6 KB/s

上传到pypi

python setup.py sdist bdist_wheel
twine check dist/*
twine upload dist/*

Release files for fenci 0.3.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fenci 0.3.5
File Size Uploaded
fenci-0.3.5.tar.gz 715.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fenci 0.3.5
File Interpreter ABI Platform
fenci-0.3.5-py3-none-any.whl Python 3 none any Details

Total release size: 1.4 MB

Release files / fenci-0.3.5.tar.gz

Download URL fenci-0.3.5.tar.gz
Size 715.7 kB
Tags Source
SHA-256 checksum
How to use checksums
145b4b075d61bb0541f2d9d80b55548fba895928bca637fd86db7f58de952d95
BLAKE2b-256 checksum
How to use checksums
a48d36479823171141990a97a942faec1930a503ad9b01367d318e5bdd3f9af8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / fenci-0.3.5-py3-none-any.whl

Download URL fenci-0.3.5-py3-none-any.whl
Size 711.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
75dedbb2e14b45580cc3084e18e22f2bcc740dd732ef00ca28af12c5bb65c464
BLAKE2b-256 checksum
How to use checksums
aa6de590f0eb9e7cfe2fe543ee68286270f9e4290d70d5e5ac9c51813bd253f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release history Release notifications | RSS feed

0.4.1

2 release files

0.4.0

2 release files

This release

0.3.5 This release

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.0

3 release files

0.2.2

3 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page