Skip to main content

中文分词

Project description

fenci

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

设计

数据存储格式

不使用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()

上传到pypi

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

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

fenci-0.3.4.tar.gz (714.6 kB view details)

Uploaded Source

Built Distribution

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

fenci-0.3.4-py3-none-any.whl (711.3 kB view details)

Uploaded Python 3

File details

Details for the file fenci-0.3.4.tar.gz.

File metadata

  • Download URL: fenci-0.3.4.tar.gz
  • Upload date:
  • Size: 714.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for fenci-0.3.4.tar.gz
Algorithm Hash digest
SHA256 d8f55a6a6d8a31fac1ae7c69da3d8bdd61dab79477c18a0841f2a532489f2a7d
MD5 18b08853f47c0f6de1786485d635bf29
BLAKE2b-256 71cfd6d29129431ac727bc54009547b98f7c5c66538da3ee4203d48a9d15423d

See more details on using hashes here.

File details

Details for the file fenci-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: fenci-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 711.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for fenci-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 645a3762b634895e00a474d5be5daaec1b6eb95e6c9faef7b7f9888a81e86256
MD5 d9654905518e3ab600dd898173f1f78a
BLAKE2b-256 35f4febefdc9c1c6cc43eeb8377e648983327e9155370ffbcf9a63cb20293609

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