crispy-ipa 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.
- crispy_ipa/__init__.py +9 -0
- crispy_ipa/data/__init__.py +1 -0
- crispy_ipa/data/en_dict.pkl +0 -0
- crispy_ipa/data/eng_to_ipa_rules.pkl +0 -0
- crispy_ipa/eng_to_ipa_hybrid.py +263 -0
- crispy_ipa/rule_engine.py +140 -0
- crispy_ipa/transcriber.py +86 -0
- crispy_ipa-0.1.0.dist-info/METADATA +59 -0
- crispy_ipa-0.1.0.dist-info/RECORD +12 -0
- crispy_ipa-0.1.0.dist-info/WHEEL +5 -0
- crispy_ipa-0.1.0.dist-info/licenses/LICENSE +21 -0
- crispy_ipa-0.1.0.dist-info/top_level.txt +1 -0
crispy_ipa/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Пустой файл — маркер пакета для importlib.resources
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"""Гибридный движок транскрипции: словарь исключений + rule_engine."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from functools import lru_cache
|
|
7
|
+
from . import rule_engine
|
|
8
|
+
from .rule_engine import CompiledRule
|
|
9
|
+
|
|
10
|
+
_WORD_PATTERN = re.compile(r"^[a-zA-Z']+$")
|
|
11
|
+
_TOKEN_PATTERN = re.compile(r"[a-zA-Z']+|[^a-zA-Z']+")
|
|
12
|
+
|
|
13
|
+
# Регулярки для умного разбиения и нормализации регистра
|
|
14
|
+
_CAMEL_BOUNDARY = re.compile(r'(?<=[a-z])(?=[A-Z])')
|
|
15
|
+
_ABBR_WORD_BOUNDARY = re.compile(r'(?<=[A-Z])(?=[A-Z][a-z])')
|
|
16
|
+
_UPPER_SEQUENCE = re.compile(r'(?<=[A-Z])(?=[A-Z])')
|
|
17
|
+
_WORD_START = re.compile(r'([A-Z])(?=[a-z])')
|
|
18
|
+
|
|
19
|
+
_SUFFIXES = sorted({
|
|
20
|
+
"ization", "isation",
|
|
21
|
+
"izing", "ising",
|
|
22
|
+
"izer", "iser",
|
|
23
|
+
"ment", "ness", "tion", "sion", "able", "ible",
|
|
24
|
+
"ful", "less", "ous", "ive",
|
|
25
|
+
"ing", "ed", "ly", "er", "or", "est",
|
|
26
|
+
"es", "s",
|
|
27
|
+
}, key=len, reverse=True)
|
|
28
|
+
|
|
29
|
+
MAX_DICT_WORD_LEN = 8
|
|
30
|
+
_MIN_PREFIX_WORD_LEN = 4
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# ─────────────────────────── Токенизация ───────────────────────────
|
|
34
|
+
|
|
35
|
+
def smart_lower(text: str) -> str:
|
|
36
|
+
text = _CAMEL_BOUNDARY.sub('-', text)
|
|
37
|
+
text = _ABBR_WORD_BOUNDARY.sub('-', text)
|
|
38
|
+
text = _UPPER_SEQUENCE.sub('-', text)
|
|
39
|
+
text = _WORD_START.sub(lambda m: m.group(1).lower(), text)
|
|
40
|
+
return text
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _tokenize(text: str) -> list[str]:
|
|
44
|
+
return _TOKEN_PATTERN.findall(text)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _is_word_token(token: str) -> bool:
|
|
48
|
+
return bool(_WORD_PATTERN.match(token))
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _is_abbreviation(token: str) -> bool:
|
|
52
|
+
return len(token) >= 2 and token.isupper()
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
# ─────────────────────────── Суффиксы и разбиение ───────────────────────────
|
|
56
|
+
|
|
57
|
+
def _find_suffix(word_lower: str) -> tuple[int, str] | None:
|
|
58
|
+
for suffix in _SUFFIXES:
|
|
59
|
+
if word_lower.endswith(suffix) and len(word_lower) > len(suffix):
|
|
60
|
+
return (len(word_lower) - len(suffix), suffix)
|
|
61
|
+
return None
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _ends_with_dict_word(
|
|
65
|
+
segments: list[tuple[str, bool]],
|
|
66
|
+
min_len: int = _MIN_PREFIX_WORD_LEN,
|
|
67
|
+
) -> bool:
|
|
68
|
+
if not segments:
|
|
69
|
+
return False
|
|
70
|
+
last_text, is_dict = segments[-1]
|
|
71
|
+
return is_dict and len(last_text) >= min_len
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _split_word_from_end(
|
|
75
|
+
word: str,
|
|
76
|
+
word_lower: str,
|
|
77
|
+
boundary: int,
|
|
78
|
+
long_keys: frozenset[str],
|
|
79
|
+
) -> list[tuple[str, bool]]:
|
|
80
|
+
if not long_keys:
|
|
81
|
+
return [(word[:boundary], False)]
|
|
82
|
+
|
|
83
|
+
segments: list[tuple[str, bool]] = []
|
|
84
|
+
pos = boundary
|
|
85
|
+
|
|
86
|
+
while pos > 0:
|
|
87
|
+
best_match = None
|
|
88
|
+
max_len = min(pos, MAX_DICT_WORD_LEN)
|
|
89
|
+
for length in range(max_len, 3, -1):
|
|
90
|
+
start = pos - length
|
|
91
|
+
candidate = word_lower[start:pos]
|
|
92
|
+
if candidate in long_keys:
|
|
93
|
+
best_match = (start, pos)
|
|
94
|
+
break
|
|
95
|
+
|
|
96
|
+
if best_match:
|
|
97
|
+
start, end = best_match
|
|
98
|
+
segments.append((word[start:end], True))
|
|
99
|
+
pos = start
|
|
100
|
+
else:
|
|
101
|
+
pos -= 1
|
|
102
|
+
if segments and not segments[-1][1]:
|
|
103
|
+
segments[-1] = (word[pos] + segments[-1][0], False)
|
|
104
|
+
else:
|
|
105
|
+
segments.append((word[pos], False))
|
|
106
|
+
|
|
107
|
+
segments.reverse()
|
|
108
|
+
return segments
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
@lru_cache(maxsize=10000)
|
|
112
|
+
def _split_word_cached(
|
|
113
|
+
word: str,
|
|
114
|
+
word_lower: str,
|
|
115
|
+
boundary: int,
|
|
116
|
+
long_keys: frozenset[str],
|
|
117
|
+
) -> tuple[tuple[str, bool], ...]:
|
|
118
|
+
return tuple(_split_word_from_end(word, word_lower, boundary, long_keys))
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
# ─────────────────────────── Шаг 1: Разбиение токенов на сегменты ───────────────────────────
|
|
122
|
+
|
|
123
|
+
def _build_word_segments(
|
|
124
|
+
tokens: list[str],
|
|
125
|
+
custom_exceptions: dict[str, str],
|
|
126
|
+
long_keys: frozenset[str],
|
|
127
|
+
) -> dict[str, list[tuple[str, bool]]]:
|
|
128
|
+
word_segments: dict[str, list[tuple[str, bool]]] = {}
|
|
129
|
+
|
|
130
|
+
for token in tokens:
|
|
131
|
+
if not _is_word_token(token):
|
|
132
|
+
continue
|
|
133
|
+
|
|
134
|
+
word_lower = token.lower()
|
|
135
|
+
if word_lower in word_segments:
|
|
136
|
+
continue
|
|
137
|
+
|
|
138
|
+
if _is_abbreviation(token):
|
|
139
|
+
word_segments[word_lower] = [(token, False)]
|
|
140
|
+
continue
|
|
141
|
+
|
|
142
|
+
if word_lower in custom_exceptions:
|
|
143
|
+
word_segments[word_lower] = [(token, True)]
|
|
144
|
+
continue
|
|
145
|
+
|
|
146
|
+
suffix_result = _find_suffix(word_lower)
|
|
147
|
+
if suffix_result:
|
|
148
|
+
boundary, suffix = suffix_result
|
|
149
|
+
prefix_segments = list(
|
|
150
|
+
_split_word_cached(token, word_lower, boundary, long_keys)
|
|
151
|
+
)
|
|
152
|
+
if _ends_with_dict_word(prefix_segments):
|
|
153
|
+
segments = prefix_segments + [(suffix, False)]
|
|
154
|
+
else:
|
|
155
|
+
segments = list(
|
|
156
|
+
_split_word_cached(token, word_lower, len(word_lower), long_keys)
|
|
157
|
+
)
|
|
158
|
+
else:
|
|
159
|
+
segments = list(
|
|
160
|
+
_split_word_cached(token, word_lower, len(word_lower), long_keys)
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
word_segments[word_lower] = segments
|
|
164
|
+
|
|
165
|
+
return word_segments
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
# ─────────────────────────── Шаг 2: Сбор неизвестных слов ───────────────────────────
|
|
169
|
+
|
|
170
|
+
def _collect_unknown_words(
|
|
171
|
+
word_segments: dict[str, list[tuple[str, bool]]],
|
|
172
|
+
) -> list[str]:
|
|
173
|
+
unknown: list[str] = []
|
|
174
|
+
seen: set[str] = set()
|
|
175
|
+
|
|
176
|
+
for segments in word_segments.values():
|
|
177
|
+
for seg_text, is_dict in segments:
|
|
178
|
+
if not is_dict and seg_text not in seen:
|
|
179
|
+
unknown.append(seg_text)
|
|
180
|
+
seen.add(seg_text)
|
|
181
|
+
|
|
182
|
+
return unknown
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
# ─────────────────────────── Шаг 3: Транскрипция неизвестных слов ───────────────────────────
|
|
186
|
+
|
|
187
|
+
def _bulk_transcribe(
|
|
188
|
+
words: list[str],
|
|
189
|
+
transcription_rules: Sequence[CompiledRule],
|
|
190
|
+
) -> dict[str, str]:
|
|
191
|
+
if not words:
|
|
192
|
+
return {}
|
|
193
|
+
|
|
194
|
+
rules_tuple = (
|
|
195
|
+
transcription_rules
|
|
196
|
+
if isinstance(transcription_rules, tuple)
|
|
197
|
+
else tuple(transcription_rules)
|
|
198
|
+
)
|
|
199
|
+
return {word: rule_engine.transcribe(word, rules_tuple) for word in words}
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
# ─────────────────────────── Шаг 4: Сборка результата ───────────────────────────
|
|
203
|
+
|
|
204
|
+
def _build_final_tokens(
|
|
205
|
+
tokens: list[str],
|
|
206
|
+
word_segments: dict[str, list[tuple[str, bool]]],
|
|
207
|
+
custom_exceptions: dict[str, str],
|
|
208
|
+
english_mapping: dict[str, str],
|
|
209
|
+
) -> list[str]:
|
|
210
|
+
final_tokens: list[str] = []
|
|
211
|
+
|
|
212
|
+
for token in tokens:
|
|
213
|
+
if not _is_word_token(token):
|
|
214
|
+
final_tokens.append(token)
|
|
215
|
+
continue
|
|
216
|
+
|
|
217
|
+
word_lower = token.lower()
|
|
218
|
+
segments = word_segments[word_lower]
|
|
219
|
+
ipa_parts: list[str] = []
|
|
220
|
+
|
|
221
|
+
for seg_text, is_dict in segments:
|
|
222
|
+
if is_dict:
|
|
223
|
+
ipa_parts.append(custom_exceptions[seg_text.lower()])
|
|
224
|
+
else:
|
|
225
|
+
ipa_parts.append(english_mapping.get(seg_text, seg_text))
|
|
226
|
+
|
|
227
|
+
final_tokens.append("".join(ipa_parts))
|
|
228
|
+
|
|
229
|
+
return final_tokens
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
# ─────────────────────────── Главная функция ───────────────────────────
|
|
233
|
+
|
|
234
|
+
def transcribe(
|
|
235
|
+
text: str,
|
|
236
|
+
custom_exceptions: dict[str, str],
|
|
237
|
+
long_keys: frozenset[str] | None,
|
|
238
|
+
transcription_rules: Sequence[CompiledRule],
|
|
239
|
+
) -> str:
|
|
240
|
+
if not text:
|
|
241
|
+
return ""
|
|
242
|
+
|
|
243
|
+
text = smart_lower(text)
|
|
244
|
+
tokens = _tokenize(text)
|
|
245
|
+
if not tokens:
|
|
246
|
+
return ""
|
|
247
|
+
|
|
248
|
+
if long_keys is None:
|
|
249
|
+
long_keys = frozenset(k for k in custom_exceptions if len(k) >= 4)
|
|
250
|
+
|
|
251
|
+
word_segments = _build_word_segments(tokens, custom_exceptions, long_keys)
|
|
252
|
+
unknown_words = _collect_unknown_words(word_segments)
|
|
253
|
+
english_mapping = _bulk_transcribe(unknown_words, transcription_rules)
|
|
254
|
+
final_tokens = _build_final_tokens(
|
|
255
|
+
tokens, word_segments, custom_exceptions, english_mapping
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
return "".join(final_tokens).strip()
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def clear_split_cache() -> None:
|
|
262
|
+
_split_word_cached.cache_clear()
|
|
263
|
+
rule_engine.clear_cache()
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Сверхбыстрый движок параллельного неразрушающего применения фонологических правил."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import functools
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
|
|
7
|
+
Step = tuple[bool, tuple[str, ...]]
|
|
8
|
+
CompiledRule = tuple[str, str, tuple[Step, ...], tuple[Step, ...]]
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def check_right(text: str, pos: int, steps: tuple[Step, ...]) -> bool:
|
|
12
|
+
for i, (is_neg, variants) in enumerate(steps):
|
|
13
|
+
if is_neg:
|
|
14
|
+
if text.startswith(variants, pos):
|
|
15
|
+
return False
|
|
16
|
+
rest = steps[i + 1 :]
|
|
17
|
+
if not rest:
|
|
18
|
+
return True
|
|
19
|
+
max_skip = max(len(v) for v in variants)
|
|
20
|
+
for skip in range(1, max_skip + 1):
|
|
21
|
+
if pos + skip <= len(text) and check_right(text, pos + skip, rest):
|
|
22
|
+
return True
|
|
23
|
+
return False
|
|
24
|
+
else:
|
|
25
|
+
val = variants[0]
|
|
26
|
+
if not text.startswith(val, pos):
|
|
27
|
+
return False
|
|
28
|
+
pos += len(val)
|
|
29
|
+
return True
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def check_left(text: str, pos: int, steps: tuple[Step, ...]) -> bool:
|
|
33
|
+
for i in range(len(steps) - 1, -1, -1):
|
|
34
|
+
is_neg, variants = steps[i]
|
|
35
|
+
if is_neg:
|
|
36
|
+
if text.endswith(variants, 0, pos):
|
|
37
|
+
return False
|
|
38
|
+
rest = steps[:i]
|
|
39
|
+
if not rest:
|
|
40
|
+
return True
|
|
41
|
+
max_skip = max(len(v) for v in variants)
|
|
42
|
+
for skip in range(1, max_skip + 1):
|
|
43
|
+
if pos - skip >= 0 and check_left(text, pos - skip, rest):
|
|
44
|
+
return True
|
|
45
|
+
return False
|
|
46
|
+
else:
|
|
47
|
+
val = variants[0]
|
|
48
|
+
if not text.endswith(val, 0, pos):
|
|
49
|
+
return False
|
|
50
|
+
pos -= len(val)
|
|
51
|
+
return True
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@functools.lru_cache(maxsize=8)
|
|
55
|
+
def _index_rules(
|
|
56
|
+
rules: tuple[CompiledRule, ...],
|
|
57
|
+
) -> dict[str, tuple[tuple[int, CompiledRule], ...]]:
|
|
58
|
+
"""Группирует правила по первому символу L с сохранением их индекса приоритета."""
|
|
59
|
+
buckets: dict[str, list[tuple[int, CompiledRule]]] = {}
|
|
60
|
+
for idx, rule in enumerate(rules):
|
|
61
|
+
first_char = rule[0][0]
|
|
62
|
+
buckets.setdefault(first_char, []).append((idx, rule))
|
|
63
|
+
return {ch: tuple(r_list) for ch, r_list in buckets.items()}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def apply_rules(word: str, rules: tuple[CompiledRule, ...]) -> str:
|
|
67
|
+
if not word:
|
|
68
|
+
return word
|
|
69
|
+
|
|
70
|
+
src = f"#{word}#"
|
|
71
|
+
src_len = len(src)
|
|
72
|
+
free_chars = src_len - 2
|
|
73
|
+
occupied = [False] * src_len
|
|
74
|
+
edits: list[tuple[int, int, str]] = []
|
|
75
|
+
|
|
76
|
+
buckets = _index_rules(rules)
|
|
77
|
+
|
|
78
|
+
candidate_rules: list[tuple[int, CompiledRule]] = []
|
|
79
|
+
for ch in dict.fromkeys(word):
|
|
80
|
+
if ch in buckets:
|
|
81
|
+
candidate_rules.extend(buckets[ch])
|
|
82
|
+
candidate_rules.sort()
|
|
83
|
+
|
|
84
|
+
for _, (L, R, left_steps, right_steps) in candidate_rules:
|
|
85
|
+
if L not in src:
|
|
86
|
+
continue
|
|
87
|
+
|
|
88
|
+
pat_len = len(L)
|
|
89
|
+
index = src.find(L, 1)
|
|
90
|
+
while index != -1:
|
|
91
|
+
end = index + pat_len
|
|
92
|
+
if not any(occupied[index:end]):
|
|
93
|
+
if check_left(src, index, left_steps) and check_right(src, end, right_steps):
|
|
94
|
+
edits.append((index, end, R))
|
|
95
|
+
for i in range(index, end):
|
|
96
|
+
occupied[i] = True
|
|
97
|
+
free_chars -= pat_len
|
|
98
|
+
if free_chars <= 0:
|
|
99
|
+
break
|
|
100
|
+
index = src.find(L, end)
|
|
101
|
+
|
|
102
|
+
if free_chars <= 0:
|
|
103
|
+
break
|
|
104
|
+
|
|
105
|
+
if not edits:
|
|
106
|
+
return word
|
|
107
|
+
|
|
108
|
+
if len(edits) > 1:
|
|
109
|
+
edits.sort(key=lambda e: e[0])
|
|
110
|
+
|
|
111
|
+
parts: list[str] = []
|
|
112
|
+
last_pos = 1
|
|
113
|
+
for start, end, replacement in edits:
|
|
114
|
+
parts.append(src[last_pos:start])
|
|
115
|
+
parts.append(replacement)
|
|
116
|
+
last_pos = end
|
|
117
|
+
parts.append(src[last_pos:-1])
|
|
118
|
+
|
|
119
|
+
return "".join(parts)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@functools.lru_cache(maxsize=10000)
|
|
123
|
+
def _transcribe_cached(text: str, rules_tuple: tuple[CompiledRule, ...]) -> str:
|
|
124
|
+
"""Внутренняя функция с LRU-кэшем. Принимает хэшируемый кортеж правил."""
|
|
125
|
+
return apply_rules(text, rules_tuple)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def transcribe(text: str, transcription_rules: Sequence[CompiledRule]) -> str:
|
|
129
|
+
"""Применяет правила транскрипции к слову (с кэшированием результатов)."""
|
|
130
|
+
rules_tuple = (
|
|
131
|
+
transcription_rules
|
|
132
|
+
if isinstance(transcription_rules, tuple)
|
|
133
|
+
else tuple(transcription_rules)
|
|
134
|
+
)
|
|
135
|
+
return _transcribe_cached(text, rules_tuple)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def clear_cache() -> None:
|
|
139
|
+
"""Очищает кэш транскрипции."""
|
|
140
|
+
_transcribe_cached.cache_clear()
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""Класс-оркестратор транскрипции."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import pickle
|
|
5
|
+
from importlib import resources
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from . import eng_to_ipa_hybrid
|
|
8
|
+
from .rule_engine import CompiledRule
|
|
9
|
+
|
|
10
|
+
_PACKAGE_DATA = resources.files("crispy_ipa.data")
|
|
11
|
+
|
|
12
|
+
# Пути к скомпилированным данным внутри пакета
|
|
13
|
+
_ENG_TO_IPA_RULES_PATH = Path(str(_PACKAGE_DATA.joinpath("eng_to_ipa_rules.pkl")))
|
|
14
|
+
_EN_DICT_PATH = Path(str(_PACKAGE_DATA.joinpath("en_dict.pkl")))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Transcriber:
|
|
18
|
+
|
|
19
|
+
def __init__(
|
|
20
|
+
self,
|
|
21
|
+
custom_vocabulary: dict[str, str] | None = None,
|
|
22
|
+
) -> None:
|
|
23
|
+
"""
|
|
24
|
+
Args:
|
|
25
|
+
custom_vocabulary: Пользовательский словарь (дополняет встроенный).
|
|
26
|
+
"""
|
|
27
|
+
# --- Пользовательский словарь (сохраняем оригинал + вариант в нижнем регистре) ---
|
|
28
|
+
self._custom_vocabulary: dict[str, str] = {}
|
|
29
|
+
if custom_vocabulary:
|
|
30
|
+
for k, v in custom_vocabulary.items():
|
|
31
|
+
self._custom_vocabulary[k] = v
|
|
32
|
+
k_lower = k.lower()
|
|
33
|
+
if k_lower not in custom_vocabulary:
|
|
34
|
+
self._custom_vocabulary[k_lower] = v
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# --- Загрузка ресурсов ---
|
|
38
|
+
self._eng_to_ipa_rules: tuple[CompiledRule, ...] = tuple(
|
|
39
|
+
self._load_pickle(_ENG_TO_IPA_RULES_PATH)
|
|
40
|
+
)
|
|
41
|
+
self._en_dict: dict[str, str] = self._load_pickle(_EN_DICT_PATH)
|
|
42
|
+
|
|
43
|
+
# --- Объединённый словарь (собирается один раз) ---
|
|
44
|
+
if self._custom_vocabulary:
|
|
45
|
+
self._merged_vocabulary = {**self._en_dict, **self._custom_vocabulary}
|
|
46
|
+
else:
|
|
47
|
+
self._merged_vocabulary = self._en_dict
|
|
48
|
+
|
|
49
|
+
# --- Ключи длиной ≥ 4 для разбиения незнакомых слов ---
|
|
50
|
+
self._long_keys = frozenset(
|
|
51
|
+
k for k in self._merged_vocabulary if len(k) >= 4
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
# ─────────────────────────── Публичный API ───────────────────────────
|
|
55
|
+
|
|
56
|
+
def transcribe(self, text: str) -> str:
|
|
57
|
+
return eng_to_ipa_hybrid.transcribe(
|
|
58
|
+
text,
|
|
59
|
+
self._merged_vocabulary,
|
|
60
|
+
self._long_keys,
|
|
61
|
+
self._eng_to_ipa_rules,
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
# ─────────────────────────── Доступ к ресурсам ───────────────────────
|
|
65
|
+
|
|
66
|
+
def get_eng_to_ipa_rules(self) -> tuple[CompiledRule, ...]:
|
|
67
|
+
"""Возвращает скомпилированные правила English → IPA."""
|
|
68
|
+
return self._eng_to_ipa_rules
|
|
69
|
+
|
|
70
|
+
# ─────────────────────────── Внутренние методы ───────────────────────
|
|
71
|
+
|
|
72
|
+
@staticmethod
|
|
73
|
+
def _load_pickle(path: Path):
|
|
74
|
+
if not path.exists():
|
|
75
|
+
raise FileNotFoundError(f"Файл не найден: {path}")
|
|
76
|
+
with open(path, "rb") as f:
|
|
77
|
+
return pickle.load(f)
|
|
78
|
+
|
|
79
|
+
# ─────────────────────────── Магические методы ────────────────────────
|
|
80
|
+
|
|
81
|
+
def __repr__(self) -> str:
|
|
82
|
+
return (
|
|
83
|
+
f"Transcriber("
|
|
84
|
+
f"eng_to_ipa_rules={len(self._eng_to_ipa_rules)}, "
|
|
85
|
+
f"dict={len(self._merged_vocabulary)})"
|
|
86
|
+
)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: crispy-ipa
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: automatic english-to-IPA text transcriber
|
|
5
|
+
Author: kodovy-pesets
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Isyarname/crispy-ipa
|
|
8
|
+
Project-URL: Repository, https://github.com/Isyarname/crispy-ipa
|
|
9
|
+
Project-URL: Issues, https://github.com/Isyarname/crispy-ipa/issues
|
|
10
|
+
Keywords: transcription,english,tts,text-to-speech,nlp,text-processing,g2p,ipa
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
20
|
+
Classifier: Natural Language :: English
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: build>=1.0.0; extra == "dev"
|
|
26
|
+
Requires-Dist: twine>=5.0.0; extra == "dev"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# crispy-ipa
|
|
30
|
+
|
|
31
|
+
Fast English-to-IPA transcription in pure Python with zero external dependencies.
|
|
32
|
+
|
|
33
|
+
Designed as a fast, deterministic intermediate grapheme-to-phoneme (G2P) stage for practical transcription pipelines.
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
* **Zero external dependencies:** built entirely on the Python standard library.
|
|
38
|
+
* **Hybrid engine:** combines a 125,000+ word phonetic dictionary, compound-word splitting, and a non-destructive rule engine.
|
|
39
|
+
* **High performance:** sub-millisecond processing without neural networks, heavy parsing, or sentence-level context analysis, backed by LRU caching.
|
|
40
|
+
* **Custom vocabulary support:** easily extend or override transcriptions for domain-specific terms and proper nouns.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install crispy-ipa
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from crispy_ipa import Transcriber
|
|
52
|
+
|
|
53
|
+
t = Transcriber(custom_vocabulary={
|
|
54
|
+
"nginx": "ˈɛndʒɪnˈɛks",
|
|
55
|
+
"ChatGPT": "ˈtʃætˈdʒiˈpiˈti",
|
|
56
|
+
})
|
|
57
|
+
print(t.transcribe("ChatGPT and nginx"))
|
|
58
|
+
# ˈtʃætˈdʒiˈpiˈti ˈænd ˈɛndʒɪnˈɛks
|
|
59
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
crispy_ipa/__init__.py,sha256=mX6yxxDEAFVy2tmB5gXTqdQxM7lk2-tTXWZUIkoQk8A,230
|
|
2
|
+
crispy_ipa/eng_to_ipa_hybrid.py,sha256=aZ2Kj5VpkdloWGEc8TxKJzbZ9sEk0y25EcX8OkylUlU,8521
|
|
3
|
+
crispy_ipa/rule_engine.py,sha256=OdZsoBLyhXD12AEU05PoE1rQ7EG791ncUCuLmck1h6w,4717
|
|
4
|
+
crispy_ipa/transcriber.py,sha256=bm1PlW3rY-eFZgU7QP9hroGfO0rptYOHcWpMh1nCumY,3680
|
|
5
|
+
crispy_ipa/data/__init__.py,sha256=JKbGumA93NoAVhiLFZVUp92DUkisq5as9FDevEQr62M,80
|
|
6
|
+
crispy_ipa/data/en_dict.pkl,sha256=Aag7WwxIgUyCkPoYf94UIPRqXhKuxbbH2UwFpOfSEjs,3276259
|
|
7
|
+
crispy_ipa/data/eng_to_ipa_rules.pkl,sha256=cndjbxONzFFYZltAdq37nFNg9S3M_IOtT2iX_fFbuMc,16170
|
|
8
|
+
crispy_ipa-0.1.0.dist-info/licenses/LICENSE,sha256=qHhQ0xdTp-fUUymVJphRkdwjmta5P-ipirihe4Kp4bQ,1084
|
|
9
|
+
crispy_ipa-0.1.0.dist-info/METADATA,sha256=Xcl8X4KnMcgofVbXYJq6TzmBq8O25jjYfWsB4T00cjs,2210
|
|
10
|
+
crispy_ipa-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
11
|
+
crispy_ipa-0.1.0.dist-info/top_level.txt,sha256=Abvo0-ZHDEEga9lyIRavCX3pOFdL0CRoUQyPyKRMMGc,11
|
|
12
|
+
crispy_ipa-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Andrew
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
crispy_ipa
|