multilingual-num2text 0.1.0__tar.gz → 0.2.0__tar.gz
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.
- multilingual_num2text-0.2.0/PKG-INFO +318 -0
- multilingual_num2text-0.2.0/README.md +294 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/pyproject.toml +1 -1
- multilingual_num2text-0.2.0/src/multilingual_num2text/__init__.py +99 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/core/__init__.py +7 -1
- multilingual_num2text-0.2.0/src/multilingual_num2text/core/detection.py +249 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/core/language.py +10 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/core/lexicon.py +8 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/core/number_rules.py +42 -8
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/core/registry.py +5 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/core/text_rules.py +1 -1
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/bangla/language.py +1 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/bangla/lexicon.py +8 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/bangla/rules.py +44 -6
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/bangla/vocabulary.py +11 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/english/language.py +1 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/english/lexicon.py +12 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/english/rules.py +2 -1
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/english/vocabulary.py +4 -0
- multilingual_num2text-0.2.0/src/multilingual_num2text.egg-info/PKG-INFO +318 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text.egg-info/SOURCES.txt +2 -0
- multilingual_num2text-0.2.0/tests/test_auto_language.py +433 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/tests/test_combinations.py +1 -1
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/tests/test_edges.py +59 -6
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/tests/test_multilingual_num2text.py +48 -4
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/tests/test_word_tables.py +28 -2
- multilingual_num2text-0.1.0/PKG-INFO +0 -197
- multilingual_num2text-0.1.0/README.md +0 -173
- multilingual_num2text-0.1.0/src/multilingual_num2text/__init__.py +0 -57
- multilingual_num2text-0.1.0/src/multilingual_num2text.egg-info/PKG-INFO +0 -197
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/LICENSE +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/setup.cfg +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/core/currencies.py +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/core/digits.py +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/core/patterns.py +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/core/words.py +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/__init__.py +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/bangla/__init__.py +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/bangla/currencies.py +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/english/__init__.py +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text/languages/english/currencies.py +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text.egg-info/dependency_links.txt +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/src/multilingual_num2text.egg-info/top_level.txt +0 -0
- {multilingual_num2text-0.1.0 → multilingual_num2text-0.2.0}/tests/test_currencies.py +0 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: multilingual-num2text
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Convert numbers in text to words: amounts, money, phone numbers, dates, times, codes and more. Bangla and English built in.
|
|
5
|
+
Author: Kawshik Kumar Paul
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/kawshikbuet17/multilingual_num2text
|
|
8
|
+
Project-URL: Issues, https://github.com/kawshikbuet17/multilingual_num2text/issues
|
|
9
|
+
Keywords: number to words,num2words,bangla,bengali,text normalization,multilingual
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Natural Language :: Bengali
|
|
18
|
+
Classifier: Natural Language :: English
|
|
19
|
+
Classifier: Topic :: Text Processing :: Linguistic
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# multilingual_num2text
|
|
26
|
+
|
|
27
|
+
Turns every number in a piece of text into words, the way a person would say it.
|
|
28
|
+
|
|
29
|
+
It works out what a number is from how it is written: `$1,250.50` becomes money with cents,
|
|
30
|
+
`01711-123456` a phone number read digit by digit, `25/12/2024` a date, `15:45` a time, `*123#` a code.
|
|
31
|
+
The same in Bangla: `৳১,২৫০.৫০` becomes money with paisa, `০১৭১১-১২৩৪৫৬` a phone number, `২৫/১২/২০২৪` a date, `১৫:৪৫` a time.
|
|
32
|
+
Amounts, percentages, 153 currencies, years, ordinals, ranges, fractions and units are covered.
|
|
33
|
+
Use it wherever numbers must appear as words.
|
|
34
|
+
|
|
35
|
+
English (`en`) and Bangla/Bengali (`bn`) are built in, and new languages can be added.
|
|
36
|
+
No dependencies. Python 3.10+.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install multilingual-num2text
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Latest from GitHub: `pip install git+https://github.com/kawshikbuet17/multilingual_num2text.git`
|
|
45
|
+
|
|
46
|
+
## Use
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from multilingual_num2text import convert_text, number_to_text
|
|
50
|
+
|
|
51
|
+
# One number
|
|
52
|
+
number_to_text(2712, lang="en") # two thousand seven hundred twelve
|
|
53
|
+
number_to_text(2712, lang="en", style="digitwise") # two seven one two
|
|
54
|
+
number_to_text("$1,250.50", lang="en", style="auto") # one thousand two hundred fifty dollars and fifty cents
|
|
55
|
+
number_to_text(2712, lang="bn") # দুই হাজার সাতশত বারো
|
|
56
|
+
number_to_text(2712, lang="bn", style="digitwise") # দুই সাত এক দুই
|
|
57
|
+
number_to_text("৳১,২৫০.৫০", lang="bn", style="auto") # এক হাজার দুইশত পঞ্চাশ টাকা পঞ্চাশ পয়সা
|
|
58
|
+
|
|
59
|
+
# Every number inside a text
|
|
60
|
+
convert_text("Send $1,250.50 on May 10, 2024", lang="en")
|
|
61
|
+
# Send one thousand two hundred fifty dollars and fifty cents on May tenth, twenty twenty-four
|
|
62
|
+
convert_text("৳১,২৫০.৫০ পাঠান ০১৭১১-১২৩৪৫৬ নম্বরে", lang="bn")
|
|
63
|
+
# এক হাজার দুইশত পঞ্চাশ টাকা পঞ্চাশ পয়সা পাঠান শূন্য এক সাত এক এক এক দুই তিন চার পাঁচ ছয় নম্বরে
|
|
64
|
+
|
|
65
|
+
# Language detected per sentence
|
|
66
|
+
convert_text("Pay $5 now. ৫০০ টাকা দিন।", lang="auto") # Pay five dollars now. পাঁচশত টাকা দিন।
|
|
67
|
+
convert_text("Hello. مرحبا 500. ৫০০ টাকা।", lang="auto") # Hello. مرحبا 500. পাঁচশত টাকা।
|
|
68
|
+
number_to_text("২৭১২", lang="auto") # দুই হাজার সাতশত বারো
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Positional arguments also work: `number_to_text(2712, "en", "digitwise")`.
|
|
72
|
+
|
|
73
|
+
## Parameters
|
|
74
|
+
|
|
75
|
+
### `convert_text(text, lang, keep=None, splitter=None)` → `str`
|
|
76
|
+
|
|
77
|
+
Converts every number inside a text.
|
|
78
|
+
|
|
79
|
+
| Parameter | Type | Meaning |
|
|
80
|
+
|---|---|---|
|
|
81
|
+
| `text` | `str` | The text. `""` and `None` are returned unchanged. |
|
|
82
|
+
| `lang` | `str` | `"en"`, `"bn"`, any code added with `register_language`, or `"auto"` (see below). Not case-sensitive. An unknown code raises `ValueError`. |
|
|
83
|
+
| `keep` | `re.Pattern`, optional | Parts to leave exactly as they are, such as tags like `[warm]`. The pattern needs one capturing group around the whole match: `re.compile(r"(\[[a-z]+\])")`. With `"auto"`, these parts also do not count when detecting the language. |
|
|
84
|
+
| `splitter` | function, optional | Only for `"auto"`: your own function that takes the text and returns its sentences. Default: `split_sentences`. |
|
|
85
|
+
|
|
86
|
+
### `number_to_text(value, lang, style="cardinal")` → `str`
|
|
87
|
+
|
|
88
|
+
Converts one number.
|
|
89
|
+
|
|
90
|
+
| Parameter | Type | Meaning |
|
|
91
|
+
|---|---|---|
|
|
92
|
+
| `value` | `int`, `float` or `str` | `2712`, `2.5`, `"১,২৫০.৫০"`, `"-15"`. With `style="auto"` also `"৳500"`, `"*123#"`, `"01711-123456"`. |
|
|
93
|
+
| `lang` | `str` | As in `convert_text`. With `"auto"`, a number in digits of an unsupported language is returned unchanged. |
|
|
94
|
+
| `style` | `str` or `NumberStyle` | `"cardinal"` (default): the value as one number. `"digitwise"`: digit by digit. `"auto"`: decided by the shape, like `convert_text` (leading `0` or `+` → digit by digit, leading `*` or `#` → a code, currency → money). |
|
|
95
|
+
|
|
96
|
+
Raises `ValueError` when `value` is not a number (in `cardinal` style) or `style` is unknown.
|
|
97
|
+
|
|
98
|
+
### `detect_language(text, default="en", ignore=None)` → `str` or `None`
|
|
99
|
+
|
|
100
|
+
The language `lang="auto"` would use for a text.
|
|
101
|
+
|
|
102
|
+
| Parameter | Type | Meaning |
|
|
103
|
+
|---|---|---|
|
|
104
|
+
| `text` | `str` | The text to look at. |
|
|
105
|
+
| `default` | `str` | Returned when the text has no words or digits. ASCII digits also count as this language. |
|
|
106
|
+
| `ignore` | `re.Pattern`, optional | Parts that do not count, like `keep` in `convert_text`. |
|
|
107
|
+
|
|
108
|
+
Returns the language code, or `None` when the text is written in a language that is not registered.
|
|
109
|
+
|
|
110
|
+
### Languages and sentences
|
|
111
|
+
|
|
112
|
+
| Call | Parameters |
|
|
113
|
+
|---|---|
|
|
114
|
+
| `EnglishLanguage(indian_scales=False, lexicon=None)` | `indian_scales=True` reads `2,50,000` as two lakh fifty thousand. `lexicon` replaces the built-in data (see below). |
|
|
115
|
+
| `BanglaLanguage(lexicon=None)` | `lexicon` replaces the built-in data. |
|
|
116
|
+
| `register_language(language, code=None)` | Makes a language usable by its code. `code` defaults to `language.code`; registering an existing code replaces it. |
|
|
117
|
+
| `unregister_language(code)` | Removes a language and returns it, or `None` if it was not registered. |
|
|
118
|
+
| `get_language(code)` | The registered language object, with `convert_text(text, keep=None)` and `number_to_text(value, style="cardinal")`. |
|
|
119
|
+
| `available_languages()` | The registered codes, sorted. |
|
|
120
|
+
| `split_sentences(text, ignore=None)` | The sentences `lang="auto"` works with. |
|
|
121
|
+
| `detect_sentence_languages(sentences, default="en", ignore=None)` | The language of each sentence, as `lang="auto"` uses it (sentences without words take their context). |
|
|
122
|
+
|
|
123
|
+
### `lang="auto"`
|
|
124
|
+
|
|
125
|
+
The text is split into sentences, and each sentence is converted in the language it is written in.
|
|
126
|
+
Every number in a sentence follows the sentence.
|
|
127
|
+
|
|
128
|
+
**Which language a sentence is in:**
|
|
129
|
+
|
|
130
|
+
| Rule | Example |
|
|
131
|
+
|---|---|
|
|
132
|
+
| The language with the most words wins | `আপনার account এ ৫০০ টাকা পাঠানো হয়েছে` → bn, `Cash out charge ১.৮৫%` → en |
|
|
133
|
+
| Acronyms (`OTP`, `ATM`), emails, web addresses, abbreviations (`Mr.`, `ডা.`) and `keep=` tags do not count | `OTP কোড 123456` → bn, `যোগাযোগ: support@example.com অথবা ০১৭১১-১২৩৪৫৬` → bn |
|
|
134
|
+
| Equal words: the language whose own digits appear wins, then the one whose word comes first | `আপনার balance ৳৫০০` → bn, `Send Money ফি ৫ টাকা` → bn, `আপনার balance 500` → bn |
|
|
135
|
+
| No words: the digits decide, ASCII digits count as English, most digits win, a tie goes to the first digit | `১` → এক, `1` → one, `5০০০` → পাঁচ হাজার, `50০০` → five thousand, `৫০00` → পাঁচ হাজার |
|
|
136
|
+
| A sentence without words inside a longer text takes the language of the sentences around it | `মোট পরিমাণ:⏎৫০০⏎300` → মোট পরিমাণ: পাঁচশত তিনশত |
|
|
137
|
+
| A sentence in a language that is not supported is kept exactly as it is | `مرحبا 500` → مرحبا 500, `Hello. مرحبا 500. ৫০০ টাকা।` → Hello. مرحبا 500. পাঁচশত টাকা। |
|
|
138
|
+
|
|
139
|
+
**Where a sentence ends:**
|
|
140
|
+
|
|
141
|
+
| Rule | Example |
|
|
142
|
+
|---|---|
|
|
143
|
+
| A line break | `Total:⏎৫০০` → two sentences |
|
|
144
|
+
| An end mark (`।` `!` `?` and `.`) followed by a space or the end | `Pay $5 now. ৫০০ টাকা দিন।`, `Thanks! ৫০০ টাকা জমা হয়েছে` |
|
|
145
|
+
| `.` only when the next sentence starts with a capital or another script | `First. Second` splits; `e.g. this one`, `1.5 kg` do not |
|
|
146
|
+
| Never after an abbreviation | `Mr. Karim`, `Dr. Rahman`, `e.g.`, `etc.`, `ডা. রহিম`, `মো. করিম` |
|
|
147
|
+
| Never after a currency symbol before a number | `Tk. ৫০০ মাত্র` → পাঁচশত টাকা মাত্র, `BDT. 500` → five hundred taka |
|
|
148
|
+
| Never after a bare list number | `১. Open app`, `১। প্রথম ধাপ` |
|
|
149
|
+
| Never without a space after the mark | `Wow!Really`, `example.com/?page=2`, `ধন্যবাদ।আপনার` |
|
|
150
|
+
|
|
151
|
+
End marks and abbreviations come from each language's `Lexicon` (`sentence_end_marks`, `abbreviations`) and can be extended:
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
register_language(EnglishLanguage(lexicon=ENGLISH_LEXICON.extended(abbreviations=("Engr.",), sentence_end_marks=(";",))), code="en")
|
|
155
|
+
convert_text("Engr. Karim paid $5; ৫০০ টাকা দিন", lang="auto") # Engr. Karim paid five dollars; পাঁচশত টাকা দিন
|
|
156
|
+
convert_text(text, lang="auto", splitter=lambda text: text.split("|")) # or split sentences yourself
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
`detect_language(text)` returns the code alone, or `None` when unsupported.
|
|
160
|
+
|
|
161
|
+
ASCII, Bangla or mixed digits all work (`50,000` = `৫০,০০০` = `৫০,000`).
|
|
162
|
+
|
|
163
|
+
## What gets converted
|
|
164
|
+
|
|
165
|
+
| Case | English (`en`) | Bangla/Bengali (`bn`) |
|
|
166
|
+
|---|---|---|
|
|
167
|
+
| Number | `1,000,000` → one million | `২,৫০,০০০` → দুই লক্ষ পঞ্চাশ হাজার |
|
|
168
|
+
| Decimal | `0.5` → zero point five | `১.৪৯` → এক দশমিক চার নয় |
|
|
169
|
+
| Negative | `-5` → minus five | `-৫` → ঋণাত্মক পাঁচ |
|
|
170
|
+
| Percent | `90%` → ninety percent | `১.৪৯%` → এক দশমিক চার নয় শতাংশ |
|
|
171
|
+
| Money (sign before) | `$1.50` → one dollar and fifty cents | `৳১,২৫০.৫০` → এক হাজার দুইশত পঞ্চাশ টাকা পঞ্চাশ পয়সা |
|
|
172
|
+
| Money (unit after) | `500 taka` → five hundred taka | `১০.৫০ টাকার` → দশ টাকা পঞ্চাশ পয়সার, `৫০০/-` → পাঁচশত টাকা |
|
|
173
|
+
| Any currency (153 ISO codes) | `KWD 1.250` → one Kuwaiti dinar and two hundred fifty fils | `SAR ৫০.২৫` → পঞ্চাশ সৌদি রিয়াল পঁচিশ হালালা |
|
|
174
|
+
| Money with scale | `$5 million` → five million dollars | `৳২.৫ কোটি` → আড়াই কোটি টাকা |
|
|
175
|
+
| Currency symbol with a dot | `BDT. 500` → five hundred taka, `Rs. 500` → five hundred rupees | `Tk.৫০০` → পাঁচশত টাকা |
|
|
176
|
+
| Code after OTP, PIN, code, ID, reference (4+ digits, up to two words after) | `Your OTP is 482913` → Your OTP is four eight two nine one three, `PIN: 1234` → PIN: one two three four | `OTP কোড 123456` → ওটিপি কোড এক দুই তিন চার পাঁচ ছয়, `আপনার ওটিপি ৪৮২৯১৩` → … চার আট দুই নয় এক তিন |
|
|
177
|
+
| Starts with `0` or `+` | `+880 1711 123456` → plus eight eight zero … | `০১৭১১-১২৩৪৫৬` → শূন্য এক সাত এক এক … |
|
|
178
|
+
| Starts with `*` or `#` | `*123#` → star one two three hash | `*১২৩#` → স্টার এক দুই তিন হ্যাশ |
|
|
179
|
+
| Long number (10+ digits), card | `account 123456789012` → account one two three … | `১২৩৪ ৫৬৭৮ ৯০১২ ৩৪৫৬` → এক দুই তিন চার … |
|
|
180
|
+
| Date | `25/12/2024`, `12/25/2024` → the twenty-fifth of December, twenty twenty-four; `May 10` → May tenth | `২৫/১২/২০২৪`, `২৫ ডিসেম্বর ২০২৪` → পঁচিশে ডিসেম্বর দুই হাজার চব্বিশ |
|
|
181
|
+
| Date where day and month could swap | `2/5/2024` → two five twenty twenty-four, `2/3/24` → two three twenty-four | `২/৫/২০২৪` → দুই পাঁচ দুই হাজার চব্বিশ, `২/৩/২৪` → দুই তিন চব্বিশ |
|
|
182
|
+
| Month name from the other language | `৫ মে ২০২৪` → the fifth of May, twenty twenty-four | `10 May 2024` → দশই মে দুই হাজার চব্বিশ |
|
|
183
|
+
| Day form | — | `১লা বৈশাখ` → পহেলা বৈশাখ, `২১শে` → একুশে |
|
|
184
|
+
| Year | `in 1971` → in nineteen seventy-one, `1990s` → nineteen nineties | `১৯৭১ সালে` → উনিশশো একাত্তর সালে, `২০২৪-২৫` → দুই হাজার চব্বিশ পঁচিশ |
|
|
185
|
+
| Time | `10:05` → ten oh five, `15:45` → three forty-five PM, `10.30 AM` → ten thirty AM | `১০:৩০` → দশটা ত্রিশ মিনিট, `১৫:৪৫` → দুপুর তিনটা পঁয়তাল্লিশ মিনিট, `১০ AM` → সকাল দশটা |
|
|
186
|
+
| Time with টা / টায় / টার or a period word (`.` or `:`) | — | `সকাল ১০.৩০ টায়` → সকাল দশটা ত্রিশ মিনিটে, `১০:০০ টায়` → দশটায়, `বিকেল ৪.১৫ টার` → বিকেল চারটা পনেরো মিনিটের, `রাত ২২:৩০` → রাত দশটা ত্রিশ মিনিট |
|
|
187
|
+
| Ratio | `1:2` → one to two | `১:২` → এক অনুপাত দুই |
|
|
188
|
+
| Ordinal | `21st` → twenty-first | `১ম` → প্রথম, `২য়` → দ্বিতীয়, `১১তম` → এগারোতম |
|
|
189
|
+
| Range | `5-10%` → five to ten percent | `৫০০-১০০০ টাকা` → পাঁচশত থেকে এক হাজার টাকা |
|
|
190
|
+
| Slash | `12/3` → twelve slash three, `6/12/24 months` → six, twelve, twenty-four months, `৬/১২/২৪ মাস` → six, twelve, twenty-four মাস | `১২/৩` → বারো বাই তিন, `৬/১২/২৪ মাস` → ছয়, বারো, চব্বিশ মাস, `6/12/24 months` → ছয়, বারো, চব্বিশ months |
|
|
191
|
+
| Fraction | `2½` → two and a half | `২.৫ লাখ` → আড়াই লাখ, `১.৫ ঘণ্টা` → দেড় ঘণ্টা, `২½` → আড়াই |
|
|
192
|
+
| Unit, shorthand | `20km` → twenty kilometers, `2.5K` → two thousand five hundred | `২০GB` → বিশ জিবি, `৩৫°C` → পঁয়ত্রিশ ডিগ্রি সেলসিয়াস, `৫K` → পাঁচ হাজার |
|
|
193
|
+
| Arithmetic | `2+2=4` → two plus two equals four | `২+২=৪` → দুই যোগ দুই সমান চার, `৫০০+` → পাঁচশত প্লাস |
|
|
194
|
+
| Version, IP | `1.2.3` → one dot two dot three | `১.২.৩` → এক ডট দুই ডট তিন |
|
|
195
|
+
| Email, URL | `user1@example.com` → user one at example dot com | `user1@example.com` → user এক অ্যাট example ডট কম |
|
|
196
|
+
|
|
197
|
+
Dates: when day and month are both 12 or less and differ, the order can't be known, so the numbers are read as written
|
|
198
|
+
instead of guessing a month. Equal values (`5/5/2024`), one value above 12, ISO `2024-05-10` and written month names use the month name.
|
|
199
|
+
|
|
200
|
+
Leftover symbols (`*`, `#`, brackets, quotes, `**bold**`) are removed. The text is NFC-normalized first.
|
|
201
|
+
|
|
202
|
+
### Keep parts unchanged
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
import re
|
|
206
|
+
tags = re.compile(r"(\[[a-z]+\])")
|
|
207
|
+
convert_text("[warm] 500 dollars", lang="en", keep=tags) # [warm] five hundred dollars
|
|
208
|
+
convert_text("[warm] ৫০০ টাকা", lang="bn", keep=tags) # [warm] পাঁচশত টাকা
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Add your own data
|
|
212
|
+
|
|
213
|
+
Project data (hotlines, brand names, acronyms, tags to strip) stays in your code:
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
from multilingual_num2text import (
|
|
217
|
+
BANGLA_LEXICON, ENGLISH_LEXICON, BanglaLanguage, EnglishLanguage, convert_text, register_language,
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
lexicon = ENGLISH_LEXICON.extended(
|
|
221
|
+
digitwise_numbers=("54321",), # always digit by digit
|
|
222
|
+
acronyms={"KYC": "K Y C"},
|
|
223
|
+
fixed_phrases={"N/A": "not applicable"},
|
|
224
|
+
removed_patterns=(r"\[Source\s*\d*\]",), # deleted before converting
|
|
225
|
+
strip_decimal_trailing_zeros=False, # 2.60 → two point six zero
|
|
226
|
+
)
|
|
227
|
+
register_language(EnglishLanguage(lexicon=lexicon), code="en")
|
|
228
|
+
convert_text("[Source 1] Call 54321", lang="en") # Call five four three two one
|
|
229
|
+
|
|
230
|
+
lexicon = BANGLA_LEXICON.extended(
|
|
231
|
+
digitwise_numbers=("54321",),
|
|
232
|
+
domain_words={"example": "এক্সাম্পল"}, # example.com → এক্সাম্পল ডট কম
|
|
233
|
+
email_name_words={"support": "সাপোর্ট"},
|
|
234
|
+
)
|
|
235
|
+
register_language(BanglaLanguage(lexicon=lexicon), code="bn")
|
|
236
|
+
convert_text("৫৪৩২১ নম্বরে কল করুন", lang="bn") # পাঁচ চার তিন দুই এক নম্বরে কল করুন
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
`extended()` merges dictionaries, appends to tuples and replaces other settings. The built-in lexicon is not changed.
|
|
240
|
+
Every setting:
|
|
241
|
+
|
|
242
|
+
| Setting | Type (default) | Meaning |
|
|
243
|
+
|---|---|---|
|
|
244
|
+
| `currencies` | tuple of `Currency` | Currencies with their names and symbols. Built in: all 153 ISO currencies. |
|
|
245
|
+
| `digitwise_numbers` | tuple of `str` | Numbers always read digit by digit wherever they appear (hotlines, short codes). |
|
|
246
|
+
| `fixed_phrases` | dict | Written → spoken, matched in any case: `{"24/7": "twenty-four seven"}`. |
|
|
247
|
+
| `word_replacements` | dict | Plain text replacements done first: `{" + ": " plus "}`. |
|
|
248
|
+
| `removed_patterns` | tuple of regex `str` | Deleted before converting: `(r"\[Source\s*\d*\]",)`. |
|
|
249
|
+
| `acronyms` | dict | How to say acronyms: `{"ATM": "এটিএম"}`. |
|
|
250
|
+
| `domain_words` | dict | How to say parts of web and email addresses: `{"com": "কম"}`. |
|
|
251
|
+
| `bare_domain_endings` | tuple (`com`, `net`, `org`, `gov`, `edu`, `bd`, `info`, `io`) | Endings that make `example.com` a domain without `https://`. |
|
|
252
|
+
| `email_name_words` | dict | How to say the part before `@`: `{"support": "সাপোর্ট"}`. |
|
|
253
|
+
| `units` | dict | Units after a number: `{"km": "kilometers"}`. |
|
|
254
|
+
| `magnitudes` | dict (`k`, `K` = 1,000, `M` = 1,000,000, `bn` = 1,000,000,000) | Shorthand after a number: `5K` → 5000. |
|
|
255
|
+
| `scale_words` | tuple | Words after an amount that belong to it: `$5 million` → five million dollars. |
|
|
256
|
+
| `duration_words` | tuple | Words that make `6/12/24` a list instead of a date: `6/12/24 months`. Built in: both languages' words in both lexicons. |
|
|
257
|
+
| `digitwise_after` | tuple | Words after which a number of 4+ digits (up to two words later) is read digit by digit: `("OTP", "PIN", "code", "ID")`. |
|
|
258
|
+
| `sentence_end_marks` | tuple | Marks that end a sentence for `lang="auto"`: Bangla `("।", "!", "?")`, English `(".", "!", "?")`. |
|
|
259
|
+
| `abbreviations` | tuple | Words whose dot never ends a sentence and which do not count for `lang="auto"`: `("Mr.", "Dr.", "e.g.", "ডা.")`. |
|
|
260
|
+
| `year_markers_after` / `year_markers_before` | tuple | Words that mark a year: `১৯৭১ সালে`, `in 1971`. |
|
|
261
|
+
| `digitwise_min_length` | `int` (10) | Digit runs this long or longer are read digit by digit (account numbers). |
|
|
262
|
+
| `phone_max_digits` | `int` (15) | Most digits joined into one phone number across spaces and hyphens. |
|
|
263
|
+
| `phone_min_digits_after_plus` | `int` (6) | Fewer digits after `+` are read as a number (`+500`), not a phone. |
|
|
264
|
+
| `strip_decimal_trailing_zeros` | `bool` (True) | `2.60` → two point six. Set `False` for two point six zero. |
|
|
265
|
+
|
|
266
|
+
## Add a language
|
|
267
|
+
|
|
268
|
+
Subclass `Language` and implement five methods. Everything else (money, phones, ranges …) is shared.
|
|
269
|
+
|
|
270
|
+
```python
|
|
271
|
+
from multilingual_num2text import Language, Lexicon, Words, register_language
|
|
272
|
+
|
|
273
|
+
class HindiLanguage(Language):
|
|
274
|
+
code = "hi"
|
|
275
|
+
script = "\u0900-\u097F" # Devanagari, used by lang="auto"
|
|
276
|
+
words = Words(digits=("शून्य", "एक", "दो", "तीन", "चार", "पाँच", "छह", "सात", "आठ", "नौ"),
|
|
277
|
+
negative="ऋण", decimal_point="दशमलव", percent="प्रतिशत", range="से", slash="बटा",
|
|
278
|
+
ratio="अनुपात", dot="डॉट", at="एट", star="स्टार", hash="हैश", plus="प्लस")
|
|
279
|
+
lexicon = Lexicon()
|
|
280
|
+
|
|
281
|
+
def to_cardinal(self, number): ...
|
|
282
|
+
def to_ordinal(self, number): ...
|
|
283
|
+
def to_year(self, year): ...
|
|
284
|
+
def to_date(self, day, month, year=None): ...
|
|
285
|
+
def to_time(self, hour, minute=0, second=None, with_period=False): ...
|
|
286
|
+
|
|
287
|
+
register_language(HindiLanguage())
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
Optional: `language_rules()` for rules only that language needs, `build_currencies()` in `core/currencies.py`
|
|
291
|
+
to name all currencies, and overrides of `to_money`, `to_mixed_fraction`, `spell_scaled_amount`.
|
|
292
|
+
See `src/multilingual_num2text/languages/bangla/` for a complete example.
|
|
293
|
+
|
|
294
|
+
## Test
|
|
295
|
+
|
|
296
|
+
```bash
|
|
297
|
+
pip install -e .
|
|
298
|
+
python -m unittest discover -s tests
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
The tests cover every case above, for both languages, in all combinations:
|
|
302
|
+
|
|
303
|
+
| File | Checks |
|
|
304
|
+
|---|---|
|
|
305
|
+
| `test_multilingual_num2text.py` | 303 example cases across every category in the table |
|
|
306
|
+
| `test_combinations.py` | every case paired with every other case in one text (49,349 pairs), and all cases at once |
|
|
307
|
+
| `test_word_tables.py` | every word from 0 to 99, every hundred and scale, every Bangla number up to 1,00,000, every ordinal, date day, month, day/month pair from 1 to 12, hour, minute and year boundary; no digit left for any number up to 10,000 |
|
|
308
|
+
| `test_currencies.py` | all 153 currencies in both languages: code before and after, every symbol, every name, fractions, negatives, scale words |
|
|
309
|
+
| `test_auto_language.py` | `lang="auto"`, every rule above: 55 detection cases (words, ties, what does not count, digits, mixed digits, unsupported languages), 38 sentence-splitting cases, 91 conversions (code-mixed text, codes, addresses, sentences without words, abbreviations, dates and times, dots), every sample whose language is clear, `keep=`, and extending abbreviations, end marks, code keywords, the splitter and new languages |
|
|
310
|
+
| `test_edges.py` | impossible dates and times, every setting's limit, Unicode forms and digit scripts, bad arguments, the registry, `Lexicon.extended()` |
|
|
311
|
+
|
|
312
|
+
## Contributing
|
|
313
|
+
|
|
314
|
+
Issues and pull requests are welcome. See [CONTRIBUTING.md](https://github.com/kawshikbuet17/multilingual_num2text/blob/main/CONTRIBUTING.md).
|
|
315
|
+
|
|
316
|
+
## License
|
|
317
|
+
|
|
318
|
+
Copyright 2026 Kawshik Kumar Paul. Licensed under the [Apache License 2.0](https://github.com/kawshikbuet17/multilingual_num2text/blob/main/LICENSE).
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# multilingual_num2text
|
|
2
|
+
|
|
3
|
+
Turns every number in a piece of text into words, the way a person would say it.
|
|
4
|
+
|
|
5
|
+
It works out what a number is from how it is written: `$1,250.50` becomes money with cents,
|
|
6
|
+
`01711-123456` a phone number read digit by digit, `25/12/2024` a date, `15:45` a time, `*123#` a code.
|
|
7
|
+
The same in Bangla: `৳১,২৫০.৫০` becomes money with paisa, `০১৭১১-১২৩৪৫৬` a phone number, `২৫/১২/২০২৪` a date, `১৫:৪৫` a time.
|
|
8
|
+
Amounts, percentages, 153 currencies, years, ordinals, ranges, fractions and units are covered.
|
|
9
|
+
Use it wherever numbers must appear as words.
|
|
10
|
+
|
|
11
|
+
English (`en`) and Bangla/Bengali (`bn`) are built in, and new languages can be added.
|
|
12
|
+
No dependencies. Python 3.10+.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install multilingual-num2text
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Latest from GitHub: `pip install git+https://github.com/kawshikbuet17/multilingual_num2text.git`
|
|
21
|
+
|
|
22
|
+
## Use
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from multilingual_num2text import convert_text, number_to_text
|
|
26
|
+
|
|
27
|
+
# One number
|
|
28
|
+
number_to_text(2712, lang="en") # two thousand seven hundred twelve
|
|
29
|
+
number_to_text(2712, lang="en", style="digitwise") # two seven one two
|
|
30
|
+
number_to_text("$1,250.50", lang="en", style="auto") # one thousand two hundred fifty dollars and fifty cents
|
|
31
|
+
number_to_text(2712, lang="bn") # দুই হাজার সাতশত বারো
|
|
32
|
+
number_to_text(2712, lang="bn", style="digitwise") # দুই সাত এক দুই
|
|
33
|
+
number_to_text("৳১,২৫০.৫০", lang="bn", style="auto") # এক হাজার দুইশত পঞ্চাশ টাকা পঞ্চাশ পয়সা
|
|
34
|
+
|
|
35
|
+
# Every number inside a text
|
|
36
|
+
convert_text("Send $1,250.50 on May 10, 2024", lang="en")
|
|
37
|
+
# Send one thousand two hundred fifty dollars and fifty cents on May tenth, twenty twenty-four
|
|
38
|
+
convert_text("৳১,২৫০.৫০ পাঠান ০১৭১১-১২৩৪৫৬ নম্বরে", lang="bn")
|
|
39
|
+
# এক হাজার দুইশত পঞ্চাশ টাকা পঞ্চাশ পয়সা পাঠান শূন্য এক সাত এক এক এক দুই তিন চার পাঁচ ছয় নম্বরে
|
|
40
|
+
|
|
41
|
+
# Language detected per sentence
|
|
42
|
+
convert_text("Pay $5 now. ৫০০ টাকা দিন।", lang="auto") # Pay five dollars now. পাঁচশত টাকা দিন।
|
|
43
|
+
convert_text("Hello. مرحبا 500. ৫০০ টাকা।", lang="auto") # Hello. مرحبا 500. পাঁচশত টাকা।
|
|
44
|
+
number_to_text("২৭১২", lang="auto") # দুই হাজার সাতশত বারো
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Positional arguments also work: `number_to_text(2712, "en", "digitwise")`.
|
|
48
|
+
|
|
49
|
+
## Parameters
|
|
50
|
+
|
|
51
|
+
### `convert_text(text, lang, keep=None, splitter=None)` → `str`
|
|
52
|
+
|
|
53
|
+
Converts every number inside a text.
|
|
54
|
+
|
|
55
|
+
| Parameter | Type | Meaning |
|
|
56
|
+
|---|---|---|
|
|
57
|
+
| `text` | `str` | The text. `""` and `None` are returned unchanged. |
|
|
58
|
+
| `lang` | `str` | `"en"`, `"bn"`, any code added with `register_language`, or `"auto"` (see below). Not case-sensitive. An unknown code raises `ValueError`. |
|
|
59
|
+
| `keep` | `re.Pattern`, optional | Parts to leave exactly as they are, such as tags like `[warm]`. The pattern needs one capturing group around the whole match: `re.compile(r"(\[[a-z]+\])")`. With `"auto"`, these parts also do not count when detecting the language. |
|
|
60
|
+
| `splitter` | function, optional | Only for `"auto"`: your own function that takes the text and returns its sentences. Default: `split_sentences`. |
|
|
61
|
+
|
|
62
|
+
### `number_to_text(value, lang, style="cardinal")` → `str`
|
|
63
|
+
|
|
64
|
+
Converts one number.
|
|
65
|
+
|
|
66
|
+
| Parameter | Type | Meaning |
|
|
67
|
+
|---|---|---|
|
|
68
|
+
| `value` | `int`, `float` or `str` | `2712`, `2.5`, `"১,২৫০.৫০"`, `"-15"`. With `style="auto"` also `"৳500"`, `"*123#"`, `"01711-123456"`. |
|
|
69
|
+
| `lang` | `str` | As in `convert_text`. With `"auto"`, a number in digits of an unsupported language is returned unchanged. |
|
|
70
|
+
| `style` | `str` or `NumberStyle` | `"cardinal"` (default): the value as one number. `"digitwise"`: digit by digit. `"auto"`: decided by the shape, like `convert_text` (leading `0` or `+` → digit by digit, leading `*` or `#` → a code, currency → money). |
|
|
71
|
+
|
|
72
|
+
Raises `ValueError` when `value` is not a number (in `cardinal` style) or `style` is unknown.
|
|
73
|
+
|
|
74
|
+
### `detect_language(text, default="en", ignore=None)` → `str` or `None`
|
|
75
|
+
|
|
76
|
+
The language `lang="auto"` would use for a text.
|
|
77
|
+
|
|
78
|
+
| Parameter | Type | Meaning |
|
|
79
|
+
|---|---|---|
|
|
80
|
+
| `text` | `str` | The text to look at. |
|
|
81
|
+
| `default` | `str` | Returned when the text has no words or digits. ASCII digits also count as this language. |
|
|
82
|
+
| `ignore` | `re.Pattern`, optional | Parts that do not count, like `keep` in `convert_text`. |
|
|
83
|
+
|
|
84
|
+
Returns the language code, or `None` when the text is written in a language that is not registered.
|
|
85
|
+
|
|
86
|
+
### Languages and sentences
|
|
87
|
+
|
|
88
|
+
| Call | Parameters |
|
|
89
|
+
|---|---|
|
|
90
|
+
| `EnglishLanguage(indian_scales=False, lexicon=None)` | `indian_scales=True` reads `2,50,000` as two lakh fifty thousand. `lexicon` replaces the built-in data (see below). |
|
|
91
|
+
| `BanglaLanguage(lexicon=None)` | `lexicon` replaces the built-in data. |
|
|
92
|
+
| `register_language(language, code=None)` | Makes a language usable by its code. `code` defaults to `language.code`; registering an existing code replaces it. |
|
|
93
|
+
| `unregister_language(code)` | Removes a language and returns it, or `None` if it was not registered. |
|
|
94
|
+
| `get_language(code)` | The registered language object, with `convert_text(text, keep=None)` and `number_to_text(value, style="cardinal")`. |
|
|
95
|
+
| `available_languages()` | The registered codes, sorted. |
|
|
96
|
+
| `split_sentences(text, ignore=None)` | The sentences `lang="auto"` works with. |
|
|
97
|
+
| `detect_sentence_languages(sentences, default="en", ignore=None)` | The language of each sentence, as `lang="auto"` uses it (sentences without words take their context). |
|
|
98
|
+
|
|
99
|
+
### `lang="auto"`
|
|
100
|
+
|
|
101
|
+
The text is split into sentences, and each sentence is converted in the language it is written in.
|
|
102
|
+
Every number in a sentence follows the sentence.
|
|
103
|
+
|
|
104
|
+
**Which language a sentence is in:**
|
|
105
|
+
|
|
106
|
+
| Rule | Example |
|
|
107
|
+
|---|---|
|
|
108
|
+
| The language with the most words wins | `আপনার account এ ৫০০ টাকা পাঠানো হয়েছে` → bn, `Cash out charge ১.৮৫%` → en |
|
|
109
|
+
| Acronyms (`OTP`, `ATM`), emails, web addresses, abbreviations (`Mr.`, `ডা.`) and `keep=` tags do not count | `OTP কোড 123456` → bn, `যোগাযোগ: support@example.com অথবা ০১৭১১-১২৩৪৫৬` → bn |
|
|
110
|
+
| Equal words: the language whose own digits appear wins, then the one whose word comes first | `আপনার balance ৳৫০০` → bn, `Send Money ফি ৫ টাকা` → bn, `আপনার balance 500` → bn |
|
|
111
|
+
| No words: the digits decide, ASCII digits count as English, most digits win, a tie goes to the first digit | `১` → এক, `1` → one, `5০০০` → পাঁচ হাজার, `50০০` → five thousand, `৫০00` → পাঁচ হাজার |
|
|
112
|
+
| A sentence without words inside a longer text takes the language of the sentences around it | `মোট পরিমাণ:⏎৫০০⏎300` → মোট পরিমাণ: পাঁচশত তিনশত |
|
|
113
|
+
| A sentence in a language that is not supported is kept exactly as it is | `مرحبا 500` → مرحبا 500, `Hello. مرحبا 500. ৫০০ টাকা।` → Hello. مرحبا 500. পাঁচশত টাকা। |
|
|
114
|
+
|
|
115
|
+
**Where a sentence ends:**
|
|
116
|
+
|
|
117
|
+
| Rule | Example |
|
|
118
|
+
|---|---|
|
|
119
|
+
| A line break | `Total:⏎৫০০` → two sentences |
|
|
120
|
+
| An end mark (`।` `!` `?` and `.`) followed by a space or the end | `Pay $5 now. ৫০০ টাকা দিন।`, `Thanks! ৫০০ টাকা জমা হয়েছে` |
|
|
121
|
+
| `.` only when the next sentence starts with a capital or another script | `First. Second` splits; `e.g. this one`, `1.5 kg` do not |
|
|
122
|
+
| Never after an abbreviation | `Mr. Karim`, `Dr. Rahman`, `e.g.`, `etc.`, `ডা. রহিম`, `মো. করিম` |
|
|
123
|
+
| Never after a currency symbol before a number | `Tk. ৫০০ মাত্র` → পাঁচশত টাকা মাত্র, `BDT. 500` → five hundred taka |
|
|
124
|
+
| Never after a bare list number | `১. Open app`, `১। প্রথম ধাপ` |
|
|
125
|
+
| Never without a space after the mark | `Wow!Really`, `example.com/?page=2`, `ধন্যবাদ।আপনার` |
|
|
126
|
+
|
|
127
|
+
End marks and abbreviations come from each language's `Lexicon` (`sentence_end_marks`, `abbreviations`) and can be extended:
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
register_language(EnglishLanguage(lexicon=ENGLISH_LEXICON.extended(abbreviations=("Engr.",), sentence_end_marks=(";",))), code="en")
|
|
131
|
+
convert_text("Engr. Karim paid $5; ৫০০ টাকা দিন", lang="auto") # Engr. Karim paid five dollars; পাঁচশত টাকা দিন
|
|
132
|
+
convert_text(text, lang="auto", splitter=lambda text: text.split("|")) # or split sentences yourself
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
`detect_language(text)` returns the code alone, or `None` when unsupported.
|
|
136
|
+
|
|
137
|
+
ASCII, Bangla or mixed digits all work (`50,000` = `৫০,০০০` = `৫০,000`).
|
|
138
|
+
|
|
139
|
+
## What gets converted
|
|
140
|
+
|
|
141
|
+
| Case | English (`en`) | Bangla/Bengali (`bn`) |
|
|
142
|
+
|---|---|---|
|
|
143
|
+
| Number | `1,000,000` → one million | `২,৫০,০০০` → দুই লক্ষ পঞ্চাশ হাজার |
|
|
144
|
+
| Decimal | `0.5` → zero point five | `১.৪৯` → এক দশমিক চার নয় |
|
|
145
|
+
| Negative | `-5` → minus five | `-৫` → ঋণাত্মক পাঁচ |
|
|
146
|
+
| Percent | `90%` → ninety percent | `১.৪৯%` → এক দশমিক চার নয় শতাংশ |
|
|
147
|
+
| Money (sign before) | `$1.50` → one dollar and fifty cents | `৳১,২৫০.৫০` → এক হাজার দুইশত পঞ্চাশ টাকা পঞ্চাশ পয়সা |
|
|
148
|
+
| Money (unit after) | `500 taka` → five hundred taka | `১০.৫০ টাকার` → দশ টাকা পঞ্চাশ পয়সার, `৫০০/-` → পাঁচশত টাকা |
|
|
149
|
+
| Any currency (153 ISO codes) | `KWD 1.250` → one Kuwaiti dinar and two hundred fifty fils | `SAR ৫০.২৫` → পঞ্চাশ সৌদি রিয়াল পঁচিশ হালালা |
|
|
150
|
+
| Money with scale | `$5 million` → five million dollars | `৳২.৫ কোটি` → আড়াই কোটি টাকা |
|
|
151
|
+
| Currency symbol with a dot | `BDT. 500` → five hundred taka, `Rs. 500` → five hundred rupees | `Tk.৫০০` → পাঁচশত টাকা |
|
|
152
|
+
| Code after OTP, PIN, code, ID, reference (4+ digits, up to two words after) | `Your OTP is 482913` → Your OTP is four eight two nine one three, `PIN: 1234` → PIN: one two three four | `OTP কোড 123456` → ওটিপি কোড এক দুই তিন চার পাঁচ ছয়, `আপনার ওটিপি ৪৮২৯১৩` → … চার আট দুই নয় এক তিন |
|
|
153
|
+
| Starts with `0` or `+` | `+880 1711 123456` → plus eight eight zero … | `০১৭১১-১২৩৪৫৬` → শূন্য এক সাত এক এক … |
|
|
154
|
+
| Starts with `*` or `#` | `*123#` → star one two three hash | `*১২৩#` → স্টার এক দুই তিন হ্যাশ |
|
|
155
|
+
| Long number (10+ digits), card | `account 123456789012` → account one two three … | `১২৩৪ ৫৬৭৮ ৯০১২ ৩৪৫৬` → এক দুই তিন চার … |
|
|
156
|
+
| Date | `25/12/2024`, `12/25/2024` → the twenty-fifth of December, twenty twenty-four; `May 10` → May tenth | `২৫/১২/২০২৪`, `২৫ ডিসেম্বর ২০২৪` → পঁচিশে ডিসেম্বর দুই হাজার চব্বিশ |
|
|
157
|
+
| Date where day and month could swap | `2/5/2024` → two five twenty twenty-four, `2/3/24` → two three twenty-four | `২/৫/২০২৪` → দুই পাঁচ দুই হাজার চব্বিশ, `২/৩/২৪` → দুই তিন চব্বিশ |
|
|
158
|
+
| Month name from the other language | `৫ মে ২০২৪` → the fifth of May, twenty twenty-four | `10 May 2024` → দশই মে দুই হাজার চব্বিশ |
|
|
159
|
+
| Day form | — | `১লা বৈশাখ` → পহেলা বৈশাখ, `২১শে` → একুশে |
|
|
160
|
+
| Year | `in 1971` → in nineteen seventy-one, `1990s` → nineteen nineties | `১৯৭১ সালে` → উনিশশো একাত্তর সালে, `২০২৪-২৫` → দুই হাজার চব্বিশ পঁচিশ |
|
|
161
|
+
| Time | `10:05` → ten oh five, `15:45` → three forty-five PM, `10.30 AM` → ten thirty AM | `১০:৩০` → দশটা ত্রিশ মিনিট, `১৫:৪৫` → দুপুর তিনটা পঁয়তাল্লিশ মিনিট, `১০ AM` → সকাল দশটা |
|
|
162
|
+
| Time with টা / টায় / টার or a period word (`.` or `:`) | — | `সকাল ১০.৩০ টায়` → সকাল দশটা ত্রিশ মিনিটে, `১০:০০ টায়` → দশটায়, `বিকেল ৪.১৫ টার` → বিকেল চারটা পনেরো মিনিটের, `রাত ২২:৩০` → রাত দশটা ত্রিশ মিনিট |
|
|
163
|
+
| Ratio | `1:2` → one to two | `১:২` → এক অনুপাত দুই |
|
|
164
|
+
| Ordinal | `21st` → twenty-first | `১ম` → প্রথম, `২য়` → দ্বিতীয়, `১১তম` → এগারোতম |
|
|
165
|
+
| Range | `5-10%` → five to ten percent | `৫০০-১০০০ টাকা` → পাঁচশত থেকে এক হাজার টাকা |
|
|
166
|
+
| Slash | `12/3` → twelve slash three, `6/12/24 months` → six, twelve, twenty-four months, `৬/১২/২৪ মাস` → six, twelve, twenty-four মাস | `১২/৩` → বারো বাই তিন, `৬/১২/২৪ মাস` → ছয়, বারো, চব্বিশ মাস, `6/12/24 months` → ছয়, বারো, চব্বিশ months |
|
|
167
|
+
| Fraction | `2½` → two and a half | `২.৫ লাখ` → আড়াই লাখ, `১.৫ ঘণ্টা` → দেড় ঘণ্টা, `২½` → আড়াই |
|
|
168
|
+
| Unit, shorthand | `20km` → twenty kilometers, `2.5K` → two thousand five hundred | `২০GB` → বিশ জিবি, `৩৫°C` → পঁয়ত্রিশ ডিগ্রি সেলসিয়াস, `৫K` → পাঁচ হাজার |
|
|
169
|
+
| Arithmetic | `2+2=4` → two plus two equals four | `২+২=৪` → দুই যোগ দুই সমান চার, `৫০০+` → পাঁচশত প্লাস |
|
|
170
|
+
| Version, IP | `1.2.3` → one dot two dot three | `১.২.৩` → এক ডট দুই ডট তিন |
|
|
171
|
+
| Email, URL | `user1@example.com` → user one at example dot com | `user1@example.com` → user এক অ্যাট example ডট কম |
|
|
172
|
+
|
|
173
|
+
Dates: when day and month are both 12 or less and differ, the order can't be known, so the numbers are read as written
|
|
174
|
+
instead of guessing a month. Equal values (`5/5/2024`), one value above 12, ISO `2024-05-10` and written month names use the month name.
|
|
175
|
+
|
|
176
|
+
Leftover symbols (`*`, `#`, brackets, quotes, `**bold**`) are removed. The text is NFC-normalized first.
|
|
177
|
+
|
|
178
|
+
### Keep parts unchanged
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
import re
|
|
182
|
+
tags = re.compile(r"(\[[a-z]+\])")
|
|
183
|
+
convert_text("[warm] 500 dollars", lang="en", keep=tags) # [warm] five hundred dollars
|
|
184
|
+
convert_text("[warm] ৫০০ টাকা", lang="bn", keep=tags) # [warm] পাঁচশত টাকা
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Add your own data
|
|
188
|
+
|
|
189
|
+
Project data (hotlines, brand names, acronyms, tags to strip) stays in your code:
|
|
190
|
+
|
|
191
|
+
```python
|
|
192
|
+
from multilingual_num2text import (
|
|
193
|
+
BANGLA_LEXICON, ENGLISH_LEXICON, BanglaLanguage, EnglishLanguage, convert_text, register_language,
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
lexicon = ENGLISH_LEXICON.extended(
|
|
197
|
+
digitwise_numbers=("54321",), # always digit by digit
|
|
198
|
+
acronyms={"KYC": "K Y C"},
|
|
199
|
+
fixed_phrases={"N/A": "not applicable"},
|
|
200
|
+
removed_patterns=(r"\[Source\s*\d*\]",), # deleted before converting
|
|
201
|
+
strip_decimal_trailing_zeros=False, # 2.60 → two point six zero
|
|
202
|
+
)
|
|
203
|
+
register_language(EnglishLanguage(lexicon=lexicon), code="en")
|
|
204
|
+
convert_text("[Source 1] Call 54321", lang="en") # Call five four three two one
|
|
205
|
+
|
|
206
|
+
lexicon = BANGLA_LEXICON.extended(
|
|
207
|
+
digitwise_numbers=("54321",),
|
|
208
|
+
domain_words={"example": "এক্সাম্পল"}, # example.com → এক্সাম্পল ডট কম
|
|
209
|
+
email_name_words={"support": "সাপোর্ট"},
|
|
210
|
+
)
|
|
211
|
+
register_language(BanglaLanguage(lexicon=lexicon), code="bn")
|
|
212
|
+
convert_text("৫৪৩২১ নম্বরে কল করুন", lang="bn") # পাঁচ চার তিন দুই এক নম্বরে কল করুন
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
`extended()` merges dictionaries, appends to tuples and replaces other settings. The built-in lexicon is not changed.
|
|
216
|
+
Every setting:
|
|
217
|
+
|
|
218
|
+
| Setting | Type (default) | Meaning |
|
|
219
|
+
|---|---|---|
|
|
220
|
+
| `currencies` | tuple of `Currency` | Currencies with their names and symbols. Built in: all 153 ISO currencies. |
|
|
221
|
+
| `digitwise_numbers` | tuple of `str` | Numbers always read digit by digit wherever they appear (hotlines, short codes). |
|
|
222
|
+
| `fixed_phrases` | dict | Written → spoken, matched in any case: `{"24/7": "twenty-four seven"}`. |
|
|
223
|
+
| `word_replacements` | dict | Plain text replacements done first: `{" + ": " plus "}`. |
|
|
224
|
+
| `removed_patterns` | tuple of regex `str` | Deleted before converting: `(r"\[Source\s*\d*\]",)`. |
|
|
225
|
+
| `acronyms` | dict | How to say acronyms: `{"ATM": "এটিএম"}`. |
|
|
226
|
+
| `domain_words` | dict | How to say parts of web and email addresses: `{"com": "কম"}`. |
|
|
227
|
+
| `bare_domain_endings` | tuple (`com`, `net`, `org`, `gov`, `edu`, `bd`, `info`, `io`) | Endings that make `example.com` a domain without `https://`. |
|
|
228
|
+
| `email_name_words` | dict | How to say the part before `@`: `{"support": "সাপোর্ট"}`. |
|
|
229
|
+
| `units` | dict | Units after a number: `{"km": "kilometers"}`. |
|
|
230
|
+
| `magnitudes` | dict (`k`, `K` = 1,000, `M` = 1,000,000, `bn` = 1,000,000,000) | Shorthand after a number: `5K` → 5000. |
|
|
231
|
+
| `scale_words` | tuple | Words after an amount that belong to it: `$5 million` → five million dollars. |
|
|
232
|
+
| `duration_words` | tuple | Words that make `6/12/24` a list instead of a date: `6/12/24 months`. Built in: both languages' words in both lexicons. |
|
|
233
|
+
| `digitwise_after` | tuple | Words after which a number of 4+ digits (up to two words later) is read digit by digit: `("OTP", "PIN", "code", "ID")`. |
|
|
234
|
+
| `sentence_end_marks` | tuple | Marks that end a sentence for `lang="auto"`: Bangla `("।", "!", "?")`, English `(".", "!", "?")`. |
|
|
235
|
+
| `abbreviations` | tuple | Words whose dot never ends a sentence and which do not count for `lang="auto"`: `("Mr.", "Dr.", "e.g.", "ডা.")`. |
|
|
236
|
+
| `year_markers_after` / `year_markers_before` | tuple | Words that mark a year: `১৯৭১ সালে`, `in 1971`. |
|
|
237
|
+
| `digitwise_min_length` | `int` (10) | Digit runs this long or longer are read digit by digit (account numbers). |
|
|
238
|
+
| `phone_max_digits` | `int` (15) | Most digits joined into one phone number across spaces and hyphens. |
|
|
239
|
+
| `phone_min_digits_after_plus` | `int` (6) | Fewer digits after `+` are read as a number (`+500`), not a phone. |
|
|
240
|
+
| `strip_decimal_trailing_zeros` | `bool` (True) | `2.60` → two point six. Set `False` for two point six zero. |
|
|
241
|
+
|
|
242
|
+
## Add a language
|
|
243
|
+
|
|
244
|
+
Subclass `Language` and implement five methods. Everything else (money, phones, ranges …) is shared.
|
|
245
|
+
|
|
246
|
+
```python
|
|
247
|
+
from multilingual_num2text import Language, Lexicon, Words, register_language
|
|
248
|
+
|
|
249
|
+
class HindiLanguage(Language):
|
|
250
|
+
code = "hi"
|
|
251
|
+
script = "\u0900-\u097F" # Devanagari, used by lang="auto"
|
|
252
|
+
words = Words(digits=("शून्य", "एक", "दो", "तीन", "चार", "पाँच", "छह", "सात", "आठ", "नौ"),
|
|
253
|
+
negative="ऋण", decimal_point="दशमलव", percent="प्रतिशत", range="से", slash="बटा",
|
|
254
|
+
ratio="अनुपात", dot="डॉट", at="एट", star="स्टार", hash="हैश", plus="प्लस")
|
|
255
|
+
lexicon = Lexicon()
|
|
256
|
+
|
|
257
|
+
def to_cardinal(self, number): ...
|
|
258
|
+
def to_ordinal(self, number): ...
|
|
259
|
+
def to_year(self, year): ...
|
|
260
|
+
def to_date(self, day, month, year=None): ...
|
|
261
|
+
def to_time(self, hour, minute=0, second=None, with_period=False): ...
|
|
262
|
+
|
|
263
|
+
register_language(HindiLanguage())
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Optional: `language_rules()` for rules only that language needs, `build_currencies()` in `core/currencies.py`
|
|
267
|
+
to name all currencies, and overrides of `to_money`, `to_mixed_fraction`, `spell_scaled_amount`.
|
|
268
|
+
See `src/multilingual_num2text/languages/bangla/` for a complete example.
|
|
269
|
+
|
|
270
|
+
## Test
|
|
271
|
+
|
|
272
|
+
```bash
|
|
273
|
+
pip install -e .
|
|
274
|
+
python -m unittest discover -s tests
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
The tests cover every case above, for both languages, in all combinations:
|
|
278
|
+
|
|
279
|
+
| File | Checks |
|
|
280
|
+
|---|---|
|
|
281
|
+
| `test_multilingual_num2text.py` | 303 example cases across every category in the table |
|
|
282
|
+
| `test_combinations.py` | every case paired with every other case in one text (49,349 pairs), and all cases at once |
|
|
283
|
+
| `test_word_tables.py` | every word from 0 to 99, every hundred and scale, every Bangla number up to 1,00,000, every ordinal, date day, month, day/month pair from 1 to 12, hour, minute and year boundary; no digit left for any number up to 10,000 |
|
|
284
|
+
| `test_currencies.py` | all 153 currencies in both languages: code before and after, every symbol, every name, fractions, negatives, scale words |
|
|
285
|
+
| `test_auto_language.py` | `lang="auto"`, every rule above: 55 detection cases (words, ties, what does not count, digits, mixed digits, unsupported languages), 38 sentence-splitting cases, 91 conversions (code-mixed text, codes, addresses, sentences without words, abbreviations, dates and times, dots), every sample whose language is clear, `keep=`, and extending abbreviations, end marks, code keywords, the splitter and new languages |
|
|
286
|
+
| `test_edges.py` | impossible dates and times, every setting's limit, Unicode forms and digit scripts, bad arguments, the registry, `Lexicon.extended()` |
|
|
287
|
+
|
|
288
|
+
## Contributing
|
|
289
|
+
|
|
290
|
+
Issues and pull requests are welcome. See [CONTRIBUTING.md](https://github.com/kawshikbuet17/multilingual_num2text/blob/main/CONTRIBUTING.md).
|
|
291
|
+
|
|
292
|
+
## License
|
|
293
|
+
|
|
294
|
+
Copyright 2026 Kawshik Kumar Paul. Licensed under the [Apache License 2.0](https://github.com/kawshikbuet17/multilingual_num2text/blob/main/LICENSE).
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "multilingual-num2text"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.2.0"
|
|
8
8
|
description = "Convert numbers in text to words: amounts, money, phone numbers, dates, times, codes and more. Bangla and English built in."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "Apache-2.0"
|