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.
Files changed (72) hide show
  1. gladia_normalization-0.1.0.dist-info/METADATA +210 -0
  2. gladia_normalization-0.1.0.dist-info/RECORD +72 -0
  3. gladia_normalization-0.1.0.dist-info/WHEEL +4 -0
  4. gladia_normalization-0.1.0.dist-info/licenses/LICENSE +21 -0
  5. normalization/__init__.py +4 -0
  6. normalization/constants/__init__.py +3 -0
  7. normalization/constants/protectors.py +24 -0
  8. normalization/languages/__init__.py +7 -0
  9. normalization/languages/base/__init__.py +7 -0
  10. normalization/languages/base/language_config.py +83 -0
  11. normalization/languages/base/language_operator.py +65 -0
  12. normalization/languages/english/__init__.py +7 -0
  13. normalization/languages/english/number_normalizer.py +433 -0
  14. normalization/languages/english/operators.py +199 -0
  15. normalization/languages/english/replacements.py +1774 -0
  16. normalization/languages/english/sentence_replacements.py +3 -0
  17. normalization/languages/french/__init__.py +7 -0
  18. normalization/languages/french/operators.py +38 -0
  19. normalization/languages/french/replacements.py +1 -0
  20. normalization/languages/registry.py +17 -0
  21. normalization/pipeline/__init__.py +0 -0
  22. normalization/pipeline/base.py +120 -0
  23. normalization/pipeline/loader.py +70 -0
  24. normalization/pipeline/replacer.py +38 -0
  25. normalization/presets/gladia-3.yaml +119 -0
  26. normalization/steps/__init__.py +4 -0
  27. normalization/steps/base/__init__.py +6 -0
  28. normalization/steps/base/protect_step.py +24 -0
  29. normalization/steps/base/restore_step.py +23 -0
  30. normalization/steps/base/text_step.py +11 -0
  31. normalization/steps/base/word_step.py +11 -0
  32. normalization/steps/registry.py +26 -0
  33. normalization/steps/text/__init__.py +79 -0
  34. normalization/steps/text/apply_sentence_level_replacements.py +29 -0
  35. normalization/steps/text/casefold_text.py +13 -0
  36. normalization/steps/text/convert_comparison_operators_to_words.py +26 -0
  37. normalization/steps/text/convert_decimal_periods_to_decimal_word.py +30 -0
  38. normalization/steps/text/convert_degree_symbols_to_words.py +26 -0
  39. normalization/steps/text/convert_digit_word_sequences_to_digits.py +41 -0
  40. normalization/steps/text/convert_dots_to_words_in_technical_contexts.py +33 -0
  41. normalization/steps/text/convert_oclock_to_numeric_time.py +34 -0
  42. normalization/steps/text/convert_roman_numerals_to_digits.py +41 -0
  43. normalization/steps/text/convert_word_based_time_patterns.py +70 -0
  44. normalization/steps/text/expand_alphanumeric_codes.py +64 -0
  45. normalization/steps/text/expand_contractions.py +16 -0
  46. normalization/steps/text/expand_written_numbers_to_digits.py +13 -0
  47. normalization/steps/text/expand_www_abbreviation.py +15 -0
  48. normalization/steps/text/fix_ampm_letter_spacing.py +25 -0
  49. normalization/steps/text/fix_dot_adjacent_number_words.py +37 -0
  50. normalization/steps/text/fix_one_word_in_numeric_contexts.py +16 -0
  51. normalization/steps/text/fix_version_number_v_prefix.py +15 -0
  52. normalization/steps/text/format_time_patterns_with_ampm.py +53 -0
  53. normalization/steps/text/normalize_numeric_time_formats.py +16 -0
  54. normalization/steps/text/normalize_punctuation_between_number_words.py +30 -0
  55. normalization/steps/text/placeholders.py +367 -0
  56. normalization/steps/text/protect_plus_word_before_digit_words.py +37 -0
  57. normalization/steps/text/remove_acronym_periods.py +17 -0
  58. normalization/steps/text/remove_diacritics.py +40 -0
  59. normalization/steps/text/remove_filler_words.py +19 -0
  60. normalization/steps/text/remove_hash_before_numbers.py +15 -0
  61. normalization/steps/text/remove_non_numeric_trailing_dots.py +15 -0
  62. normalization/steps/text/remove_spaces_between_adjacent_digits.py +54 -0
  63. normalization/steps/text/remove_standalone_currency_symbols.py +35 -0
  64. normalization/steps/text/remove_symbols.py +24 -0
  65. normalization/steps/text/remove_thousand_separators.py +24 -0
  66. normalization/steps/text/remove_trailing_apostrophe_space.py +15 -0
  67. normalization/steps/text/remove_trailing_dot_word_from_emails.py +22 -0
  68. normalization/steps/text/remove_trailing_period.py +15 -0
  69. normalization/steps/text/remove_zero_minutes_from_time.py +30 -0
  70. normalization/steps/text/replace_currency.py +31 -0
  71. normalization/steps/word/__init__.py +3 -0
  72. normalization/steps/word/apply_word_replacements.py +33 -0
