File size: 8,490 Bytes
3784ae7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import re


DIGITS_MAP = {
    '0': 'صِفر', '1': 'یک', '2': 'دو', '3': 'سه', '4': 'چهار',
    '5': 'پنج', '6': 'شِش', '7': 'هفت', '8': 'هشت', '9': 'نُه'
}

TENS = {
    10: 'دَه', 11: 'یازده', 12: 'دوازده', 13: 'سیزده', 14: 'چهارده',
    15: 'پانزده', 16: 'شانزده', 17: 'هفده', 18: 'هجده', 19: 'نوزده',
    20: 'بیست', 30: 'سی', 40: 'چهل', 50: 'پنجاه',
    60: 'شصت', 70: 'هفتاد', 80: 'هشتاد', 90: 'نود'
}

HUNDREDS = {
    100: 'صَد', 200: 'دویست', 300: 'سیصد', 400: 'چهارصد', 500: 'پانصد',
    600: 'ششصد', 700: 'هفتصد', 800: 'هشتصد', 900: 'نهصد'
}


def _convert_three_digit(num: int) -> str:
    if num == 0:
        return ''

    if num < 10:
        return DIGITS_MAP[str(num)]
    elif num < 20:
        return TENS[num]
    elif num < 100:
        tens_part = (num // 10) * 10
        ones_part = num % 10
        if ones_part == 0:
            return TENS[tens_part]
        return f"{TENS[tens_part]} و {DIGITS_MAP[str(ones_part)]}"
    else:
        hundreds_part = (num // 100) * 100
        rem = num % 100
        if rem == 0:
            return HUNDREDS[hundreds_part]
        return f"{HUNDREDS[hundreds_part]} و {_convert_three_digit(rem)}"


def num_to_text(num: int) -> str:
    if num == 0:
        return 'صِفر'

    if num < 0:
        return f"مَنفی {num_to_text(abs(num))}"

    if num < 1000:
        return _convert_three_digit(num)

    parts = []

    if num >= 1_000_000_000:
        billions = num // 1_000_000_000
        parts.append(f"{_convert_three_digit(billions)} میلیارد")
        num %= 1_000_000_000

    if num >= 1_000_000:
        millions = num // 1_000_000
        parts.append(f"{_convert_three_digit(millions)} میلیون")
        num %= 1_000_000

    if num >= 1000:
        thousands = num // 1000
        parts.append(f"{_convert_three_digit(thousands)} هزار")
        num %= 1000

    if num > 0:
        parts.append(_convert_three_digit(num))

    return ' و '.join(parts)


def _read_phone_chunk(chunk: str) -> str:
    if not chunk:
        return ""

    if all(c == '0' for c in chunk):
        count = len(chunk)
        if count == 2:
            return "دو صِفر"
        elif count == 3:
            return "سِِتا صفر"
        elif count == 4:
            return "چهارتا صفر"
        else:
            return f"{num_to_text(count)} تا صِفر"

    result_parts = []
    temp_chunk = chunk

    while temp_chunk.startswith('0'):
        result_parts.append("صِفر")
        temp_chunk = temp_chunk[1:]

    if temp_chunk:
        val = int(temp_chunk)
        result_parts.append(num_to_text(val))

    return " ".join(result_parts)


def _smart_split_phone(phone_str: str, has_plus: bool = False) -> list:
    length = len(phone_str)
    chunks = []

    if has_plus:
        if phone_str.startswith('98') and len(phone_str) > 5:
            chunks.append("+" + phone_str[:2])
            rest = phone_str[2:]
            if rest.startswith('9'):

                inner_chunks = _smart_split_phone("0" + rest)
                chunks.extend(inner_chunks)
                return chunks
            else:
                chunks.append(rest)
                return chunks

        elif phone_str.startswith('1') and length == 11:
            chunks.append("+" + phone_str[:1])
            chunks.append(phone_str[1:4])
            chunks.append(phone_str[4:7])
            chunks.append(phone_str[7:])
            return chunks

    if phone_str.startswith('09') and length == 11:
        chunks.append(phone_str[:4])
        rest = phone_str[4:]

        part_mid = rest[:3]
        part_end = rest[3:]

        is_end_round = False
        if part_end == '0000':
            is_end_round = True
        elif part_end.endswith('00'):
            is_end_round = True
        elif part_end[1] == '0' and part_end[2] == '0':
            is_end_round = True
        if part_mid == '000':
            is_end_round = True

        if is_end_round:
            chunks.append(part_mid)
            chunks.append(part_end)
        else:
            chunks.append(rest[:3])
            chunks.append(rest[3:5])
            chunks.append(rest[5:])
        return chunks

    if phone_str.startswith('0') and length == 11:
        chunks.append(phone_str[:3])
        rest = phone_str[3:]

        part1 = rest[:4]
        part2 = rest[4:]

        if (part1.endswith('00') and part2.endswith('00')) or (part2 == '0000'):
            chunks.append(part1)
            chunks.append(part2)
            return chunks

        p3_1 = rest[:3]
        p3_2 = rest[3:6]
        if p3_1.endswith('0') and p3_2.endswith('0'):
            chunks.append(p3_1)
            chunks.append(p3_2)
            chunks.append(rest[6:])
            return chunks

        chunks.append(rest[:2])
        chunks.append(rest[2:4])
        chunks.append(rest[4:6])
        chunks.append(rest[6:])
        return chunks

    if not phone_str.startswith('0'):
        if length == 8:
            chunks.append(phone_str[:2])
            chunks.append(phone_str[2:4])
            chunks.append(phone_str[4:6])
            chunks.append(phone_str[6:])
            return chunks
        elif length == 4:
            chunks.append(phone_str)
            return chunks
        elif length == 5:
            chunks.append(phone_str)
            return chunks

    if length == 10 and phone_str.startswith('9'):
        chunks.append(phone_str[:3])
        chunks.append(phone_str[3:6])
        chunks.append(phone_str[6:8])
        chunks.append(phone_str[8:])
        return chunks

    return [phone_str]


def phone_to_text(raw_input: str) -> str:
    clean_input = raw_input.replace(' ', '').replace(
        '-', '').replace('(', '').replace(')', '')

    persian_digits = '۰۱۲۳۴۵۶۷۸۹'
    english_digits = '0123456789'
    trans_table = str.maketrans(persian_digits, english_digits)
    clean_input = clean_input.translate(trans_table)

    has_plus = False
    if clean_input.startswith('+'):
        has_plus = True
        clean_input = clean_input[1:]

    if not clean_input.isdigit():
        return raw_input

    chunks = _smart_split_phone(clean_input, has_plus)

    text_parts = []
    for ch in chunks:
        if ch.startswith('+'):
            val = int(ch[1:])
            text_parts.append(f"مثبت {num_to_text(val)}")
        else:
            text_parts.append(_read_phone_chunk(ch))

    return "، ".join(text_parts)


def _is_likely_phone(num_str: str) -> bool:
    if num_str.startswith('+'):
        return True

    if num_str.startswith('09') and len(num_str) == 11:
        return True

    if num_str.startswith('0') and len(num_str) >= 7:
        return True

    return False


def find_and_normalize_numbers(text: str) -> str:
    text = text.translate(str.maketrans('٠١٢٣٤٥٦٧٨٩', '0123456789'))\
                .translate(str.maketrans('۰۱۲۳۴۵۶۷۸۹', '0123456789'))

    pattern = r'(?:\+|-)?\d+(?:[,\-]\d+)*'

    def replace_match(match):
        original_str = match.group()
        clean_str = original_str.replace(',', '')

        if _is_likely_phone(clean_str):
            return phone_to_text(clean_str)
        else:
            try:
                val = int(clean_str)
                return num_to_text(val)
            except ValueError:
                return original_str

    return re.sub(pattern, replace_match, text)


if __name__ == "__main__":
    examples = [

        "شماره من ۰۹۱۲۳۴۵۶۷۸۹ است",
        "تلفن شرکت ۰۲۱۸۸۰۵۶۰۷۰ می باشد",
        "کد تایید: ۸۸۹۹۱۱۰۰",
        "تماس بین المللی: +۹۸۹۱۵۱۰۰۲۰۳۰",
        "شارژ مستقیم ۰۹۳۵۲۰۰۳۰۴۰",
        "کد پستی ۱۱۱۱۱۰۰۰۰۰",
        "و با تلفن ۰۲۱-۸۸۸۰۳۳۵۴ تماس بگیرید",


        "قیمت این کالا ۵,۴۰۰ تومان است",
        "جمعیت ایران ۸۵۰۰۰۰۰۰ نفر است",
        "دمای هوا منفی ۵ درجه است: -5",
        "تعداد ۱۰۰۱ شب",
        "عدد صفر 0"
    ]

    print("--- بررسی عملکرد کد ادغام شده ---\n")
    for ex in examples:
        converted = find_and_normalize_numbers(ex)
        print(f"Original: {ex}")
        print(f"Converted: {converted}")
        print("-" * 30)