gapiring 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.
- gapiring/__init__.py +100 -0
- gapiring/__main__.py +3 -0
- gapiring/cli.py +41 -0
- gapiring/engine.py +199 -0
- gapiring/frontend/__init__.py +0 -0
- gapiring/frontend/common.py +16 -0
- gapiring/frontend/lexicon_extra.tsv +6 -0
- gapiring/frontend/tashkent_dialect.py +222 -0
- gapiring/frontend/terms.py +58 -0
- gapiring/frontend/terms_reviewed.json +4618 -0
- gapiring/frontend/terms_uz.py +71 -0
- gapiring/frontend/text_to_phoneme/__init__.py +0 -0
- gapiring/frontend/text_to_phoneme/phoneme_converter.py +329 -0
- gapiring/frontend/text_to_phoneme/pipelene_text_to_phoneme.py +55 -0
- gapiring/frontend/text_to_phoneme/text_normalization_for_tts.py +208 -0
- gapiring/frontend/text_to_phoneme/tools/__init__.py +0 -0
- gapiring/frontend/text_to_phoneme/tools/convert_currency_to_text.py +76 -0
- gapiring/frontend/text_to_phoneme/tools/convert_date_time_to_text.py +371 -0
- gapiring/frontend/text_to_phoneme/tools/convert_non_roman_numbers_to_text.py +609 -0
- gapiring/frontend/text_to_phoneme/tools/convert_phone_numbers_to_text.py +268 -0
- gapiring/frontend/text_to_phoneme/tools/convert_roman_numerals_to_text.py +235 -0
- gapiring/frontend/text_to_phoneme/tools/convert_symbols_to_uzbek_text.py +258 -0
- gapiring/frontend/text_to_phoneme/tools/expand_uzbek_abbreviations.py +293 -0
- gapiring/frontend/text_to_phoneme/tools/full_normalize.py +257 -0
- gapiring/frontend/text_to_phoneme/tools/pronounce_links_and_emails.py +99 -0
- gapiring/frontend/text_to_phoneme/tools/split_text_numbers.py +36 -0
- gapiring/frontend/text_to_phoneme/tools/spoken_lexicon.py +40 -0
- gapiring/frontend/text_to_phoneme/tools/spoken_prepass.py +116 -0
- gapiring/frontend/text_to_phoneme/uzbek_orthography.py +24 -0
- gapiring/frontend/textnorm.py +172 -0
- gapiring/frontend/tts_frontend.py +344 -0
- gapiring/lexicon.py +188 -0
- gapiring/text_front.py +77 -0
- gapiring-0.1.0.dist-info/METADATA +69 -0
- gapiring-0.1.0.dist-info/RECORD +39 -0
- gapiring-0.1.0.dist-info/WHEEL +5 -0
- gapiring-0.1.0.dist-info/entry_points.txt +2 -0
- gapiring-0.1.0.dist-info/licenses/LICENSE +4 -0
- gapiring-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from gapiring.frontend.text_to_phoneme.uzbek_orthography import normalize_uzbek_apostrophes
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def standardize_number_dashes(text):
|
|
7
|
+
dash_symbols = ['-', '–', '—', '−', '‐', '‑', '⁃', '⁻', '₋']
|
|
8
|
+
|
|
9
|
+
escaped_dashes = [re.escape(d) for d in dash_symbols]
|
|
10
|
+
dash_pattern_part = '(?:' + '|'.join(escaped_dashes) + ')'
|
|
11
|
+
|
|
12
|
+
pattern = f'(\\d+)\\s*{dash_pattern_part}\\s*(\\w+)'
|
|
13
|
+
text = re.sub(pattern, r'\1-\2', text)
|
|
14
|
+
|
|
15
|
+
return text
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def turkish_to_uzbek(text):
|
|
19
|
+
replacements = {
|
|
20
|
+
'ç': 'ch',
|
|
21
|
+
'Ç': 'Ch',
|
|
22
|
+
'ş': 'sh',
|
|
23
|
+
'Ş': 'Sh',
|
|
24
|
+
'ğ': 'g',
|
|
25
|
+
'Ğ': 'G',
|
|
26
|
+
'ı': 'i',
|
|
27
|
+
'İ': 'I',
|
|
28
|
+
'ö': 'o',
|
|
29
|
+
'Ö': 'O',
|
|
30
|
+
'ü': 'u',
|
|
31
|
+
'Ü': 'U'
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for turkish, uzbek in replacements.items():
|
|
35
|
+
text = text.replace(turkish, uzbek)
|
|
36
|
+
|
|
37
|
+
return text
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def cyrillic_to_latin(text):
|
|
41
|
+
mapping = {
|
|
42
|
+
'а': 'a', 'А': 'A',
|
|
43
|
+
'б': 'b', 'Б': 'B',
|
|
44
|
+
'в': 'v', 'В': 'V',
|
|
45
|
+
'г': 'g', 'Г': 'G',
|
|
46
|
+
'д': 'd', 'Д': 'D',
|
|
47
|
+
'е': 'e', 'Е': 'E',
|
|
48
|
+
'ё': 'yo', 'Ё': 'Yo',
|
|
49
|
+
'ж': 'j', 'Ж': 'J',
|
|
50
|
+
'з': 'z', 'З': 'Z',
|
|
51
|
+
'и': 'i', 'И': 'I',
|
|
52
|
+
'й': 'y', 'Й': 'Y',
|
|
53
|
+
'к': 'k', 'К': 'K',
|
|
54
|
+
'л': 'l', 'Л': 'L',
|
|
55
|
+
'м': 'm', 'М': 'M',
|
|
56
|
+
'н': 'n', 'Н': 'N',
|
|
57
|
+
'о': 'o', 'О': 'O',
|
|
58
|
+
'п': 'p', 'П': 'P',
|
|
59
|
+
'р': 'r', 'Р': 'R',
|
|
60
|
+
'с': 's', 'С': 'S',
|
|
61
|
+
'т': 't', 'Т': 'T',
|
|
62
|
+
'у': 'u', 'У': 'U',
|
|
63
|
+
'ф': 'f', 'Ф': 'F',
|
|
64
|
+
'х': 'x', 'Х': 'X',
|
|
65
|
+
'ц': 'ts', 'Ц': 'Ts',
|
|
66
|
+
'ч': 'ch', 'Ч': 'Ch',
|
|
67
|
+
'ш': 'sh', 'Ш': 'Sh',
|
|
68
|
+
'щ': 'sch', 'Щ': 'Sch',
|
|
69
|
+
'ъ': ''', 'Ъ': ''',
|
|
70
|
+
'ы': 'y', 'Ы': 'Y',
|
|
71
|
+
'э': 'e', 'Э': 'E',
|
|
72
|
+
'ю': 'yu', 'Ю': 'Yu',
|
|
73
|
+
'я': 'ya', 'Я': 'Ya',
|
|
74
|
+
'ў': 'o\'', 'Ў': 'O\'',
|
|
75
|
+
'қ': 'q', 'Қ': 'Q',
|
|
76
|
+
'ғ': 'g\'', 'Ғ': 'G\'',
|
|
77
|
+
'ҳ': 'h', 'Ҳ': 'H',
|
|
78
|
+
'ь': '', 'Ь': ''
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
text = text.replace('Ья', 'Ya').replace('ья', 'ya')
|
|
82
|
+
text = text.replace('Ьи', 'Yi').replace('ьи', 'yi')
|
|
83
|
+
text = text.replace('Ьо', 'Yo').replace('ьо', 'yo')
|
|
84
|
+
text = text.replace('Ьу', 'Yu').replace('ьу', 'yu')
|
|
85
|
+
|
|
86
|
+
for cyrillic, latin in mapping.items():
|
|
87
|
+
text = text.replace(cyrillic, latin)
|
|
88
|
+
|
|
89
|
+
text = re.sub(r'[А-Яа-яЁёЎўҚқҒғҲҳ]', '', text)
|
|
90
|
+
|
|
91
|
+
return text
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def ensure_sentence_endings(text):
|
|
95
|
+
text = re.sub(r'([?!])\s*\.+\s*(\w)', r'\1 \2', text)
|
|
96
|
+
|
|
97
|
+
return text
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def fix_multiple_periods(text):
|
|
101
|
+
text = re.sub(r'([.!?])\s*\.+\s*(?=\w)', r'\1 ', text)
|
|
102
|
+
text = re.sub(r'(?<=\w)\s*\.{2,}\s*(?=\w)', r'. ', text)
|
|
103
|
+
text = re.sub(r'([.!?])\s*\.+\n', r'\1\n', text)
|
|
104
|
+
text = re.sub(r'([.!?])\s*\.+$', r'\1', text)
|
|
105
|
+
text = re.sub(r'\.{2,}(?!\w)', '.', text)
|
|
106
|
+
text = re.sub(r'(?<=\w)\.{2,}', '.', text)
|
|
107
|
+
|
|
108
|
+
return text
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def standartize_text(text):
|
|
112
|
+
text = normalize_uzbek_apostrophes(text)
|
|
113
|
+
text = standardize_number_dashes(text)
|
|
114
|
+
text = re.sub(r'(\d+)-(\w+)', r'NUM_DASH_\1_\2', text)
|
|
115
|
+
|
|
116
|
+
text = text.replace('"', '"').replace('"', '"').replace('„', '"')
|
|
117
|
+
text = text.replace(''', "'").replace(''', "'").replace('`', "'") \
|
|
118
|
+
.replace('´', "'").replace('ʻ', "'").replace('ʼ', "'").replace("‘","'")
|
|
119
|
+
|
|
120
|
+
text = fix_multiple_periods(text)
|
|
121
|
+
text = text.replace("...", ".").replace("..", ".")
|
|
122
|
+
|
|
123
|
+
text = cyrillic_to_latin(text)
|
|
124
|
+
|
|
125
|
+
pattern = r'[^a-zA-Z0-9\s\'\!\?\,\.NUM_DASH_]'
|
|
126
|
+
text = re.sub(pattern, ' ', text)
|
|
127
|
+
|
|
128
|
+
while ' ' in text:
|
|
129
|
+
text = text.replace(' ', ' ')
|
|
130
|
+
text = text.strip()
|
|
131
|
+
|
|
132
|
+
text = re.sub(r'NUM_DASH_(\d+)_(\w+)', r'\1-\2', text)
|
|
133
|
+
|
|
134
|
+
text = ensure_sentence_endings(text)
|
|
135
|
+
text = re.sub(r'\n+', '\n', text).strip()
|
|
136
|
+
|
|
137
|
+
return text
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def normalize_text(text: str) -> str:
|
|
141
|
+
text = normalize_uzbek_apostrophes(text)
|
|
142
|
+
|
|
143
|
+
text = re.sub(r'g[`\']', "g'", text)
|
|
144
|
+
text = re.sub(r'o[`\']', "o'", text)
|
|
145
|
+
|
|
146
|
+
text = standardize_number_dashes(text)
|
|
147
|
+
text = re.sub(r'(\d+)-(\w+)', r'NUM_DASH_\1_\2', text)
|
|
148
|
+
|
|
149
|
+
text = text.replace('-', ' ')
|
|
150
|
+
|
|
151
|
+
text = re.sub(r'[^a-zA-Z0-9\s\'.,?!NUM_DASH_]', '', text)
|
|
152
|
+
|
|
153
|
+
text = re.sub(r'\.{2,}', '.', text)
|
|
154
|
+
text = re.sub(r'\s+', ' ', text)
|
|
155
|
+
|
|
156
|
+
text = re.sub(r'NUM_DASH_(\d+)_(\w+)', r'\1-\2', text)
|
|
157
|
+
text = text.strip()
|
|
158
|
+
|
|
159
|
+
return text
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def full_normalize(text):
|
|
163
|
+
text = turkish_to_uzbek(text)
|
|
164
|
+
text = standartize_text(text)
|
|
165
|
+
text = normalize_text(text)
|
|
166
|
+
return text
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def handle_uzb_letters(text: str) -> str:
|
|
170
|
+
return text.replace("o'", "ō").replace("g'", "ḡ").replace("O'", "Ō").replace("G'", "Ḡ")
|
|
171
|
+
|
|
172
|
+
def add_space_around_symbols(text: str) -> str:
|
|
173
|
+
if not isinstance(text, str):
|
|
174
|
+
raise TypeError("Input must be a string.")
|
|
175
|
+
|
|
176
|
+
pattern = r"([^\w\s'])|(?<!\w)'|'(?!\w)"
|
|
177
|
+
|
|
178
|
+
text_with_spaces = re.sub(pattern, r' \g<0> ', text)
|
|
179
|
+
|
|
180
|
+
cleaned_text = re.sub(r'\s+', ' ', text_with_spaces)
|
|
181
|
+
|
|
182
|
+
return cleaned_text.strip()
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
if __name__ == "__main__":
|
|
186
|
+
samples = [
|
|
187
|
+
"Ç б Bu hammaga ma'qul tushdi. Ko'pchilik nomidan xat yozib, Yefim Yefimovichning bu yerda?",
|
|
188
|
+
"1921-yil mart oyi",
|
|
189
|
+
"1921 - yil mart oyi",
|
|
190
|
+
"10000",
|
|
191
|
+
"2-talaba",
|
|
192
|
+
"1-sinf",
|
|
193
|
+
"102-raqam",
|
|
194
|
+
"2023-yilgi rejamiz",
|
|
195
|
+
"2023 - yilgi rejamiz",
|
|
196
|
+
"2023—yilgi rejamiz",
|
|
197
|
+
"2023–yilgi rejamiz",
|
|
198
|
+
"Men 7-sinfda o'qiyman",
|
|
199
|
+
"9-qavatda yashaymiz",
|
|
200
|
+
"3-xonada dars bo'ladi",
|
|
201
|
+
"This is 25% discount",
|
|
202
|
+
"Inflyatsiya darajasi %7 ga tushdi",
|
|
203
|
+
"Inflyatsiya darajasi % 7 ga tushdi",
|
|
204
|
+
"Mahsulot narxi 10% oshdi",
|
|
205
|
+
"% belgisi foizni bildiradi",
|
|
206
|
+
"50% chegirma!",
|
|
207
|
+
"Mahsulot a-b-c turlarini o'z ichiga oladi",
|
|
208
|
+
"So'z... Keyingi so'z.",
|
|
209
|
+
"Gap tugadi!.. Yangi gap.",
|
|
210
|
+
"Привет мир! Ўзбекча матн. Қаттиқ ЎҒҲ.",
|
|
211
|
+
"Bu matnda \"maxsus\" 'belgilar' bor.",
|
|
212
|
+
" Salom\n\nQalaysan ",
|
|
213
|
+
"Raqamlar: 123-uy, 45-maktab.",
|
|
214
|
+
"Oxirgi gap",
|
|
215
|
+
"chollar?.. Halqa",
|
|
216
|
+
"word......nextWord",
|
|
217
|
+
"word?......nextWord"
|
|
218
|
+
]
|
|
219
|
+
|
|
220
|
+
print("Normalized outputs (NO auto-dots added, only ?!., punctuation kept for pronunciation):")
|
|
221
|
+
for i, sample in enumerate(samples):
|
|
222
|
+
print(f"--- Sample {i + 1} ---")
|
|
223
|
+
print(f"Original: {sample}")
|
|
224
|
+
normalized = full_normalize(sample)
|
|
225
|
+
print(f"Normalized: {normalized}")
|
|
226
|
+
print("-" * 50)
|
|
227
|
+
|
|
228
|
+
print("\n--- Specific Request Test (% symbols removed) ---")
|
|
229
|
+
sample_test_1 = "Inflyatsiya darajasi %7 ga tushdi"
|
|
230
|
+
print(f"Original: {sample_test_1}")
|
|
231
|
+
normalized_1 = full_normalize(sample_test_1)
|
|
232
|
+
print(f"Normalized: {normalized_1}")
|
|
233
|
+
|
|
234
|
+
sample_test_2 = "Chegirma % 15 miqdorida"
|
|
235
|
+
print(f"Original: {sample_test_2}")
|
|
236
|
+
normalized_2 = full_normalize(sample_test_2)
|
|
237
|
+
print(f"Normalized: {normalized_2}")
|
|
238
|
+
|
|
239
|
+
sample_test_3 = "Foiz: %."
|
|
240
|
+
print(f"Original: {sample_test_3}")
|
|
241
|
+
normalized_3 = full_normalize(sample_test_3)
|
|
242
|
+
print(f"Normalized: {normalized_3}")
|
|
243
|
+
|
|
244
|
+
print("\n--- Testing punctuation removal ---")
|
|
245
|
+
test_punct = 'Hello: "world"; (test) [brackets] {braces} @symbol #hash $dollar & more!'
|
|
246
|
+
print(f"Original: {test_punct}")
|
|
247
|
+
normalized_punct = full_normalize(test_punct)
|
|
248
|
+
print(f"Normalized: {normalized_punct}")
|
|
249
|
+
|
|
250
|
+
print("\n--- Testing NO auto-dots ---")
|
|
251
|
+
test_no_dots = "Oxirgi gap"
|
|
252
|
+
print(f"Original: {test_no_dots}")
|
|
253
|
+
normalized_no_dots = full_normalize(test_no_dots)
|
|
254
|
+
print(f"Normalized: {normalized_no_dots}")
|
|
255
|
+
print("ASR normalization completed - NO automatic periods added, only ?!., punctuation retained for pronunciation.")
|
|
256
|
+
|
|
257
|
+
print(add_space_around_symbols("bir ming tōqqiz yuz tōqson birinchi yilda 'ōzbekiston' ōz mustaqilligini e'lon qildi."))
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
UZBEK_SYMBOLS = {
|
|
4
|
+
'.': 'nuqta',
|
|
5
|
+
'@': 'kuchukcha',
|
|
6
|
+
'/': 'slesh',
|
|
7
|
+
':': 'ikki nuqta',
|
|
8
|
+
'-': 'chiziqcha',
|
|
9
|
+
'_': 'pastki chiziqcha',
|
|
10
|
+
'?': 'savol belgisi',
|
|
11
|
+
'=': 'tenglik belgisi',
|
|
12
|
+
'&': 'va belgisi',
|
|
13
|
+
'#': 'panjara',
|
|
14
|
+
'%': 'foiz',
|
|
15
|
+
'+': 'plyus'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _convert_match_to_spoken_uzbek(match_obj):
|
|
20
|
+
original_text = match_obj.group(0)
|
|
21
|
+
final_spoken_segments = []
|
|
22
|
+
current_text_to_process = original_text
|
|
23
|
+
|
|
24
|
+
processed_prefix = False
|
|
25
|
+
if current_text_to_process.startswith("https://"):
|
|
26
|
+
final_spoken_segments.append("https")
|
|
27
|
+
final_spoken_segments.append(UZBEK_SYMBOLS[':'])
|
|
28
|
+
final_spoken_segments.append("ikkita")
|
|
29
|
+
final_spoken_segments.append(UZBEK_SYMBOLS['/'])
|
|
30
|
+
current_text_to_process = current_text_to_process[len("https://"):]
|
|
31
|
+
processed_prefix = True
|
|
32
|
+
elif current_text_to_process.startswith("http://"):
|
|
33
|
+
final_spoken_segments.append("http")
|
|
34
|
+
final_spoken_segments.append(UZBEK_SYMBOLS[':'])
|
|
35
|
+
final_spoken_segments.append("ikkita")
|
|
36
|
+
final_spoken_segments.append(UZBEK_SYMBOLS['/'])
|
|
37
|
+
current_text_to_process = current_text_to_process[len("http://"):]
|
|
38
|
+
processed_prefix = True
|
|
39
|
+
|
|
40
|
+
if current_text_to_process.startswith("www."):
|
|
41
|
+
final_spoken_segments.append("dablyu")
|
|
42
|
+
final_spoken_segments.append("dablyu")
|
|
43
|
+
final_spoken_segments.append("dablyu")
|
|
44
|
+
final_spoken_segments.append(UZBEK_SYMBOLS['.'])
|
|
45
|
+
current_text_to_process = current_text_to_process[len("www."):]
|
|
46
|
+
processed_prefix = True
|
|
47
|
+
|
|
48
|
+
if not processed_prefix or current_text_to_process:
|
|
49
|
+
current_alnum_chunk = ""
|
|
50
|
+
|
|
51
|
+
for char_code in current_text_to_process:
|
|
52
|
+
if char_code.isalnum():
|
|
53
|
+
current_alnum_chunk += char_code
|
|
54
|
+
elif char_code in UZBEK_SYMBOLS:
|
|
55
|
+
if current_alnum_chunk:
|
|
56
|
+
final_spoken_segments.append(current_alnum_chunk)
|
|
57
|
+
current_alnum_chunk = ""
|
|
58
|
+
final_spoken_segments.append(UZBEK_SYMBOLS[char_code])
|
|
59
|
+
else:
|
|
60
|
+
if current_alnum_chunk:
|
|
61
|
+
final_spoken_segments.append(current_alnum_chunk)
|
|
62
|
+
current_alnum_chunk = ""
|
|
63
|
+
final_spoken_segments.append(char_code)
|
|
64
|
+
|
|
65
|
+
if current_alnum_chunk:
|
|
66
|
+
final_spoken_segments.append(current_alnum_chunk)
|
|
67
|
+
|
|
68
|
+
return " ".join(filter(None, final_spoken_segments))
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def pronounce_links_and_emails(text):
|
|
72
|
+
if not text:
|
|
73
|
+
return ""
|
|
74
|
+
email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
|
|
75
|
+
url_pattern = r'\b(?:https?://|www\.)[\w\-\._~:/?#[\]@!\$&\'()*+,;=%]+\b'
|
|
76
|
+
|
|
77
|
+
processed_text = re.sub(url_pattern, _convert_match_to_spoken_uzbek, text)
|
|
78
|
+
processed_text = re.sub(email_pattern, _convert_match_to_spoken_uzbek, processed_text)
|
|
79
|
+
return processed_text
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
if __name__ == "__main__":
|
|
83
|
+
test_texts = [
|
|
84
|
+
"Mening emailim user.name123@example.co.uk va saytim https://www.my-site.com/path?query=value#section.",
|
|
85
|
+
"Boshqa bir sayt http://test-site.org va email info@company.name.",
|
|
86
|
+
"Oddiy matn, linklarsiz.",
|
|
87
|
+
"Faqat email: test@mail.uz",
|
|
88
|
+
"Faqat sayt: www.google.com",
|
|
89
|
+
"Sayt port bilan: http://localhost:8080/api",
|
|
90
|
+
"Aralash: n1a2m3e@domain.com va http://site-v2.com/p1a2th",
|
|
91
|
+
"Yana bir sayt https://www.another-example.net/test/page.html?id=456&type=A#content",
|
|
92
|
+
"Sayt nuqta bilan tugaydi: www.example.com."
|
|
93
|
+
]
|
|
94
|
+
|
|
95
|
+
for i, test_text in enumerate(test_texts):
|
|
96
|
+
print(f"--- Test {i + 1} ---")
|
|
97
|
+
print(f"Original: {test_text}")
|
|
98
|
+
processed = pronounce_links_and_emails(test_text)
|
|
99
|
+
print(f"Processed: {processed}\n")
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
def split_text_numbers(text):
|
|
4
|
+
result = re.sub(r'([a-zA-Z])(\d)', r'\1 \2', text)
|
|
5
|
+
result = re.sub(r'(\d)([a-zA-Z])', r'\1 \2', result)
|
|
6
|
+
|
|
7
|
+
return result
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def main():
|
|
11
|
+
|
|
12
|
+
test_cases = [
|
|
13
|
+
"1a2s",
|
|
14
|
+
"hi1my323",
|
|
15
|
+
"abc123def456",
|
|
16
|
+
"test1",
|
|
17
|
+
"123test",
|
|
18
|
+
"a1b2c3",
|
|
19
|
+
"hello123world456",
|
|
20
|
+
"noNumbers",
|
|
21
|
+
"123456",
|
|
22
|
+
"a1b2c3d4e5"
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
print("Text-Number Splitter Examples:")
|
|
26
|
+
print("=" * 40)
|
|
27
|
+
|
|
28
|
+
for test in test_cases:
|
|
29
|
+
result = split_text_numbers(test)
|
|
30
|
+
print(f"Input: '{test}'")
|
|
31
|
+
print(f"Output: '{result}'")
|
|
32
|
+
print()
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
if __name__ == "__main__":
|
|
36
|
+
main()
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
|
|
2
|
+
import re
|
|
3
|
+
|
|
4
|
+
LEXICON = {
|
|
5
|
+
"SMS": "es-em-es", "IT": "ay-ti", "AI": "ey-ay", "SMM": "es-em-em", "HR": "eych-ar",
|
|
6
|
+
"CRM": "si-ar-em", "ID": "ay-di", "VIP": "vip", "IELTS": "ayelts", "TOEFL": "tofl",
|
|
7
|
+
"PDF": "pe-de-ef", "QR": "kyu-ar", "Wi-Fi": "vay-fay", "MRT": "em-er-te", "UZI": "uzi",
|
|
8
|
+
"EKG": "e-ka-ge", "MSKT": "em-es-ka-te", "NDS": "en-de-es",
|
|
9
|
+
"YouTube": "Yutub", "iPhone": "Ayfon", "WhatsApp": "Vatsap", "Google": "Gugl", "Zoom": "Zum",
|
|
10
|
+
"Click": "Klik", "Payme": "Peymi", "Uzcard": "Uzkard", "BYD": "bi-vay-di",
|
|
11
|
+
"Chevrolet": "Shevrole", "Excel": "Eksel", "Gmail": "Jimeyl",
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
_TERM_RE = re.compile(r"(?<![\w'-])(" + "|".join(sorted(map(re.escape, LEXICON), key=len, reverse=True))
|
|
15
|
+
+ r")(?![\w'-])")
|
|
16
|
+
_PLACEHOLDER_RE = re.compile(r"zqterm([a-z]+)qz", re.IGNORECASE)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _index_to_letters(index):
|
|
20
|
+
letters = ""
|
|
21
|
+
index += 1
|
|
22
|
+
while index:
|
|
23
|
+
index, rest = divmod(index - 1, 26)
|
|
24
|
+
letters = chr(ord("a") + rest) + letters
|
|
25
|
+
return letters
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def hide_terms(text):
|
|
29
|
+
found = []
|
|
30
|
+
|
|
31
|
+
def replace(match):
|
|
32
|
+
found.append(match.group(1))
|
|
33
|
+
return f"zqterm{_index_to_letters(len(found) - 1)}qz"
|
|
34
|
+
|
|
35
|
+
return _TERM_RE.sub(replace, text), found
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def restore_terms(text, found):
|
|
39
|
+
lookup = {_index_to_letters(i): LEXICON[term] for i, term in enumerate(found)}
|
|
40
|
+
return _PLACEHOLDER_RE.sub(lambda match: lookup.get(match.group(1).lower(), match.group(0)), text)
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
|
|
2
|
+
import re
|
|
3
|
+
|
|
4
|
+
from gapiring.frontend.text_to_phoneme.tools.convert_non_roman_numbers_to_text import number_to_ordinal_uz, number_to_words_uz
|
|
5
|
+
|
|
6
|
+
CASE_SUFFIXES = ("gacha", "lardan", "larda", "dagi", "dan", "da", "ga", "ning", "ni", "lar")
|
|
7
|
+
_SUFFIX_RE = "|".join(CASE_SUFFIXES)
|
|
8
|
+
_CURRENCY_AFTER = re.compile(r"\s*(so'm|sum|dollar|yevro|rubl|uzs|usd|eur)\b", re.IGNORECASE)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def say_number(value):
|
|
12
|
+
return number_to_words_uz(int(value)) if int(value) else "nol"
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def attach_suffix(words, suffix):
|
|
16
|
+
if not suffix:
|
|
17
|
+
return words
|
|
18
|
+
suffix = suffix.lower()
|
|
19
|
+
if suffix.startswith("g") and words[-1] in "kq":
|
|
20
|
+
suffix = words[-1] + suffix[1:]
|
|
21
|
+
return words + suffix
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def connective_u(words):
|
|
25
|
+
return words + ("yu" if words[-1] in "aeiou" else "u")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def say_time(hour, minute, second=None, after_soat=True):
|
|
29
|
+
if hour == 0 and minute == 0 and not second:
|
|
30
|
+
return "nol nol"
|
|
31
|
+
words = say_number(hour)
|
|
32
|
+
if minute:
|
|
33
|
+
words += f" nol {say_number(minute)}" if minute < 10 else f" {say_number(minute)}"
|
|
34
|
+
if second:
|
|
35
|
+
words += f" {say_number(second)}"
|
|
36
|
+
return words
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _after_soat(match):
|
|
40
|
+
return bool(re.search(r"\bsoat\s*$", match.string[:match.start()], flags=re.IGNORECASE))
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _time_match_to_text(match, suffix_group):
|
|
44
|
+
hour, minute = int(match.group("h")), int(match.group("m"))
|
|
45
|
+
second = int(match.group("s")) if match.group("s") else None
|
|
46
|
+
if hour > 24 or minute > 59 or (second is not None and second > 59):
|
|
47
|
+
return match.group(0)
|
|
48
|
+
return attach_suffix(say_time(hour, minute, second, _after_soat(match)), match.group(suffix_group) or "")
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def convert_times(text):
|
|
52
|
+
time = r"(?P<h{n}>\d{{1,2}}):(?P<m{n}>\d{{2}})"
|
|
53
|
+
range_re = re.compile(rf"(?<![\d:]){time.format(n=1)}\s*[-–]\s*{time.format(n=2)}(?![\d:])")
|
|
54
|
+
|
|
55
|
+
def replace_range(match):
|
|
56
|
+
h1, m1, h2, m2 = (int(match.group(g)) for g in ("h1", "m1", "h2", "m2"))
|
|
57
|
+
if max(h1, h2) > 24 or max(m1, m2) > 59:
|
|
58
|
+
return match.group(0)
|
|
59
|
+
soat = _after_soat(match)
|
|
60
|
+
return f"{attach_suffix(say_time(h1, m1, after_soat=soat), 'dan')} " \
|
|
61
|
+
f"{attach_suffix(say_time(h2, m2, after_soat=soat), 'gacha')}"
|
|
62
|
+
|
|
63
|
+
text = range_re.sub(replace_range, text)
|
|
64
|
+
single_re = re.compile(
|
|
65
|
+
rf"(?<![\d:.])(?P<h>\d{{1,2}}):(?P<m>\d{{2}})(?::(?P<s>\d{{2}}))?(?![\d:])(?:[\s-]?(?P<suffix>{_SUFFIX_RE})\b)?",
|
|
66
|
+
flags=re.IGNORECASE)
|
|
67
|
+
return single_re.sub(lambda match: _time_match_to_text(match, "suffix"), text)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def convert_decimals(text):
|
|
71
|
+
text = re.sub(r"(?<![\d.,])(\d+),(\d{1,2})(?![\d,]|\.\d)", r"\1.\2", text)
|
|
72
|
+
|
|
73
|
+
def half(match):
|
|
74
|
+
whole = int(match.group(1))
|
|
75
|
+
return "yarim" if whole == 0 else f"{say_number(whole)} yarim"
|
|
76
|
+
|
|
77
|
+
return re.sub(r"(?<![\d.,])(\d+)\.5(?![\d]|\.\d)", half, text)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def convert_ranges_and_codes(text):
|
|
81
|
+
if "=" in text:
|
|
82
|
+
return text
|
|
83
|
+
range_re = re.compile(
|
|
84
|
+
rf"(?<![\d.,:/-])(\d{{1,3}})\s?[-–]\s?(\d{{1,3}})(?![\d.,:/])(?:[\s-]?(?P<ord>inchi|nchi|chi)\b"
|
|
85
|
+
rf"|[\s-]?(?P<suffix>{_SUFFIX_RE}|ta)\b|-(?=[A-Za-z']))?", flags=re.IGNORECASE)
|
|
86
|
+
|
|
87
|
+
def replace_range(match):
|
|
88
|
+
first, second = int(match.group(1)), int(match.group(2))
|
|
89
|
+
if first >= second:
|
|
90
|
+
return match.group(0)
|
|
91
|
+
if match.group("ord") or match.group(0).endswith("-"):
|
|
92
|
+
tail = " " if match.group(0).endswith("-") else ""
|
|
93
|
+
return f"{number_to_ordinal_uz(str(first))}-{number_to_ordinal_uz(str(second))}{tail}"
|
|
94
|
+
return f"{say_number(first)}-{attach_suffix(say_number(second), match.group('suffix') or '')}"
|
|
95
|
+
|
|
96
|
+
text = range_re.sub(replace_range, text)
|
|
97
|
+
text = re.sub(r"(?<=\d)\s?[-–/]\s?(?=\d)", ", ", text)
|
|
98
|
+
text = re.sub(r"(?<=[A-Za-z])-(?=\d)", " ", text)
|
|
99
|
+
return text
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def convert_identifiers(text):
|
|
103
|
+
def replace(match):
|
|
104
|
+
digits = match.group(0)
|
|
105
|
+
if _CURRENCY_AFTER.match(match.string, match.end()) or len(digits) > 12:
|
|
106
|
+
return digits
|
|
107
|
+
groups, rest = [digits[:3]], digits[3:]
|
|
108
|
+
while rest:
|
|
109
|
+
take = 3 if len(rest) == 3 else 2
|
|
110
|
+
groups.append(rest[:take])
|
|
111
|
+
rest = rest[take:]
|
|
112
|
+
spoken = [" ".join(say_number(c) for c in g) if g.startswith("0") else say_number(g) for g in groups]
|
|
113
|
+
space = " " if match.start() and match.string[match.start() - 1].isalpha() else ""
|
|
114
|
+
return space + ", ".join(spoken)
|
|
115
|
+
|
|
116
|
+
return re.sub(r"(?<![+\d])\d{7,}(?!\d)", replace, text)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
UZBEK_APOSTROPHE_VARIANTS = {
|
|
2
|
+
"`": "'",
|
|
3
|
+
"´": "'",
|
|
4
|
+
"ʻ": "'",
|
|
5
|
+
"ʼ": "'",
|
|
6
|
+
"‘": "'",
|
|
7
|
+
"’": "'",
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
LEGACY_UZBEK_SENTINELS = {
|
|
11
|
+
"Ō": "O'",
|
|
12
|
+
"ō": "o'",
|
|
13
|
+
"Ḡ": "G'",
|
|
14
|
+
"ḡ": "g'",
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def normalize_uzbek_apostrophes(text: str) -> str:
|
|
19
|
+
normalized = text
|
|
20
|
+
for source, replacement in LEGACY_UZBEK_SENTINELS.items():
|
|
21
|
+
normalized = normalized.replace(source, replacement)
|
|
22
|
+
for source, replacement in UZBEK_APOSTROPHE_VARIANTS.items():
|
|
23
|
+
normalized = normalized.replace(source, replacement)
|
|
24
|
+
return normalized
|