@@ -0,0 +1,367 @@
1
+ """All protect/restore placeholder step pairs.
2
+
3
+ Every protect step in Stage 1 has a matching restore step in Stage 3.
4
+ They live together in this file to keep the dependency explicit.
5
+ """
6
+
7
+ import re
8
+
9
+ from normalization.constants.protectors import ProtectPlaceholder
10
+ from normalization.languages.base import LanguageOperators
11
+ from normalization.steps.base import ProtectStep, RestoreStep, TextStep
12
+ from normalization.steps.registry import register_step
13
+
14
+ # ---------------------------------------------------------------------------
15
+ # Email symbols
16
+ # ---------------------------------------------------------------------------
17
+
18
+ _RE_EMAIL = re.compile(r"[a-zA-Z0-9_.+\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9.\-]+")
19
+
20
+
21
+ @register_step
22
+ class ProtectEmailSymbolsStep(TextStep):
23
+ """Replace @ and . inside email addresses with placeholders.
24
+
25
+ A single email match requires protecting two different symbols (@ → EMAIL_AT, . → EMAIL_DOT) in one pass. ProtectStep handles exactly one placeholder per substitution.
26
+ """
27
+
28
+ name = "protect_email_symbols"
29
+
30
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
31
+ def _replace_email(match: re.Match) -> str:
32
+ email = match.group(0)
33
+ email = email.replace("@", f" {ProtectPlaceholder.EMAIL_AT.value} ")
34
+ email = email.replace(".", f" {ProtectPlaceholder.EMAIL_DOT.value} ")
35
+ return email
36
+
37
+ return _RE_EMAIL.sub(_replace_email, text)
38
+
39
+
40
+ @register_step
41
+ class RestoreEmailAtSymbolWithWordStep(TextStep):
42
+ """Restore XATX placeholder with the language-specific 'at' word.
43
+
44
+ When no word is configured for '@', restores the original '@' character
45
+ so that placeholders never leak into the final output.
46
+ """
47
+
48
+ name = "restore_email_at_symbol_with_word"
49
+
50
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
51
+ at_word = operators.config.symbols_to_words.get("@")
52
+ if at_word is None:
53
+ return text
54
+ text = re.sub(
55
+ rf"\s*{re.escape(ProtectPlaceholder.EMAIL_AT.value)}\s*",
56
+ f" {at_word} ",
57
+ text,
58
+ flags=re.IGNORECASE,
59
+ )
60
+ return text
61
+
62
+
63
+ @register_step
64
+ class RestoreEmailDotSymbolWithWordStep(TextStep):
65
+ """Restore XDOTX placeholder with the language-specific 'dot' word.
66
+
67
+ When no word is configured for '.', restores the original '.' character
68
+ so that placeholders never leak into the final output.
69
+ """
70
+
71
+ name = "restore_email_dot_symbol_with_word"
72
+
73
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
74
+ dot_word = operators.config.symbols_to_words.get(".")
75
+ if dot_word is None:
76
+ return text
77
+ text = re.sub(
78
+ rf"\s*{re.escape(ProtectPlaceholder.EMAIL_DOT.value)}\s*",
79
+ f" {dot_word} ",
80
+ text,
81
+ flags=re.IGNORECASE,
82
+ )
83
+ return text
84
+
85
+
86
+ # ---------------------------------------------------------------------------
87
+ # Phone plus symbol
88
+ # ---------------------------------------------------------------------------
89
+
90
+ _RE_PLUS_DIGIT = re.compile(r"\+(?=\d)")
91
+ _RE_PLUS_SPACE_DIGIT = re.compile(r"\+\s+(\d)")
92
+
93
+
94
+ @register_step
95
+ class ProtectPhonePlusSymbolStep(TextStep):
96
+ """Replace "+" when it appears right before a digit (like in phone numbers) with a special placeholder.
97
+
98
+ The regex pattern "\\+(?=\\d)" means:
99
+ - Match a literal "+" character
100
+ - But only if it is immediately followed by a number (0-9)
101
+ - The digit is NOT included in the match (it stays untouched)
102
+
103
+ For example:
104
+ "+123456" → the "+" is replaced, but "123456" stays the same.
105
+
106
+ Note: The pattern uses a lookahead (?=\\d), which checks what comes next
107
+ without capturing it as part of the match.
108
+ """
109
+
110
+ name = "protect_phone_plus_symbol"
111
+
112
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
113
+ return _RE_PLUS_DIGIT.sub(ProtectPlaceholder.PHONE_PLUS.value, text)
114
+
115
+
116
+ @register_step
117
+ class RestorePhonePlusSymbolStep(TextStep):
118
+ """Restore XPLUSX placeholder back to + and collapse trailing space.
119
+
120
+ Beyond the plain replace, it must also collapse "+ <digit>" → "+<digit>" (e.g. after casefold splits the token). RestoreStep only does plain string replacement.
121
+ """
122
+
123
+ name = "restore_phone_plus_symbol"
124
+
125
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
126
+ text = text.replace(ProtectPlaceholder.PHONE_PLUS.value.lower(), "+")
127
+ text = text.replace(ProtectPlaceholder.PHONE_PLUS.value, "+")
128
+ text = _RE_PLUS_SPACE_DIGIT.sub(r"+\1", text)
129
+ return text
130
+
131
+
132
+ # ---------------------------------------------------------------------------
133
+ # Time colon
134
+ # ---------------------------------------------------------------------------
135
+
136
+ _RE_TIME_COLON = re.compile(r"(\d{1,2}):(\d{2})")
137
+
138
+
139
+ @register_step
140
+ class ProtectTimeColonStep(ProtectStep):
141
+ """Protect the colon used in time expressions like HH:MM.
142
+ Matches times written with one or two digits for the hour and exactly two digits for the minutes (e.g., "9:30", "12:05").
143
+ The colon between them is temporarily replaced with a placeholder so it is not modified or removed by later text-processing steps.
144
+
145
+ Example:
146
+ "9:30" → "9§30" (colon replaced with placeholder)
147
+ """
148
+
149
+ name = "protect_time_colon"
150
+ placeholder = ProtectPlaceholder.TIME_COLON
151
+
152
+ def _pattern(self, operators: LanguageOperators) -> re.Pattern:
153
+ return _RE_TIME_COLON
154
+
155
+
156
+ @register_step
157
+ class RestoreTimeColonStep(RestoreStep):
158
+ """Restore § placeholder back to colon."""
159
+
160
+ name = "restore_time_colon"
161
+ placeholder = ProtectPlaceholder.TIME_COLON
162
+
163
+ def _replacement(self, operators: LanguageOperators) -> str:
164
+ return ":"
165
+
166
+
167
+ # ---------------------------------------------------------------------------
168
+ # Unit decimal (9.8 m/s -> 9‡8 m/s)
169
+ # ---------------------------------------------------------------------------
170
+
171
+ _RE_DECIMAL_IN_UNIT = re.compile(r"(\d+)\.(\d+)(?=\s*(?:km|m)/[hs]\b)")
172
+
173
+
174
+ @register_step
175
+ class ProtectUnitDecimalStep(ProtectStep):
176
+ """Replace . in decimal unit expressions with ‡ placeholder (e.g. 9.8 m/s → 9‡8 m/s)."""
177
+
178
+ name = "protect_unit_decimal"
179
+ placeholder = ProtectPlaceholder.UNIT_DECIMAL
180
+
181
+ def _pattern(self, operators: LanguageOperators) -> re.Pattern:
182
+ return _RE_DECIMAL_IN_UNIT
183
+
184
+
185
+ # ---------------------------------------------------------------------------
186
+ # Unit slash (km/h, m/s -> km†h, m†s)
187
+ # ---------------------------------------------------------------------------
188
+
189
+ _RE_UNIT_SLASH = re.compile(r"\b(km|m)/([hs])\b")
190
+
191
+
192
+ @register_step
193
+ class ProtectUnitSlashStep(ProtectStep):
194
+ """Replace / in unit expressions with † placeholder (e.g. km/h → km†h)."""
195
+
196
+ name = "protect_unit_slash"
197
+ placeholder = ProtectPlaceholder.UNIT_SLASH
198
+
199
+ def _pattern(self, operators: LanguageOperators) -> re.Pattern:
200
+ return _RE_UNIT_SLASH
201
+
202
+
203
+ @register_step
204
+ class RestoreUnitSlashStep(RestoreStep):
205
+ """Restore † placeholder back to /."""
206
+
207
+ name = "restore_unit_slash"
208
+ placeholder = ProtectPlaceholder.UNIT_SLASH
209
+
210
+ def _replacement(self, operators: LanguageOperators) -> str:
211
+ return "/"
212
+
213
+
214
+ @register_step
215
+ class RestoreUnitDecimalAsWordStep(RestoreStep):
216
+ """Restore ‡ placeholder with the language-specific decimal word."""
217
+
218
+ name = "restore_unit_decimal_as_word"
219
+ placeholder = ProtectPlaceholder.UNIT_DECIMAL
220
+
221
+ def _replacement(self, operators: LanguageOperators) -> str:
222
+ if operators.config.decimal_word is None:
223
+ return " "
224
+ return f" {operators.config.decimal_word} "
225
+
226
+
227
+ # ---------------------------------------------------------------------------
228
+ # Number separator commas (1, 2, 3 -> 1 ¤ 2 ¤ 3)
229
+ # ---------------------------------------------------------------------------
230
+
231
+ _RE_COMMA_SEP = re.compile(r"(\d+),\s+(?=\d)")
232
+ _RE_DOT_SEP = re.compile(r"(\d+)\.\s+(?=\d)")
233
+
234
+
235
+ @register_step
236
+ class ProtectNumberSeparatorCommasStep(TextStep):
237
+ """Replace comma/dot+space between digits with ¤ marker. (1, 2, 3 -> 1 ¤ 2 ¤ 3)
238
+
239
+ Two independent patterns (comma-separated and
240
+ dot-separated) both collapse to the same marker. ProtectStep handles a
241
+ single pattern mapping to a single placeholder.
242
+ """
243
+
244
+ name = "protect_number_separator_commas"
245
+
246
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
247
+ text = _RE_COMMA_SEP.sub(
248
+ rf"\1 {ProtectPlaceholder.NUMBER_SEPARATOR.value} ",
249
+ text,
250
+ )
251
+ text = _RE_DOT_SEP.sub(
252
+ rf"\1 {ProtectPlaceholder.NUMBER_SEPARATOR.value} ",
253
+ text,
254
+ )
255
+ return text
256
+
257
+
258
+ @register_step
259
+ class RemoveNumberSeparatorMarkersStep(TextStep):
260
+ """Strip ¤ markers from the text.
261
+
262
+ Uses TextStep directly: the marker is deleted entirely (not restored to a
263
+ character or word). RestoreStep replaces with a non-empty string; here the
264
+ surrounding whitespace must also be collapsed to nothing.
265
+ """
266
+
267
+ name = "remove_number_separator_markers"
268
+
269
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
270
+ return re.sub(
271
+ rf"\s*{re.escape(ProtectPlaceholder.NUMBER_SEPARATOR.value)}\s*",
272
+ "",
273
+ text,
274
+ )
275
+
276
+
277
+ # ---------------------------------------------------------------------------
278
+ # Decimal separator (moved from protect_decimal_separator.py)
279
+ # ---------------------------------------------------------------------------
280
+
281
+
282
+ @register_step
283
+ class ProtectDecimalSeparatorStep(ProtectStep):
284
+ """Protect the decimal separator from being removed by RemoveSymbolsStep."""
285
+
286
+ name = "protect_decimal_separator"
287
+ placeholder = ProtectPlaceholder.DECIMAL_SEPARATOR
288
+
289
+ def _pattern(self, operators: LanguageOperators) -> re.Pattern:
290
+ safe_separator = re.escape(operators.config.decimal_separator)
291
+ return re.compile(rf"(\d+){safe_separator}(\d+)")
292
+
293
+
294
+ @register_step
295
+ class RestoreDecimalSeparatorWithWordStep(RestoreStep):
296
+ """Restore XDECIMALX placeholder with the language-specific decimal word."""
297
+
298
+ name = "restore_decimal_separator_with_word"
299
+ placeholder = ProtectPlaceholder.DECIMAL_SEPARATOR
300
+
301
+ def _replacement(self, operators: LanguageOperators) -> str:
302
+ if operators.config.decimal_word is None:
303
+ return " "
304
+ return f" {operators.config.decimal_word} "
305
+
306
+
307
+ # ---------------------------------------------------------------------------
308
+ # Hyphenated letter spelling (b-o-b -> bxltrx oxltrx bxltrx)
309
+ # ---------------------------------------------------------------------------
310
+
311
+
312
+ @register_step
313
+ class ProtectHyphenatedLetterSpellingStep(TextStep):
314
+ """Mark single-letter-hyphen sequences to prevent false conversions.
315
+
316
+ Uses TextStep directly: the replacement is a per-match function that splits on '-' and suffixes each individual letter. Example: "b-o-b" → "bxltrx oxltrx bxltrx".
317
+ """
318
+
319
+ name = "protect_hyphenated_letter_spelling"
320
+
321
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
322
+ def _add_marker(match: re.Match) -> str:
323
+ letters = match.group(0).split("-")
324
+ suffix = ProtectPlaceholder.SPELLING_SUFFIX.value
325
+ return " ".join(f"{letter}{suffix}" for letter in letters)
326
+
327
+ return re.sub(r"\b([a-zA-Z](?:-[a-zA-Z])+)\b", _add_marker, text)
328
+
329
+
330
+ # ---------------------------------------------------------------------------
331
+ # Space-separated letter spelling (j o h a n n -> jxltrx oxltrx ...)
332
+ # ---------------------------------------------------------------------------
333
+
334
+
335
+ @register_step
336
+ class ProtectSpaceSeparatedLetterSpellingStep(TextStep):
337
+ """Mark sequences of 3+ single letters to prevent false conversions.
338
+
339
+ Uses TextStep directly: same reason as ProtectHyphenatedLetterSpellingStep —
340
+ the replacement is a per-match function that suffixes each individual letter.
341
+ """
342
+
343
+ name = "protect_space_separated_letter_spelling"
344
+
345
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
346
+ def _add_marker(match: re.Match) -> str:
347
+ letters = match.group(0).split()
348
+ suffix = ProtectPlaceholder.SPELLING_SUFFIX.value
349
+ return " ".join(f"{letter}{suffix}" for letter in letters)
350
+
351
+ return re.sub(r"\b([a-zA-Z](?:\s+[a-zA-Z]){2,})\b", _add_marker, text)
352
+
353
+
354
+ @register_step
355
+ class RemoveLetterSpellingMarkersStep(TextStep):
356
+ """Strip xltrx suffix markers from letters.
357
+
358
+ Uses TextStep directly: the suffix is a token-embedded string (not a
359
+ standalone token), so removal requires a regex word-boundary match.
360
+ Example: "bxltrx oxltrx bxltrx" → "b o b"
361
+ """
362
+
363
+ name = "remove_letter_spelling_markers"
364
+
365
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
366
+ suffix = ProtectPlaceholder.SPELLING_SUFFIX.value
367
+ return re.sub(rf"\b([a-z]){re.escape(suffix)}\b", r"\1", text)
@@ -0,0 +1,37 @@
1
+ import re
2
+
3
+ from normalization.constants.protectors import ProtectPlaceholder
4
+ from normalization.languages.base import LanguageOperators
5
+ from normalization.steps.base import TextStep
6
+ from normalization.steps.registry import register_step
7
+
8
+
9
+ @register_step
10
+ class ProtectPlusWordBeforeDigitWordsStep(TextStep):
11
+ """Convert the plus word to XPLUSX before digit words (phone number context).
12
+
13
+ Reads operators.config.plus_word and operators.config.digit_words.
14
+ No-op when plus_word is None or digit_words is empty/None.
15
+ """
16
+
17
+ name = "protect_plus_word_before_digit_words"
18
+
19
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
20
+ plus_word = operators.config.plus_word
21
+ if plus_word is None:
22
+ return text
23
+
24
+ digit_words = operators.config.digit_words
25
+ if not digit_words:
26
+ return text
27
+
28
+ placeholder = ProtectPlaceholder.PHONE_PLUS.value
29
+ sorted_words = sorted(digit_words, key=lambda w: len(w), reverse=True)
30
+ word_alts = "|".join(re.escape(w) for w in sorted_words)
31
+ lookahead = rf"(?=(?:{word_alts}|\d))"
32
+ return re.sub(
33
+ rf"\b{re.escape(plus_word)}\s+{lookahead}",
34
+ f"{placeholder} ",
35
+ text,
36
+ flags=re.IGNORECASE,
37
+ )
@@ -0,0 +1,17 @@
1
+ import re
2
+
3
+ from normalization.languages.base import LanguageOperators
4
+ from normalization.steps.base import TextStep
5
+ from normalization.steps.registry import register_step
6
+
7
+
8
+ @register_step
9
+ class RemoveAcronymPeriodsStep(TextStep):
10
+ """Remove periods from acronyms (U.S.A. -> USA, U.S. -> US)."""
11
+
12
+ name = "remove_acronym_periods"
13
+
14
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
15
+ text = re.sub(r"\b([A-Z])\.([A-Z])\.([A-Z])\.", r"\1\2\3", text)
16
+ text = re.sub(r"\b([A-Z])\.([A-Z])\.", r"\1\2", text)
17
+ return text
@@ -0,0 +1,40 @@
1
+ import unicodedata
2
+
3
+ from normalization.languages.base import LanguageOperators
4
+ from normalization.steps.base import TextStep
5
+ from normalization.steps.registry import register_step
6
+
7
+ _ADDITIONAL_DIACRITICS = {
8
+ "œ": "oe",
9
+ "Œ": "OE",
10
+ "ø": "o",
11
+ "Ø": "O",
12
+ "æ": "ae",
13
+ "Æ": "AE",
14
+ "ß": "ss",
15
+ "ẞ": "SS",
16
+ "đ": "d",
17
+ "Đ": "D",
18
+ "ð": "d",
19
+ "Ð": "D",
20
+ "þ": "th",
21
+ "Þ": "th",
22
+ "ł": "l",
23
+ "Ł": "L",
24
+ }
25
+
26
+
27
+ @register_step
28
+ class RemoveDiacriticsStep(TextStep):
29
+ """Normalize text by removing diacritics and converting special accented letters to their ASCII equivalents. (é -> e, ê -> e, etc.)"""
30
+
31
+ name = "remove_diacritics"
32
+
33
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
34
+ res = ""
35
+ for c in unicodedata.normalize("NFKD", text):
36
+ if c in _ADDITIONAL_DIACRITICS:
37
+ res += _ADDITIONAL_DIACRITICS[c]
38
+ elif unicodedata.category(c) != "Mn":
39
+ res += c
40
+ return res
@@ -0,0 +1,19 @@
1
+ import re
2
+
3
+ from normalization.languages.base import LanguageOperators
4
+ from normalization.steps.base import TextStep
5
+ from normalization.steps.registry import register_step
6
+
7
+
8
+ @register_step
9
+ class RemoveFillerWordsStep(TextStep):
10
+ """Remove filler words defined in the language config (um, uh, euh, etc.)."""
11
+
12
+ name = "remove_filler_words"
13
+
14
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
15
+ fillers = operators.config.filler_words
16
+ if not fillers:
17
+ return text
18
+ pattern = r"\b(?:" + "|".join(re.escape(f) for f in fillers) + r")\b[,.]?\s*"
19
+ return re.sub(pattern, "", text, flags=re.IGNORECASE)
@@ -0,0 +1,15 @@
1
+ import re
2
+
3
+ from normalization.languages.base import LanguageOperators
4
+ from normalization.steps.base import TextStep
5
+ from normalization.steps.registry import register_step
6
+
7
+
8
+ @register_step
9
+ class RemoveHashBeforeNumbersStep(TextStep):
10
+ """Remove # symbol before numbers (#1 -> 1)."""
11
+
12
+ name = "remove_hash_before_numbers"
13
+
14
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
15
+ return re.sub(r"#(\d+)", r"\1", text)
@@ -0,0 +1,15 @@
1
+ import re
2
+
3
+ from normalization.languages.base import LanguageOperators
4
+ from normalization.steps.base import TextStep
5
+ from normalization.steps.registry import register_step
6
+
7
+
8
+ @register_step
9
+ class RemoveNonNumericTrailingDotsStep(TextStep):
10
+ """Remove dots that are not between digits (.X -> ' X', trailing .)."""
11
+
12
+ name = "remove_non_numeric_trailing_dots"
13
+
14
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
15
+ return re.sub(r"\.([^0-9]|$)", r" \1", text)
@@ -0,0 +1,54 @@
1
+ import re
2
+
3
+ from normalization.constants.protectors import ProtectPlaceholder
4
+ from normalization.languages.base import LanguageOperators
5
+ from normalization.steps.base import TextStep
6
+ from normalization.steps.registry import register_step
7
+
8
+ _RE_SPACES_BETWEEN_DIGITS = re.compile(r"(\d)\s+(?=\d+(?![a-z]))")
9
+
10
+
11
+ @register_step
12
+ class RemoveSpacesBetweenAdjacentDigitsStep(TextStep):
13
+ """Collapse spaces between adjacent digits (1 2 3 -> 123).
14
+
15
+ Preserves spaces around 'point' (decimal word) and before ordinals.
16
+ Handles ¤ markers by processing segments separately.
17
+ """
18
+
19
+ name = "remove_spaces_between_adjacent_digits"
20
+
21
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
22
+ decimal_word = operators.config.decimal_word
23
+ sep = ProtectPlaceholder.NUMBER_SEPARATOR.value
24
+
25
+ if decimal_word is not None:
26
+ text = text.replace(f" {decimal_word} ", "XDECWORDX")
27
+
28
+ ordinals: list[str] = []
29
+ ordinal_suffixes = operators.config.ordinal_suffixes
30
+
31
+ if ordinal_suffixes:
32
+ suffix_alt = "|".join(re.escape(s) for s in ordinal_suffixes)
33
+ ordinal_re = re.compile(rf"(\s)(\d+)({suffix_alt})\b")
34
+
35
+ def _protect_ordinal(match: re.Match) -> str:
36
+ ordinals.append(match.group(0))
37
+ return f" XORDINAL{len(ordinals) - 1}X"
38
+
39
+ text = ordinal_re.sub(_protect_ordinal, text)
40
+
41
+ if sep not in text:
42
+ text = _RE_SPACES_BETWEEN_DIGITS.sub(r"\1", text)
43
+ else:
44
+ parts = text.split(sep)
45
+ parts = [_RE_SPACES_BETWEEN_DIGITS.sub(r"\1", p) for p in parts]
46
+ text = sep.join(parts)
47
+
48
+ for i, ordinal in enumerate(ordinals):
49
+ text = text.replace(f"XORDINAL{i}X", ordinal.strip())
50
+
51
+ if decimal_word is not None:
52
+ text = text.replace("XDECWORDX", f" {decimal_word} ")
53
+
54
+ return text
@@ -0,0 +1,35 @@
1
+ import re
2
+
3
+ from normalization.languages.base import LanguageOperators
4
+ from normalization.steps.base import TextStep
5
+ from normalization.steps.registry import register_step
6
+
7
+
8
+ def _make_standalone_patterns(
9
+ symbols: frozenset[str],
10
+ ) -> tuple[re.Pattern, re.Pattern, re.Pattern, re.Pattern]:
11
+ char_class = "[" + re.escape("".join(symbols)) + "]"
12
+ between = re.compile(rf"([^0-9]){char_class}([^0-9])")
13
+ start = re.compile(rf"^{char_class}([^0-9])")
14
+ end = re.compile(rf"([^0-9]){char_class}$")
15
+ standalone = re.compile(rf"^{char_class}$")
16
+ return between, start, end, standalone
17
+
18
+
19
+ @register_step
20
+ class RemoveStandaloneCurrencySymbolsStep(TextStep):
21
+ """Remove currency symbols that are not adjacent to numbers."""
22
+
23
+ name = "remove_standalone_currency_symbols"
24
+
25
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
26
+ symbols = frozenset(operators.config.currency_symbol_to_word.keys())
27
+ if not symbols:
28
+ return text
29
+
30
+ between, start, end, standalone = _make_standalone_patterns(symbols)
31
+ text = between.sub(r"\1 \2", text)
32
+ text = start.sub(r" \1", text)
33
+ text = end.sub(r"\1 ", text)
34
+ text = standalone.sub(" ", text)
35
+ return text
@@ -0,0 +1,24 @@
1
+ import unicodedata
2
+
3
+ from normalization.constants.protectors import ProtectPlaceholder
4
+ from normalization.languages.base import LanguageOperators
5
+ from normalization.steps.base import TextStep
6
+ from normalization.steps.registry import register_step
7
+
8
+ _KEEP_CHARS = "".join(p.value for p in ProtectPlaceholder) + "€$£¥¢%"
9
+
10
+
11
+ @register_step
12
+ class RemoveSymbolsStep(TextStep):
13
+ """Replace markers, symbols, and punctuation with spaces.
14
+
15
+ Preserves letters, digits, and all placeholder characters.
16
+ """
17
+
18
+ name = "remove_symbols"
19
+
20
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
21
+ return "".join(
22
+ c if c in _KEEP_CHARS else " " if unicodedata.category(c)[0] in "MSP" else c
23
+ for c in unicodedata.normalize("NFKC", text)
24
+ )
@@ -0,0 +1,24 @@
1
+ import re
2
+
3
+ from normalization.languages.base import LanguageOperators
4
+ from normalization.steps.base import TextStep
5
+ from normalization.steps.registry import register_step
6
+
7
+
8
+ @register_step
9
+ class RemoveThousandSeparatorsStep(TextStep):
10
+ """Remove thousand separators based on the language config.
11
+
12
+ English uses comma (1,234 -> 1234), European languages use period (1.234 -> 1234).
13
+ """
14
+
15
+ name = "remove_thousand_separators"
16
+
17
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
18
+ sep = re.escape(operators.config.thousand_separator)
19
+ dec = re.escape(operators.config.decimal_separator)
20
+ full = re.compile(rf"(\d){sep}(\d{{3}}(?:{sep}\d{{3}})*(?:{dec}\d+)?)\b")
21
+ simple = re.compile(rf"(\d){sep}(\d{{3}})\b")
22
+ text = full.sub(r"\1\2", text)
23
+ text = simple.sub(r"\1\2", text)
24
+ return text
@@ -0,0 +1,15 @@
1
+ import re
2
+
3
+ from normalization.languages.base import LanguageOperators
4
+ from normalization.steps.base import TextStep
5
+ from normalization.steps.registry import register_step
6
+
7
+
8
+ @register_step
9
+ class RemoveTrailingApostropheSpaceStep(TextStep):
10
+ """Remove space before apostrophe (' s -> 's)."""
11
+
12
+ name = "remove_trailing_apostrophe_space"
13
+
14
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
15
+ return re.sub(r"\s+'", "'", text)