tts-polyphone
About 3 min
tts-polyphone
中文 TTS 多音字消歧库:把「一行代码」(读 háng)这类易错词在合成输入里改写成同音同调替身字(读「一航代码」),屏幕展示的文字保持原文不变。
任何 TTS 引擎(edge-tts / Azure / HoJo / Qwen3 / Web Speech)都没有可靠的读音标记通道——本库用改字法在引擎之前解决,引擎无感知。
在线试听与演示视频
- Interactive playground: aiti.pw/demo/tts-polyphone — click 🔊 to hear the rewritten pronunciation (browser Web Speech API, free).
- Demo video (81s): aiti.pw/demo/tts-polyphone/demo.mp4 — the Chinese narration is disambiguated live by the default table (
一行代码reads háng,行数reads hángshù,第一百行reads bǎiháng) while subtitles keep the original text.
How the demo video was made
The demo video was recorded automatically by aiti-video-studio using these two files:
| File | Purpose |
|---|---|
| scene.json | video-studio recording script: setup auto-hosts the demo page, 8 narration steps with scrolling + overlays, pronounce disambiguation config |
| index.html | the demo page itself (the recording surface) |
During recording, narration text is rewritten by the tts-polyphone default table before hitting edge-tts — the pronunciation in the video is the library's real output (dogfooding).
安装
| 语言 | 包 | 安装 |
|---|---|---|
| JavaScript / TypeScript | @aiti-ai/tts-polyphone(npm) | npm i @aiti-ai/tts-polyphone |
| CLI | 同包 bin: tts-polyphone | npx @aiti-ai/tts-polyphone |
| Java | pw.aiti:tts-polyphone(Maven Central) | pom.xml 加依赖 |
| Rust | tts-polyphone(crates.io) | cargo add tts-polyphone |
快速上手
Node.js / 浏览器
import { applyPolyphone } from "@aiti-ai/tts-polyphone";
// 内置默认表
applyPolyphone("一行代码快速演示"); // "一航代码快速演示"
// 关闭默认表、只用自己的规则
applyPolyphone("产品名杉山", { useDefaults: false, rules: [{ from: "杉山", to: "山山" }] });CLI(四种输入方式)
echo "一行代码快速演示,中标了。" | npx -y @aiti-ai/tts-polyphone # ① 管道
npx -y @aiti-ai/tts-polyphone -i "一行代码快速演示" # ② 内联
npx -y @aiti-ai/tts-polyphone -f input.txt -o spoken.txt # ③ 文件 → 文件
npx -y @aiti-ai/tts-polyphone input.txt --json # ④ 位置参数 + JSONJava
import pw.aiti.ttspolyphone.TtsPolyphone;
TtsPolyphone.rewrite("一行代码快速演示"); // "一航代码快速演示"Rust
use tts_polyphone::rewrite;
rewrite("一行代码快速演示"); // "一航代码快速演示"内置默认表(22 条)
来自 azure-tts-lexicon-cn(MIT)+ PaddleSpeech polyphonic.yaml(Apache-2.0), 离线转换、逐条人工审核。
| 原文 | 替身 | 读音 |
|---|---|---|
| 一行代码 | 一航代码 | háng |
| 一行人 | 一形人 | xíng |
| 中标 | 众标 | zhòng |
| 湖泊 | 湖坡 | pō |
| 刹车 | 杀车 | shā |
| 曝光 | 爆光 | bào |
| 抗住 | 扛住 | káng |
| 第3行 / 第一百行 / 三千行 | …航 | háng |
| 行数 / 行间距 / 行号 | …航 | háng |
规则结构
{ from: string, to: string, regex?: boolean, note?: string }- literal:
from做子串替换(最常用) - regex:
from当正则($1..$n反向引用),语境锚定用 - 顺序应用,后者改写前者输出;同 key 后者覆盖前者
在 aiti 生态中
| 产品 | 集成方式 |
|---|---|
| aiti-ai-speek(跟读模式) | 朗读文本自动消歧 + 设置面板「多音字映射」管理 |
| aiti-video-studio(视频工作室) | pronounce.rules 工程级/步级规则 + 共享用户映射文件 |
| CLI | 任何管线的 TTS 前置处理(npx 直接用) |
数据来源
- azure-tts-lexicon-cn(MIT)— 社区 Azure/Edge 读错词表
- PaddleSpeech polyphonic.yaml(Apache-2.0)— TTS 前端纠音字典
- 离线转换、逐条人工审核;上游更新后重跑
tools/generate-rules.mjs
License
MIT。
Install the agent skill individually (skills.sh ecosystem, works with any coding agent):
npx skills add aiti-ai/aiti-ai --skill chinese-tts-spoken-text -a claude-codeUse without installing: npx skills use aiti-ai/aiti-ai@chinese-tts-spoken-text.