phonofix 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- phonofix-0.1.0/.gitignore +122 -0
- phonofix-0.1.0/CLAUDE.md +323 -0
- phonofix-0.1.0/PKG-INFO +806 -0
- phonofix-0.1.0/README.md +762 -0
- phonofix-0.1.0/README.zh-TW.md +756 -0
- phonofix-0.1.0/examples/chinese_examples.py +420 -0
- phonofix-0.1.0/examples/english_examples.py +434 -0
- phonofix-0.1.0/examples/mixed_language_examples.py +286 -0
- phonofix-0.1.0/examples/realtime_streaming_examples.py +375 -0
- phonofix-0.1.0/pyproject.toml +138 -0
- phonofix-0.1.0/references/corrector.py +860 -0
- phonofix-0.1.0/references/fuzzy_toolbox.py +432 -0
- phonofix-0.1.0/requirements.txt +32 -0
- phonofix-0.1.0/scripts/setup_espeak.ps1 +125 -0
- phonofix-0.1.0/scripts/setup_espeak.sh +245 -0
- phonofix-0.1.0/scripts/setup_espeak_windows.bat +117 -0
- phonofix-0.1.0/src/phonofix/__init__.py +161 -0
- phonofix-0.1.0/src/phonofix/backend/__init__.py +22 -0
- phonofix-0.1.0/src/phonofix/backend/base.py +70 -0
- phonofix-0.1.0/src/phonofix/backend/chinese_backend.py +257 -0
- phonofix-0.1.0/src/phonofix/backend/english_backend.py +390 -0
- phonofix-0.1.0/src/phonofix/core/phonetic_interface.py +57 -0
- phonofix-0.1.0/src/phonofix/core/tokenizer_interface.py +45 -0
- phonofix-0.1.0/src/phonofix/correction/__init__.py +31 -0
- phonofix-0.1.0/src/phonofix/correction/protocol.py +87 -0
- phonofix-0.1.0/src/phonofix/correction/streaming_corrector.py +469 -0
- phonofix-0.1.0/src/phonofix/correction/unified_corrector.py +295 -0
- phonofix-0.1.0/src/phonofix/engine/__init__.py +22 -0
- phonofix-0.1.0/src/phonofix/engine/base.py +122 -0
- phonofix-0.1.0/src/phonofix/engine/chinese_engine.py +256 -0
- phonofix-0.1.0/src/phonofix/engine/english_engine.py +262 -0
- phonofix-0.1.0/src/phonofix/engine/unified_engine.py +202 -0
- phonofix-0.1.0/src/phonofix/languages/chinese/__init__.py +33 -0
- phonofix-0.1.0/src/phonofix/languages/chinese/config.py +122 -0
- phonofix-0.1.0/src/phonofix/languages/chinese/corrector.py +614 -0
- phonofix-0.1.0/src/phonofix/languages/chinese/fuzzy_generator.py +307 -0
- phonofix-0.1.0/src/phonofix/languages/chinese/number_variants.py +203 -0
- phonofix-0.1.0/src/phonofix/languages/chinese/phonetic_impl.py +103 -0
- phonofix-0.1.0/src/phonofix/languages/chinese/tokenizer.py +50 -0
- phonofix-0.1.0/src/phonofix/languages/chinese/utils.py +261 -0
- phonofix-0.1.0/src/phonofix/languages/english/__init__.py +42 -0
- phonofix-0.1.0/src/phonofix/languages/english/config.py +145 -0
- phonofix-0.1.0/src/phonofix/languages/english/corrector.py +321 -0
- phonofix-0.1.0/src/phonofix/languages/english/fuzzy_generator.py +231 -0
- phonofix-0.1.0/src/phonofix/languages/english/phonetic_impl.py +429 -0
- phonofix-0.1.0/src/phonofix/languages/english/tokenizer.py +51 -0
- phonofix-0.1.0/src/phonofix/router/language_router.py +64 -0
- phonofix-0.1.0/src/phonofix/utils/__init__.py +35 -0
- phonofix-0.1.0/src/phonofix/utils/lazy_imports.py +247 -0
- phonofix-0.1.0/src/phonofix/utils/logger.py +217 -0
- phonofix-0.1.0/tests/__init__.py +3 -0
- phonofix-0.1.0/tests/test_chinese_corrector.py +85 -0
- phonofix-0.1.0/tests/test_engine.py +195 -0
- phonofix-0.1.0/tests/test_english_corrector.py +98 -0
- phonofix-0.1.0/tests/test_unified_corrector.py +200 -0
- phonofix-0.1.0/tools/benchmark_phonetic.py +134 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
*.pyd
|
|
9
|
+
*.dll
|
|
10
|
+
|
|
11
|
+
# Distribution / packaging
|
|
12
|
+
.build/
|
|
13
|
+
develop-eggs/
|
|
14
|
+
dist/
|
|
15
|
+
downloads/
|
|
16
|
+
eggs/
|
|
17
|
+
.eggs/
|
|
18
|
+
lib/
|
|
19
|
+
lib64/
|
|
20
|
+
parts/
|
|
21
|
+
sdist/
|
|
22
|
+
var/
|
|
23
|
+
wheels/
|
|
24
|
+
pip-wheel-metadata/
|
|
25
|
+
share/python-wheels/
|
|
26
|
+
*.egg-info/
|
|
27
|
+
.installed.cfg
|
|
28
|
+
*.egg
|
|
29
|
+
MANIFEST
|
|
30
|
+
|
|
31
|
+
# PyInstaller
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py,cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
|
|
53
|
+
# Jupyter Notebook
|
|
54
|
+
.ipynb_checkpoints
|
|
55
|
+
|
|
56
|
+
# pyenv
|
|
57
|
+
.python-version
|
|
58
|
+
|
|
59
|
+
# pipenv
|
|
60
|
+
Pipfile.lock
|
|
61
|
+
|
|
62
|
+
# poetry
|
|
63
|
+
poetry.lock
|
|
64
|
+
|
|
65
|
+
# PEP 582; used by e.g. uv, pdm
|
|
66
|
+
__pypackages__/
|
|
67
|
+
|
|
68
|
+
# Environments
|
|
69
|
+
.env
|
|
70
|
+
.venv
|
|
71
|
+
env/
|
|
72
|
+
venv/
|
|
73
|
+
ENV/
|
|
74
|
+
env.bak/
|
|
75
|
+
venv.bak/
|
|
76
|
+
|
|
77
|
+
# VS Code
|
|
78
|
+
.vscode/
|
|
79
|
+
.history/
|
|
80
|
+
|
|
81
|
+
# PyCharm / IntelliJ
|
|
82
|
+
.idea/
|
|
83
|
+
|
|
84
|
+
# MyPy / type checking
|
|
85
|
+
.mypy_cache/
|
|
86
|
+
.dmypy.json
|
|
87
|
+
dmypy.json
|
|
88
|
+
|
|
89
|
+
# Pyre type checker
|
|
90
|
+
.pyre/
|
|
91
|
+
|
|
92
|
+
# Ruff / lint
|
|
93
|
+
.ruff_cache/
|
|
94
|
+
|
|
95
|
+
# Logs
|
|
96
|
+
*.log
|
|
97
|
+
|
|
98
|
+
# OS-specific
|
|
99
|
+
.DS_Store
|
|
100
|
+
Thumbs.db
|
|
101
|
+
|
|
102
|
+
# Local data / backups
|
|
103
|
+
*.bak
|
|
104
|
+
*.swp
|
|
105
|
+
*.swo
|
|
106
|
+
*.tmp
|
|
107
|
+
|
|
108
|
+
# Project-specific (可按需要增減)
|
|
109
|
+
# 例如:如果有本地測試資料或大型語料不想進 Git,可在這裡加上:
|
|
110
|
+
# data/
|
|
111
|
+
# tmp/
|
|
112
|
+
|
|
113
|
+
# uv
|
|
114
|
+
.venv/
|
|
115
|
+
uv.lock
|
|
116
|
+
|
|
117
|
+
# Debug files
|
|
118
|
+
debug_*.py
|
|
119
|
+
|
|
120
|
+
__pycache__/**
|
|
121
|
+
|
|
122
|
+
backups/**
|
phonofix-0.1.0/CLAUDE.md
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
## Project Overview
|
|
6
|
+
|
|
7
|
+
**Phonetic Substitution Engine** - 多語言語音相似替換引擎
|
|
8
|
+
|
|
9
|
+
基於語音相似度的多語言詞彙替換引擎。本工具**不維護任何字典**,僅提供替換引擎。使用者提供專有名詞字典後,工具會自動生成語音變體並進行智能替換。
|
|
10
|
+
|
|
11
|
+
**核心理念**:
|
|
12
|
+
- 工具只提供替換引擎,不維護任何預設字典
|
|
13
|
+
- 使用者維護符合業務領域的專有名詞字典
|
|
14
|
+
- 工具負責自動生成語音變體與智能替換
|
|
15
|
+
- **不是全文糾錯工具**,專注於「專有名詞的語音相似替換」
|
|
16
|
+
|
|
17
|
+
**支援語言**:
|
|
18
|
+
- **中文**:拼音模糊音匹配(台灣國語特徵)
|
|
19
|
+
- **英文**:IPA 語音相似度匹配
|
|
20
|
+
- **混合文本**:自動語言片段識別與分段處理
|
|
21
|
+
|
|
22
|
+
**適用場景**:
|
|
23
|
+
- **ASR 語音識別後處理**:修正語音轉文字產生的專有名詞錯誤(含中英混合)
|
|
24
|
+
- **LLM 輸出後處理**:修正大型語言模型因專有名詞罕見而選錯的同音字
|
|
25
|
+
- **專有名詞標準化**:技術術語、品牌名稱、人名地名的統一
|
|
26
|
+
- **地域詞彙轉換**:中國 ↔ 台灣慣用詞
|
|
27
|
+
- **縮寫擴展**:口語簡稱 → 正式全稱
|
|
28
|
+
|
|
29
|
+
## Development Commands
|
|
30
|
+
|
|
31
|
+
### Setup
|
|
32
|
+
```bash
|
|
33
|
+
# Install dependencies
|
|
34
|
+
pip install pypinyin Levenshtein Pinyin2Hanzi hanziconv
|
|
35
|
+
# Or use requirements.txt
|
|
36
|
+
pip install -r requirements.txt
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Run Examples
|
|
40
|
+
```bash
|
|
41
|
+
# Auto-correct examples (6 examples, recommended)
|
|
42
|
+
python auto_correct_examples.py
|
|
43
|
+
|
|
44
|
+
# Legacy examples
|
|
45
|
+
python examples/examples.py
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Testing
|
|
49
|
+
```bash
|
|
50
|
+
# Basic functionality test
|
|
51
|
+
python -c "from chinese_text_corrector import ChineseTextCorrector; print('Import successful')"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Architecture
|
|
55
|
+
|
|
56
|
+
### Module Structure
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
multi_language_corrector/
|
|
60
|
+
├── core/ # 語言抽象層
|
|
61
|
+
│ ├── phonetic_interface.py # PhoneticSystem 抽象介面
|
|
62
|
+
│ └── tokenizer_interface.py # Tokenizer 抽象介面
|
|
63
|
+
├── languages/ # 語言特定實現
|
|
64
|
+
│ ├── chinese/
|
|
65
|
+
│ │ ├── phonetic_config.py # 中文拼音模糊音配置
|
|
66
|
+
│ │ ├── phonetic_utils.py # 中文拼音工具函數
|
|
67
|
+
│ │ ├── phonetic_impl.py # ChinesePhoneticSystem
|
|
68
|
+
│ │ ├── tokenizer.py # 字元級 tokenizer
|
|
69
|
+
│ │ └── generator.py # 中文模糊音詞典生成器
|
|
70
|
+
│ └── english/
|
|
71
|
+
│ ├── phonetic_impl.py # EnglishPhoneticSystem (IPA)
|
|
72
|
+
│ ├── tokenizer.py # 單字級 tokenizer
|
|
73
|
+
│ └── generator.py # 英文模糊音詞典生成器
|
|
74
|
+
├── router/
|
|
75
|
+
│ └── language_router.py # 語言片段識別與路由
|
|
76
|
+
└── correction/
|
|
77
|
+
└── unified_corrector.py # UnifiedCorrector (統一入口)
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Key Components
|
|
82
|
+
|
|
83
|
+
**1. PhoneticSystem** (`core/phonetic_interface.py`)
|
|
84
|
+
- **抽象介面**定義語言發音系統
|
|
85
|
+
- 關鍵方法:
|
|
86
|
+
- `to_phonetic(text)`: 將文本轉為音標表示(拼音/IPA/假名)
|
|
87
|
+
- `are_fuzzy_similar(phonetic1, phonetic2)`: 判斷兩個音標是否模糊相似
|
|
88
|
+
- `get_tolerance(word_length)`: 根據詞長取得容錯率
|
|
89
|
+
- **實現類別**:
|
|
90
|
+
- `ChinesePhoneticSystem`: 中文拼音模糊音匹配
|
|
91
|
+
- `EnglishPhoneticSystem`: 英文 IPA 語音相似度匹配
|
|
92
|
+
|
|
93
|
+
**2. Tokenizer** (`core/tokenizer_interface.py`)
|
|
94
|
+
- **抽象介面**處理語言特定的 token 切分
|
|
95
|
+
- 解決字元流 vs 單字流的差異:
|
|
96
|
+
- 中文/日文:字元級滑動視窗
|
|
97
|
+
- 英文/韓文:單字級 token 流(依空白/標點)
|
|
98
|
+
- 避免英文詞被錯誤切割導致誤判
|
|
99
|
+
|
|
100
|
+
**3. LanguageRouter** (`router/language_router.py`)
|
|
101
|
+
- 識別混合語言文本中的語言片段
|
|
102
|
+
- 將不同語言片段路由至對應的 PhoneticSystem
|
|
103
|
+
- 支援策略:rule-based、fastText、Whisper diarization
|
|
104
|
+
- 避免中文模組誤處理英文詞(或反之)
|
|
105
|
+
|
|
106
|
+
**4. UnifiedCorrector** (`correction/unified_corrector.py`)
|
|
107
|
+
- **主要入口類別**,統一處理多語言文本替換
|
|
108
|
+
- 自動識別語言片段並調用對應的語言系統
|
|
109
|
+
- **核心機制**(語言無關):
|
|
110
|
+
- 滑動視窗匹配算法(依賴 Tokenizer 介面)
|
|
111
|
+
- 上下文關鍵字加權(距離越近加分越多)
|
|
112
|
+
- 動態容錯率調整(依語言和詞長)
|
|
113
|
+
- 豁免清單避免誤改
|
|
114
|
+
|
|
115
|
+
**5. ChinesePhoneticSystem** (`languages/chinese/phonetic_impl.py`)
|
|
116
|
+
- 台灣國語拼音模糊音規則:
|
|
117
|
+
- n/l 不分、f/h 混淆、r/l 混淆、捲舌音混淆
|
|
118
|
+
- 韻母模糊對應、特例音節映射
|
|
119
|
+
- 容錯率:2字詞 0.20 → 4+字詞 0.40
|
|
120
|
+
- 使用 `pypinyin` 進行漢字轉拼音
|
|
121
|
+
|
|
122
|
+
**6. EnglishPhoneticSystem** (`languages/english/phonetic_impl.py`)
|
|
123
|
+
- IPA (國際音標) 語音相似度計算
|
|
124
|
+
- 解決 ASR 專有名詞錯誤(如 "1kg" → "EKG")
|
|
125
|
+
- 使用 `eng_to_ipa` 或 `epitran` 進行文字轉 IPA
|
|
126
|
+
- 容錯率:4字詞 0.35 → 長詞 0.45
|
|
127
|
+
|
|
128
|
+
### Critical Architecture Decisions
|
|
129
|
+
|
|
130
|
+
**語言抽象層設計**:
|
|
131
|
+
- `PhoneticSystem` 介面統一處理不同語言的發音系統
|
|
132
|
+
- `Tokenizer` 介面解決字元流 vs 單字流的差異
|
|
133
|
+
- `LanguageRouter` 處理混合語言文本的片段識別
|
|
134
|
+
- 核心算法(滑動視窗、上下文加權)完全語言無關
|
|
135
|
+
|
|
136
|
+
**拼音/音標去重機制**:
|
|
137
|
+
- 自動過濾重複音標的別名(類似 Set 行為)
|
|
138
|
+
- 避免字典膨脹和重複匹配
|
|
139
|
+
- 中文:拼音去重後通常只有 5-20 個變體
|
|
140
|
+
- 英文:IPA 去重後變體數量較少
|
|
141
|
+
|
|
142
|
+
**歸一化策略**:
|
|
143
|
+
- 內部固定歸一化為標準詞(正向轉換)
|
|
144
|
+
- 所有別名一律轉換為使用者指定的正確詞
|
|
145
|
+
- 若不想轉換,直接不加入字典即可
|
|
146
|
+
|
|
147
|
+
**混合語言處理**:
|
|
148
|
+
- 自動識別文本中的語言片段(中文/英文/混合)
|
|
149
|
+
- 分段交給對應的語言系統處理
|
|
150
|
+
- 避免跨語言誤匹配(中文模組處理英文詞)
|
|
151
|
+
|
|
152
|
+
## Dictionary Design Patterns
|
|
153
|
+
|
|
154
|
+
### Three Dictionary Types
|
|
155
|
+
|
|
156
|
+
參考 `docs/DICTIONARY_ORGANIZATION.md` 了解詳細分類建議:
|
|
157
|
+
|
|
158
|
+
1. **地域慣用詞** (`weight: 0.0`)
|
|
159
|
+
- 雙向索引:台灣 ↔ 中國慣用詞
|
|
160
|
+
- 100% 轉換,不需上下文判斷
|
|
161
|
+
- 範例:土豆 ↔ 馬鈴薯、視頻 ↔ 影片
|
|
162
|
+
|
|
163
|
+
2. **ASR 錯誤/錯別字** (`weight > 0`)
|
|
164
|
+
- 單向修正(錯誤 → 正確)
|
|
165
|
+
- 需要上下文判斷和拼音模糊匹配
|
|
166
|
+
- 範例:流奶 → 牛奶、花揮 → 發揮
|
|
167
|
+
|
|
168
|
+
3. **縮寫擴展** (`weight: 0.0 或 > 0`)
|
|
169
|
+
- 口語簡稱 → 正式全稱
|
|
170
|
+
- 常需上下文關鍵字判斷(如「永豆」可能是「永和豆漿」或「勇者鬥惡龍」)
|
|
171
|
+
|
|
172
|
+
### Recommended Organization
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
# 建議分檔管理不同類型詞典
|
|
176
|
+
from dictionaries.asr_errors import ASR_ERRORS
|
|
177
|
+
from dictionaries.region_cn_to_tw import REGION_CN_TO_TW
|
|
178
|
+
from dictionaries.abbreviations import ABBREVIATIONS
|
|
179
|
+
|
|
180
|
+
# 場景組合
|
|
181
|
+
asr_corrector = ChineseTextCorrector.from_terms({
|
|
182
|
+
**ASR_ERRORS,
|
|
183
|
+
**ABBREVIATIONS,
|
|
184
|
+
})
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## API Usage Patterns
|
|
188
|
+
|
|
189
|
+
### Recommended: Unified Corrector (多語言混合)
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
from multi_language_corrector.correction.unified_corrector import UnifiedCorrector
|
|
193
|
+
|
|
194
|
+
# 定義混合語言專有名詞
|
|
195
|
+
terms = [
|
|
196
|
+
"台北車站", # 中文詞
|
|
197
|
+
"TensorFlow", # 英文專有名詞
|
|
198
|
+
"EKG", # 英文縮寫
|
|
199
|
+
"Python"
|
|
200
|
+
]
|
|
201
|
+
|
|
202
|
+
corrector = UnifiedCorrector(terms)
|
|
203
|
+
|
|
204
|
+
# 自動處理混合語言文本
|
|
205
|
+
text = "我在北車用Pyton寫code,這個1kg設備很貴"
|
|
206
|
+
result = corrector.correct(text)
|
|
207
|
+
# "我在台北車站用Python寫code,這個EKG設備很貴"
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Legacy: Chinese-Only Corrector
|
|
211
|
+
|
|
212
|
+
```python
|
|
213
|
+
from chinese_text_corrector import ChineseTextCorrector
|
|
214
|
+
|
|
215
|
+
# 最簡格式 - 僅提供關鍵字列表
|
|
216
|
+
corrector = ChineseTextCorrector.from_terms(["台北車站", "牛奶", "發揮"])
|
|
217
|
+
|
|
218
|
+
# 完整格式 - 別名 + 關鍵字 + 權重
|
|
219
|
+
corrector = ChineseTextCorrector.from_terms({
|
|
220
|
+
"永和豆漿": {
|
|
221
|
+
"aliases": ["永豆", "勇豆"],
|
|
222
|
+
"keywords": ["吃", "喝", "買", "宵夜"],
|
|
223
|
+
"weight": 0.3
|
|
224
|
+
}
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
result = corrector.correct("我在北車買了流奶,他花揮了才能")
|
|
228
|
+
# '我在台北車站買了牛奶,他發揮了才能'
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## Important Implementation Notes
|
|
232
|
+
|
|
233
|
+
### Language Detection & Routing
|
|
234
|
+
|
|
235
|
+
**混合語言處理流程**:
|
|
236
|
+
1. 語言片段識別(中文/英文/混合)
|
|
237
|
+
2. 分段交給對應的 PhoneticSystem 處理
|
|
238
|
+
3. 合併結果並保持原文格式
|
|
239
|
+
|
|
240
|
+
**Language Router 策略**:
|
|
241
|
+
- **Rule-based**: 簡單正則表達式判斷(快速但不精確)
|
|
242
|
+
- **fastText**: 語言識別模型(平衡速度與準確度)
|
|
243
|
+
- **Whisper diarization**: ASR 自帶語言標記(最準確)
|
|
244
|
+
|
|
245
|
+
### Phonetic Matching Algorithm
|
|
246
|
+
|
|
247
|
+
**中文拼音相似度計算**:
|
|
248
|
+
1. 特例音節匹配(優先,如 fa ↔ hua)
|
|
249
|
+
2. 韻母模糊匹配(in/ing, en/eng 等)
|
|
250
|
+
3. Levenshtein 編輯距離計算
|
|
251
|
+
4. 短詞聲母嚴格檢查(2字詞必須聲母匹配)
|
|
252
|
+
|
|
253
|
+
**英文 IPA 相似度計算**:
|
|
254
|
+
1. 文字轉 IPA 音標(使用 eng_to_ipa/epitran)
|
|
255
|
+
2. IPA 編輯距離計算(Levenshtein)
|
|
256
|
+
3. 容錯率依音標長度調整
|
|
257
|
+
|
|
258
|
+
**容錯率動態調整**:
|
|
259
|
+
- 中文 2 字詞: 0.20(必須非常準確)
|
|
260
|
+
- 中文 3 字詞: 0.30
|
|
261
|
+
- 中文 4+ 字詞: 0.40(寬容度最高)
|
|
262
|
+
- 英文短詞: 0.35
|
|
263
|
+
- 英文長詞: 0.45
|
|
264
|
+
|
|
265
|
+
### Context Keyword Weighting
|
|
266
|
+
|
|
267
|
+
**距離加權機制** (參考 `docs/DISTANCE_WEIGHTING_FEATURE.md`):
|
|
268
|
+
- 關鍵字距離越近,加分越多
|
|
269
|
+
- 公式: `bonus = base_bonus * (1 / (1 + distance))`
|
|
270
|
+
- 避免遠距離關鍵字過度影響判斷
|
|
271
|
+
|
|
272
|
+
### Exclusion List
|
|
273
|
+
|
|
274
|
+
豁免清單避免特定詞被修正:
|
|
275
|
+
```python
|
|
276
|
+
corrector = ChineseTextCorrector.from_terms(
|
|
277
|
+
["台北車站"],
|
|
278
|
+
exclusions=["北側", "車站"] # 這些詞不會被修正
|
|
279
|
+
)
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
## File References
|
|
283
|
+
|
|
284
|
+
- **README.md**: 完整使用說明和範例(多語言支援)
|
|
285
|
+
- **auto_correct_examples.py**: 6 個完整範例展示不同使用方式
|
|
286
|
+
- **docs/MULTI_LANGUAGE_EXPANSION.md**: 多語言擴展架構分析與實作建議
|
|
287
|
+
- **docs/DICTIONARY_ORGANIZATION.md**: 詞典分類管理建議
|
|
288
|
+
- **docs/DISTANCE_WEIGHTING_FEATURE.md**: 距離加權機制說明
|
|
289
|
+
- **docs/CHANGELOG.md**: 版本更新記錄
|
|
290
|
+
|
|
291
|
+
## Common Pitfalls
|
|
292
|
+
|
|
293
|
+
1. **混合語言需使用 UnifiedCorrector**: 中英混合文本必須使用 `UnifiedCorrector`,避免語言誤匹配
|
|
294
|
+
2. **Tokenization 差異**: 中文字元級滑窗 vs 英文單字級 token,需正確選擇 Tokenizer
|
|
295
|
+
3. **Language Router 必要性**: 混合語言場景務必啟用語言路由,否則性能崩潰
|
|
296
|
+
4. **不要限制變體數量**: 使用所有音標去重後的變體,避免遺漏有效候選
|
|
297
|
+
5. **權重設定**: 地域詞 `weight: 0.0`,一般替換 `weight > 0` 需上下文判斷
|
|
298
|
+
6. **音標去重自動進行**: 自動過濾重複音標的別名(中文拼音/英文 IPA)
|
|
299
|
+
7. **歸一化固定為 True**: 所有別名一律轉換為標準詞,若不想轉換則不加入字典
|
|
300
|
+
8. **本工具不是全文糾錯**: 專注於專有名詞的語音相似替換,不處理語法或一般拼寫錯誤
|
|
301
|
+
|
|
302
|
+
## Development Roadmap
|
|
303
|
+
|
|
304
|
+
參考 `docs/MULTI_LANGUAGE_EXPANSION.md` 了解完整的多語言擴展計畫:
|
|
305
|
+
|
|
306
|
+
### Phase 1: 重構現有架構(1-2 週)
|
|
307
|
+
- [x] 定義 `PhoneticSystem` 抽象介面
|
|
308
|
+
- [x] 定義 `Tokenizer` 抽象介面
|
|
309
|
+
- [x] 實現 `LanguageRouter` 語言片段識別
|
|
310
|
+
- [x] 重構為 `UnifiedCorrector` 統一入口
|
|
311
|
+
- [x] 中文實現包裝為 `ChinesePhoneticSystem`
|
|
312
|
+
- [x] 英文實現為 `EnglishPhoneticSystem` (IPA-based)
|
|
313
|
+
|
|
314
|
+
### Phase 2: 擴展新語言支援(每種語言 3-5 天)
|
|
315
|
+
- [ ] 日文支援:假名/羅馬音轉換與模糊音規則
|
|
316
|
+
- [ ] 韓文支援:韓文拼音與收音混淆規則
|
|
317
|
+
- [ ] 其他語言:依需求優先級添加
|
|
318
|
+
|
|
319
|
+
### 關鍵挑戰
|
|
320
|
+
- **Tokenization 差異**: 字元流 vs 單字流需正確處理
|
|
321
|
+
- **模糊音規則**: 需要語言專家協助定義各語言規則
|
|
322
|
+
- **詞頻/語料依賴**: 英文同音詞判斷可能需要額外語料
|
|
323
|
+
- **Code-Switching**: 混合語言場景需要完善的語言路由機制
|