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.
Files changed (39) hide show
  1. gapiring/__init__.py +100 -0
  2. gapiring/__main__.py +3 -0
  3. gapiring/cli.py +41 -0
  4. gapiring/engine.py +199 -0
  5. gapiring/frontend/__init__.py +0 -0
  6. gapiring/frontend/common.py +16 -0
  7. gapiring/frontend/lexicon_extra.tsv +6 -0
  8. gapiring/frontend/tashkent_dialect.py +222 -0
  9. gapiring/frontend/terms.py +58 -0
  10. gapiring/frontend/terms_reviewed.json +4618 -0
  11. gapiring/frontend/terms_uz.py +71 -0
  12. gapiring/frontend/text_to_phoneme/__init__.py +0 -0
  13. gapiring/frontend/text_to_phoneme/phoneme_converter.py +329 -0
  14. gapiring/frontend/text_to_phoneme/pipelene_text_to_phoneme.py +55 -0
  15. gapiring/frontend/text_to_phoneme/text_normalization_for_tts.py +208 -0
  16. gapiring/frontend/text_to_phoneme/tools/__init__.py +0 -0
  17. gapiring/frontend/text_to_phoneme/tools/convert_currency_to_text.py +76 -0
  18. gapiring/frontend/text_to_phoneme/tools/convert_date_time_to_text.py +371 -0
  19. gapiring/frontend/text_to_phoneme/tools/convert_non_roman_numbers_to_text.py +609 -0
  20. gapiring/frontend/text_to_phoneme/tools/convert_phone_numbers_to_text.py +268 -0
  21. gapiring/frontend/text_to_phoneme/tools/convert_roman_numerals_to_text.py +235 -0
  22. gapiring/frontend/text_to_phoneme/tools/convert_symbols_to_uzbek_text.py +258 -0
  23. gapiring/frontend/text_to_phoneme/tools/expand_uzbek_abbreviations.py +293 -0
  24. gapiring/frontend/text_to_phoneme/tools/full_normalize.py +257 -0
  25. gapiring/frontend/text_to_phoneme/tools/pronounce_links_and_emails.py +99 -0
  26. gapiring/frontend/text_to_phoneme/tools/split_text_numbers.py +36 -0
  27. gapiring/frontend/text_to_phoneme/tools/spoken_lexicon.py +40 -0
  28. gapiring/frontend/text_to_phoneme/tools/spoken_prepass.py +116 -0
  29. gapiring/frontend/text_to_phoneme/uzbek_orthography.py +24 -0
  30. gapiring/frontend/textnorm.py +172 -0
  31. gapiring/frontend/tts_frontend.py +344 -0
  32. gapiring/lexicon.py +188 -0
  33. gapiring/text_front.py +77 -0
  34. gapiring-0.1.0.dist-info/METADATA +69 -0
  35. gapiring-0.1.0.dist-info/RECORD +39 -0
  36. gapiring-0.1.0.dist-info/WHEEL +5 -0
  37. gapiring-0.1.0.dist-info/entry_points.txt +2 -0
  38. gapiring-0.1.0.dist-info/licenses/LICENSE +4 -0
  39. gapiring-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,609 @@
