bg-text-normalizer 1.0.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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Radi Totev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,147 @@
1
+ Metadata-Version: 2.4
2
+ Name: bg-text-normalizer
3
+ Version: 1.0.0
4
+ Summary: Bulgarian text normalization for TTS — converts numbers, dates, currency, abbreviations to spoken form
5
+ Author: Radi Totev
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/raditotev/bg-text-normalizer
8
+ Project-URL: Repository, https://github.com/raditotev/bg-text-normalizer
9
+ Project-URL: Issues, https://github.com/raditotev/bg-text-normalizer/issues
10
+ Keywords: bulgarian,tts,text-normalization,nlp,speech-synthesis,bulgarian-language
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Speech
22
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
23
+ Classifier: Topic :: Text Processing :: Linguistic
24
+ Classifier: Natural Language :: Bulgarian
25
+ Requires-Python: >=3.8
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Dynamic: license-file
29
+
30
+ # Bulgarian Text Normalizer for TTS
31
+
32
+ A comprehensive text normalization package that converts written Bulgarian text into its spoken form, designed as a preprocessing step for Text-to-Speech (TTS) systems.
33
+
34
+ ## Features
35
+
36
+ | Category | Examples |
37
+ |----------|----------|
38
+ | **Numbers** | `1500` → `хиляда и петстотин` |
39
+ | **Dates** | `15.02.2026 г.` → `петнадесети февруари две хиляди двадесет и шеста година` |
40
+ | **Time** | `14:30 ч.` → `четиринадесет и тридесет часа` |
41
+ | **Currency** | `99.99 лв.` → `деветдесет и девет лева и деветдесет и девет стотинки` |
42
+ | **Percentages** | `15.5%` → `петнадесет цяло и пет десети процента` |
43
+ | **Ordinals** | `21-ви` → `двадесет и първи` |
44
+ | **Abbreviations** | `бул. Витоша, гр. София` → `булевард Витоша, град София` |
45
+ | **Phone numbers** | `+359 888 123 456` → digit-by-digit reading |
46
+ | **Roman numerals** | `век XXI` → `век двадесет и първи` |
47
+ | **Symbols** | `№10` → `номер десет` |
48
+
49
+ ## Grammatical Correctness
50
+
51
+ - **Gender agreement**: Handles masculine/feminine/neuter (`един`/`една`/`едно`, `два`/`две`)
52
+ - **Ordinal forms**: Full gender-aware ordinals (`първи`/`първа`/`първо`)
53
+ - **Year reading**: Ordinal feminine form matching "година" (`две хиляди двадесет и шеста`)
54
+ - **Space-separated thousands**: `7 000 000` → `седем милиона`
55
+
56
+ ## Usage
57
+
58
+ ### Quick usage
59
+ ```python
60
+ from bg_text_normalizer import normalize_text
61
+
62
+ result = normalize_text("На 15.02.2026 г. в 14:30 ч. цената е 1500.50 лв.")
63
+ # "На петнадесети февруари две хиляди двадесет и шеста година в четиринадесет
64
+ # и тридесет часа цената е хиляда и петстотин лева и петдесет стотинки."
65
+ ```
66
+
67
+ ### Class-based usage
68
+ ```python
69
+ from bg_text_normalizer import BulgarianTextNormalizer
70
+
71
+ normalizer = BulgarianTextNormalizer(expand_abbrevs=True, verbose=False)
72
+ result = normalizer.normalize("бул. Витоша №10, гр. София")
73
+ # "булевард Витоша номер десет, град София"
74
+ ```
75
+
76
+ ### Individual modules
77
+ ```python
78
+ from bg_text_normalizer.bg_numbers import number_to_words_cardinal, number_to_words_ordinal
79
+ from bg_text_normalizer.bg_dates import normalize_date
80
+ from bg_text_normalizer.bg_currency import normalize_currency
81
+
82
+ number_to_words_cardinal(2500, gender='m') # "две хиляди и петстотин"
83
+ number_to_words_ordinal(15, gender='m') # "петнадесети"
84
+ normalize_date(15, 2, 2026) # "петнадесети февруари две хиляди двадесет и шеста"
85
+ normalize_currency("99.99", "BGN") # "деветдесет и девет лева и деветдесет и девет стотинки"
86
+ ```
87
+
88
+ ## Integration with TTS Training (Qwen3-TTS)
89
+
90
+ Use this normalizer as a preprocessing step when preparing your training data:
91
+
92
+ ```python
93
+ import json
94
+ from bg_text_normalizer import normalize_text
95
+
96
+ # Process your JSONL training data
97
+ with open('raw_data.jsonl', 'r') as f_in, open('normalized_data.jsonl', 'w') as f_out:
98
+ for line in f_in:
99
+ entry = json.loads(line)
100
+ entry['text'] = normalize_text(entry['text'])
101
+ f_out.write(json.dumps(entry, ensure_ascii=False) + '\n')
102
+ ```
103
+
104
+ For inference (runtime TTS), add normalization before synthesis:
105
+
106
+ ```python
107
+ from bg_text_normalizer import normalize_text
108
+
109
+ def synthesize(text: str):
110
+ normalized = normalize_text(text)
111
+ # ... pass normalized text to TTS model
112
+ ```
113
+
114
+ ## File Structure
115
+
116
+ ```
117
+ bg-text-normalizer/
118
+ ├── src/
119
+ │ └── bg_text_normalizer/
120
+ │ ├── __init__.py # Package entry point
121
+ │ ├── bg_normalizer.py # Main orchestrator
122
+ │ ├── bg_numbers.py # Cardinal, ordinal, decimal numbers
123
+ │ ├── bg_dates.py # Date normalization
124
+ │ ├── bg_time.py # Time normalization
125
+ │ ├── bg_currency.py # Currency (BGN, EUR, USD, GBP)
126
+ │ ├── bg_abbreviations.py # 100+ Bulgarian abbreviations
127
+ │ ├── bg_phone.py # Phone number reading
128
+ │ └── bg_roman.py # Roman numeral conversion
129
+ ├── test_normalizer.py # Test suite
130
+ ├── pyproject.toml
131
+ └── README.md
132
+ ```
133
+
134
+ ## Adding Custom Abbreviations
135
+
136
+ Edit `src/bg_text_normalizer/bg_abbreviations.py` and add entries to the appropriate dictionary:
137
+
138
+ ```python
139
+ # In ADDRESS_ABBREVS, TITLE_ABBREVS, etc.
140
+ CUSTOM_ABBREVS = {
141
+ 'your_abbrev.': 'пълна форма',
142
+ }
143
+ ```
144
+
145
+ ## Dependencies
146
+
147
+ **None** — pure Python, no external dependencies required.
@@ -0,0 +1,118 @@
1
+ # Bulgarian Text Normalizer for TTS
2
+
3
+ A comprehensive text normalization package that converts written Bulgarian text into its spoken form, designed as a preprocessing step for Text-to-Speech (TTS) systems.
4
+
5
+ ## Features
6
+
7
+ | Category | Examples |
8
+ |----------|----------|
9
+ | **Numbers** | `1500` → `хиляда и петстотин` |
10
+ | **Dates** | `15.02.2026 г.` → `петнадесети февруари две хиляди двадесет и шеста година` |
11
+ | **Time** | `14:30 ч.` → `четиринадесет и тридесет часа` |
12
+ | **Currency** | `99.99 лв.` → `деветдесет и девет лева и деветдесет и девет стотинки` |
13
+ | **Percentages** | `15.5%` → `петнадесет цяло и пет десети процента` |
14
+ | **Ordinals** | `21-ви` → `двадесет и първи` |
15
+ | **Abbreviations** | `бул. Витоша, гр. София` → `булевард Витоша, град София` |
16
+ | **Phone numbers** | `+359 888 123 456` → digit-by-digit reading |
17
+ | **Roman numerals** | `век XXI` → `век двадесет и първи` |
18
+ | **Symbols** | `№10` → `номер десет` |
19
+
20
+ ## Grammatical Correctness
21
+
22
+ - **Gender agreement**: Handles masculine/feminine/neuter (`един`/`една`/`едно`, `два`/`две`)
23
+ - **Ordinal forms**: Full gender-aware ordinals (`първи`/`първа`/`първо`)
24
+ - **Year reading**: Ordinal feminine form matching "година" (`две хиляди двадесет и шеста`)
25
+ - **Space-separated thousands**: `7 000 000` → `седем милиона`
26
+
27
+ ## Usage
28
+
29
+ ### Quick usage
30
+ ```python
31
+ from bg_text_normalizer import normalize_text
32
+
33
+ result = normalize_text("На 15.02.2026 г. в 14:30 ч. цената е 1500.50 лв.")
34
+ # "На петнадесети февруари две хиляди двадесет и шеста година в четиринадесет
35
+ # и тридесет часа цената е хиляда и петстотин лева и петдесет стотинки."
36
+ ```
37
+
38
+ ### Class-based usage
39
+ ```python
40
+ from bg_text_normalizer import BulgarianTextNormalizer
41
+
42
+ normalizer = BulgarianTextNormalizer(expand_abbrevs=True, verbose=False)
43
+ result = normalizer.normalize("бул. Витоша №10, гр. София")
44
+ # "булевард Витоша номер десет, град София"
45
+ ```
46
+
47
+ ### Individual modules
48
+ ```python
49
+ from bg_text_normalizer.bg_numbers import number_to_words_cardinal, number_to_words_ordinal
50
+ from bg_text_normalizer.bg_dates import normalize_date
51
+ from bg_text_normalizer.bg_currency import normalize_currency
52
+
53
+ number_to_words_cardinal(2500, gender='m') # "две хиляди и петстотин"
54
+ number_to_words_ordinal(15, gender='m') # "петнадесети"
55
+ normalize_date(15, 2, 2026) # "петнадесети февруари две хиляди двадесет и шеста"
56
+ normalize_currency("99.99", "BGN") # "деветдесет и девет лева и деветдесет и девет стотинки"
57
+ ```
58
+
59
+ ## Integration with TTS Training (Qwen3-TTS)
60
+
61
+ Use this normalizer as a preprocessing step when preparing your training data:
62
+
63
+ ```python
64
+ import json
65
+ from bg_text_normalizer import normalize_text
66
+
67
+ # Process your JSONL training data
68
+ with open('raw_data.jsonl', 'r') as f_in, open('normalized_data.jsonl', 'w') as f_out:
69
+ for line in f_in:
70
+ entry = json.loads(line)
71
+ entry['text'] = normalize_text(entry['text'])
72
+ f_out.write(json.dumps(entry, ensure_ascii=False) + '\n')
73
+ ```
74
+
75
+ For inference (runtime TTS), add normalization before synthesis:
76
+
77
+ ```python
78
+ from bg_text_normalizer import normalize_text
79
+
80
+ def synthesize(text: str):
81
+ normalized = normalize_text(text)
82
+ # ... pass normalized text to TTS model
83
+ ```
84
+
85
+ ## File Structure
86
+
87
+ ```
88
+ bg-text-normalizer/
89
+ ├── src/
90
+ │ └── bg_text_normalizer/
91
+ │ ├── __init__.py # Package entry point
92
+ │ ├── bg_normalizer.py # Main orchestrator
93
+ │ ├── bg_numbers.py # Cardinal, ordinal, decimal numbers
94
+ │ ├── bg_dates.py # Date normalization
95
+ │ ├── bg_time.py # Time normalization
96
+ │ ├── bg_currency.py # Currency (BGN, EUR, USD, GBP)
97
+ │ ├── bg_abbreviations.py # 100+ Bulgarian abbreviations
98
+ │ ├── bg_phone.py # Phone number reading
99
+ │ └── bg_roman.py # Roman numeral conversion
100
+ ├── test_normalizer.py # Test suite
101
+ ├── pyproject.toml
102
+ └── README.md
103
+ ```
104
+
105
+ ## Adding Custom Abbreviations
106
+
107
+ Edit `src/bg_text_normalizer/bg_abbreviations.py` and add entries to the appropriate dictionary:
108
+
109
+ ```python
110
+ # In ADDRESS_ABBREVS, TITLE_ABBREVS, etc.
111
+ CUSTOM_ABBREVS = {
112
+ 'your_abbrev.': 'пълна форма',
113
+ }
114
+ ```
115
+
116
+ ## Dependencies
117
+
118
+ **None** — pure Python, no external dependencies required.
@@ -0,0 +1,42 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "bg-text-normalizer"
7
+ version = "1.0.0"
8
+ description = "Bulgarian text normalization for TTS — converts numbers, dates, currency, abbreviations to spoken form"
9
+ readme = "README.md"
10
+ license = "MIT"
11
+ requires-python = ">=3.8"
12
+ authors = [
13
+ {name = "Radi Totev"},
14
+ ]
15
+ keywords = ["bulgarian", "tts", "text-normalization", "nlp", "speech-synthesis", "bulgarian-language"]
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Developers",
19
+ "Intended Audience :: Science/Research",
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3.8",
22
+ "Programming Language :: Python :: 3.9",
23
+ "Programming Language :: Python :: 3.10",
24
+ "Programming Language :: Python :: 3.11",
25
+ "Programming Language :: Python :: 3.12",
26
+ "Programming Language :: Python :: 3.13",
27
+ "Topic :: Multimedia :: Sound/Audio :: Speech",
28
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
29
+ "Topic :: Text Processing :: Linguistic",
30
+ "Natural Language :: Bulgarian",
31
+ ]
32
+
33
+ [project.urls]
34
+ Homepage = "https://github.com/raditotev/bg-text-normalizer"
35
+ Repository = "https://github.com/raditotev/bg-text-normalizer"
36
+ Issues = "https://github.com/raditotev/bg-text-normalizer/issues"
37
+
38
+ [tool.setuptools]
39
+ license-files = ["LICENSE"]
40
+
41
+ [tool.setuptools.packages.find]
42
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,40 @@
1
+ """
2
+ bg-text-norm: Bulgarian Text Normalizer for TTS
3
+ =================================================
4
+ Converts written Bulgarian text (numbers, dates, times, currency,
5
+ abbreviations) into spoken word forms for TTS preprocessing.
6
+
7
+ Usage:
8
+ from bg_text_normalizer import normalize_text
9
+ result = normalize_text("На 15.02.2026 г. в 14:30 ч. цената е 1500.50 лв.")
10
+
11
+ from bg_text_normalizer import BulgarianTextNormalizer
12
+ normalizer = BulgarianTextNormalizer()
13
+ result = normalizer.normalize("...")
14
+ """
15
+
16
+ from .bg_normalizer import BulgarianTextNormalizer, normalize_text
17
+ from .bg_numbers import number_to_words_cardinal, number_to_words_ordinal, float_to_words
18
+ from .bg_dates import normalize_date, normalize_year
19
+ from .bg_time import normalize_time
20
+ from .bg_currency import normalize_currency
21
+ from .bg_abbreviations import normalize_abbreviations, expand_abbreviation
22
+ from .bg_phone import normalize_phone_number
23
+ from .bg_roman import roman_to_arabic
24
+
25
+ __version__ = "1.0.0"
26
+ __all__ = [
27
+ 'BulgarianTextNormalizer',
28
+ 'normalize_text',
29
+ 'number_to_words_cardinal',
30
+ 'number_to_words_ordinal',
31
+ 'float_to_words',
32
+ 'normalize_date',
33
+ 'normalize_year',
34
+ 'normalize_time',
35
+ 'normalize_currency',
36
+ 'normalize_abbreviations',
37
+ 'expand_abbreviation',
38
+ 'normalize_phone_number',
39
+ 'roman_to_arabic',
40
+ ]
@@ -0,0 +1,238 @@
1
+ """
2
+ Bulgarian Abbreviation Expansion
3
+ ===================================
4
+ Expands common Bulgarian abbreviations to their full spoken forms.
5
+
6
+ Categories:
7
+ - Address abbreviations (бул., ул., пл., ж.к., кв.)
8
+ - Title abbreviations (г-н, г-жа, д-р, проф., инж.)
9
+ - Geographic (гр., с., обл.)
10
+ - Measurement units (км, м, кг, л, мм, см)
11
+ - Administrative (ЕТ, ООД, ЕООД, АД, ЕАД)
12
+ - Common abbreviations (т.е., т.н., напр., и т.н., и др.)
13
+ - Institution abbreviations (НАП, НОИ, МОН, МВР, НЗОК)
14
+ """
15
+
16
+ import re
17
+ from typing import Dict, Tuple
18
+
19
+ # Abbreviation → (full form, is_case_sensitive)
20
+ # Order matters: longer abbreviations should be checked first
21
+
22
+ # Address abbreviations
23
+ ADDRESS_ABBREVS: Dict[str, str] = {
24
+ 'бул.': 'булевард',
25
+ 'ул.': 'улица',
26
+ 'пл.': 'площад',
27
+ 'ж.к.': 'жилищен комплекс',
28
+ 'ж. к.': 'жилищен комплекс',
29
+ 'кв.': 'квартал',
30
+ 'бл.': 'блок',
31
+ 'вх.': 'вход',
32
+ 'ет.': 'етаж',
33
+ 'ап.': 'апартамент',
34
+ 'п.к.': 'пощенски код',
35
+ }
36
+
37
+ # Geographic abbreviations
38
+ GEO_ABBREVS: Dict[str, str] = {
39
+ 'гр.': 'град',
40
+ 'с.': 'село',
41
+ 'обл.': 'област',
42
+ 'общ.': 'община',
43
+ 'р-н': 'район',
44
+ }
45
+
46
+ # Title abbreviations
47
+ TITLE_ABBREVS: Dict[str, str] = {
48
+ 'г-н': 'господин',
49
+ 'г-жа': 'госпожа',
50
+ 'г-ца': 'госпожица',
51
+ 'д-р': 'доктор',
52
+ 'проф.': 'професор',
53
+ 'доц.': 'доцент',
54
+ 'акад.': 'академик',
55
+ 'инж.': 'инженер',
56
+ 'арх.': 'архитект',
57
+ 'адв.': 'адвокат',
58
+ 'св.': 'свети',
59
+ }
60
+
61
+ # Measurement unit abbreviations
62
+ MEASUREMENT_ABBREVS: Dict[str, str] = {
63
+ 'км/ч': 'километра в час',
64
+ 'м/с': 'метра в секунда',
65
+ 'кв.м': 'квадратни метра',
66
+ 'кв. м': 'квадратни метра',
67
+ 'кв.м.': 'квадратни метра',
68
+ 'куб.м': 'кубически метра',
69
+ 'куб. м': 'кубически метра',
70
+ 'км': 'километра',
71
+ 'м': 'метра',
72
+ 'см': 'сантиметра',
73
+ 'мм': 'милиметра',
74
+ 'кг': 'килограма',
75
+ 'гр': 'грама', # Note: context-dependent (could also be "град")
76
+ 'мг': 'милиграма',
77
+ 'т': 'тона',
78
+ 'л': 'литра',
79
+ 'мл': 'милилитра',
80
+ 'дка': 'декара',
81
+ 'ха': 'хектара',
82
+ }
83
+
84
+ # Administrative/business abbreviations
85
+ BUSINESS_ABBREVS: Dict[str, str] = {
86
+ 'ЕООД': 'еоод', # Spelled out as abbreviation
87
+ 'ООД': 'оод',
88
+ 'ЕТ': 'едноличен търговец',
89
+ 'АД': 'акционерно дружество',
90
+ 'ЕАД': 'еад',
91
+ 'КД': 'командитно дружество',
92
+ 'СД': 'събирателно дружество',
93
+ }
94
+
95
+ # Common text abbreviations
96
+ COMMON_ABBREVS: Dict[str, str] = {
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
+ # Institution abbreviations (spelled out letter by letter or as words)
127
+ INSTITUTION_ABBREVS: Dict[str, str] = {
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
+ # Number sign / hash
153
+ SYMBOL_ABBREVS: Dict[str, str] = {
154
+ '№': 'номер',
155
+ '&': 'и',
156
+ '@': 'кльомба',
157
+ }
158
+
159
+
160
+ def expand_abbreviation(abbrev: str) -> str:
161
+ """
162
+ Expand a single abbreviation to its full form.
163
+
164
+ Args:
165
+ abbrev: The abbreviation to expand
166
+
167
+ Returns:
168
+ Full form or original if not found
169
+ """
170
+ # Check all dictionaries
171
+ for d in [ADDRESS_ABBREVS, GEO_ABBREVS, TITLE_ABBREVS,
172
+ MEASUREMENT_ABBREVS, BUSINESS_ABBREVS, COMMON_ABBREVS,
173
+ INSTITUTION_ABBREVS, SYMBOL_ABBREVS]:
174
+ if abbrev in d:
175
+ return d[abbrev]
176
+ # Case-insensitive check
177
+ for key, val in d.items():
178
+ if key.lower() == abbrev.lower():
179
+ return val
180
+ return abbrev
181
+
182
+
183
+ def normalize_abbreviations(text: str) -> str:
184
+ """
185
+ Expand all recognized abbreviations in the text.
186
+
187
+ Processes abbreviations in order of specificity (longer first)
188
+ to avoid partial matches.
189
+ """
190
+ # Collect all abbreviations, sorted by length (longest first)
191
+ all_abbrevs = {}
192
+ for d in [COMMON_ABBREVS, ADDRESS_ABBREVS, GEO_ABBREVS, TITLE_ABBREVS,
193
+ BUSINESS_ABBREVS, INSTITUTION_ABBREVS, SYMBOL_ABBREVS]:
194
+ all_abbrevs.update(d)
195
+
196
+ # Sort by length descending to match longer abbreviations first
197
+ sorted_abbrevs = sorted(all_abbrevs.items(), key=lambda x: len(x[0]), reverse=True)
198
+
199
+ for abbrev, full_form in sorted_abbrevs:
200
+ # Escape special regex characters in the abbreviation
201
+ escaped = re.escape(abbrev)
202
+ # Use word boundary or space/start/end boundary
203
+ # Be careful with dots - they're part of some abbreviations
204
+ pattern = r'(?<!\w)' + escaped + r'(?!\w)'
205
+ text = re.sub(pattern, full_form, text, flags=re.IGNORECASE)
206
+
207
+ # Handle measurement units (these need number context)
208
+ # Match: digit + space? + unit
209
+ for unit, full_form in sorted(MEASUREMENT_ABBREVS.items(),
210
+ key=lambda x: len(x[0]), reverse=True):
211
+ escaped_unit = re.escape(unit)
212
+ pattern = r'(\d)\s*' + escaped_unit + r'\b'
213
+ text = re.sub(pattern, r'\1 ' + full_form, text)
214
+
215
+ return text
216
+
217
+
218
+ if __name__ == '__main__':
219
+ test_cases = [
220
+ "бул. Витоша №10, гр. София",
221
+ "г-н Иванов е д-р по медицина",
222
+ "Офис на 5 км от центъра",
223
+ "Площ: 120 кв.м",
224
+ "т.е. не може да се направи",
225
+ "Фирма Тест ЕООД",
226
+ "Скорост: 60 км/ч",
227
+ "Заплата: 2500 лв. (вкл. ДДС)",
228
+ "ж.к. Люлин, бл. 305, вх. А, ет. 5, ап. 20",
229
+ "проф. д-р Петров",
230
+ "и т.н. и т.н.",
231
+ ]
232
+
233
+ print("=== Abbreviation Expansion ===")
234
+ for test in test_cases:
235
+ result = normalize_abbreviations(test)
236
+ print(f" Input: {test}")
237
+ print(f" Output: {result}")
238
+ print()