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,268 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
UZBEK_DIGITS = {
|
|
4
|
+
"0": "nol", "1": "bir", "2": "ikki", "3": "uch", "4": "to'rt",
|
|
5
|
+
"5": "besh", "6": "olti", "7": "yetti", "8": "sakkiz", "9": "to'qqiz"
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
UZBEK_TENS = {
|
|
9
|
+
"1": "o'n", "2": "yigirma", "3": "o'ttiz", "4": "qirq",
|
|
10
|
+
"5": "ellik", "6": "oltmish", "7": "yetmish", "8": "sakson", "9": "to'qson"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
UZBEK_HUNDRED = "yuz"
|
|
14
|
+
UZBEK_PLUS = "plyus"
|
|
15
|
+
UZBEK_COUNTRY_CODE = "to'qqiz yuz to'qson sakkiz"
|
|
16
|
+
UZBEK_OPERATOR_CODES = {"20", "33", "50", "55", "61", "62", "65", "66", "67", "69", "70", "71", "72", "73",
|
|
17
|
+
"74", "75", "76", "77", "78", "79", "88", "90", "91", "93", "94", "95", "97", "98", "99"}
|
|
18
|
+
_CURRENCY_AFTER = re.compile(r"\s*(so'm|sum|dollar|yevro|rubl|uzs|usd|eur)\b", re.IGNORECASE)
|
|
19
|
+
|
|
20
|
+
PHONE_NUMBER_PATTERN = re.compile(r"""
|
|
21
|
+
( # Capturing group for the whole phone number match
|
|
22
|
+
\+ \s* 9 \s* 9 \s* 8 (?: [\s\(\)-]* \d ){9} (?!\d)
|
|
23
|
+
|
|
|
24
|
+
(?:
|
|
25
|
+
(?<!\+) (?<!\d)
|
|
26
|
+
\s*
|
|
27
|
+
9 \s* 9 \s* 8 (?: [\s\(\)-]* \d ){9} (?!\d)
|
|
28
|
+
)
|
|
29
|
+
|
|
|
30
|
+
(?:
|
|
31
|
+
(?<!\+) (?<!\d)
|
|
32
|
+
\s*
|
|
33
|
+
(?: [\s\(\)-]* \d ){2}
|
|
34
|
+
(?: [\s\(\)-]* \d ){7}
|
|
35
|
+
(?!\d)
|
|
36
|
+
)
|
|
37
|
+
)
|
|
38
|
+
""", re.VERBOSE | re.UNICODE)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _convert_chunk_to_text(chunk_str):
|
|
42
|
+
if not chunk_str:
|
|
43
|
+
return ""
|
|
44
|
+
|
|
45
|
+
original_len = len(chunk_str)
|
|
46
|
+
|
|
47
|
+
if original_len == 3 and chunk_str == "000":
|
|
48
|
+
return f"{UZBEK_DIGITS['0']} {UZBEK_DIGITS['0']} {UZBEK_DIGITS['0']}"
|
|
49
|
+
|
|
50
|
+
if original_len == 2:
|
|
51
|
+
if chunk_str == "00":
|
|
52
|
+
return f"{UZBEK_DIGITS['0']} {UZBEK_DIGITS['0']}"
|
|
53
|
+
elif chunk_str[0] == '0':
|
|
54
|
+
return f"{UZBEK_DIGITS['0']} {UZBEK_DIGITS[chunk_str[1]]}"
|
|
55
|
+
|
|
56
|
+
if original_len > 1:
|
|
57
|
+
num_str = chunk_str.lstrip('0')
|
|
58
|
+
if not num_str:
|
|
59
|
+
num_str = "0"
|
|
60
|
+
else:
|
|
61
|
+
num_str = chunk_str
|
|
62
|
+
|
|
63
|
+
words = []
|
|
64
|
+
if len(num_str) == 3:
|
|
65
|
+
if num_str[0] != "1":
|
|
66
|
+
words.append(UZBEK_DIGITS[num_str[0]])
|
|
67
|
+
words.append(UZBEK_HUNDRED)
|
|
68
|
+
|
|
69
|
+
tens_digit = num_str[1]
|
|
70
|
+
unit_digit = num_str[2]
|
|
71
|
+
|
|
72
|
+
if tens_digit == '0' and unit_digit == '0':
|
|
73
|
+
pass
|
|
74
|
+
elif tens_digit == '0':
|
|
75
|
+
words.append(UZBEK_DIGITS[unit_digit])
|
|
76
|
+
elif unit_digit == '0':
|
|
77
|
+
words.append(UZBEK_TENS[tens_digit])
|
|
78
|
+
else:
|
|
79
|
+
words.append(UZBEK_TENS[tens_digit])
|
|
80
|
+
words.append(UZBEK_DIGITS[unit_digit])
|
|
81
|
+
|
|
82
|
+
elif len(num_str) == 2:
|
|
83
|
+
tens_digit = num_str[0]
|
|
84
|
+
unit_digit = num_str[1]
|
|
85
|
+
if unit_digit == '0':
|
|
86
|
+
words.append(UZBEK_TENS[tens_digit])
|
|
87
|
+
else:
|
|
88
|
+
words.append(UZBEK_TENS[tens_digit])
|
|
89
|
+
words.append(UZBEK_DIGITS[unit_digit])
|
|
90
|
+
|
|
91
|
+
elif len(num_str) == 1:
|
|
92
|
+
words.append(UZBEK_DIGITS[num_str[0]])
|
|
93
|
+
|
|
94
|
+
return " ".join(filter(None, words))
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def phone_to_text_replacer(match_obj):
|
|
98
|
+
original_match_str = match_obj.group(0)
|
|
99
|
+
|
|
100
|
+
leading_whitespace_match = re.match(r"(\s*)", original_match_str)
|
|
101
|
+
leading_whitespace = leading_whitespace_match.group(1) if leading_whitespace_match else ""
|
|
102
|
+
|
|
103
|
+
core_number_str = original_match_str[len(leading_whitespace):]
|
|
104
|
+
digits = re.sub(r'\D', '', core_number_str)
|
|
105
|
+
group_lengths = [len(group) for group in re.findall(r'\d+', core_number_str)]
|
|
106
|
+
if _CURRENCY_AFTER.match(match_obj.string, match_obj.end()):
|
|
107
|
+
return original_match_str
|
|
108
|
+
|
|
109
|
+
final_text_segments = []
|
|
110
|
+
|
|
111
|
+
has_plus = core_number_str.strip().startswith('+')
|
|
112
|
+
current_digits_for_grouping = ""
|
|
113
|
+
valid_format_found = False
|
|
114
|
+
|
|
115
|
+
cc_and_prefix_parts = []
|
|
116
|
+
if has_plus and digits.startswith("998") and len(digits) == 12:
|
|
117
|
+
cc_and_prefix_parts.append(UZBEK_PLUS)
|
|
118
|
+
cc_and_prefix_parts.append(UZBEK_COUNTRY_CODE)
|
|
119
|
+
current_digits_for_grouping = digits[3:]
|
|
120
|
+
valid_format_found = True
|
|
121
|
+
elif not has_plus and digits.startswith("998") and len(digits) == 12:
|
|
122
|
+
cc_and_prefix_parts.append(UZBEK_COUNTRY_CODE)
|
|
123
|
+
current_digits_for_grouping = digits[3:]
|
|
124
|
+
valid_format_found = True
|
|
125
|
+
elif not has_plus and len(digits) == 9 and (
|
|
126
|
+
group_lengths == [2, 3, 2, 2] or
|
|
127
|
+
(group_lengths in ([9], [2, 7], [2, 3, 4]) and digits[:2] in UZBEK_OPERATOR_CODES)):
|
|
128
|
+
current_digits_for_grouping = digits
|
|
129
|
+
valid_format_found = True
|
|
130
|
+
else:
|
|
131
|
+
return original_match_str
|
|
132
|
+
|
|
133
|
+
if not valid_format_found or len(current_digits_for_grouping) != 9:
|
|
134
|
+
return original_match_str
|
|
135
|
+
|
|
136
|
+
if cc_and_prefix_parts:
|
|
137
|
+
final_text_segments.append(" ".join(cc_and_prefix_parts))
|
|
138
|
+
|
|
139
|
+
op_text = _convert_chunk_to_text(current_digits_for_grouping[0:2])
|
|
140
|
+
if op_text: final_text_segments.append(op_text)
|
|
141
|
+
|
|
142
|
+
sub1_text = _convert_chunk_to_text(current_digits_for_grouping[2:5])
|
|
143
|
+
if sub1_text: final_text_segments.append(sub1_text)
|
|
144
|
+
|
|
145
|
+
sub2_text = _convert_chunk_to_text(current_digits_for_grouping[5:7])
|
|
146
|
+
if sub2_text: final_text_segments.append(sub2_text)
|
|
147
|
+
|
|
148
|
+
sub3_text = _convert_chunk_to_text(current_digits_for_grouping[7:9])
|
|
149
|
+
if sub3_text: final_text_segments.append(sub3_text)
|
|
150
|
+
|
|
151
|
+
converted_phone_text = ", ".join(s for s in final_text_segments if s)
|
|
152
|
+
|
|
153
|
+
return leading_whitespace + converted_phone_text
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def convert_phone_numbers_to_text(text):
|
|
157
|
+
return PHONE_NUMBER_PATTERN.sub(phone_to_text_replacer, text)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
test_cases = [
|
|
161
|
+
{
|
|
162
|
+
"id": 1,
|
|
163
|
+
"input": "Qoʻngʻiroq qiling: +998901234567.",
|
|
164
|
+
"expected": "Qoʻngʻiroq qiling: plyus to'qqiz yuz to'qson sakkiz, to'qson, yuz yigirma uch, qirq besh, oltmish yetti."
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
"id": 2,
|
|
168
|
+
"input": "Telefon raqami (97) 777-66-55.",
|
|
169
|
+
"expected": "Telefon raqami to'qson yetti, yetti yuz yetmish yetti, oltmish olti, ellik besh."
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"id": 3,
|
|
173
|
+
"input": "Mening raqamim 998998504312.",
|
|
174
|
+
"expected": "Mening raqamim to'qqiz yuz to'qson sakkiz, to'qson to'qqiz, sakkiz yuz ellik, qirq uch, o'n ikki."
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
"id": 4,
|
|
178
|
+
"input": "Raqam: 331234567.",
|
|
179
|
+
"expected": "Raqam: o'ttiz uch, yuz yigirma uch, qirq besh, oltmish yetti."
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
"id": 5,
|
|
183
|
+
"input": "Aloqa uchun: +998 (71) 200-00-00.",
|
|
184
|
+
"expected": "Aloqa uchun: plyus to'qqiz yuz to'qson sakkiz, yetmish bir, ikki yuz, nol nol, nol nol."
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
"id": 6,
|
|
188
|
+
"input": "Bu yerda raqam yo'q 12345.",
|
|
189
|
+
"expected": "Bu yerda raqam yo'q 12345."
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
"id": 7,
|
|
193
|
+
"input": "+998 90 123 45 67 xalqaro raqam.",
|
|
194
|
+
"expected": "plyus to'qqiz yuz to'qson sakkiz, to'qson, yuz yigirma uch, qirq besh, oltmish yetti xalqaro raqam."
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
"id": 8,
|
|
198
|
+
"input": "Mahalliy: (55) 501-02-03.",
|
|
199
|
+
"expected": "Mahalliy: ellik besh, besh yuz bir, nol ikki, nol uch."
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
"id": 9,
|
|
203
|
+
"input": "MTS: 97-700-00-00",
|
|
204
|
+
"expected": "MTS: to'qson yetti, yetti yuz, nol nol, nol nol"
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
"id": 10,
|
|
208
|
+
"input": "Yana bir: 998504312.",
|
|
209
|
+
"expected": "Yana bir: to'qson to'qqiz, sakkiz yuz ellik, qirq uch, o'n ikki."
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
"id": 11,
|
|
213
|
+
"input": "Telefon: +998(20)0000000",
|
|
214
|
+
"expected": "Telefon: plyus to'qqiz yuz to'qson sakkiz, yigirma, nol nol nol, nol nol, nol nol"
|
|
215
|
+
},
|
|
216
|
+
{
|
|
217
|
+
"id": 12,
|
|
218
|
+
"input": "Noma'lum +123456789012",
|
|
219
|
+
"expected": "Noma'lum +123456789012"
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
"id": 13,
|
|
223
|
+
"input": "Tekshiruv 911234567 va 998931234567",
|
|
224
|
+
"expected": "Tekshiruv to'qson bir, yuz yigirma uch, qirq besh, oltmish yetti va to'qqiz yuz to'qson sakkiz, to'qson uch, yuz yigirma uch, qirq besh, oltmish yetti"
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
"id": 14,
|
|
228
|
+
"input": "Raqamlar: 998712000000, (90)1234567.",
|
|
229
|
+
"expected": "Raqamlar: to'qqiz yuz to'qson sakkiz, yetmish bir, ikki yuz, nol nol, nol nol, to'qson, yuz yigirma uch, qirq besh, oltmish yetti."
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
"id": 15,
|
|
233
|
+
"input": "(90)1234567 is a number.",
|
|
234
|
+
"expected": "to'qson, yuz yigirma uch, qirq besh, oltmish yetti is a number."
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
"id": 16,
|
|
238
|
+
"input": "Call +998901234567 now.",
|
|
239
|
+
"expected": "Call plyus to'qqiz yuz to'qson sakkiz, to'qson, yuz yigirma uch, qirq besh, oltmish yetti now."
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
"id": 17,
|
|
243
|
+
"input": "Raqam: +998000000000.",
|
|
244
|
+
"expected": "Raqam: plyus to'qqiz yuz to'qson sakkiz, nol nol, nol nol nol, nol nol, nol nol."
|
|
245
|
+
}
|
|
246
|
+
]
|
|
247
|
+
|
|
248
|
+
if __name__ == "__main__":
|
|
249
|
+
print("Running tests:")
|
|
250
|
+
passed_all = True
|
|
251
|
+
for test in test_cases:
|
|
252
|
+
print("-" * 30)
|
|
253
|
+
print(f"Test {test['id']}:")
|
|
254
|
+
print(f" Input: '{test['input']}'")
|
|
255
|
+
actual_output = convert_phone_numbers_to_text(test['input'])
|
|
256
|
+
print(f" Actual: '{actual_output}'")
|
|
257
|
+
print(f" Expected: '{test['expected']}'")
|
|
258
|
+
if actual_output == test['expected']:
|
|
259
|
+
print(" Result: PASSED")
|
|
260
|
+
else:
|
|
261
|
+
print(" Result: FAILED")
|
|
262
|
+
passed_all = False
|
|
263
|
+
|
|
264
|
+
print("-" * 30)
|
|
265
|
+
if passed_all:
|
|
266
|
+
print("All tests passed successfully!")
|
|
267
|
+
else:
|
|
268
|
+
print("Some tests FAILED.")
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
ASR_KEYWORDS = {
|
|
4
|
+
"asr", "asri", "asrim", "asring", "asrimiz", "asringiz", "asrlari",
|
|
5
|
+
"asrga", "asrda", "asrdan", "asrning", "asrlarga", "asrlarda",
|
|
6
|
+
"asrlardan", "asrlarning", "asrlarimiz", "asrlaringiz",
|
|
7
|
+
"asrgacha", "asrdayoq", "asrlab", "asrdagi"
|
|
8
|
+
}
|
|
9
|
+
ORDINAL_KEYWORDS_GENERAL_BASES = {
|
|
10
|
+
"bob", "modda", "band", "qism", "sinf", "guruh", "bosqich",
|
|
11
|
+
"daraja", "sahifa", "bet", "jild", "nashr", "versiya",
|
|
12
|
+
"yil", "qirol",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _number_to_words_uz_small(n):
|
|
17
|
+
if n < 0 or n > 999: return str(n)
|
|
18
|
+
units = ["", "bir", "ikki", "uch", "to'rt", "besh", "olti", "yetti", "sakkiz", "to'qqiz"]
|
|
19
|
+
tens = ["", "o'n", "yigirma", "o'ttiz", "qirq", "ellik", "oltmish", "yetmish", "sakson", "to'qson"]
|
|
20
|
+
if n == 0: return "nol"
|
|
21
|
+
parts = []
|
|
22
|
+
if n >= 100:
|
|
23
|
+
hundreds_digit = n // 100
|
|
24
|
+
if hundreds_digit == 1:
|
|
25
|
+
parts.append("yuz")
|
|
26
|
+
else:
|
|
27
|
+
parts.extend([units[hundreds_digit], "yuz"])
|
|
28
|
+
n %= 100
|
|
29
|
+
if n >= 10:
|
|
30
|
+
parts.append(tens[n // 10])
|
|
31
|
+
n %= 10
|
|
32
|
+
if n > 0: parts.append(units[n])
|
|
33
|
+
return " ".join(filter(None, parts))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def number_to_words_uz_for_roman(n_int_positive):
|
|
37
|
+
if not isinstance(n_int_positive, int) or n_int_positive < 0:
|
|
38
|
+
return str(n_int_positive)
|
|
39
|
+
|
|
40
|
+
if n_int_positive == 0: return "nol"
|
|
41
|
+
|
|
42
|
+
parts_int = []
|
|
43
|
+
scales_int = [(10 ** 12, "trillion"), (10 ** 9, "milliard"), (10 ** 6, "million"), (1000, "ming")]
|
|
44
|
+
special_cases_int = {val: name for val, name in scales_int}
|
|
45
|
+
|
|
46
|
+
if n_int_positive in special_cases_int and n_int_positive >= 1000:
|
|
47
|
+
return special_cases_int[n_int_positive]
|
|
48
|
+
|
|
49
|
+
current_n_int = n_int_positive
|
|
50
|
+
for scale_value, scale_name in scales_int:
|
|
51
|
+
if current_n_int >= scale_value:
|
|
52
|
+
count = current_n_int // scale_value
|
|
53
|
+
word_for_count = number_to_words_uz_for_roman(count)
|
|
54
|
+
parts_int.append(word_for_count)
|
|
55
|
+
parts_int.append(scale_name)
|
|
56
|
+
current_n_int %= scale_value
|
|
57
|
+
if current_n_int == 0: break
|
|
58
|
+
if current_n_int > 0:
|
|
59
|
+
small_part_words_int = _number_to_words_uz_small(current_n_int)
|
|
60
|
+
if small_part_words_int: parts_int.append(small_part_words_int)
|
|
61
|
+
|
|
62
|
+
return " ".join(filter(None, parts_int)).strip()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _get_ordinal_suffix_uz(word_for_suffix):
|
|
66
|
+
if not word_for_suffix: return ""
|
|
67
|
+
vowels = "aeiou"
|
|
68
|
+
return "nchi" if word_for_suffix[-1].lower() in vowels else "inchi"
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def number_to_ordinal_uz_for_roman(n_int_positive, append_suffix_directly=True):
|
|
72
|
+
if not isinstance(n_int_positive, int) or n_int_positive < 0:
|
|
73
|
+
return str(n_int_positive)
|
|
74
|
+
if n_int_positive == 0:
|
|
75
|
+
base_word = "nol"
|
|
76
|
+
return base_word + "inchi" if append_suffix_directly else base_word
|
|
77
|
+
|
|
78
|
+
cardinal_word_full = number_to_words_uz_for_roman(n_int_positive)
|
|
79
|
+
if not cardinal_word_full or (n_int_positive != 0 and cardinal_word_full == "nol"):
|
|
80
|
+
return str(n_int_positive)
|
|
81
|
+
if not append_suffix_directly: return cardinal_word_full
|
|
82
|
+
if not cardinal_word_full.strip(): return str(n_int_positive)
|
|
83
|
+
|
|
84
|
+
last_word_component = cardinal_word_full.split()[-1]
|
|
85
|
+
suffix = _get_ordinal_suffix_uz(last_word_component)
|
|
86
|
+
return cardinal_word_full + suffix
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def roman_to_int(s_upper):
|
|
90
|
+
roman_map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
|
|
91
|
+
int_val, prev_val = 0, 0
|
|
92
|
+
for char_s in reversed(s_upper):
|
|
93
|
+
val = roman_map.get(char_s)
|
|
94
|
+
if not val: return None
|
|
95
|
+
int_val += val if val >= prev_val else -val
|
|
96
|
+
prev_val = val
|
|
97
|
+
return int_val
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _has_ordinal_context(following_text):
|
|
101
|
+
word_match = re.match(r"([^\s\W\d_']+)", following_text)
|
|
102
|
+
if not word_match:
|
|
103
|
+
return False
|
|
104
|
+
word = word_match.group(1).lower()
|
|
105
|
+
if word in ASR_KEYWORDS or re.fullmatch(r'(inchi|nchi|chi)', word):
|
|
106
|
+
return True
|
|
107
|
+
return any(word.startswith(base) for base in ORDINAL_KEYWORDS_GENERAL_BASES)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def convert_roman_numerals_to_text(text_input):
|
|
111
|
+
text = str(text_input)
|
|
112
|
+
|
|
113
|
+
roman_hyphen_inner_pattern = r'(M{0,3}(?:CM|CD|D?C{0,3})(?:XC|XL|L?X{0,3})(?:IX|IV|V?I{0,3}))'
|
|
114
|
+
roman_ordinal_hyphen_pattern = re.compile(r"(?<![\w'])(?=[MDCLXVI])" + roman_hyphen_inner_pattern + r'-')
|
|
115
|
+
|
|
116
|
+
processed_parts_rh = []
|
|
117
|
+
last_idx_rh = 0
|
|
118
|
+
for match_rh in re.finditer(roman_ordinal_hyphen_pattern, text):
|
|
119
|
+
roman_part = match_rh.group(1)
|
|
120
|
+
start_rh, hyphen_end_rh = match_rh.span()
|
|
121
|
+
|
|
122
|
+
processed_parts_rh.append(text[last_idx_rh:start_rh])
|
|
123
|
+
num_val = roman_to_int(roman_part.upper())
|
|
124
|
+
if len(roman_part) == 1 and not _has_ordinal_context(text[hyphen_end_rh:]):
|
|
125
|
+
num_val = None
|
|
126
|
+
|
|
127
|
+
if num_val is not None and num_val > 0:
|
|
128
|
+
try:
|
|
129
|
+
ordinal_word = number_to_ordinal_uz_for_roman(num_val, append_suffix_directly=True)
|
|
130
|
+
processed_parts_rh.append(ordinal_word)
|
|
131
|
+
|
|
132
|
+
current_pos_after_hyphen = hyphen_end_rh
|
|
133
|
+
suffix_matched_len = 0
|
|
134
|
+
suffix_pattern = r'(inchi|nchi|chi)(?![a-zA-Z])'
|
|
135
|
+
suffix_match = re.match(suffix_pattern, text[current_pos_after_hyphen:], re.IGNORECASE)
|
|
136
|
+
if suffix_match:
|
|
137
|
+
suffix_matched_len = len(suffix_match.group(1))
|
|
138
|
+
|
|
139
|
+
final_idx_for_this_construct = hyphen_end_rh + suffix_matched_len
|
|
140
|
+
|
|
141
|
+
if final_idx_for_this_construct < len(text) and \
|
|
142
|
+
text[final_idx_for_this_construct].isalnum() and \
|
|
143
|
+
not text[final_idx_for_this_construct].isspace():
|
|
144
|
+
processed_parts_rh.append(" ")
|
|
145
|
+
|
|
146
|
+
last_idx_rh = final_idx_for_this_construct
|
|
147
|
+
|
|
148
|
+
except Exception:
|
|
149
|
+
processed_parts_rh.append(match_rh.group(0))
|
|
150
|
+
last_idx_rh = hyphen_end_rh
|
|
151
|
+
else:
|
|
152
|
+
processed_parts_rh.append(match_rh.group(0))
|
|
153
|
+
last_idx_rh = hyphen_end_rh
|
|
154
|
+
processed_parts_rh.append(text[last_idx_rh:])
|
|
155
|
+
text = "".join(processed_parts_rh)
|
|
156
|
+
|
|
157
|
+
roman_pattern_core_UPPERCASE = r'M{0,3}(?:CM|CD|D?C{0,3})(?:XC|XL|L?X{0,3})(?:IX|IV|V?I{0,3})'
|
|
158
|
+
general_roman_pattern = re.compile(r"(?<![a-zA-Z\d'-])(" + roman_pattern_core_UPPERCASE + r")(?![a-zA-Z\d'])")
|
|
159
|
+
|
|
160
|
+
def replace_general_roman(match):
|
|
161
|
+
roman_str = match.group(1)
|
|
162
|
+
original_match_text = match.group(0)
|
|
163
|
+
if not roman_str: return original_match_text
|
|
164
|
+
|
|
165
|
+
num_val = roman_to_int(roman_str.upper())
|
|
166
|
+
if num_val is None or num_val <= 0: return original_match_text
|
|
167
|
+
|
|
168
|
+
next_context_match = re.match(r'\s*([^\s\W\d_]+)', match.string[match.end():])
|
|
169
|
+
next_word_lower = ""
|
|
170
|
+
if next_context_match:
|
|
171
|
+
next_word_lower = next_context_match.group(1).lower()
|
|
172
|
+
|
|
173
|
+
is_asr_context = bool(next_word_lower and next_word_lower in ASR_KEYWORDS)
|
|
174
|
+
is_general_ordinal_context = False
|
|
175
|
+
if not is_asr_context and next_word_lower:
|
|
176
|
+
for base_keyword in ORDINAL_KEYWORDS_GENERAL_BASES:
|
|
177
|
+
if next_word_lower.startswith(base_keyword):
|
|
178
|
+
is_general_ordinal_context = True
|
|
179
|
+
break
|
|
180
|
+
|
|
181
|
+
process_as_roman = False
|
|
182
|
+
if is_asr_context or is_general_ordinal_context:
|
|
183
|
+
process_as_roman = True
|
|
184
|
+
else:
|
|
185
|
+
if len(roman_str) >= 2:
|
|
186
|
+
process_as_roman = True
|
|
187
|
+
|
|
188
|
+
if process_as_roman:
|
|
189
|
+
try:
|
|
190
|
+
return number_to_ordinal_uz_for_roman(num_val, append_suffix_directly=True)
|
|
191
|
+
except Exception:
|
|
192
|
+
return original_match_text
|
|
193
|
+
else:
|
|
194
|
+
return original_match_text
|
|
195
|
+
|
|
196
|
+
text = general_roman_pattern.sub(replace_general_roman, text)
|
|
197
|
+
|
|
198
|
+
text = re.sub(r'\s+', ' ', text).strip()
|
|
199
|
+
return text
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
if __name__ == "__main__":
|
|
203
|
+
test_texts_roman = [
|
|
204
|
+
"I asr.",
|
|
205
|
+
"XV asrda yashagan.",
|
|
206
|
+
"I-asr.",
|
|
207
|
+
"M.",
|
|
208
|
+
"X va M korxonalar.",
|
|
209
|
+
"Bu MMXXIV yil.",
|
|
210
|
+
"Bu X yil.",
|
|
211
|
+
"Muzeyga V.I. Lenin nomi berilgan.",
|
|
212
|
+
"Muzeyga V I Lenin nomi berilgan.",
|
|
213
|
+
"VI asrning boshlarida",
|
|
214
|
+
"IV-asrda",
|
|
215
|
+
"XI asr",
|
|
216
|
+
"O'zbekiston Respublikasining Konstitutsiyasi VIII bobida",
|
|
217
|
+
"Chapter IX of the book.",
|
|
218
|
+
"IV qism, II band.",
|
|
219
|
+
"Bu VII versiya.",
|
|
220
|
+
"Mening I guruhim.",
|
|
221
|
+
"5-IV sinflar",
|
|
222
|
+
"Test III-modda",
|
|
223
|
+
"Raqamlar: 1, -2, 3. XIV.",
|
|
224
|
+
"Bu IV-chi bob.",
|
|
225
|
+
"Lyudovik XIV qirol boʻlgan.",
|
|
226
|
+
"Karl V qirol.",
|
|
227
|
+
"XV-nchi qoida",
|
|
228
|
+
"XV-nchingizni oling",
|
|
229
|
+
]
|
|
230
|
+
|
|
231
|
+
print("Faqat Rim raqamlarini normallashtirish (yangilangan logika):\n")
|
|
232
|
+
for text_input in test_texts_roman:
|
|
233
|
+
normalized_text = convert_roman_numerals_to_text(str(text_input))
|
|
234
|
+
print(f"Asl matn: '{text_input}'")
|
|
235
|
+
print(f"Normallashgan matn:'{normalized_text}'\n")
|