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,22 @@
|
|
|
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 RemoveTrailingDotWordFromEmailsStep(TextStep):
|
|
10
|
+
"""Remove trailing 'dot' after email-like words at end of text."""
|
|
11
|
+
|
|
12
|
+
name = "remove_trailing_dot_word_from_emails"
|
|
13
|
+
|
|
14
|
+
def __call__(self, text: str, operators: LanguageOperators) -> str:
|
|
15
|
+
dot_word = operators.config.symbols_to_words.get(".")
|
|
16
|
+
if dot_word is None:
|
|
17
|
+
return text
|
|
18
|
+
return re.sub(
|
|
19
|
+
rf"([a-z]{{2,}})\s+{re.escape(dot_word)}\s*$",
|
|
20
|
+
r"\1",
|
|
21
|
+
text,
|
|
22
|
+
)
|
|
@@ -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 RemoveTrailingPeriodStep(TextStep):
|
|
10
|
+
"""Remove trailing period from text."""
|
|
11
|
+
|
|
12
|
+
name = "remove_trailing_period"
|
|
13
|
+
|
|
14
|
+
def __call__(self, text: str, operators: LanguageOperators) -> str:
|
|
15
|
+
return re.sub(r"\s*\.\s*$", "", 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 RemoveZeroMinutesFromTimeStep(TextStep):
|
|
10
|
+
"""Remove :00 from time expressions (10:00 pm -> 10 pm).
|
|
11
|
+
|
|
12
|
+
Reads operators.config.am_word and operators.config.pm_word.
|
|
13
|
+
No-op when either is None.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
name = "remove_zero_minutes_from_time"
|
|
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
|
+
ampm_alt = f"({re.escape(am_word)}|{re.escape(pm_word)})"
|
|
25
|
+
return re.sub(
|
|
26
|
+
rf"\b(\d{{1,2}}):00\s+{ampm_alt}\b",
|
|
27
|
+
r"\1 \2",
|
|
28
|
+
text,
|
|
29
|
+
flags=re.IGNORECASE,
|
|
30
|
+
)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from normalization.constants 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
|
+
_CURRENCY_NUM = rf"\d+(?:{ProtectPlaceholder.DECIMAL_SEPARATOR.value}\d+)?"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _make_currency_patterns(symbol: str) -> tuple[re.Pattern, re.Pattern]:
|
|
12
|
+
escaped = re.escape(symbol)
|
|
13
|
+
before = re.compile(rf"{escaped}\s*({_CURRENCY_NUM})", re.IGNORECASE)
|
|
14
|
+
after = re.compile(rf"({_CURRENCY_NUM})\s*{escaped}", re.IGNORECASE)
|
|
15
|
+
return before, after
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@register_step
|
|
19
|
+
class ReplaceCurrencyStep(TextStep):
|
|
20
|
+
"""
|
|
21
|
+
Replace currency symbols with their corresponding words.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
name = "replace_currency"
|
|
25
|
+
|
|
26
|
+
def __call__(self, text: str, operators: LanguageOperators) -> str:
|
|
27
|
+
for symbol, word in operators.config.currency_symbol_to_word.items():
|
|
28
|
+
before, after = _make_currency_patterns(symbol)
|
|
29
|
+
text = before.sub(rf"\1 {word}", text)
|
|
30
|
+
text = after.sub(rf"\1 {word}", text)
|
|
31
|
+
return text
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from normalization.languages.base import LanguageOperators
|
|
4
|
+
from normalization.pipeline.replacer import Replacer
|
|
5
|
+
from normalization.steps.base import WordStep
|
|
6
|
+
from normalization.steps.registry import register_step
|
|
7
|
+
|
|
8
|
+
_RE_EMAIL = re.compile(r"^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$")
|
|
9
|
+
|
|
10
|
+
_REPLACER_CACHE: dict[str, Replacer] = {}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _get_replacer(operators: LanguageOperators) -> Replacer:
|
|
14
|
+
code = operators.config.code
|
|
15
|
+
if code not in _REPLACER_CACHE:
|
|
16
|
+
_REPLACER_CACHE[code] = Replacer(operators.get_word_replacements())
|
|
17
|
+
return _REPLACER_CACHE[code]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@register_step
|
|
21
|
+
class ApplyWordReplacementsStep(WordStep):
|
|
22
|
+
"""Apply single-word replacements from the language operators.
|
|
23
|
+
|
|
24
|
+
Skips email tokens. Uses a cached Replacer keyed on the language code.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
name = "apply_word_replacements"
|
|
28
|
+
|
|
29
|
+
def __call__(self, word: str, operators: LanguageOperators) -> str:
|
|
30
|
+
if _RE_EMAIL.match(word):
|
|
31
|
+
return word
|
|
32
|
+
replacer = _get_replacer(operators)
|
|
33
|
+
return replacer(word).strip()
|