Skip to main content

itntext

PyPI Python License

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/:

项目结构

itntext/
  normalizer.py
  fst_processor.py
  fst/
  grammars/
scripts/
tests/
docs/
reports/

规则开发原则

项目不在 Normalizer.normalize() 里用 if/replace/regex 修补结果。新增语言行为时,应修改 itntext/grammars 下的 grammar 或 TSV,再重新编译 FST。这样可以保持运行时简单、结果可复现,也更接近 wetext 的编译式使用方式。

Metadata

Release files for itntext 0.1.6

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

Source distribution (sdist)

Source distribution for itntext 0.1.6
File Size Uploaded
itntext-0.1.6.tar.gz 15.8 MB Details

Built distribution (wheel)

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

Total release size: 32.5 MB

Release files / itntext-0.1.6.tar.gz

Download URL itntext-0.1.6.tar.gz
Size 15.8 MB
Tags Source
SHA-256 checksum
How to use checksums
4413354867a8afb61699aaab9f6cc0b8779db2c0011d5615fc062cf324403ffb
BLAKE2b-256 checksum
How to use checksums
ce084b6d7dc91b24b0dd43c3e29775986305c36981e07b6d891f53fa7918d617
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release files / itntext-0.1.6-py3-none-any.whl

Download URL itntext-0.1.6-py3-none-any.whl
Size 16.7 MB
Tags Python 3
SHA-256 checksum
How to use checksums
d668a39cbde974e979ca0e63010e7857220be142af6c60e5538b077b35bad3aa
BLAKE2b-256 checksum
How to use checksums
ab95d0b026437504f2d73650bb008d8e654d4c080a85bfb0baf5999de015a848
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

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