itntext multilingual text normalization and inverse text normalization
Project description
itntext
itntext 是一个 wetext 风格的多语言文本规范化库,支持文本规范化(TN)和逆文本规范化(ITN)。项目内置多语言 grammar、TSV 资源、FST 编译脚本和基于 kaldifst 的运行时,既可以直接加载预编译 FST 使用,也可以在本地修改规则后重新编译。
from itntext import Normalizer
normalizer = Normalizer(lang="zh", operator="itn")
print(normalizer.normalize("上午十点三十分"))
# 上午 10:30
特性
- wetext 风格 API:
Normalizer(lang="zh", operator="itn").normalize(text)。 - 多语言覆盖:ITN 支持 12 种语言,TN 支持 5 种语言。
- 自主编译 FST:修改
itntext/grammars后运行scripts/build_fsts.py生成新的.fst。 - 规则层优化:中文数字、日期、时间、金额、单位、电话等核心场景在 grammar/TSV/FST 层处理。
- 服务端友好:支持全局 FST 缓存、
preload()预加载和normalize_list()批量处理。 - 可复现实验:内置与 wetext 的对比脚本、全场景测试报告和 CSV 明细。
支持语言
| 模式 | 支持语言 |
|---|---|
| ITN | de, en, es, fr, id, ja, ko, pt, ru, tl, vi, zh |
| TN | de, en, es, ru, zh |
auto 目前按 wetext 风格路由到 zh。
安装
从 PyPI 安装:
pip install itntext
从 GitHub 安装:
pip install git+https://github.com/alanpaine/itntext.git
从本地源码安装:
pip install -e .
从本地 wheel 安装:
pip install dist/itntext-0.1.6-py3-none-any.whl
安装包中的 wheel 会包含预编译 FST。普通用户安装后可以直接运行,不需要安装 pynini,只有修改 grammar 并重新编译 FST 时才需要额外安装:
pip install "pynini>=2.1.5"
运行测试和对比脚本:
pip install -e ".[test]"
快速使用
中文 TN
from itntext import Normalizer
tn = Normalizer(lang="zh", operator="tn")
print(tn.normalize("123"))
# 一百二十三
print(tn.normalize("2026年6月12日"))
# 二零二六年六月十二日
print(tn.normalize("13800000000"))
# 幺三八零零零零零零零零
中文 ITN
from itntext import Normalizer
itn = Normalizer(lang="zh", operator="itn")
print(itn.normalize("五月二十三号"))
# 5月23号
print(itn.normalize("上午十点三十分"))
# 上午 10:30
print(itn.normalize("十二元五角三分"))
# ¥12.53
print(itn.normalize("十二公斤"))
# 12kg
英文与多语言
from itntext import Normalizer
print(Normalizer(lang="en", operator="tn").normalize("$12.50"))
# twelve dollars fifty cents
print(Normalizer(lang="ja", operator="itn").normalize("百二十三"))
# 123
print(Normalizer(lang="ru", operator="tn").normalize("123"))
# сто двадцать три
服务端用法
服务中不要每句话都创建新的 Normalizer。复用同一个实例可以避免重复加载 FST。
from itntext import Normalizer, preload
preload(["zh", "en"], operators=["tn", "itn"])
normalizer = Normalizer(lang="zh", operator="itn")
results = normalizer.normalize_list([
"五月二十三号",
"上午十点三十分",
"十二元五角",
])
CLI
itntext --lang zh --operator tn "今天是2026年6月12日"
itntext --lang zh --operator itn "十二元五角三分"
itntext --lang en --operator itn "twelve dollars fifty cents"
编译 FST
源码包和 wheel 已包含预编译 FST。只有修改 itntext/grammars 下的 grammar 或 TSV 后,才需要重新编译。
python scripts/build_fsts.py --operator all --language all --overwrite
python scripts/build_fsts.py --operator tn --language zh --overwrite
python scripts/build_fsts.py --operator itn --language zh --overwrite
FST 输出目录:
itntext/fst/itn/<lang>/<lang>_itn_tagger.fst
itntext/fst/itn/<lang>/<lang>_itn_verbalizer.fst
itntext/fst/tn/<lang>/<lang>_tn_tagger.fst
itntext/fst/tn/<lang>/<lang>_tn_verbalizer.fst
更完整的编译说明见 docs/BUILDING_FSTS.md。
测试与对比
python -m pytest tests -q
python scripts/full_scenario_compare.py
python scripts/compare_wetext.py
python scripts/comprehensive_compare.py
正式文档只有三个,位于 docs/:
测试、对比和验收报告放在 reports/:
reports/FULL_SCENARIO_COMPARE.mdreports/FULL_SCENARIO_COMPARE.csvreports/FULL_SCENARIO_PERFORMANCE.csvreports/COMPREHENSIVE_COMPARE.mdreports/COMPREHENSIVE_COMPARE.csvreports/WETEXT_COMPARE.mdreports/DELIVERY_VALIDATION.md
项目结构
itntext/
normalizer.py
fst_processor.py
fst/
grammars/
scripts/
tests/
docs/
reports/
规则开发原则
项目不在 Normalizer.normalize() 里用 if/replace/regex 修补结果。新增语言行为时,应修改 itntext/grammars 下的 grammar 或 TSV,再重新编译 FST。这样可以保持运行时简单、结果可复现,也更接近 wetext 的编译式使用方式。
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
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 itntext-0.1.6.tar.gz.
File metadata
- Download URL: itntext-0.1.6.tar.gz
- Upload date:
- Size: 15.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4413354867a8afb61699aaab9f6cc0b8779db2c0011d5615fc062cf324403ffb
|
|
| MD5 |
2714393f3d4e7716adcf57a3729cb865
|
|
| BLAKE2b-256 |
ce084b6d7dc91b24b0dd43c3e29775986305c36981e07b6d891f53fa7918d617
|
File details
Details for the file itntext-0.1.6-py3-none-any.whl.
File metadata
- Download URL: itntext-0.1.6-py3-none-any.whl
- Upload date:
- Size: 16.7 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d668a39cbde974e979ca0e63010e7857220be142af6c60e5538b077b35bad3aa
|
|
| MD5 |
58d9681364481f1deffcfe06dbcae5aa
|
|
| BLAKE2b-256 |
ab95d0b026437504f2d73650bb008d8e654d4c080a85bfb0baf5999de015a848
|