Skip to main content

基于隐马尔可夫模型(HMM)的中文分词和序列标注工具库

Project description

pynlptool

PyPI version Python Version License: MIT

基于隐马尔可夫模型(HMM)的中文分词和序列标注库。

A Hidden Markov Model (HMM) based Chinese word segmentation and sequence labeling library.

特性 / Features

  • 纯 Python 实现,无外部依赖
  • 内置预训练模型,开箱即用
  • 支持 BMES 标注体系
  • 支持自定义训练数据
  • 内置模型评估和超参数调优
  • 模型序列化保存与加载

安装 / Installation

pip install pynlptool

快速开始 / Quick Start

使用内置模型(推荐)

from pynlptool import cut, load_model

# 方式一:直接分词(最简单)
words = cut("今天天气不错")
print(words)  # ['今天', '天气', '不错']

# 方式二:加载模型后多次使用
model = load_model()
words1 = model.cut("今天天气不错")
words2 = model.cut("我喜欢编程")

# 获取标注序列
tags = model.decode(list("今天天气"))
print(list(zip(list("今天天气"), tags)))

训练模型

from pynlptool import train, HMM

# 准备训练数据: 列表,每个元素为 (observations, tags) 元组
sequences = [
    (["今", "天", "天", "气", "不", "错"], ["B", "E", "B", "E", "B", "E"]),
    (["我", "喜", "欢", "编", "程"], ["S", "B", "E", "B", "E"]),
]

# 训练模型
model = train(sequences, alpha=0.5, min_freq=1)

# 保存模型
model.save("my_model.pkl")

加载模型并预测

from pynlptool import HMM

# 加载模型
model = HMM.load("my_model.pkl")

# 预测
text = list("今天天气真好")
tags = model.decode(text)
print(list(zip(text, tags)))

使用数据工具

from pynlptool import load_data, norm_char, norm_seq

# 加载标注数据
sentences = load_data("train.txt")

# 字符归一化
normalized = norm_seq(list("2024年"))
print(normalized)  # ['<NUM>', '<NUM>', '<NUM>', '<NUM>', '年']

模型评估

from pynlptool import evaluate, report

# 评估模型
test_sequences = [(s.observations, s.tags) for s in test_sentences]
predictions = [model.decode(obs) for obs, _ in test_sequences]
metrics = evaluate(test_sequences, predictions)

# 生成报告
r = report(metrics)
print(r)

命令行工具 / CLI

安装后可以使用命令行工具进行预测:

pynlptool "今天天气不错"

数据格式 / Data Format

训练数据应为空行分隔的句子,每行一个字符及其标签:

今 B
天 E
天 B
气 E

我 S
喜 B
欢 E

API 参考 / API Reference

load_model()

加载内置的预训练模型,可直接用于分词。模型会被缓存,多次调用不会重复加载。

cut(text)

使用内置模型对文本进行分词的便捷函数。

  • text: 待分词的中文文本

tag(text)

使用内置模型获取字符标注结果。

  • text: 待标注的中文文本
  • 返回: List[Tuple[str, str]] 字符-标签对列表

show(text)

使用内置模型获取格式化的标注显示结果。

  • text: 待标注的中文文本
  • 返回: 格式化的字符串

train(sequences, alpha=0.1, min_freq=3, unk_token="<UNK>", tag_penalty=-20.0)

训练 HMM 模型。

  • sequences: 训练数据,Iterable[Tuple[List[str], List[str]]]
  • alpha: 平滑参数
  • min_freq: 最小词频阈值
  • unk_token: 未知词标记
  • tag_penalty: 标签惩罚系数

HMM

HMM 模型类。

  • decode(observations): Viterbi 解码,返回标签序列
  • cut(text): 对中文文本进行分词,返回词列表
  • save(path): 保存模型
  • load(path): 加载模型(静态方法)

load_data(path)

加载标注数据文件。

evaluate(sequences, predictions)

评估模型预测结果。

norm_char(ch) / norm_seq(seq)

字符/序列归一化处理。

许可证 / License

MIT License

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

pynlptool-0.1.0.tar.gz (457.1 kB view details)

Uploaded Source

Built Distribution

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

pynlptool-0.1.0-py3-none-any.whl (460.0 kB view details)

Uploaded Python 3

File details

Details for the file pynlptool-0.1.0.tar.gz.

File metadata

  • Download URL: pynlptool-0.1.0.tar.gz
  • Upload date:
  • Size: 457.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for pynlptool-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7aab7e80551f648ab9051fc9e6562fc5bd4560204a9d588713844dbec295eb6a
MD5 67f19d006ff7ba03cd671a122aee9c33
BLAKE2b-256 a6eaa00daf962338df8a47a3ad26da8ffa59ce6bc704bbe31edb7f264f4e6e99

See more details on using hashes here.

File details

Details for the file pynlptool-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pynlptool-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 460.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.5

File hashes

Hashes for pynlptool-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a0277b194c4a29984ddf7c211b6ef724d2209f61f4abd9f69ae2c94ed0d2812
MD5 ac47977a2613dbc814bacd2844faeeb0
BLAKE2b-256 1c960f04f2a2a9da657e562e2ada57341024eef837128280e66daf67099d57a3

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