拼音分割器,用于将连续的拼音字符串分割为有效的拼音片段
Project description
Pinyin Segmenter 拼音分割器
一个高效的中文拼音分割器,用于将连续的拼音字符串分割为有效的拼音片段。
功能特点
- ✅ 准确分割:能够准确分割连续的拼音字符串
- ✅ 智能识别:支持带声调和不带声调的拼音
- ✅ 高效算法:使用递归回溯算法,性能优秀
- ✅ 完整覆盖:基于 pypinyin 的标准拼音字典
- ✅ 易于使用:简洁的 API 接口
安装
使用 pip 安装
pip install pinyin-segmenter
快速开始
基础用法
from pinyin_segmenter import PinyinSegmenter, segment_pinyin
# 创建分割器实例
segmenter = PinyinSegmenter()
# 分割连续的拼音字符串
result = segmenter.segment("zhongguoren")
print(result) # 输出: ['zhong', 'guo', 'ren']
# 使用便捷函数
result = segment_pinyin("nihao")
print(result) # 输出: ['ni', 'hao']
更多示例
from pinyin_segmenter import segment_pinyin
# 示例 1:基本分割
print(segment_pinyin("woaizhongguo")) # ['wo', 'ai', 'zhong', 'guo']
# 示例 2:长字符串分割
print(segment_pinyin("mingyuejishiyoubajiuwenqingtian。")) # ['ming', 'yue', 'ji', 'shi', 'you', 'ba', 'jiu', 'wen', 'qing', 'tian']
# 示例 3:特殊拼音
print(segment_pinyin("erzi")) # ['er', 'zi']
# 示例 4:无法分割的情况
print(segment_pinyin("xyz")) # []
API 文档
PinyinSegmenter 类
PinyinSegmenter()
创建拼音分割器实例。
segment(pinyin_str: str) -> List[str]
分割拼音字符串。
参数:
pinyin_str: 待分割的拼音字符串(可以包含声调符号)
返回:
分割后的拼音列表,如果无法分割则返回空列表。
segment_pinyin(pinyin_str: str) -> List[str] 便捷函数
使用 PinyinSegmenter 将拼音字符串分割。
参数:
pinyin_str: 待分割的拼音字符串(可以包含声调符号)
返回:
分割后的拼音列表,如果无法分割则返回空列表。
工作原理
拼音分割器基于以下原理工作:
- 字典匹配:使用标准的拼音字典进行匹配
- 结构验证:检查拼音的声母-韵母结构是否合法
- 递归回溯:使用深度优先搜索算法寻找所有可能的分割方案
- 最长匹配:优先尝试较长的拼音匹配
- 支持的拼音特征:
- 所有标准声母(包括 zh, ch, sh)
- 所有标准韵母(包括 er, ng 等特殊组合)
- 自动去除 Unicode 声调符号
注意(AI生成标识)
- 本代码核心算法由DeepSeek大模型生成和完善。
- 包管理者对由AI生成的代码局部进行了修改和优化。
版本信息
v0.1.0
- 初始版本发布
- 实现核心分割功能
- 提供完整的 API 接口
- 添加测试用例
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pinyin_segmenter-0.1.0.tar.gz.
File metadata
- Download URL: pinyin_segmenter-0.1.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97f931531808bf7945b59242672135d4086e38d048aa77f0a822fc9a48319676
|
|
| MD5 |
971469a2071f5bf7fa56c7a8b2786e92
|
|
| BLAKE2b-256 |
d77a85ab2ad1500972b617f9b1ae41d9e0e16c398269f78c9437c9760324d436
|
File details
Details for the file pinyin_segmenter-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pinyin_segmenter-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
beeec710eb4a6e6c26cd327c3d2fcfac7dfebf9a35d0ae549894bb9294dc892b
|
|
| MD5 |
2eb77c48ac71a3bd941971ca26f00a57
|
|
| BLAKE2b-256 |
80ae608b8e40c48f199f6fd422f7bd5d2453991829c14e19a2af21651071c22e
|