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,26 @@
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
+ _RE_GREATER_THAN = re.compile(r"(\d+)\s*>\s*(\d+)")
8
+ _RE_LESS_THAN = re.compile(r"(\d+)\s*<\s*(\d+)")
9
+ _RE_EQUALS = re.compile(r"(\w+)\s*=\s*(\d+)")
10
+
11
+
12
+ @register_step
13
+ class ConvertComparisonOperatorsToWordsStep(TextStep):
14
+ """Convert >, <, = to language-specific words in numeric contexts using language config from operators."""
15
+
16
+ name = "convert_comparison_operators_to_words"
17
+
18
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
19
+ stw = operators.config.symbols_to_words
20
+ if ">" in stw:
21
+ text = _RE_GREATER_THAN.sub(rf"\1 {stw['>']} \2", text)
22
+ if "<" in stw:
23
+ text = _RE_LESS_THAN.sub(rf"\1 {stw['<']} \2", text)
24
+ if "=" in stw:
25
+ text = _RE_EQUALS.sub(rf"\1 {stw['=']} \2", text)
26
+ return text
@@ -0,0 +1,30 @@
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 ConvertDecimalPeriodsToDecimalWordStep(TextStep):
10
+ """Convert remaining decimal periods to the language decimal word, defined in language config from operators.
11
+
12
+ '10.5' -> '10 point 5' (English).
13
+ Avoids patterns already converted to 'dot' (IPs, versions).
14
+ """
15
+
16
+ name = "convert_decimal_periods_to_decimal_word"
17
+
18
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
19
+ dot_word = operators.config.symbols_to_words.get(".")
20
+ if dot_word is None:
21
+ return text
22
+ if operators.config.decimal_word is None:
23
+ return text
24
+ decimal_word = operators.config.decimal_word
25
+ text = re.sub(
26
+ rf"(?<!\d\s{re.escape(dot_word)}\s)(\d+)\.(\d+)(?!\s+{re.escape(dot_word)}\s+\d)",
27
+ rf"\1 {decimal_word} \2",
28
+ text,
29
+ )
30
+ return text
@@ -0,0 +1,26 @@
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
+ _RE_DEGREE_CELSIUS = re.compile(r"(\d+)°C", re.IGNORECASE)
8
+ _RE_DEGREE_FAHRENHEIT = re.compile(r"(\d+)°F", re.IGNORECASE)
9
+ _RE_DEGREE = re.compile(r"(\d+)°", re.IGNORECASE)
10
+
11
+
12
+ @register_step
13
+ class ConvertDegreeSymbolsToWordsStep(TextStep):
14
+ """Convert °C and °F to language-specific words using language config from operators."""
15
+
16
+ name = "convert_degree_symbols_to_words"
17
+
18
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
19
+ stw = operators.config.symbols_to_words
20
+ if "°C" in stw:
21
+ text = _RE_DEGREE_CELSIUS.sub(rf"\1 {stw['°C']}", text)
22
+ if "°F" in stw:
23
+ text = _RE_DEGREE_FAHRENHEIT.sub(rf"\1 {stw['°F']}", text)
24
+ if "°" in stw:
25
+ text = _RE_DEGREE.sub(rf"\1 {stw['°']}", text)
26
+ return text
@@ -0,0 +1,41 @@
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 ConvertDigitWordSequencesToDigitsStep(TextStep):
10
+ """Convert sequences of 3+ digit words to actual digits.
11
+
12
+ 'two one three four' -> '2134', 'seven zero' stays (only 2 words).
13
+ Delegates the word-to-digit mapping to operators.config.digit_words.
14
+ """
15
+
16
+ name = "convert_digit_word_sequences_to_digits"
17
+
18
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
19
+ digit_words = operators.config.digit_words
20
+ if not digit_words:
21
+ return text
22
+
23
+ # Sort longest first so alternation prefers longer matches (e.g. "oh" before "o")
24
+ sorted_words = sorted(digit_words, key=lambda w: len(w), reverse=True)
25
+ word_pattern = "|".join(re.escape(w) for w in sorted_words)
26
+ pattern = (
27
+ rf"\b(?:{word_pattern})"
28
+ rf"(?:\s+(?:{word_pattern})){{2,}}\b"
29
+ )
30
+
31
+ def _convert(match: re.Match) -> str:
32
+ words = match.group(0).lower().split()
33
+ digits = []
34
+ for word in words:
35
+ if word in digit_words:
36
+ digits.append(digit_words[word])
37
+ else:
38
+ return match.group(0)
39
+ return "".join(digits)
40
+
41
+ return re.sub(pattern, _convert, text, flags=re.IGNORECASE)
@@ -0,0 +1,33 @@
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
+ _RE_DOMAIN_3PARTS = re.compile(r"\b([a-zA-Z]+)\.([a-zA-Z]+)\.([a-zA-Z]+)(\d*)\b")
8
+ _RE_DOMAIN_2PARTS = re.compile(r"\b([a-zA-Z]+)\.([a-zA-Z]{2,})\b")
9
+ _RE_IP_ADDRESS = re.compile(r"\b(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\b")
10
+ _RE_VERSION_V_PREFIX = re.compile(r"\b(v)(\d+)\.(\d+)\.(\d+)\b")
11
+ _RE_VERSION_3PARTS = re.compile(r"\b(\d+)\.(\d+)\.(\d+)\b")
12
+ _RE_VERSION_V_SUFFIX = re.compile(r"\b([a-z]+)\.([a-z]+)\.v(\d+)\b")
13
+ _RE_FILE_EXTENSION = re.compile(r"\b([a-zA-Z0-9_\-]+)\.([a-zA-Z]{2,4})\b")
14
+
15
+
16
+ @register_step
17
+ class ConvertDotsToWordsInTechnicalContextsStep(TextStep):
18
+ """Convert dots in domains, IPs, versions, file extensions to the language dot word, defined in language config in operator."""
19
+
20
+ name = "convert_dots_to_words_in_technical_contexts"
21
+
22
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
23
+ dot = operators.config.symbols_to_words.get(".")
24
+ if dot is None:
25
+ return text
26
+ text = _RE_DOMAIN_3PARTS.sub(rf"\1 {dot} \2 {dot} \3\4", text)
27
+ text = _RE_DOMAIN_2PARTS.sub(rf"\1 {dot} \2", text)
28
+ text = _RE_IP_ADDRESS.sub(rf"\1 {dot} \2 {dot} \3 {dot} \4", text)
29
+ text = _RE_VERSION_V_PREFIX.sub(rf"\1\2 {dot} \3 {dot} \4", text)
30
+ text = _RE_VERSION_3PARTS.sub(rf"\1 {dot} \2 {dot} \3", text)
31
+ text = _RE_VERSION_V_SUFFIX.sub(rf"\1 {dot} \2 {dot} v\3", text)
32
+ text = _RE_FILE_EXTENSION.sub(rf"\1 {dot} \2", text)
33
+ return text
@@ -0,0 +1,34 @@
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 ConvertOclockToNumericTimeStep(TextStep):
10
+ """Convert 'ten o'clock' -> '10:00'.
11
+
12
+ Reads operators.config.oclock_word and operators.config.time_words.
13
+ Only processes time_words entries with numeric values 1-12. Values above 12 (minute expressions like "twenty", "thirty") are skipped because o'clock only applies to full hours.
14
+ No operation when either field is None.
15
+ """
16
+
17
+ name = "convert_oclock_to_numeric_time"
18
+
19
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
20
+ oclock_word = operators.config.oclock_word
21
+ time_words = operators.config.time_words
22
+ if oclock_word is None or time_words is None:
23
+ # TODO: warn or log when oclock_word/time_words is not configured for this language
24
+ return text
25
+ for word, num in time_words.items():
26
+ if int(num) > 12:
27
+ continue # skip minute-range values; only clock hours are valid for o'clock
28
+ text = re.sub(
29
+ rf"\b({word}|{num})\s+{re.escape(oclock_word)}\b",
30
+ rf"{num}:00",
31
+ text,
32
+ flags=re.IGNORECASE,
33
+ )
34
+ return text
@@ -0,0 +1,41 @@
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
+ _ROMAN_REPLACEMENTS: dict[str, str] = {
8
+ "ii": "2",
9
+ "iii": "3",
10
+ "iv": "4",
11
+ "v": "5",
12
+ "vi": "6",
13
+ "vii": "7",
14
+ "viii": "8",
15
+ "ix": "9",
16
+ }
17
+
18
+
19
+ @register_step
20
+ class ConvertRomanNumeralsToDigitsStep(TextStep):
21
+ """Convert simple Roman numerals (II-IX) to Arabic digits in full text.
22
+
23
+ Runs before expand_alphanumeric_codes to prevent 'VIII' -> 'V I I I'.
24
+ Only converts ii-ix to avoid false positives with single letters like 'I'.
25
+ Skips 'v' when adjacent to digits (version-like contexts: v2, v 12).
26
+ """
27
+
28
+ name = "convert_roman_numerals_to_digits"
29
+
30
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
31
+ for roman, arabic in _ROMAN_REPLACEMENTS.items():
32
+ if roman == "v":
33
+ text = re.sub(
34
+ r"(?<!\d )(?<!\d)\bv\b(?!\s*\d)",
35
+ arabic,
36
+ text,
37
+ flags=re.IGNORECASE,
38
+ )
39
+ else:
40
+ text = re.sub(rf"\b{roman}\b", arabic, text, flags=re.IGNORECASE)
41
+ return text
@@ -0,0 +1,70 @@
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 _ampm_prefix_class(am_word: str, pm_word: str) -> str:
9
+ """Build a regex character class from the first characters of am/pm words."""
10
+ return f"[{re.escape(am_word[0])}{re.escape(pm_word[0])}]"
11
+
12
+
13
+ @register_step
14
+ class ConvertWordBasedTimePatternsStep(TextStep):
15
+ """Convert word-based time patterns (two p.m -> 2 pm, two thirty p.m -> 2:30 pm).
16
+
17
+ Reads operators.config.time_words, operators.config.am_word,
18
+ operators.config.pm_word, operators.config.oclock_word, and
19
+ operators.get_compound_minutes().
20
+ No-op when required config is None.
21
+ """
22
+
23
+ name = "convert_word_based_time_patterns"
24
+
25
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
26
+ time_words = operators.config.time_words
27
+ am_word = operators.config.am_word
28
+ pm_word = operators.config.pm_word
29
+ oclock_word = operators.config.oclock_word
30
+
31
+ if time_words is not None and am_word is not None and pm_word is not None:
32
+ compound = operators.get_compound_minutes()
33
+ prefix_class = _ampm_prefix_class(am_word, pm_word)
34
+
35
+ for hour_word, hour_num in time_words.items():
36
+ for min_pattern, min_num in compound.items():
37
+ text = re.sub(
38
+ rf"\b{hour_word}\s+{min_pattern}\s+({prefix_class})\.?m\.?\b",
39
+ rf"{hour_num}:{min_num} \1m",
40
+ text,
41
+ flags=re.IGNORECASE,
42
+ )
43
+
44
+ for hour_word, hour_num in time_words.items():
45
+ for min_word, min_num in time_words.items():
46
+ text = re.sub(
47
+ rf"\b{hour_word}\s+{min_word}\s+({prefix_class})\.?m\.?\b",
48
+ rf"{hour_num}:{min_num} \1m",
49
+ text,
50
+ flags=re.IGNORECASE,
51
+ )
52
+
53
+ for word, num in time_words.items():
54
+ text = re.sub(
55
+ rf"\b{word}\s+({prefix_class})\.?m\.?\b",
56
+ rf"{num} \1m",
57
+ text,
58
+ flags=re.IGNORECASE,
59
+ )
60
+
61
+ if oclock_word is not None:
62
+ escaped = re.escape(oclock_word)
63
+ text = re.sub(
64
+ rf"(\d{{1,2}})\s+{escaped}", r"\1:00", text, flags=re.IGNORECASE
65
+ )
66
+ if am_word is not None and pm_word is not None:
67
+ prefix_class = _ampm_prefix_class(am_word, pm_word)
68
+ text = re.sub(rf"(\d{{1,2}}):00\s+({prefix_class}m)", r"\1 \2", text)
69
+
70
+ return text
@@ -0,0 +1,64 @@
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
+ _PROTECTION_MARKERS = [
9
+ ProtectPlaceholder.DECIMAL_SEPARATOR.value,
10
+ ProtectPlaceholder.EMAIL_AT.value,
11
+ ProtectPlaceholder.EMAIL_DOT.value,
12
+ ProtectPlaceholder.PHONE_PLUS.value,
13
+ ]
14
+
15
+
16
+ @register_step
17
+ class ExpandAlphanumericCodesStep(TextStep):
18
+ """Space out uppercase words and alphanumeric codes.
19
+
20
+ 'ABC123' -> 'A B C 1 2 3', 'CNN' -> 'C N N'.
21
+ Skips pure numbers, ordinals (1st, 2nd), and protection markers. Must run before casefold_text.
22
+ """
23
+
24
+ name = "expand_alphanumeric_codes"
25
+
26
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
27
+ ordinal_suffixes = operators.config.ordinal_suffixes
28
+ if ordinal_suffixes:
29
+ suffix_pattern = re.compile(
30
+ r"^\d+(" + "|".join(re.escape(s) for s in ordinal_suffixes) + r")$",
31
+ re.IGNORECASE,
32
+ )
33
+ else:
34
+ suffix_pattern = None
35
+
36
+ def _expand_word(match: re.Match) -> str:
37
+ word = match.group(0)
38
+ has_digit = any(c.isdigit() for c in word)
39
+ if word.isupper():
40
+ return " ".join(word)
41
+ if has_digit:
42
+ return " ".join(word.upper())
43
+ return word
44
+
45
+ def _should_process(match: re.Match) -> str:
46
+ word = match.group(0)
47
+
48
+ if word.isdigit():
49
+ return word
50
+
51
+ if suffix_pattern and suffix_pattern.match(word):
52
+ return word
53
+
54
+ word_upper = word.upper()
55
+ if any(marker in word_upper for marker in _PROTECTION_MARKERS):
56
+ return word
57
+
58
+ has_digit = any(c.isdigit() for c in word)
59
+ if word.isupper() or has_digit:
60
+ return _expand_word(match)
61
+
62
+ return word
63
+
64
+ return re.sub(r"\b[A-Za-z0-9]{2,}\b", _should_process, text)
@@ -0,0 +1,16 @@
1
+ from normalization.languages.base import LanguageOperators
2
+ from normalization.steps.base import TextStep
3
+ from normalization.steps.registry import register_step
4
+
5
+
6
+ @register_step
7
+ class ExpandContractionsStep(TextStep):
8
+ """Expand contractions (it's -> it is, can't -> cannot).
9
+
10
+ Delegates to operators.expand_contractions().
11
+ """
12
+
13
+ name = "expand_contractions"
14
+
15
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
16
+ return operators.expand_contractions(text)
@@ -0,0 +1,13 @@
1
+ from normalization.languages.base import LanguageOperators
2
+ from normalization.steps.base import TextStep
3
+ from normalization.steps.registry import register_step
4
+
5
+
6
+ @register_step
7
+ class ExpandWrittenNumbersToDigitsStep(TextStep):
8
+ """Convert written numbers to digits (fifty -> 50, twenty three -> 23). Delegates to operators.expand_written_numbers()."""
9
+
10
+ name = "expand_written_numbers_to_digits"
11
+
12
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
13
+ return operators.expand_written_numbers(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 ExpandWwwAbbreviationStep(TextStep):
10
+ """Expand 'www' to 'W W W'."""
11
+
12
+ name = "expand_www_abbreviation"
13
+
14
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
15
+ return re.sub(r"\bwww\b", "W W W", text, flags=re.IGNORECASE)
@@ -0,0 +1,25 @@
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 FixAmpmLetterSpacingStep(TextStep):
10
+ """Collapse 'a m' / 'p m' into 'am' / 'pm' after time digits.
11
+
12
+ Reads operators.config.am_word and operators.config.pm_word.
13
+ No-op when either is None.
14
+ """
15
+
16
+ name = "fix_ampm_letter_spacing"
17
+
18
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
19
+ am_word = operators.config.am_word
20
+ pm_word = operators.config.pm_word
21
+ if am_word is None or pm_word is None:
22
+ return text
23
+
24
+ prefix_class = f"[{re.escape(am_word[0])}{re.escape(pm_word[0])}]"
25
+ return re.sub(rf"\b(\d+:?\d*\s+)({prefix_class})\s+m\b", r"\1\2m", text)
@@ -0,0 +1,37 @@
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 FixDotAdjacentNumberWordsStep(TextStep):
10
+ """Convert number words back to digits when adjacent to 'dot' (IPs/versions).
11
+
12
+ 'zero dot one dot two' -> '0 dot 1 dot 2'.
13
+ Single-character entries (e.g. 'o') are excluded to avoid false positives
14
+ in non-numeric contexts.
15
+ """
16
+
17
+ name = "fix_dot_adjacent_number_words"
18
+
19
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
20
+ digit_words = operators.config.digit_words
21
+ if not digit_words:
22
+ return text
23
+
24
+ dot_word = operators.config.symbols_to_words.get(".")
25
+ if dot_word is None:
26
+ return text
27
+ # Skip single-char entries: standalone "o" is too ambiguous in dot contexts
28
+ for word, digit in digit_words.items():
29
+ if len(word) < 2:
30
+ continue
31
+ text = re.sub(
32
+ rf"\b{re.escape(word)}\b(?=\s+{re.escape(dot_word)}\b)", digit, text
33
+ )
34
+ text = re.sub(
35
+ rf"(?<=\b{re.escape(dot_word)}\s){re.escape(word)}\b", digit, text
36
+ )
37
+ return text
@@ -0,0 +1,16 @@
1
+ from normalization.languages.base import LanguageOperators
2
+ from normalization.steps.base import TextStep
3
+ from normalization.steps.registry import register_step
4
+
5
+
6
+ @register_step
7
+ class FixOneWordInNumericContextsStep(TextStep):
8
+ """Convert the word for 'one' to its digit when adjacent to other digits.
9
+
10
+ Example (English): '10 one one' -> '10 1 1', 'one 5' -> '1 5'
11
+ """
12
+
13
+ name = "fix_one_word_in_numeric_contexts"
14
+
15
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
16
+ return operators.fix_one_word_in_numeric_contexts(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 FixVersionNumberVPrefixStep(TextStep):
10
+ """Collapse space between 'v' and digit (v 2 -> v2). 'v' must be followed by a digit."""
11
+
12
+ name = "fix_version_number_v_prefix"
13
+
14
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
15
+ return re.sub(r"\bv\s+(\d)", r"v\1", text)
@@ -0,0 +1,53 @@
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 FormatTimePatternsWithAmpmStep(TextStep):
10
+ """Format '5 45 p m' -> '5:45 pm' and '545 pm' -> '5:45 pm'.
11
+
12
+ Reads operators.config.am_word and operators.config.pm_word.
13
+ No-op when either is None.
14
+ """
15
+
16
+ name = "format_time_patterns_with_ampm"
17
+
18
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
19
+ am_word = operators.config.am_word
20
+ pm_word = operators.config.pm_word
21
+ if am_word is None or pm_word is None:
22
+ return text
23
+
24
+ prefix_class = f"[{re.escape(am_word[0])}{re.escape(pm_word[0])}]"
25
+
26
+ def _format_time(match: re.Match) -> str:
27
+ hour = str(int(match.group(1)))
28
+ minute = match.group(2)
29
+ ampm = match.group(3)
30
+ return f"{hour}:{minute} {ampm}m"
31
+
32
+ def _format_compact_time(match: re.Match) -> str:
33
+ digits = match.group(1)
34
+ ampm = match.group(2)
35
+ if len(digits) == 3:
36
+ hour = str(int(digits[0]))
37
+ minute = digits[1:3]
38
+ else:
39
+ hour = str(int(digits[0:2]))
40
+ minute = digits[2:4]
41
+ return f"{hour}:{minute} {ampm}m"
42
+
43
+ text = re.sub(
44
+ rf"\b(\d{{1,2}})\s+(\d{{2}})\s+({prefix_class})\s*m\b",
45
+ _format_time,
46
+ text,
47
+ )
48
+ text = re.sub(
49
+ rf"\b(\d{{3,4}})\s+({prefix_class})\s*m\b",
50
+ _format_compact_time,
51
+ text,
52
+ )
53
+ return text
@@ -0,0 +1,16 @@
1
+ from normalization.languages.base import LanguageOperators
2
+ from normalization.steps.base import TextStep
3
+ from normalization.steps.registry import register_step
4
+
5
+
6
+ @register_step
7
+ class NormalizeNumericTimeFormatsStep(TextStep):
8
+ """Normalize numeric time formats (05:45pm -> 5:45 pm, 5.45 p.m. -> 5:45 pm).
9
+
10
+ Delegates to operators.normalize_numeric_time_formats().
11
+ """
12
+
13
+ name = "normalize_numeric_time_formats"
14
+
15
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
16
+ return operators.normalize_numeric_time_formats(text)
@@ -0,0 +1,30 @@
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 NormalizePunctuationBetweenNumberWordsStep(TextStep):
10
+ """Replace commas, dots, hyphens between number words with a single space.
11
+
12
+ Handles: 'seven, zero' -> 'seven zero', 'two-one-three' -> 'two one three'.
13
+ Reads operators.config.number_words. No-op when None.
14
+ """
15
+
16
+ name = "normalize_punctuation_between_number_words"
17
+
18
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
19
+ number_words = operators.config.number_words
20
+ if not number_words:
21
+ return text
22
+
23
+ sorted_words = sorted(number_words, key=lambda w: len(w), reverse=True)
24
+ alternation = "|".join(re.escape(w) for w in sorted_words)
25
+ number_group = f"(?:{alternation})"
26
+ pattern = re.compile(
27
+ rf"({number_group})\s*[,.\-…]+\s*(?={number_group})",
28
+ flags=re.IGNORECASE,
29
+ )
30
+ return pattern.sub(r"\1 ", text)