gladia-normalization 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.
- gladia_normalization-0.1.0.dist-info/METADATA +210 -0
- gladia_normalization-0.1.0.dist-info/RECORD +72 -0
- gladia_normalization-0.1.0.dist-info/WHEEL +4 -0
- gladia_normalization-0.1.0.dist-info/licenses/LICENSE +21 -0
- normalization/__init__.py +4 -0
- normalization/constants/__init__.py +3 -0
- normalization/constants/protectors.py +24 -0
- normalization/languages/__init__.py +7 -0
- normalization/languages/base/__init__.py +7 -0
- normalization/languages/base/language_config.py +83 -0
- normalization/languages/base/language_operator.py +65 -0
- normalization/languages/english/__init__.py +7 -0
- normalization/languages/english/number_normalizer.py +433 -0
- normalization/languages/english/operators.py +199 -0
- normalization/languages/english/replacements.py +1774 -0
- normalization/languages/english/sentence_replacements.py +3 -0
- normalization/languages/french/__init__.py +7 -0
- normalization/languages/french/operators.py +38 -0
- normalization/languages/french/replacements.py +1 -0
- normalization/languages/registry.py +17 -0
- normalization/pipeline/__init__.py +0 -0
- normalization/pipeline/base.py +120 -0
- normalization/pipeline/loader.py +70 -0
- normalization/pipeline/replacer.py +38 -0
- normalization/presets/gladia-3.yaml +119 -0
- normalization/steps/__init__.py +4 -0
- normalization/steps/base/__init__.py +6 -0
- normalization/steps/base/protect_step.py +24 -0
- normalization/steps/base/restore_step.py +23 -0
- normalization/steps/base/text_step.py +11 -0
- normalization/steps/base/word_step.py +11 -0
- normalization/steps/registry.py +26 -0
- normalization/steps/text/__init__.py +79 -0
- normalization/steps/text/apply_sentence_level_replacements.py +29 -0
- normalization/steps/text/casefold_text.py +13 -0
- normalization/steps/text/convert_comparison_operators_to_words.py +26 -0
- normalization/steps/text/convert_decimal_periods_to_decimal_word.py +30 -0
- normalization/steps/text/convert_degree_symbols_to_words.py +26 -0
- normalization/steps/text/convert_digit_word_sequences_to_digits.py +41 -0
- normalization/steps/text/convert_dots_to_words_in_technical_contexts.py +33 -0
- normalization/steps/text/convert_oclock_to_numeric_time.py +34 -0
- normalization/steps/text/convert_roman_numerals_to_digits.py +41 -0
- normalization/steps/text/convert_word_based_time_patterns.py +70 -0
- normalization/steps/text/expand_alphanumeric_codes.py +64 -0
- normalization/steps/text/expand_contractions.py +16 -0
- normalization/steps/text/expand_written_numbers_to_digits.py +13 -0
- normalization/steps/text/expand_www_abbreviation.py +15 -0
- normalization/steps/text/fix_ampm_letter_spacing.py +25 -0
- normalization/steps/text/fix_dot_adjacent_number_words.py +37 -0
- normalization/steps/text/fix_one_word_in_numeric_contexts.py +16 -0
- normalization/steps/text/fix_version_number_v_prefix.py +15 -0
- normalization/steps/text/format_time_patterns_with_ampm.py +53 -0
- normalization/steps/text/normalize_numeric_time_formats.py +16 -0
- normalization/steps/text/normalize_punctuation_between_number_words.py +30 -0
- normalization/steps/text/placeholders.py +367 -0
- normalization/steps/text/protect_plus_word_before_digit_words.py +37 -0
- normalization/steps/text/remove_acronym_periods.py +17 -0
- normalization/steps/text/remove_diacritics.py +40 -0
- normalization/steps/text/remove_filler_words.py +19 -0
- normalization/steps/text/remove_hash_before_numbers.py +15 -0
- normalization/steps/text/remove_non_numeric_trailing_dots.py +15 -0
- normalization/steps/text/remove_spaces_between_adjacent_digits.py +54 -0
- normalization/steps/text/remove_standalone_currency_symbols.py +35 -0
- normalization/steps/text/remove_symbols.py +24 -0
- normalization/steps/text/remove_thousand_separators.py +24 -0
- normalization/steps/text/remove_trailing_apostrophe_space.py +15 -0
- normalization/steps/text/remove_trailing_dot_word_from_emails.py +22 -0
- normalization/steps/text/remove_trailing_period.py +15 -0
- normalization/steps/text/remove_zero_minutes_from_time.py +30 -0
- normalization/steps/text/replace_currency.py +31 -0
- normalization/steps/word/__init__.py +3 -0
- normalization/steps/word/apply_word_replacements.py +33 -0
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from fractions import Fraction
|
|
3
|
+
from typing import Iterator, Match
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class EnglishNumberNormalizer:
|
|
7
|
+
"""Convert any spelled-out numbers into arabic numbers, while handling:
|
|
8
|
+
|
|
9
|
+
- remove any commas
|
|
10
|
+
- keep the suffixes such as: `1960s`, `274th`, `32nd`, etc.
|
|
11
|
+
- spell out currency symbols after the number
|
|
12
|
+
e.g. `$20 million` -> `20000000 dollars`
|
|
13
|
+
- spell out `one` and `ones`
|
|
14
|
+
- interpret successive single-digit numbers as nominal: `one oh one` -> `101`
|
|
15
|
+
|
|
16
|
+
Derived from OpenAI Whisper's EnglishNumberNormalizer. This is a standalone
|
|
17
|
+
class living in the English language folder.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def __init__(self):
|
|
21
|
+
self.zeros = {"o", "oh", "zero"}
|
|
22
|
+
self.ones: dict[str, int] = {
|
|
23
|
+
name: i
|
|
24
|
+
for i, name in enumerate(
|
|
25
|
+
[
|
|
26
|
+
"one",
|
|
27
|
+
"two",
|
|
28
|
+
"three",
|
|
29
|
+
"four",
|
|
30
|
+
"five",
|
|
31
|
+
"six",
|
|
32
|
+
"seven",
|
|
33
|
+
"eight",
|
|
34
|
+
"nine",
|
|
35
|
+
"ten",
|
|
36
|
+
"eleven",
|
|
37
|
+
"twelve",
|
|
38
|
+
"thirteen",
|
|
39
|
+
"fourteen",
|
|
40
|
+
"fifteen",
|
|
41
|
+
"sixteen",
|
|
42
|
+
"seventeen",
|
|
43
|
+
"eighteen",
|
|
44
|
+
"nineteen",
|
|
45
|
+
],
|
|
46
|
+
start=1,
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
self.ones_plural = {
|
|
50
|
+
"sixes" if name == "six" else name + "s": (value, "s")
|
|
51
|
+
for name, value in self.ones.items()
|
|
52
|
+
}
|
|
53
|
+
self.ones_ordinal = {
|
|
54
|
+
"zeroth": (0, "th"),
|
|
55
|
+
"first": (1, "st"),
|
|
56
|
+
"second": (2, "nd"),
|
|
57
|
+
"third": (3, "rd"),
|
|
58
|
+
"fifth": (5, "th"),
|
|
59
|
+
"twelfth": (12, "th"),
|
|
60
|
+
**{
|
|
61
|
+
name + ("h" if name.endswith("t") else "th"): (value, "th")
|
|
62
|
+
for name, value in self.ones.items()
|
|
63
|
+
if value > 3 and value != 5 and value != 12
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
self.ones_suffixed: dict[str, tuple[int, str]] = {
|
|
67
|
+
**self.ones_plural,
|
|
68
|
+
**self.ones_ordinal,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
self.tens = {
|
|
72
|
+
"twenty": 20,
|
|
73
|
+
"thirty": 30,
|
|
74
|
+
"forty": 40,
|
|
75
|
+
"fifty": 50,
|
|
76
|
+
"sixty": 60,
|
|
77
|
+
"seventy": 70,
|
|
78
|
+
"eighty": 80,
|
|
79
|
+
"ninety": 90,
|
|
80
|
+
}
|
|
81
|
+
self.tens_plural = {
|
|
82
|
+
name.replace("y", "ies"): (value, "s") for name, value in self.tens.items()
|
|
83
|
+
}
|
|
84
|
+
self.tens_ordinal = {
|
|
85
|
+
name.replace("y", "ieth"): (value, "th")
|
|
86
|
+
for name, value in self.tens.items()
|
|
87
|
+
}
|
|
88
|
+
self.tens_suffixed = {**self.tens_plural, **self.tens_ordinal}
|
|
89
|
+
|
|
90
|
+
self.multipliers = {
|
|
91
|
+
"hundred": 100,
|
|
92
|
+
"thousand": 1_000,
|
|
93
|
+
"million": 1_000_000,
|
|
94
|
+
"billion": 1_000_000_000,
|
|
95
|
+
"trillion": 1_000_000_000_000,
|
|
96
|
+
"quadrillion": 1_000_000_000_000_000,
|
|
97
|
+
"quintillion": 1_000_000_000_000_000_000,
|
|
98
|
+
"sextillion": 1_000_000_000_000_000_000_000,
|
|
99
|
+
"septillion": 1_000_000_000_000_000_000_000_000,
|
|
100
|
+
"octillion": 1_000_000_000_000_000_000_000_000_000,
|
|
101
|
+
"nonillion": 1_000_000_000_000_000_000_000_000_000_000,
|
|
102
|
+
"decillion": 1_000_000_000_000_000_000_000_000_000_000_000,
|
|
103
|
+
}
|
|
104
|
+
self.multipliers_plural = {
|
|
105
|
+
name + "s": (value, "s") for name, value in self.multipliers.items()
|
|
106
|
+
}
|
|
107
|
+
self.multipliers_ordinal = {
|
|
108
|
+
name + "th": (value, "th") for name, value in self.multipliers.items()
|
|
109
|
+
}
|
|
110
|
+
self.multipliers_suffixed = {
|
|
111
|
+
**self.multipliers_plural,
|
|
112
|
+
**self.multipliers_ordinal,
|
|
113
|
+
}
|
|
114
|
+
self.decimals = {*self.ones, *self.tens, *self.zeros}
|
|
115
|
+
|
|
116
|
+
self.preceding_prefixers = {
|
|
117
|
+
"minus": "-",
|
|
118
|
+
"negative": "-",
|
|
119
|
+
"plus": "+",
|
|
120
|
+
"positive": "+",
|
|
121
|
+
"zero": "0",
|
|
122
|
+
}
|
|
123
|
+
self.following_prefixers = {
|
|
124
|
+
"pound": "£",
|
|
125
|
+
"pounds": "£",
|
|
126
|
+
"euro": "€",
|
|
127
|
+
"euros": "€",
|
|
128
|
+
"yen": "¥",
|
|
129
|
+
"yens": "¥",
|
|
130
|
+
"dollar": "$",
|
|
131
|
+
"dollars": "$",
|
|
132
|
+
"cent": "¢",
|
|
133
|
+
"cents": "¢",
|
|
134
|
+
}
|
|
135
|
+
self.prefixes = set(
|
|
136
|
+
list(self.preceding_prefixers.values())
|
|
137
|
+
+ list(self.following_prefixers.values())
|
|
138
|
+
)
|
|
139
|
+
self.suffixers = {
|
|
140
|
+
"per": {"cent": "%"},
|
|
141
|
+
"percent": "%",
|
|
142
|
+
}
|
|
143
|
+
self.specials = {"and", "double", "triple", "point"}
|
|
144
|
+
|
|
145
|
+
self.words = {
|
|
146
|
+
key
|
|
147
|
+
for mapping in [
|
|
148
|
+
self.zeros,
|
|
149
|
+
self.ones,
|
|
150
|
+
self.ones_suffixed,
|
|
151
|
+
self.tens,
|
|
152
|
+
self.tens_suffixed,
|
|
153
|
+
self.multipliers,
|
|
154
|
+
self.multipliers_suffixed,
|
|
155
|
+
self.preceding_prefixers,
|
|
156
|
+
self.following_prefixers,
|
|
157
|
+
self.suffixers,
|
|
158
|
+
self.specials,
|
|
159
|
+
]
|
|
160
|
+
for key in mapping
|
|
161
|
+
}
|
|
162
|
+
self.literal_words = {"one", "ones"}
|
|
163
|
+
|
|
164
|
+
def process_words(self, words: list[str]) -> Iterator[str]: # noqa: C901
|
|
165
|
+
prefix: str | None = None
|
|
166
|
+
value: str | int | None = None
|
|
167
|
+
skip = False
|
|
168
|
+
|
|
169
|
+
def to_fraction(s: str | float):
|
|
170
|
+
try:
|
|
171
|
+
return Fraction(s)
|
|
172
|
+
except ValueError:
|
|
173
|
+
return None
|
|
174
|
+
|
|
175
|
+
def output(result: str | int):
|
|
176
|
+
nonlocal prefix, value
|
|
177
|
+
result = str(result)
|
|
178
|
+
if prefix is not None:
|
|
179
|
+
result = prefix + result
|
|
180
|
+
value = None
|
|
181
|
+
prefix = None
|
|
182
|
+
return result
|
|
183
|
+
|
|
184
|
+
if len(words) == 0:
|
|
185
|
+
return
|
|
186
|
+
|
|
187
|
+
for i, current in enumerate(words):
|
|
188
|
+
prev = words[i - 1] if i != 0 else None
|
|
189
|
+
next = words[i + 1] if i != len(words) - 1 else None # noqa: A001
|
|
190
|
+
if skip:
|
|
191
|
+
skip = False
|
|
192
|
+
continue
|
|
193
|
+
|
|
194
|
+
current_lower = current.lower()
|
|
195
|
+
prev_lower = prev.lower() if prev is not None else None
|
|
196
|
+
next_lower = next.lower() if next is not None else None
|
|
197
|
+
|
|
198
|
+
next_is_numeric = next is not None and re.match(r"^\d+(\.\d+)?$", next)
|
|
199
|
+
|
|
200
|
+
if re.match(r"^\d+$", current):
|
|
201
|
+
if value is not None:
|
|
202
|
+
yield output(value)
|
|
203
|
+
yield output(current)
|
|
204
|
+
continue
|
|
205
|
+
|
|
206
|
+
has_prefix = current[0] in self.prefixes
|
|
207
|
+
current_without_prefix = current[1:] if has_prefix else current
|
|
208
|
+
if re.match(r"^\d+(\.\d+)?$", current_without_prefix):
|
|
209
|
+
f = to_fraction(current_without_prefix)
|
|
210
|
+
if f is None:
|
|
211
|
+
raise ValueError("Converting the fraction failed")
|
|
212
|
+
|
|
213
|
+
if value is not None:
|
|
214
|
+
if isinstance(value, str) and value.endswith("."):
|
|
215
|
+
value = str(value) + str(current)
|
|
216
|
+
continue
|
|
217
|
+
else:
|
|
218
|
+
yield output(value)
|
|
219
|
+
|
|
220
|
+
prefix = current[0] if has_prefix else prefix
|
|
221
|
+
if f.denominator == 1:
|
|
222
|
+
value = f.numerator
|
|
223
|
+
else:
|
|
224
|
+
value = current_without_prefix
|
|
225
|
+
elif current_lower not in self.words:
|
|
226
|
+
if value is not None:
|
|
227
|
+
yield output(value)
|
|
228
|
+
yield output(current)
|
|
229
|
+
elif current_lower in self.zeros:
|
|
230
|
+
value = str(value or "") + "0"
|
|
231
|
+
elif current_lower in self.ones:
|
|
232
|
+
ones = self.ones[current_lower]
|
|
233
|
+
|
|
234
|
+
if value is None:
|
|
235
|
+
value = ones
|
|
236
|
+
elif isinstance(value, str) or prev_lower in self.ones:
|
|
237
|
+
if prev_lower in self.tens and ones < 10:
|
|
238
|
+
value = value[:-1] + str(ones) # type: ignore
|
|
239
|
+
else:
|
|
240
|
+
value = str(value) + str(ones)
|
|
241
|
+
elif ones < 10:
|
|
242
|
+
if value % 10 == 0:
|
|
243
|
+
value += ones
|
|
244
|
+
else:
|
|
245
|
+
value = str(value) + str(ones)
|
|
246
|
+
else:
|
|
247
|
+
if value % 100 == 0:
|
|
248
|
+
value += ones
|
|
249
|
+
else:
|
|
250
|
+
value = str(value) + str(ones)
|
|
251
|
+
elif current_lower in self.ones_suffixed:
|
|
252
|
+
ones, suffix = self.ones_suffixed[current_lower]
|
|
253
|
+
if value is None:
|
|
254
|
+
yield output(str(ones) + suffix)
|
|
255
|
+
elif isinstance(value, str) or prev_lower in self.ones:
|
|
256
|
+
if prev_lower in self.tens and ones < 10:
|
|
257
|
+
yield output(value[:-1] + str(ones) + suffix) # type: ignore
|
|
258
|
+
else:
|
|
259
|
+
yield output(str(value) + str(ones) + suffix)
|
|
260
|
+
elif ones < 10:
|
|
261
|
+
if value % 10 == 0:
|
|
262
|
+
yield output(str(value + ones) + suffix)
|
|
263
|
+
else:
|
|
264
|
+
yield output(str(value) + str(ones) + suffix)
|
|
265
|
+
else:
|
|
266
|
+
if value % 100 == 0:
|
|
267
|
+
yield output(str(value + ones) + suffix)
|
|
268
|
+
else:
|
|
269
|
+
yield output(str(value) + str(ones) + suffix)
|
|
270
|
+
value = None
|
|
271
|
+
elif current_lower in self.tens:
|
|
272
|
+
tens = self.tens[current_lower]
|
|
273
|
+
if value is None:
|
|
274
|
+
value = tens
|
|
275
|
+
elif isinstance(value, str):
|
|
276
|
+
value = str(value) + str(tens)
|
|
277
|
+
else:
|
|
278
|
+
if value % 100 == 0:
|
|
279
|
+
value += tens
|
|
280
|
+
else:
|
|
281
|
+
value = str(value) + str(tens)
|
|
282
|
+
elif current_lower in self.tens_suffixed:
|
|
283
|
+
tens, suffix = self.tens_suffixed[current_lower]
|
|
284
|
+
if value is None:
|
|
285
|
+
yield output(str(tens) + suffix)
|
|
286
|
+
elif isinstance(value, str):
|
|
287
|
+
yield output(str(value) + str(tens) + suffix)
|
|
288
|
+
else:
|
|
289
|
+
if value % 100 == 0:
|
|
290
|
+
yield output(str(value + tens) + suffix)
|
|
291
|
+
else:
|
|
292
|
+
yield output(str(value) + str(tens) + suffix)
|
|
293
|
+
elif current_lower in self.multipliers:
|
|
294
|
+
multiplier = self.multipliers[current_lower]
|
|
295
|
+
if value is None:
|
|
296
|
+
value = multiplier
|
|
297
|
+
elif isinstance(value, str) or value == 0:
|
|
298
|
+
f = to_fraction(value)
|
|
299
|
+
p = f * multiplier if f is not None else None
|
|
300
|
+
if p is not None and p.denominator == 1:
|
|
301
|
+
value = p.numerator
|
|
302
|
+
else:
|
|
303
|
+
yield output(value)
|
|
304
|
+
value = multiplier
|
|
305
|
+
else:
|
|
306
|
+
before = value // 1000 * 1000
|
|
307
|
+
residual = value % 1000
|
|
308
|
+
value = before + residual * multiplier
|
|
309
|
+
elif current_lower in self.multipliers_suffixed:
|
|
310
|
+
multiplier, suffix = self.multipliers_suffixed[current_lower]
|
|
311
|
+
if value is None:
|
|
312
|
+
yield output(str(multiplier) + suffix)
|
|
313
|
+
elif isinstance(value, str):
|
|
314
|
+
f = to_fraction(value)
|
|
315
|
+
p = f * multiplier if f is not None else None
|
|
316
|
+
if p is not None and p.denominator == 1:
|
|
317
|
+
yield output(str(p.numerator) + suffix)
|
|
318
|
+
else:
|
|
319
|
+
yield output(value)
|
|
320
|
+
yield output(str(multiplier) + suffix)
|
|
321
|
+
else:
|
|
322
|
+
before = value // 1000 * 1000
|
|
323
|
+
residual = value % 1000
|
|
324
|
+
value = before + residual * multiplier
|
|
325
|
+
yield output(str(value) + suffix)
|
|
326
|
+
value = None
|
|
327
|
+
elif current_lower in self.preceding_prefixers:
|
|
328
|
+
if value is not None:
|
|
329
|
+
yield output(value)
|
|
330
|
+
|
|
331
|
+
if next_lower in self.words or next_is_numeric:
|
|
332
|
+
prefix = self.preceding_prefixers[current_lower]
|
|
333
|
+
else:
|
|
334
|
+
yield output(current)
|
|
335
|
+
elif current_lower in self.following_prefixers:
|
|
336
|
+
if value is not None:
|
|
337
|
+
prefix = self.following_prefixers[current_lower]
|
|
338
|
+
yield output(value)
|
|
339
|
+
else:
|
|
340
|
+
yield output(current)
|
|
341
|
+
elif current_lower in self.suffixers:
|
|
342
|
+
if value is not None:
|
|
343
|
+
suffix = self.suffixers[current_lower]
|
|
344
|
+
if isinstance(suffix, dict):
|
|
345
|
+
if next in suffix:
|
|
346
|
+
yield output(str(value) + suffix[next])
|
|
347
|
+
skip = True
|
|
348
|
+
else:
|
|
349
|
+
yield output(value)
|
|
350
|
+
yield output(current)
|
|
351
|
+
else:
|
|
352
|
+
yield output(str(value) + suffix)
|
|
353
|
+
else:
|
|
354
|
+
yield output(current)
|
|
355
|
+
elif current_lower in self.specials:
|
|
356
|
+
if next_lower not in self.words and not next_is_numeric:
|
|
357
|
+
if value is not None:
|
|
358
|
+
yield output(value)
|
|
359
|
+
yield output(current)
|
|
360
|
+
elif current_lower == "and":
|
|
361
|
+
if prev_lower not in self.multipliers:
|
|
362
|
+
if value is not None:
|
|
363
|
+
yield output(value)
|
|
364
|
+
yield output(current)
|
|
365
|
+
elif current_lower == "double" or current_lower == "triple":
|
|
366
|
+
if next_lower in self.ones or next_lower in self.zeros:
|
|
367
|
+
repeats = 2 if current_lower == "double" else 3
|
|
368
|
+
ones = self.ones.get(next_lower, 0)
|
|
369
|
+
value = str(value or "") + str(ones) * repeats
|
|
370
|
+
skip = True
|
|
371
|
+
else:
|
|
372
|
+
if value is not None:
|
|
373
|
+
yield output(value)
|
|
374
|
+
yield output(current)
|
|
375
|
+
elif current_lower == "point":
|
|
376
|
+
if next_lower in self.decimals or next_is_numeric:
|
|
377
|
+
value = str(value or "") + "."
|
|
378
|
+
else:
|
|
379
|
+
raise ValueError(f"Unexpected token: {current}")
|
|
380
|
+
else:
|
|
381
|
+
raise ValueError(f"Unexpected token: {current}")
|
|
382
|
+
|
|
383
|
+
if value is not None:
|
|
384
|
+
yield output(value)
|
|
385
|
+
|
|
386
|
+
def preprocess(self, s: str) -> str:
|
|
387
|
+
results = []
|
|
388
|
+
segments = re.split(r"\band\s+a\s+half\b", s)
|
|
389
|
+
for i, segment in enumerate(segments):
|
|
390
|
+
if len(segment.strip()) == 0:
|
|
391
|
+
continue
|
|
392
|
+
if i == len(segments) - 1:
|
|
393
|
+
results.append(segment)
|
|
394
|
+
else:
|
|
395
|
+
results.append(segment)
|
|
396
|
+
last_word = segment.rsplit(maxsplit=2)[-1]
|
|
397
|
+
if last_word in self.decimals or last_word in self.multipliers:
|
|
398
|
+
results.append("point five")
|
|
399
|
+
else:
|
|
400
|
+
results.append("and a half")
|
|
401
|
+
|
|
402
|
+
s = " ".join(results)
|
|
403
|
+
s = re.sub(r"([a-z])([0-9])", r"\1 \2", s)
|
|
404
|
+
s = re.sub(r"([0-9])([a-z])", r"\1 \2", s)
|
|
405
|
+
s = re.sub(r"([0-9])\s+(st|nd|rd|th|s)\b", r"\1\2", s)
|
|
406
|
+
return s
|
|
407
|
+
|
|
408
|
+
def postprocess(self, s: str) -> str:
|
|
409
|
+
def combine_cents(m: Match):
|
|
410
|
+
try:
|
|
411
|
+
currency = m.group(1)
|
|
412
|
+
integer = m.group(2)
|
|
413
|
+
cents = int(m.group(3))
|
|
414
|
+
return f"{currency}{integer}.{cents:02d}"
|
|
415
|
+
except ValueError:
|
|
416
|
+
return m.string
|
|
417
|
+
|
|
418
|
+
def extract_cents(m: Match):
|
|
419
|
+
try:
|
|
420
|
+
return f"¢{int(m.group(1))}"
|
|
421
|
+
except ValueError:
|
|
422
|
+
return m.string
|
|
423
|
+
|
|
424
|
+
s = re.sub(r"([€£$¥])([0-9]+) (?:and )?¢([0-9]{1,2})\b", combine_cents, s)
|
|
425
|
+
s = re.sub(r"[€£$¥]0.([0-9]{1,2})\b", extract_cents, s)
|
|
426
|
+
s = re.sub(r"\b1(s?)\b", r"one\1", s)
|
|
427
|
+
return s
|
|
428
|
+
|
|
429
|
+
def __call__(self, s: str) -> str:
|
|
430
|
+
s = self.preprocess(s)
|
|
431
|
+
s = " ".join(word for word in self.process_words(s.split()) if word is not None)
|
|
432
|
+
s = self.postprocess(s)
|
|
433
|
+
return s
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from typing import cast
|
|
3
|
+
|
|
4
|
+
import contractions
|
|
5
|
+
|
|
6
|
+
from normalization.languages.base import LanguageConfig, LanguageOperators
|
|
7
|
+
from normalization.languages.english.number_normalizer import EnglishNumberNormalizer
|
|
8
|
+
from normalization.languages.english.replacements import ENGLISH_REPLACEMENTS
|
|
9
|
+
from normalization.languages.english.sentence_replacements import (
|
|
10
|
+
ENGLISH_SENTENCE_REPLACEMENTS,
|
|
11
|
+
)
|
|
12
|
+
from normalization.languages.registry import register_language
|
|
13
|
+
|
|
14
|
+
# Single digits 1-9 as words: shared by _TIME_WORDS, digit_words, and
|
|
15
|
+
# get_compound_minutes() so each word is defined exactly once.
|
|
16
|
+
_ONE_TO_NINE: dict[str, str] = {
|
|
17
|
+
"one": "1",
|
|
18
|
+
"two": "2",
|
|
19
|
+
"three": "3",
|
|
20
|
+
"four": "4",
|
|
21
|
+
"five": "5",
|
|
22
|
+
"six": "6",
|
|
23
|
+
"seven": "7",
|
|
24
|
+
"eight": "8",
|
|
25
|
+
"nine": "9",
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
# All spoken time values: clock hours (1-12) and minute-worth values (13-50).
|
|
29
|
+
# Covers both o'clock patterns and "eight thirty"-style patterns.
|
|
30
|
+
_TIME_WORDS: dict[str, str] = {
|
|
31
|
+
**_ONE_TO_NINE,
|
|
32
|
+
"ten": "10",
|
|
33
|
+
"eleven": "11",
|
|
34
|
+
"twelve": "12",
|
|
35
|
+
"noon": "12",
|
|
36
|
+
"midnight": "12",
|
|
37
|
+
"thirteen": "13",
|
|
38
|
+
"fourteen": "14",
|
|
39
|
+
"fifteen": "15",
|
|
40
|
+
"sixteen": "16",
|
|
41
|
+
"seventeen": "17",
|
|
42
|
+
"eighteen": "18",
|
|
43
|
+
"nineteen": "19",
|
|
44
|
+
"twenty": "20",
|
|
45
|
+
"thirty": "30",
|
|
46
|
+
"forty": "40",
|
|
47
|
+
"fifty": "50",
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
ENGLISH_CONFIG = LanguageConfig(
|
|
51
|
+
code="en",
|
|
52
|
+
decimal_separator=".",
|
|
53
|
+
decimal_word="point",
|
|
54
|
+
thousand_separator=",",
|
|
55
|
+
symbols_to_words={
|
|
56
|
+
"@": "at",
|
|
57
|
+
".": "dot",
|
|
58
|
+
"+": "plus",
|
|
59
|
+
"=": "equals",
|
|
60
|
+
">": "greater than",
|
|
61
|
+
"<": "less than",
|
|
62
|
+
"°": "degree",
|
|
63
|
+
"°C": "degree celsius",
|
|
64
|
+
"°F": "degree fahrenheit",
|
|
65
|
+
"%": "percent",
|
|
66
|
+
},
|
|
67
|
+
currency_symbol_to_word={
|
|
68
|
+
"€": "euros",
|
|
69
|
+
"$": "dollars",
|
|
70
|
+
"£": "pounds",
|
|
71
|
+
"¢": "cents",
|
|
72
|
+
"¥": "yens",
|
|
73
|
+
},
|
|
74
|
+
filler_words=[
|
|
75
|
+
"um",
|
|
76
|
+
"uh",
|
|
77
|
+
"uhm",
|
|
78
|
+
"ooh",
|
|
79
|
+
"yeah",
|
|
80
|
+
"yep",
|
|
81
|
+
"yup",
|
|
82
|
+
"hmm",
|
|
83
|
+
"huh",
|
|
84
|
+
"mhm",
|
|
85
|
+
"mm",
|
|
86
|
+
"mmm",
|
|
87
|
+
],
|
|
88
|
+
oclock_word="o'clock",
|
|
89
|
+
time_words=_TIME_WORDS,
|
|
90
|
+
sentence_replacements=ENGLISH_SENTENCE_REPLACEMENTS,
|
|
91
|
+
# _ONE_TO_NINE (1-9) plus zero and its spoken aliases ("oh", "o").
|
|
92
|
+
digit_words={"zero": "0", "oh": "0", "o": "0", **_ONE_TO_NINE},
|
|
93
|
+
# Full number-word vocabulary used for contextual detection.
|
|
94
|
+
# Reuses _TIME_WORDS (1-50) to avoid repeating every word,
|
|
95
|
+
# then appends zero/aliases and the remaining tens (60-90) and scale words.
|
|
96
|
+
number_words=[
|
|
97
|
+
"zero",
|
|
98
|
+
"oh",
|
|
99
|
+
"o",
|
|
100
|
+
*_TIME_WORDS, # one … fifty
|
|
101
|
+
"sixty",
|
|
102
|
+
"seventy",
|
|
103
|
+
"eighty",
|
|
104
|
+
"ninety",
|
|
105
|
+
"hundred",
|
|
106
|
+
"thousand",
|
|
107
|
+
"million",
|
|
108
|
+
"billion",
|
|
109
|
+
"trillion",
|
|
110
|
+
],
|
|
111
|
+
plus_word="plus",
|
|
112
|
+
ordinal_suffixes=["st", "nd", "rd", "th"],
|
|
113
|
+
am_word="am",
|
|
114
|
+
pm_word="pm",
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@register_language
|
|
119
|
+
class EnglishOperators(LanguageOperators):
|
|
120
|
+
def __init__(self):
|
|
121
|
+
super().__init__(ENGLISH_CONFIG)
|
|
122
|
+
self._number_normalizer = EnglishNumberNormalizer()
|
|
123
|
+
|
|
124
|
+
def expand_contractions(self, text: str) -> str:
|
|
125
|
+
text = re.sub(
|
|
126
|
+
r"\bhe ain't gonna\b", "he is not going to", text, flags=re.IGNORECASE
|
|
127
|
+
)
|
|
128
|
+
text = re.sub(
|
|
129
|
+
r"\bshe ain't gonna\b", "she is not going to", text, flags=re.IGNORECASE
|
|
130
|
+
)
|
|
131
|
+
text = re.sub(
|
|
132
|
+
r"\bit ain't gonna\b", "it is not going to", text, flags=re.IGNORECASE
|
|
133
|
+
)
|
|
134
|
+
text = re.sub(r"\bi'ma\b", "i am going to", text, flags=re.IGNORECASE)
|
|
135
|
+
text = cast(str, contractions.fix(text, slang=True))
|
|
136
|
+
return text
|
|
137
|
+
|
|
138
|
+
def get_compound_minutes(self) -> dict[str, str]:
|
|
139
|
+
"""Build compound minute expressions: 'twenty-one'/'twenty one' → '21', … 'fifty-nine' → '59'.
|
|
140
|
+
|
|
141
|
+
Uses _ONE_TO_NINE for the ones component so no word is listed twice.
|
|
142
|
+
Both hyphenated and spaced forms are produced (English uses both).
|
|
143
|
+
"""
|
|
144
|
+
result: dict[str, str] = {}
|
|
145
|
+
for tens_word, tens_val in [
|
|
146
|
+
("twenty", 20),
|
|
147
|
+
("thirty", 30),
|
|
148
|
+
("forty", 40),
|
|
149
|
+
("fifty", 50),
|
|
150
|
+
]:
|
|
151
|
+
for ones_word, ones_str in _ONE_TO_NINE.items():
|
|
152
|
+
value = str(tens_val + int(ones_str))
|
|
153
|
+
result[f"{tens_word}-{ones_word}"] = value
|
|
154
|
+
result[f"{tens_word} {ones_word}"] = value
|
|
155
|
+
return result
|
|
156
|
+
|
|
157
|
+
def normalize_numeric_time_formats(self, text: str) -> str:
|
|
158
|
+
def _format_dot_time(match: re.Match) -> str:
|
|
159
|
+
hour = str(int(match.group(1)))
|
|
160
|
+
minute = match.group(2)
|
|
161
|
+
ampm = match.group(3).lower()
|
|
162
|
+
return f"{hour}:{minute} {ampm}m"
|
|
163
|
+
|
|
164
|
+
text = re.sub(
|
|
165
|
+
r"\b(\d{1,2})\.(\d{2})\s*([ap])\.?m\.?\b",
|
|
166
|
+
_format_dot_time,
|
|
167
|
+
text,
|
|
168
|
+
flags=re.IGNORECASE,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
def _format_colon_time(match: re.Match) -> str:
|
|
172
|
+
hour = str(int(match.group(1)))
|
|
173
|
+
minute = match.group(2)
|
|
174
|
+
ampm = match.group(3).lower()
|
|
175
|
+
return f"{hour}:{minute} {ampm}m"
|
|
176
|
+
|
|
177
|
+
text = re.sub(
|
|
178
|
+
r"\b(\d{1,2}):(\d{2})([ap])m\b",
|
|
179
|
+
_format_colon_time,
|
|
180
|
+
text,
|
|
181
|
+
flags=re.IGNORECASE,
|
|
182
|
+
)
|
|
183
|
+
return text
|
|
184
|
+
|
|
185
|
+
def fix_one_word_in_numeric_contexts(self, text: str) -> str:
|
|
186
|
+
text = re.sub(r"(\d+)\s+one\s+one\b", r"\1 1 1", text)
|
|
187
|
+
text = re.sub(r"\bone\s+one\s+(\d)", r"1 1 \1", text)
|
|
188
|
+
text = re.sub(r"(\d+)\s+one\s+(\d)", r"\1 1 \2", text)
|
|
189
|
+
text = re.sub(r"(\d+)\s+one\b", r"\1 1", text)
|
|
190
|
+
text = re.sub(r"\b(\d+)one\b", r"\1 1", text)
|
|
191
|
+
text = re.sub(r"\bone\s+(\d)", r"1 \1", text)
|
|
192
|
+
text = re.sub(r"^one\s+(?=[a-z])", "1 ", text)
|
|
193
|
+
return text
|
|
194
|
+
|
|
195
|
+
def get_word_replacements(self) -> dict[str, str]:
|
|
196
|
+
return ENGLISH_REPLACEMENTS
|
|
197
|
+
|
|
198
|
+
def expand_written_numbers(self, text: str) -> str:
|
|
199
|
+
return self._number_normalizer(text)
|