1
+ import re
2
+ import math
3
+
4
+
5
+ def _number_to_words_uz_small(n):
6
+ if n < 0 or n > 999: return str(n)
7
+ units = ["", "bir", "ikki", "uch", "to'rt", "besh", "olti", "yetti", "sakkiz", "to'qqiz"]
8
+ tens = ["", "o'n", "yigirma", "o'ttiz", "qirq", "ellik", "oltmish", "yetmish", "sakson", "to'qson"]
9
+ if n == 0: return ""
10
+ parts = []
11
+ if n >= 100:
12
+ hundreds_digit = n // 100
13
+ if hundreds_digit == 1:
14
+ parts.append("yuz")
15
+ else:
16
+ parts.extend([units[hundreds_digit], "yuz"])
17
+ n %= 100
18
+ if n >= 10:
19
+ parts.append(tens[n // 10])
20
+ n %= 10
21
+ if n > 0: parts.append(units[n])
22
+ return " ".join(filter(None, parts))
23
+
24
+
25
+ def number_to_words_uz(n_orig_param):
26
+ if not isinstance(n_orig_param, (int, float, str)): return str(n_orig_param)
27
+
28
+ original_input_as_str = str(n_orig_param)
29
+ input_is_string = isinstance(n_orig_param, str)
30
+
31
+ string_cleaned_for_numeric_parsing = ""
32
+ if input_is_string:
33
+ temp_str = n_orig_param.strip()
34
+ if not temp_str: return str(n_orig_param)
35
+
36
+ _sign = ''
37
+ if temp_str.startswith('-'):
38
+ _sign = '-'
39
+ temp_str = temp_str[1:]
40
+
41
+ if not temp_str and _sign: return str(n_orig_param)
42
+ if not temp_str and not _sign: return str(n_orig_param)
43
+
44
+ if temp_str.count('.') > 1:
45
+ if all(not sub.isdigit() for sub in temp_str.split('.')):
46
+ return str(n_orig_param)
47
+ string_cleaned_for_numeric_parsing = f"{_sign}{temp_str.replace(',', '').replace(' ', '').replace('.', '', temp_str.count('.') - 1)}"
48
+ elif '.' in temp_str:
49
+ _int_part, _frac_part = temp_str.split('.', 1)
50
+ _int_part_clean = _int_part.replace(',', '').replace(' ', '') if _int_part else "0"
51
+ _frac_part_clean = "".join(filter(str.isdigit, _frac_part))
52
+ string_cleaned_for_numeric_parsing = f"{_sign}{_int_part_clean}.{_frac_part_clean}"
53
+ else:
54
+ _num_part_clean = temp_str.replace(',', '').replace(' ', '')
55
+ if not _num_part_clean:
56
+ return str(n_orig_param)
57
+ string_cleaned_for_numeric_parsing = f"{_sign}{_num_part_clean}"
58
+ else:
59
+ string_cleaned_for_numeric_parsing = original_input_as_str
60
+
61
+ if string_cleaned_for_numeric_parsing == "-" or not string_cleaned_for_numeric_parsing:
62
+ return str(n_orig_param)
63
+
64
+ is_structurally_float = isinstance(n_orig_param, float) or \
65
+ (input_is_string and string_cleaned_for_numeric_parsing.count('.') == 1 and \
66
+ string_cleaned_for_numeric_parsing.replace('-', '').replace('.', '') != "")
67
+
68
+ _is_negative_from_float_path = False
69
+ _n_val_for_int_logic_from_float = None
70
+
71
+ if is_structurally_float:
72
+ try:
73
+ parsed_num_str = string_cleaned_for_numeric_parsing
74
+ if parsed_num_str.startswith('.'):
75
+ parsed_num_str = "0" + parsed_num_str
76
+ elif parsed_num_str.startswith('-.'):
77
+ parsed_num_str = "-0" + parsed_num_str[1:]
78
+
79
+ if parsed_num_str in ["-", ".", "-."]: return str(n_orig_param)
80
+
81
+ num_val_as_float = float(parsed_num_str)
82
+
83
+ is_negative = num_val_as_float < 0
84
+ if not is_negative and original_input_as_str.strip().startswith('-') and num_val_as_float == 0.0:
85
+ is_negative = True
86
+ _is_negative_from_float_path = is_negative
87
+
88
+ if num_val_as_float.is_integer():
89
+ _n_val_for_int_logic_from_float = int(num_val_as_float)
90
+ else:
91
+ prefix = "minus " if is_negative else ""
92
+
93
+ magnitude_str_to_split = parsed_num_str
94
+ if magnitude_str_to_split.startswith('-'):
95
+ magnitude_str_to_split = magnitude_str_to_split[1:]
96
+
97
+ if not magnitude_str_to_split or magnitude_str_to_split == ".": return str(n_orig_param)
98
+
99
+ parts_of_magnitude = magnitude_str_to_split.split('.', 1)
100
+ integer_part_str = parts_of_magnitude[0]
101
+ fractional_part_str_for_logic = parts_of_magnitude[1] if len(parts_of_magnitude) > 1 else ""
102
+
103
+ if fractional_part_str_for_logic and not fractional_part_str_for_logic.isdigit():
104
+ return str(n_orig_param)
105
+
106
+ integer_words = number_to_words_uz(integer_part_str if integer_part_str else "0")
107
+ if integer_words == (integer_part_str if integer_part_str else "0") and \
108
+ any(c.isdigit() for c in integer_words):
109
+ return str(n_orig_param)
110
+
111
+ fractional_words = ""
112
+ if fractional_part_str_for_logic:
113
+ if fractional_part_str_for_logic.startswith('0'):
114
+ fractional_words_list = []
115
+ for digit_char in fractional_part_str_for_logic:
116
+ digit_word = number_to_words_uz(digit_char)
117
+ if digit_word == digit_char and any(c.isdigit() for c in digit_word): return str(
118
+ n_orig_param)
119
+ fractional_words_list.append(digit_word)
120
+ fractional_words = " ".join(filter(None, fractional_words_list))
121
+ else:
122
+ if fractional_part_str_for_logic:
123
+ frac_conv_test = number_to_words_uz(fractional_part_str_for_logic)
124
+ if frac_conv_test == fractional_part_str_for_logic and any(
125
+ c.isdigit() for c in frac_conv_test):
126
+ return str(n_orig_param)
127
+ fractional_words = frac_conv_test
128
+ else:
129
+ fractional_words = ""
130
+
131
+ result_parts = []
132
+ if prefix: result_parts.append(prefix.strip())
133
+ result_parts.append(integer_words)
134
+
135
+ if fractional_part_str_for_logic:
136
+ result_parts.append("butun")
137
+ if fractional_words:
138
+ result_parts.append(fractional_words)
139
+
140
+ final_str = " ".join(filter(None, result_parts)).strip()
141
+
142
+ if num_val_as_float == 0.0 and integer_words == "nol" and not fractional_part_str_for_logic:
143
+ return prefix + "nol"
144
+
145
+ if not final_str or (prefix and final_str == prefix.strip()):
146
+ if num_val_as_float == 0.0: return prefix + "nol"
147
+ return str(n_orig_param)
148
+ return final_str
149
+ except ValueError:
150
+ return str(n_orig_param)
151
+
152
+ n_to_convert_int = 0
153
+ is_negative_for_int_path = False
154
+
155
+ if _n_val_for_int_logic_from_float is not None:
156
+ is_negative_for_int_path = _is_negative_from_float_path
157
+ n_to_convert_int = abs(_n_val_for_int_logic_from_float)
158
+ elif input_is_string:
159
+ num_str_for_int = string_cleaned_for_numeric_parsing
160
+ if num_str_for_int.startswith('-'):
161
+ is_negative_for_int_path = True
162
+ num_str_for_int = num_str_for_int[1:]
163
+
164
+ if not num_str_for_int.isdigit():
165
+ return str(n_orig_param)
166
+
167
+ if not num_str_for_int: return str(n_orig_param)
168
+
169
+ n_to_convert_int = int(num_str_for_int)
170
+ if n_to_convert_int == 0 and original_input_as_str.strip() == "-0" and not is_negative_for_int_path:
171
+ is_negative_for_int_path = True
172
+
173
+ elif isinstance(n_orig_param, int):
174
+ val_to_process_int = n_orig_param
175
+ if val_to_process_int < 0:
176
+ is_negative_for_int_path = True
177
+ n_to_convert_int = abs(val_to_process_int)
178
+ else:
179
+ n_to_convert_int = val_to_process_int
180
+ else:
181
+ return str(n_orig_param)
182
+
183
+ if n_to_convert_int == 0:
184
+ return "minus nol" if is_negative_for_int_path else "nol"
185
+
186
+ prefix_int = "minus " if is_negative_for_int_path else ""
187
+
188
+ scales_exact_map = {100: "yuz", 1000: "ming", 10 ** 6: "million", 10 ** 9: "milliard", 10 ** 12: "trillion"}
189
+ if n_to_convert_int in scales_exact_map and n_to_convert_int >= 1000:
190
+ return (prefix_int + scales_exact_map[n_to_convert_int]).strip()
191
+
192
+ parts_int = []
193
+ scales_int_factors = [(10 ** 12, "trillion"), (10 ** 9, "milliard"), (10 ** 6, "million"), (1000, "ming")]
194
+ current_n_int = n_to_convert_int
195
+ for scale_value, scale_name in scales_int_factors:
196
+ if current_n_int >= scale_value:
197
+ count = current_n_int // scale_value
198
+ count_words = number_to_words_uz(str(count))
199
+ if count_words == str(count) and any(c.isdigit() for c in count_words): return str(n_orig_param)
200
+
201
+ parts_int.append(count_words)
202
+ parts_int.append(scale_name)
203
+ current_n_int %= scale_value
204
+ if current_n_int == 0: break
205
+
206
+ if current_n_int > 0:
207
+ small_part_words_int = _number_to_words_uz_small(current_n_int)
208
+ if small_part_words_int:
209
+ parts_int.append(small_part_words_int)
210
+ elif current_n_int != 0:
211
+ return str(n_orig_param)
212
+
213
+ if not parts_int and n_to_convert_int != 0:
214
+ return str(n_orig_param)
215
+
216
+ return (prefix_int + " ".join(filter(None, parts_int))).strip()
217
+
218
+
219
+ def _get_ordinal_suffix_uz(word_for_suffix):
220
+ if not word_for_suffix: return ""
221
+ last_char = word_for_suffix[-1].lower()
222
+ vowels = "aeiouoʻ"
223
+ if last_char == "'" and len(word_for_suffix) > 1:
224
+ last_char = word_for_suffix[-2].lower()
225
+ return "nchi" if last_char in vowels else "inchi"
226
+
227
+
228
+ def number_to_ordinal_uz(n_orig_param, append_suffix_directly=True):
229
+ if isinstance(n_orig_param, str):
230
+ cleaned_str = n_orig_param.strip().replace(',', '').replace(' ', '')
231
+ if '.' in cleaned_str: return str(n_orig_param)
232
+ sign = ""
233
+ if cleaned_str.startswith('-'):
234
+ sign = "-"
235
+ cleaned_str = cleaned_str[1:]
236
+ if not cleaned_str.isdigit(): return str(n_orig_param)
237
+ if not cleaned_str:
238
+ return str(n_orig_param)
239
+ num_for_logic_str = sign + cleaned_str
240
+ elif isinstance(n_orig_param, float):
241
+ if not n_orig_param.is_integer(): return str(n_orig_param)
242
+ num_for_logic_str = str(int(n_orig_param))
243
+ elif isinstance(n_orig_param, int):
244
+ num_for_logic_str = str(n_orig_param)
245
+ else:
246
+ return str(n_orig_param)
247
+
248
+ is_negative_ord = num_for_logic_str.startswith('-')
249
+ abs_num_str_ord = num_for_logic_str[1:] if is_negative_ord else num_for_logic_str
250
+
251
+ if not abs_num_str_ord.isdigit(): return str(n_orig_param)
252
+ n_val_logic_ord = int(abs_num_str_ord)
253
+
254
+ if n_val_logic_ord == 0 and not is_negative_ord:
255
+ base_word = "nol"
256
+ return base_word + "inchi" if append_suffix_directly else base_word
257
+ elif n_val_logic_ord == 0 and is_negative_ord:
258
+ base_word = "minus nol"
259
+ return base_word + "inchi" if append_suffix_directly else base_word
260
+
261
+ cardinal_word_full = number_to_words_uz(num_for_logic_str)
262
+
263
+ if cardinal_word_full == num_for_logic_str and any(c.isdigit() for c in cardinal_word_full):
264
+ return str(n_orig_param)
265
+
266
+ prefix_ord = ""
267
+ cardinal_word_for_suffix = cardinal_word_full
268
+ if cardinal_word_full.startswith("minus "):
269
+ prefix_ord = "minus "
270
+ cardinal_word_for_suffix = cardinal_word_full[len(prefix_ord):]
271
+
272
+ if not cardinal_word_for_suffix or (n_val_logic_ord != 0 and cardinal_word_for_suffix == "nol"):
273
+ return str(n_orig_param)
274
+ if not append_suffix_directly: return cardinal_word_full
275
+ if not cardinal_word_for_suffix.strip(): return str(n_orig_param)
276
+
277
+ last_word_component = cardinal_word_for_suffix.split()[-1]
278
+ suffix = _get_ordinal_suffix_uz(last_word_component)
279
+ return prefix_ord + cardinal_word_for_suffix + suffix
280
+
281
+
282
+ def _convert_version_part(part_str):
283
+ original_part_str_for_revert = part_str
284
+
285
+ if not part_str.isdigit():
286
+ res = number_to_words_uz(part_str)
287
+ if res == part_str and any(c.isdigit() for c in res):
288
+ return original_part_str_for_revert
289
+ return res
290
+
291
+ if not part_str:
292
+ return ""
293
+
294
+ num_leading_zeros = 0
295
+ for char_val in part_str:
296
+ if char_val == '0':
297
+ num_leading_zeros += 1
298
+ else:
299
+ break
300
+
301
+ leading_zeros_word_tokens = []
302
+ if num_leading_zeros > 0:
303
+ zeros_str_part = part_str[:num_leading_zeros]
304
+ for zero_char_digit in zeros_str_part:
305
+ word_for_zero = number_to_words_uz(zero_char_digit)
306
+ if word_for_zero == zero_char_digit and any(c.isdigit() for c in word_for_zero):
307
+ return original_part_str_for_revert
308
+ leading_zeros_word_tokens.append(word_for_zero)
309
+
310
+ remaining_digits_str = part_str[num_leading_zeros:]
311
+ final_word_tokens = list(leading_zeros_word_tokens)
312
+
313
+ if not remaining_digits_str:
314
+ return " ".join(filter(None, final_word_tokens))
315
+
316
+ if num_leading_zeros > 0:
317
+ remaining_part_as_words = number_to_words_uz(remaining_digits_str)
318
+ if remaining_part_as_words == remaining_digits_str and any(c.isdigit() for c in remaining_part_as_words):
319
+ return original_part_str_for_revert
320
+ if remaining_part_as_words:
321
+ final_word_tokens.append(remaining_part_as_words)
322
+ else:
323
+ if len(remaining_digits_str) >= 3:
324
+ digit_by_digit_words_temp = []
325
+ for digit_char in remaining_digits_str:
326
+ word_for_digit = number_to_words_uz(digit_char)
327
+ if word_for_digit == digit_char and any(c.isdigit() for c in word_for_digit):
328
+ return original_part_str_for_revert
329
+ digit_by_digit_words_temp.append(word_for_digit)
330
+ final_word_tokens.extend(digit_by_digit_words_temp)
331
+ else:
332
+ std_conversion = number_to_words_uz(remaining_digits_str)
333
+ if std_conversion == remaining_digits_str and any(c.isdigit() for c in std_conversion):
334
+ return original_part_str_for_revert
335
+ final_word_tokens.append(std_conversion)
336
+
337
+ return " ".join(filter(None, final_word_tokens))
338
+
339
+
340
+ def convert_non_roman_numbers_to_text(text_input):
341
+ text = str(text_input)
342
+ _num_re_core = r'\d{1,3}(?:[,\s]\d{3})*(?:\.\d+)?|\d+(?:\.\d+)?'
343
+ _num_re_int_core = r'\d{1,3}(?:[,\s]\d{3})*|\d+'
344
+
345
+ percent_pattern = re.compile(rf'(%)\s*(-?)({_num_re_core})|(-?)({_num_re_core})\s*(%)')
346
+
347
+ def replace_percent(match):
348
+ sign, digits_str = (match.group(2), match.group(3)) if match.group(1) == '%' else \
349
+ (match.group(4), match.group(5))
350
+ num_val_str = (sign or '') + (digits_str or '')
351
+ try:
352
+ num_words = number_to_words_uz(num_val_str)
353
+ if num_words == num_val_str and any(c.isdigit() for c in num_words):
354
+ return match.group(0)
355
+ return f"{num_words} foiz"
356
+ except Exception:
357
+ return match.group(0)
358
+
359
+ text = percent_pattern.sub(replace_percent, text)
360
+
361
+ number_with_suffix_pattern = re.compile(rf'(-?)({_num_re_int_core})-([dg][ae]|ga|da|dan)(?=\b|[\s\.,!?;:])',
362
+ flags=re.IGNORECASE)
363
+ processed_parts_ns = []
364
+ last_idx_ns = 0
365
+ current_text_for_ns_processing = text
366
+ for match_ns in re.finditer(number_with_suffix_pattern, current_text_for_ns_processing):
367
+ sign, digits, suffix_val_orig = match_ns.group(1), match_ns.group(2), match_ns.group(3).lower()
368
+ start_ns, end_ns = match_ns.span()
369
+ processed_parts_ns.append(current_text_for_ns_processing[last_idx_ns:start_ns])
370
+ num_str_ns = (sign or '') + digits
371
+ suffix_val_ns = suffix_val_orig
372
+ try:
373
+ cardinal_word_full_ns = number_to_words_uz(num_str_ns)
374
+ if cardinal_word_full_ns == num_str_ns and any(c.isdigit() for c in cardinal_word_full_ns):
375
+ processed_parts_ns.append(match_ns.group(0))
376
+ else:
377
+ prefix_ns = ""
378
+ cardinal_word_ns = cardinal_word_full_ns
379
+ if cardinal_word_full_ns.startswith("minus "):
380
+ prefix_ns = "minus "
381
+ cardinal_word_ns = cardinal_word_full_ns[len(prefix_ns):]
382
+ words_ns = cardinal_word_ns.split()
383
+ if words_ns and words_ns[-1]:
384
+ last_word_ns = words_ns[-1]
385
+ jarangsiz_ns = "fkpsshxchtq"
386
+ last_char_for_harmony_ns = last_word_ns[-1]
387
+ if last_char_for_harmony_ns == "'" and len(last_word_ns) > 1:
388
+ last_char_for_harmony_ns = last_word_ns[-2]
389
+
390
+ if suffix_val_ns == "ga":
391
+ if last_char_for_harmony_ns in "kq": suffix_val_ns = "qa"
392
+ words_ns[-1] += suffix_val_ns
393
+ processed_parts_ns.append(prefix_ns + " ".join(words_ns))
394
+ else:
395
+ processed_parts_ns.append(match_ns.group(0))
396
+ except Exception:
397
+ processed_parts_ns.append(match_ns.group(0))
398
+ last_idx_ns = end_ns
399
+ processed_parts_ns.append(current_text_for_ns_processing[last_idx_ns:])
400
+ text = "".join(processed_parts_ns)
401
+
402
+ _version_str_pattern = re.compile(r'\b(\d+(?:\.\d+){2,})\b')
403
+
404
+ processed_parts_vs = []
405
+ last_idx_vs = 0
406
+ current_text_for_vs_processing = text
407
+ for match_vs in _version_str_pattern.finditer(current_text_for_vs_processing):
408
+ start_vs, end_vs = match_vs.span()
409
+ processed_parts_vs.append(current_text_for_vs_processing[last_idx_vs:start_vs])
410
+ version_full_match = match_vs.group(0)
411
+ parts = version_full_match.split('.')
412
+ try:
413
+ word_parts = [_convert_version_part(p) for p in parts]
414
+
415
+ conversion_failed_for_any_part = False
416
+ for i, p_original in enumerate(parts):
417
+ if word_parts[i] == p_original and p_original.isdigit():
418
+ conversion_failed_for_any_part = True
419
+ break
420
+
421
+ if conversion_failed_for_any_part:
422
+ processed_parts_vs.append(version_full_match)
423
+ else:
424
+ processed_parts_vs.append(".".join(word_parts))
425
+ except Exception:
426
+ processed_parts_vs.append(version_full_match)
427
+ last_idx_vs = end_vs
428
+ processed_parts_vs.append(current_text_for_vs_processing[last_idx_vs:])
429
+ text = "".join(processed_parts_vs)
430
+
431
+ ordinal_hyphen_pattern = re.compile(rf'(-?)({_num_re_int_core})-')
432
+ processed_parts_oh = []
433
+ last_idx_oh = 0
434
+ current_text_for_oh_processing = text
435
+ for match_obj_oh in re.finditer(ordinal_hyphen_pattern, current_text_for_oh_processing):
436
+ sign_oh, digits_oh = match_obj_oh.group(1), match_obj_oh.group(2)
437
+ start_oh, end_oh = match_obj_oh.span()
438
+
439
+ processed_parts_oh.append(current_text_for_oh_processing[last_idx_oh:start_oh])
440
+ num_str_oh = (sign_oh or '') + digits_oh
441
+ try:
442
+ ordinal_word_oh = number_to_ordinal_uz(num_str_oh, append_suffix_directly=True)
443
+ if ordinal_word_oh == num_str_oh and any(c.isdigit() for c in ordinal_word_oh):
444
+ processed_parts_oh.append(match_obj_oh.group(0))
445
+ else:
446
+ processed_parts_oh.append(ordinal_word_oh)
447
+ if end_oh < len(current_text_for_oh_processing) and \
448
+ not current_text_for_oh_processing[end_oh].isspace() and \
449
+ current_text_for_oh_processing[end_oh].isalnum() and \
450
+ not ordinal_word_oh.endswith(" "):
451
+ processed_parts_oh.append(" ")
452
+ except Exception:
453
+ processed_parts_oh.append(match_obj_oh.group(0))
454
+ last_idx_oh = end_oh
455
+ processed_parts_oh.append(current_text_for_oh_processing[last_idx_oh:])
456
+ text = "".join(processed_parts_oh)
457
+
458
+ cardinal_numbers_pattern = re.compile(rf'\b(-?(?:\d{{1,3}}(?:[,\s]\d{{3}})*|\d+)(?:\.\d+)?)\b')
459
+
460
+ new_text_parts_cs = []
461
+ last_pos_cs = 0
462
+ current_text_for_cs_processing = text
463
+ for match_cs in cardinal_numbers_pattern.finditer(current_text_for_cs_processing):
464
+ num_str_cs = match_cs.group(0)
465
+ start_cs, end_cs = match_cs.span()
466
+ new_text_parts_cs.append(current_text_for_cs_processing[last_pos_cs:start_cs])
467
+
468
+ should_convert_cs = True
469
+ temp_num_str_for_check = num_str_cs[1:] if num_str_cs.startswith('-') else num_str_cs
470
+ int_part_for_len_check = temp_num_str_for_check.split('.', 1)[
471
+ 0] if '.' in temp_num_str_for_check else temp_num_str_for_check
472
+ cleaned_int_part_for_len_check = int_part_for_len_check.replace(',', '').replace(' ', '')
473
+
474
+ if '.' not in num_str_cs and len(
475
+ cleaned_int_part_for_len_check) > 12 and cleaned_int_part_for_len_check.isdigit():
476
+ should_convert_cs = False
477
+
478
+ if should_convert_cs:
479
+ try:
480
+ converted_words_cs = number_to_words_uz(num_str_cs)
481
+ if converted_words_cs == num_str_cs and any(c.isdigit() for c in converted_words_cs):
482
+ new_text_parts_cs.append(num_str_cs)
483
+ else:
484
+ new_text_parts_cs.append(converted_words_cs)
485
+ except Exception:
486
+ new_text_parts_cs.append(num_str_cs)
487
+ else:
488
+ new_text_parts_cs.append(num_str_cs)
489
+ last_pos_cs = end_cs
490
+ new_text_parts_cs.append(current_text_for_cs_processing[last_pos_cs:])
491
+ text = "".join(new_text_parts_cs)
492
+
493
+ text = re.sub(r'\(-([a-zA-Z\'o‘ʻ]+)\b\)', r'(minus \1)', text, flags=re.IGNORECASE)
494
+ text = re.sub(r'^-([a-zA-Z\'o‘ʻ]+)\b', r'minus \1', text, flags=re.IGNORECASE)
495
+ text = re.sub(r'(?<=\s)-([a-zA-Z\'o‘ʻ]+)\b', r'minus \1', text, flags=re.IGNORECASE)
496
+
497
+ def replace_ta_units(current_text):
498
+ current_text = re.sub(r'\bbir\s+ta\b', 'bitta', current_text, flags=re.IGNORECASE)
499
+ ta_replacements = {
500
+ r'\bikki\s+ta\b': 'ikkita', r'\bolti\s+ta\b': 'oltita', r'\byetti\s+ta\b': 'yettita',
501
+ }
502
+ for p, r_val in ta_replacements.items(): current_text = re.sub(p, r_val, current_text, flags=re.IGNORECASE)
503
+ ta_direct_concat = {
504
+ r'\buch\s+ta\b': 'uchta', r'\bto\'rt\s+ta\b': 'to\'rtta', r'\bto‘rt\s+ta\b': 'to‘rtta',
505
+ r'\bbesh\s+ta\b': 'beshta', r'\bsakkiz\s+ta\b': 'sakkizta',
506
+ r'\bto\'qqiz\s+ta\b': 'to\'qqizta', r'\bto‘qqiz\s+ta\b': 'to‘qqizta',
507
+ }
508
+ for p, r_val in ta_direct_concat.items(): current_text = re.sub(p, r_val, current_text, flags=re.IGNORECASE)
509
+ return current_text
510
+
511
+ text = replace_ta_units(text)
512
+
513
+ text = re.sub(r'(\S+(?:inchi|nchi))\s*(?:inchi|nchi|chi)\b', r'\1', text, flags=re.IGNORECASE)
514
+
515
+ text = re.sub(r'\s+', ' ', text).strip()
516
+ return text
517
+
518
+
519
+ if __name__ == "__main__":
520
+ test_texts_non_roman = [
521
+ "Harorat -4 daraja.", "Balans: -25000 so'm.", "Jami 25,000 so'm.", "Jami 25 000 so'm.",
522
+ "Son -0.", "Raqam 0.", "Summa: -45.67 so'm", "Qiymat: 15,500.75 dollar.",
523
+ "Narx: 15 000.75 yevro.", "Narx: 15 000.00 yevro.", "Narx: 15 000.05 yevro.",
524
+ "Narx: 15 000.50 yevro.", "1.1", "101.24", "-5.3", "0.5", "-.5", "-0.5", "-0.05", "-.05",
525
+ "-0", "-0.0", "Telefon: 998902345678", "ID: 123456789012345", "Kod: -1234567890123",
526
+ "Bu -4test so'zi emas, lekin -4 alohida.", "(-4) va (20-da) test.", "(-4) va (1,200-da) test.",
527
+ "Raqamlar: 1, -2, 3.", "1-dars, 2-topshiriq.", "1,500-dars, 20 000-topshiriq.",
528
+ "10%", "10.5%", "-15.5%", "%12,345.67", "12 345.67 %", "10-sinf",
529
+ "o'n beshinchi asr va 20-asr", "sakkizinchi bobida va 5-band", "uchta olma va 2 ta anor",
530
+ "bir ta olma", " ko'p bo'shliq ", "Bu 1,234,567.890 raqami.",
531
+ "Bu 1 000 000 sondir.", "Bu 1,000,000 sondir.", "1000", "1001", "1,000", "1 000",
532
+ "100.00", "1,000.00", "0.0", "Raqam .5", "Raqam 0.05", "Salbiy -0.5", "Salbiy -.05",
533
+ "Hisobda .00 so'm bor", "Qiymat 1.0 so'm", "Qiymat -test so'z", "(-so'z)",
534
+ "Qiymat 123.005", "Qiymat 123.050", "Qiymat 123.500",
535
+ "1.2.3. Bu versiya raqami.",
536
+ "Versiya 10.2.5 ishlatilmoqda.",
537
+ "IP manzil: 192.168.1.1",
538
+ "Narx 1.2 million so'm, versiya 4.0.1 da.",
539
+ "Versiya 2.007.1",
540
+ "ID 10.0.0.1 host",
541
+ "Test 0.0.0.0",
542
+ "Test 001.002.00100",
543
+ ]
544
+
545
+ print("Rim raqamlarisiz matnni normallashtirish:\n")
546
+ for text_input in test_texts_non_roman:
547
+ normalized_text = convert_non_roman_numbers_to_text(str(text_input))
548
+ print(f"Asl matn: '{text_input}'")
549
+ print(f"Normallashgan matn:'{normalized_text}'\n")
550
+
551
+ print("\n--- Maxsus raqam testlari (number_to_words_uz & _convert_version_part) ---")
552
+ tests = {
553
+ "15,500.75": ("o'n besh ming besh yuz butun yetmish besh", number_to_words_uz),
554
+ "0.05": ("nol butun nol besh", number_to_words_uz),
555
+ "-.05": ("minus nol butun nol besh", number_to_words_uz),
556
+ "-0.0": ("minus nol", number_to_words_uz),
557
+ "1000.00": ("ming", number_to_words_uz),
558
+ ".00": ("nol", number_to_words_uz),
559
+ "0.75": ("nol butun yetmish besh", number_to_words_uz),
560
+ "123.005": ("yuz yigirma uch butun nol nol besh", number_to_words_uz),
561
+ "123.050": ("yuz yigirma uch butun nol besh nol", number_to_words_uz),
562
+ "123.500": ("yuz yigirma uch butun besh yuz", number_to_words_uz),
563
+ "1.2.3": ("bir.ikki.uch", convert_non_roman_numbers_to_text),
564
+ "1": ("bir", number_to_words_uz),
565
+ "0": ("nol", number_to_words_uz),
566
+ "-10": ("minus o'n", number_to_words_uz),
567
+ "100": ("yuz", number_to_words_uz),
568
+ "101": ("yuz bir", number_to_words_uz),
569
+ "200": ("ikki yuz", number_to_words_uz),
570
+ "1000": ("ming", number_to_words_uz),
571
+ "2000": ("ikki ming", number_to_words_uz),
572
+ "1000000": ("million", number_to_words_uz),
573
+ ".5": ("nol butun besh", number_to_words_uz),
574
+ "-.5": ("minus nol butun besh", number_to_words_uz),
575
+ "00": ("nol nol", _convert_version_part),
576
+ "007": ("nol nol yetti", _convert_version_part),
577
+ "00100": ("nol nol yuz", _convert_version_part),
578
+ "05": ("nol besh", _convert_version_part),
579
+ "192": ("bir to'qqiz ikki", _convert_version_part),
580
+ }
581
+ for num_str, (expected, func_to_call) in tests.items():
582
+ actual = func_to_call(num_str)
583
+
584
+ print(f"Test for '{num_str}' using {func_to_call.__name__}:")
585
+ print(f" Expected: '{expected}'")
586
+ print(f" Actual: '{actual}'")
587
+ if actual != expected:
588
+ print(f" MISMATCH!")
589
+ print()
590
+
591
+ print("\n--- Test case for version/IP strings (using convert_non_roman_numbers_to_text) ---")
592
+ version_tests = {
593
+ '1.2.3. Bu versiya raqami.': 'bir.ikki.uch. Bu versiya raqami.',
594
+ 'Versiya 10.2.5 ishlatilmoqda.': "Versiya o'n.ikki.besh ishlatilmoqda.",
595
+ 'IP manzil: 192.168.1.1': "IP manzil: bir to'qqiz ikki.bir olti sakkiz.bir.bir",
596
+ 'Versiya 2.007.1': "Versiya ikki.nol nol yetti.bir",
597
+ 'Test 0.0.0.0': "Test nol.nol.nol.nol",
598
+ 'Test 001.002.00100': "Test nol nol bir.nol nol ikki.nol nol yuz"
599
+ }
600
+ for input_text, expected_output in version_tests.items():
601
+ actual_output = convert_non_roman_numbers_to_text(input_text)
602
+ print(f" Input: '{input_text}'")
603
+ print(f" Expected: '{expected_output}'")
604
+ print(f" Actual: '{actual_output}'")
605
+ if actual_output == expected_output:
606
+ print(f" Result: PASS")
607
+ else:
608
+ print(f" Result: FAIL")
609
+ print()