phonofix 0.1.0__py3-none-any.whl
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/__init__.py +161 -0
- phonofix/backend/__init__.py +22 -0
- phonofix/backend/base.py +70 -0
- phonofix/backend/chinese_backend.py +257 -0
- phonofix/backend/english_backend.py +390 -0
- phonofix/core/phonetic_interface.py +57 -0
- phonofix/core/tokenizer_interface.py +45 -0
- phonofix/correction/__init__.py +31 -0
- phonofix/correction/protocol.py +87 -0
- phonofix/correction/streaming_corrector.py +469 -0
- phonofix/correction/unified_corrector.py +295 -0
- phonofix/engine/__init__.py +22 -0
- phonofix/engine/base.py +122 -0
- phonofix/engine/chinese_engine.py +256 -0
- phonofix/engine/english_engine.py +262 -0
- phonofix/engine/unified_engine.py +202 -0
- phonofix/languages/chinese/__init__.py +33 -0
- phonofix/languages/chinese/config.py +122 -0
- phonofix/languages/chinese/corrector.py +614 -0
- phonofix/languages/chinese/fuzzy_generator.py +307 -0
- phonofix/languages/chinese/number_variants.py +203 -0
- phonofix/languages/chinese/phonetic_impl.py +103 -0
- phonofix/languages/chinese/tokenizer.py +50 -0
- phonofix/languages/chinese/utils.py +261 -0
- phonofix/languages/english/__init__.py +42 -0
- phonofix/languages/english/config.py +145 -0
- phonofix/languages/english/corrector.py +321 -0
- phonofix/languages/english/fuzzy_generator.py +231 -0
- phonofix/languages/english/phonetic_impl.py +429 -0
- phonofix/languages/english/tokenizer.py +51 -0
- phonofix/router/language_router.py +64 -0
- phonofix/utils/__init__.py +35 -0
- phonofix/utils/lazy_imports.py +247 -0
- phonofix/utils/logger.py +217 -0
- phonofix-0.1.0.dist-info/METADATA +806 -0
- phonofix-0.1.0.dist-info/RECORD +37 -0
- phonofix-0.1.0.dist-info/WHEEL +4 -0
phonofix/__init__.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"""
|
|
2
|
+
phonofix - 多語言語音相似修正器 (Multi-Language Phonetic Corrector)
|
|
3
|
+
|
|
4
|
+
基於語音相似度的專有名詞修正工具,支援 ASR/LLM 後處理。
|
|
5
|
+
支援中文(拼音)和英文(IPA)的語音相似度比對。
|
|
6
|
+
|
|
7
|
+
安裝方式:
|
|
8
|
+
# 最小安裝 (僅核心)
|
|
9
|
+
pip install phonofix
|
|
10
|
+
|
|
11
|
+
# 中文支援
|
|
12
|
+
pip install "phonofix[chinese]"
|
|
13
|
+
|
|
14
|
+
# 英文支援 (還需安裝 espeak-ng)
|
|
15
|
+
pip install "phonofix[english]"
|
|
16
|
+
|
|
17
|
+
# 完整安裝
|
|
18
|
+
pip install "phonofix[all]"
|
|
19
|
+
|
|
20
|
+
使用方式:
|
|
21
|
+
from phonofix import UnifiedEngine
|
|
22
|
+
|
|
23
|
+
# 應用程式啟動時初始化 Engine (一次性成本, ~2秒)
|
|
24
|
+
engine = UnifiedEngine()
|
|
25
|
+
|
|
26
|
+
# 需要時快速建立 Corrector (毫秒級)
|
|
27
|
+
corrector = engine.create_corrector({
|
|
28
|
+
'台北車站': ['北車'],
|
|
29
|
+
'Python': ['Pyton'],
|
|
30
|
+
})
|
|
31
|
+
result = corrector.correct('我在北車學習Pyton')
|
|
32
|
+
|
|
33
|
+
# 啟用詳細日誌 (顯示計時與變體)
|
|
34
|
+
engine = UnifiedEngine(verbose=True)
|
|
35
|
+
|
|
36
|
+
# 進階: 自定義計時回呼
|
|
37
|
+
engine = UnifiedEngine(
|
|
38
|
+
verbose=True,
|
|
39
|
+
on_timing=lambda op, t: print(f"{op}: {t:.3f}s")
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# 進階: 使用標準 logging 控制
|
|
43
|
+
import logging
|
|
44
|
+
logging.getLogger("phonofix").setLevel(logging.DEBUG)
|
|
45
|
+
|
|
46
|
+
效能特性:
|
|
47
|
+
- Engine 初始化: ~2 秒 (espeak-ng 載入)
|
|
48
|
+
- Corrector 建立: <10ms (相同詞彙會更快,因為快取共享)
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
# =============================================================================
|
|
52
|
+
# 主要 API - Engine 層
|
|
53
|
+
# =============================================================================
|
|
54
|
+
from phonofix.engine import (
|
|
55
|
+
UnifiedEngine,
|
|
56
|
+
EnglishEngine,
|
|
57
|
+
ChineseEngine,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
# =============================================================================
|
|
61
|
+
# 日誌工具
|
|
62
|
+
# =============================================================================
|
|
63
|
+
from phonofix.utils.logger import (
|
|
64
|
+
get_logger,
|
|
65
|
+
enable_debug_logging,
|
|
66
|
+
enable_timing_logging,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
# =============================================================================
|
|
70
|
+
# 依賴檢查工具
|
|
71
|
+
# =============================================================================
|
|
72
|
+
from phonofix.utils.lazy_imports import (
|
|
73
|
+
is_chinese_available,
|
|
74
|
+
is_english_available,
|
|
75
|
+
check_chinese_dependencies,
|
|
76
|
+
check_english_dependencies,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
# =============================================================================
|
|
80
|
+
# Backend 層 (進階用途)
|
|
81
|
+
# =============================================================================
|
|
82
|
+
from phonofix.backend import (
|
|
83
|
+
get_english_backend,
|
|
84
|
+
get_chinese_backend,
|
|
85
|
+
EnglishPhoneticBackend,
|
|
86
|
+
ChinesePhoneticBackend,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# =============================================================================
|
|
90
|
+
# 串流處理 (ASR/LLM Streaming)
|
|
91
|
+
# =============================================================================
|
|
92
|
+
from phonofix.correction.streaming_corrector import (
|
|
93
|
+
StreamingCorrector,
|
|
94
|
+
ChunkStreamingCorrector,
|
|
95
|
+
StreamingResult,
|
|
96
|
+
create_streaming_corrector,
|
|
97
|
+
calculate_safe_overlap,
|
|
98
|
+
# 常數
|
|
99
|
+
DEFAULT_OVERLAP_SIZE,
|
|
100
|
+
MIN_OVERLAP_SIZE,
|
|
101
|
+
OVERLAP_MARGIN,
|
|
102
|
+
DEFAULT_MIN_CONFIRM_SIZE,
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
# =============================================================================
|
|
106
|
+
# Protocol 定義 (進階用途 - 擴展語言支援時使用)
|
|
107
|
+
# =============================================================================
|
|
108
|
+
from phonofix.correction.protocol import (
|
|
109
|
+
CorrectorProtocol,
|
|
110
|
+
ContextAwareCorrectorProtocol,
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
# =============================================================================
|
|
114
|
+
# 組合型修正器 (進階用途 - 手動組合多語言)
|
|
115
|
+
# =============================================================================
|
|
116
|
+
from phonofix.correction.unified_corrector import UnifiedCorrector
|
|
117
|
+
|
|
118
|
+
__all__ = [
|
|
119
|
+
# Engine 層
|
|
120
|
+
'UnifiedEngine',
|
|
121
|
+
'EnglishEngine',
|
|
122
|
+
'ChineseEngine',
|
|
123
|
+
|
|
124
|
+
# 日誌工具
|
|
125
|
+
'get_logger',
|
|
126
|
+
'enable_debug_logging',
|
|
127
|
+
'enable_timing_logging',
|
|
128
|
+
|
|
129
|
+
# 依賴檢查
|
|
130
|
+
'is_chinese_available',
|
|
131
|
+
'is_english_available',
|
|
132
|
+
'check_chinese_dependencies',
|
|
133
|
+
'check_english_dependencies',
|
|
134
|
+
|
|
135
|
+
# Backend 層 (進階)
|
|
136
|
+
'get_english_backend',
|
|
137
|
+
'get_chinese_backend',
|
|
138
|
+
'EnglishPhoneticBackend',
|
|
139
|
+
'ChinesePhoneticBackend',
|
|
140
|
+
|
|
141
|
+
# 串流處理
|
|
142
|
+
'StreamingCorrector',
|
|
143
|
+
'ChunkStreamingCorrector',
|
|
144
|
+
'StreamingResult',
|
|
145
|
+
'create_streaming_corrector',
|
|
146
|
+
'calculate_safe_overlap',
|
|
147
|
+
# 串流常數
|
|
148
|
+
'DEFAULT_OVERLAP_SIZE',
|
|
149
|
+
'MIN_OVERLAP_SIZE',
|
|
150
|
+
'OVERLAP_MARGIN',
|
|
151
|
+
'DEFAULT_MIN_CONFIRM_SIZE',
|
|
152
|
+
|
|
153
|
+
# Protocol (進階 - 擴展語言)
|
|
154
|
+
'CorrectorProtocol',
|
|
155
|
+
'ContextAwareCorrectorProtocol',
|
|
156
|
+
|
|
157
|
+
# 組合型修正器 (進階)
|
|
158
|
+
'UnifiedCorrector',
|
|
159
|
+
]
|
|
160
|
+
|
|
161
|
+
__version__ = '0.1.0'
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""
|
|
2
|
+
語音後端模組 (Backend Layer)
|
|
3
|
+
|
|
4
|
+
此模組包含語音處理的底層後端實作,負責:
|
|
5
|
+
- 初始化外部引擎 (espeak-ng, pypinyin)
|
|
6
|
+
- 管理語音轉換快取
|
|
7
|
+
- 提供基礎 G2P (Grapheme-to-Phoneme) 函數
|
|
8
|
+
|
|
9
|
+
所有後端都實作為單例模式,確保整個應用程式只初始化一次。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .base import PhoneticBackend
|
|
13
|
+
from .english_backend import EnglishPhoneticBackend, get_english_backend
|
|
14
|
+
from .chinese_backend import ChinesePhoneticBackend, get_chinese_backend
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"PhoneticBackend",
|
|
18
|
+
"EnglishPhoneticBackend",
|
|
19
|
+
"ChinesePhoneticBackend",
|
|
20
|
+
"get_english_backend",
|
|
21
|
+
"get_chinese_backend",
|
|
22
|
+
]
|
phonofix/backend/base.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""
|
|
2
|
+
語音後端抽象基類
|
|
3
|
+
|
|
4
|
+
定義所有語言語音後端必須實作的介面。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from abc import ABC, abstractmethod
|
|
8
|
+
from typing import Dict, Any
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class PhoneticBackend(ABC):
|
|
12
|
+
"""
|
|
13
|
+
語音後端抽象基類 (Abstract Base Class)
|
|
14
|
+
|
|
15
|
+
職責:
|
|
16
|
+
- 初始化外部語音引擎 (如 espeak-ng, pypinyin)
|
|
17
|
+
- 提供基礎語音轉換函數
|
|
18
|
+
- 管理語音轉換快取
|
|
19
|
+
|
|
20
|
+
所有實作類別都應該使用單例模式。
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
@abstractmethod
|
|
24
|
+
def to_phonetic(self, text: str) -> str:
|
|
25
|
+
"""
|
|
26
|
+
將文字轉換為語音表示
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
text: 輸入文字
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
str: 語音表示字串 (如 IPA 或拼音)
|
|
33
|
+
"""
|
|
34
|
+
pass
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def is_initialized(self) -> bool:
|
|
38
|
+
"""
|
|
39
|
+
檢查後端是否已初始化
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
bool: 是否已初始化
|
|
43
|
+
"""
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
@abstractmethod
|
|
47
|
+
def initialize(self) -> None:
|
|
48
|
+
"""
|
|
49
|
+
初始化後端
|
|
50
|
+
|
|
51
|
+
此方法會在首次使用時自動呼叫,也可以手動呼叫以控制初始化時機。
|
|
52
|
+
"""
|
|
53
|
+
pass
|
|
54
|
+
|
|
55
|
+
@abstractmethod
|
|
56
|
+
def get_cache_stats(self) -> Dict[str, Any]:
|
|
57
|
+
"""
|
|
58
|
+
取得快取統計資訊
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
Dict[str, Any]: 快取統計,包含 hits, misses, size 等
|
|
62
|
+
"""
|
|
63
|
+
pass
|
|
64
|
+
|
|
65
|
+
@abstractmethod
|
|
66
|
+
def clear_cache(self) -> None:
|
|
67
|
+
"""
|
|
68
|
+
清除快取
|
|
69
|
+
"""
|
|
70
|
+
pass
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"""
|
|
2
|
+
中文語音後端 (ChinesePhoneticBackend)
|
|
3
|
+
|
|
4
|
+
負責 pypinyin 的拼音轉換快取管理。
|
|
5
|
+
實作為執行緒安全的單例模式。
|
|
6
|
+
|
|
7
|
+
注意:此模組使用延遲導入 (Lazy Import) 機制,
|
|
8
|
+
僅在實際使用中文功能時才會載入 pypinyin。
|
|
9
|
+
如果未安裝中文依賴,將在首次使用時拋出清楚的 ImportError。
|
|
10
|
+
|
|
11
|
+
安裝中文支援:
|
|
12
|
+
pip install "phonofix[chinese]"
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import threading
|
|
16
|
+
from functools import lru_cache
|
|
17
|
+
from typing import Dict, Any, Optional, Tuple
|
|
18
|
+
|
|
19
|
+
from .base import PhoneticBackend
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# =============================================================================
|
|
23
|
+
# 全域狀態
|
|
24
|
+
# =============================================================================
|
|
25
|
+
|
|
26
|
+
_instance: Optional["ChinesePhoneticBackend"] = None
|
|
27
|
+
_instance_lock = threading.Lock()
|
|
28
|
+
|
|
29
|
+
# 延遲導入狀態
|
|
30
|
+
_pypinyin = None
|
|
31
|
+
_pypinyin_checked = False
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _get_pypinyin():
|
|
35
|
+
"""延遲載入 pypinyin 模組"""
|
|
36
|
+
global _pypinyin, _pypinyin_checked
|
|
37
|
+
|
|
38
|
+
if _pypinyin_checked:
|
|
39
|
+
if _pypinyin is not None:
|
|
40
|
+
return _pypinyin
|
|
41
|
+
else:
|
|
42
|
+
from phonofix.utils.lazy_imports import CHINESE_INSTALL_HINT
|
|
43
|
+
raise ImportError(CHINESE_INSTALL_HINT)
|
|
44
|
+
|
|
45
|
+
try:
|
|
46
|
+
import pypinyin
|
|
47
|
+
_pypinyin = pypinyin
|
|
48
|
+
_pypinyin_checked = True
|
|
49
|
+
return _pypinyin
|
|
50
|
+
except ImportError:
|
|
51
|
+
_pypinyin_checked = True
|
|
52
|
+
from phonofix.utils.lazy_imports import CHINESE_INSTALL_HINT
|
|
53
|
+
raise ImportError(CHINESE_INSTALL_HINT)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
# =============================================================================
|
|
57
|
+
# 拼音快取 (模組層級,所有 Backend 實例共享)
|
|
58
|
+
# =============================================================================
|
|
59
|
+
|
|
60
|
+
@lru_cache(maxsize=50000)
|
|
61
|
+
def _cached_get_pinyin_string(text: str) -> str:
|
|
62
|
+
"""
|
|
63
|
+
快取版拼音字串計算
|
|
64
|
+
|
|
65
|
+
Args:
|
|
66
|
+
text: 中文文字
|
|
67
|
+
|
|
68
|
+
Returns:
|
|
69
|
+
str: 拼音字串 (無聲調,小寫)
|
|
70
|
+
"""
|
|
71
|
+
pypinyin = _get_pypinyin()
|
|
72
|
+
pinyin_list = pypinyin.lazy_pinyin(text, style=pypinyin.NORMAL)
|
|
73
|
+
return "".join(pinyin_list).lower()
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@lru_cache(maxsize=50000)
|
|
77
|
+
def _cached_get_initials(text: str) -> Tuple[str, ...]:
|
|
78
|
+
"""
|
|
79
|
+
快取版聲母列表計算
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
text: 中文文字
|
|
83
|
+
|
|
84
|
+
Returns:
|
|
85
|
+
Tuple[str, ...]: 聲母元組
|
|
86
|
+
"""
|
|
87
|
+
pypinyin = _get_pypinyin()
|
|
88
|
+
return tuple(pypinyin.lazy_pinyin(text, style=pypinyin.INITIALS, strict=False))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@lru_cache(maxsize=50000)
|
|
92
|
+
def _cached_get_finals(text: str) -> Tuple[str, ...]:
|
|
93
|
+
"""
|
|
94
|
+
快取版韻母列表計算
|
|
95
|
+
|
|
96
|
+
Args:
|
|
97
|
+
text: 中文文字
|
|
98
|
+
|
|
99
|
+
Returns:
|
|
100
|
+
Tuple[str, ...]: 韻母元組
|
|
101
|
+
"""
|
|
102
|
+
pypinyin = _get_pypinyin()
|
|
103
|
+
return tuple(pypinyin.lazy_pinyin(text, style=pypinyin.FINALS, strict=False))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# =============================================================================
|
|
107
|
+
# ChinesePhoneticBackend 單例類別
|
|
108
|
+
# =============================================================================
|
|
109
|
+
|
|
110
|
+
class ChinesePhoneticBackend(PhoneticBackend):
|
|
111
|
+
"""
|
|
112
|
+
中文語音後端 (單例)
|
|
113
|
+
|
|
114
|
+
職責:
|
|
115
|
+
- 提供拼音轉換函數
|
|
116
|
+
- 管理拼音快取
|
|
117
|
+
|
|
118
|
+
注意:pypinyin 不需要像 espeak-ng 那樣的初始化過程,
|
|
119
|
+
但我們仍然提供 initialize() 方法以保持介面一致性。
|
|
120
|
+
|
|
121
|
+
使用方式:
|
|
122
|
+
backend = get_chinese_backend() # 取得單例
|
|
123
|
+
pinyin = backend.to_phonetic("你好") # "nihao"
|
|
124
|
+
"""
|
|
125
|
+
|
|
126
|
+
def __init__(self):
|
|
127
|
+
"""
|
|
128
|
+
初始化後端
|
|
129
|
+
|
|
130
|
+
注意:請使用 get_chinese_backend() 取得單例,不要直接呼叫此建構函數。
|
|
131
|
+
"""
|
|
132
|
+
self._initialized = False
|
|
133
|
+
self._init_lock = threading.Lock()
|
|
134
|
+
|
|
135
|
+
def initialize(self) -> None:
|
|
136
|
+
"""
|
|
137
|
+
初始化後端
|
|
138
|
+
|
|
139
|
+
pypinyin 不需要特殊初始化,此方法主要用於介面一致性。
|
|
140
|
+
"""
|
|
141
|
+
if self._initialized:
|
|
142
|
+
return
|
|
143
|
+
|
|
144
|
+
with self._init_lock:
|
|
145
|
+
if self._initialized:
|
|
146
|
+
return
|
|
147
|
+
|
|
148
|
+
# pypinyin 會在首次使用時自動載入字典
|
|
149
|
+
# 這裡預先觸發一次以確保載入
|
|
150
|
+
_cached_get_pinyin_string("測試")
|
|
151
|
+
self._initialized = True
|
|
152
|
+
|
|
153
|
+
def is_initialized(self) -> bool:
|
|
154
|
+
"""檢查是否已初始化"""
|
|
155
|
+
return self._initialized
|
|
156
|
+
|
|
157
|
+
def to_phonetic(self, text: str) -> str:
|
|
158
|
+
"""
|
|
159
|
+
將中文文字轉換為拼音
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
text: 中文文字
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
str: 拼音字串 (無聲調,小寫)
|
|
166
|
+
"""
|
|
167
|
+
return _cached_get_pinyin_string(text)
|
|
168
|
+
|
|
169
|
+
def get_initials(self, text: str) -> Tuple[str, ...]:
|
|
170
|
+
"""
|
|
171
|
+
取得文字的聲母列表
|
|
172
|
+
|
|
173
|
+
Args:
|
|
174
|
+
text: 中文文字
|
|
175
|
+
|
|
176
|
+
Returns:
|
|
177
|
+
Tuple[str, ...]: 聲母元組
|
|
178
|
+
"""
|
|
179
|
+
return _cached_get_initials(text)
|
|
180
|
+
|
|
181
|
+
def get_finals(self, text: str) -> Tuple[str, ...]:
|
|
182
|
+
"""
|
|
183
|
+
取得文字的韻母列表
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
text: 中文文字
|
|
187
|
+
|
|
188
|
+
Returns:
|
|
189
|
+
Tuple[str, ...]: 韻母元組
|
|
190
|
+
"""
|
|
191
|
+
return _cached_get_finals(text)
|
|
192
|
+
|
|
193
|
+
def get_cache_stats(self) -> Dict[str, Any]:
|
|
194
|
+
"""
|
|
195
|
+
取得拼音快取統計
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
Dict: 包含各快取的統計資訊
|
|
199
|
+
"""
|
|
200
|
+
pinyin_info = _cached_get_pinyin_string.cache_info()
|
|
201
|
+
initials_info = _cached_get_initials.cache_info()
|
|
202
|
+
finals_info = _cached_get_finals.cache_info()
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
"pinyin": {
|
|
206
|
+
"hits": pinyin_info.hits,
|
|
207
|
+
"misses": pinyin_info.misses,
|
|
208
|
+
"currsize": pinyin_info.currsize,
|
|
209
|
+
"maxsize": pinyin_info.maxsize,
|
|
210
|
+
},
|
|
211
|
+
"initials": {
|
|
212
|
+
"hits": initials_info.hits,
|
|
213
|
+
"misses": initials_info.misses,
|
|
214
|
+
"currsize": initials_info.currsize,
|
|
215
|
+
"maxsize": initials_info.maxsize,
|
|
216
|
+
},
|
|
217
|
+
"finals": {
|
|
218
|
+
"hits": finals_info.hits,
|
|
219
|
+
"misses": finals_info.misses,
|
|
220
|
+
"currsize": finals_info.currsize,
|
|
221
|
+
"maxsize": finals_info.maxsize,
|
|
222
|
+
},
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
def clear_cache(self) -> None:
|
|
226
|
+
"""清除所有拼音快取"""
|
|
227
|
+
_cached_get_pinyin_string.cache_clear()
|
|
228
|
+
_cached_get_initials.cache_clear()
|
|
229
|
+
_cached_get_finals.cache_clear()
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
# =============================================================================
|
|
233
|
+
# 便捷函數
|
|
234
|
+
# =============================================================================
|
|
235
|
+
|
|
236
|
+
def get_chinese_backend() -> ChinesePhoneticBackend:
|
|
237
|
+
"""
|
|
238
|
+
取得 ChinesePhoneticBackend 單例
|
|
239
|
+
|
|
240
|
+
這是取得中文語音後端的推薦方式。
|
|
241
|
+
|
|
242
|
+
Returns:
|
|
243
|
+
ChinesePhoneticBackend: 單例實例
|
|
244
|
+
|
|
245
|
+
Example:
|
|
246
|
+
backend = get_chinese_backend()
|
|
247
|
+
pinyin = backend.to_phonetic("你好") # "nihao"
|
|
248
|
+
"""
|
|
249
|
+
global _instance
|
|
250
|
+
|
|
251
|
+
if _instance is not None:
|
|
252
|
+
return _instance
|
|
253
|
+
|
|
254
|
+
with _instance_lock:
|
|
255
|
+
if _instance is None:
|
|
256
|
+
_instance = ChinesePhoneticBackend()
|
|
257
|
+
return _instance
|