gladia-normalization 0.1.0a4__tar.gz → 0.1.1__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.
Files changed (110) hide show
  1. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/.github/workflows/cd.yml +9 -6
  2. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/.github/workflows/ci.yml +1 -1
  3. gladia_normalization-0.1.1/CONTRIBUTING.md +86 -0
  4. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/PKG-INFO +2 -2
  5. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/README.md +1 -1
  6. gladia_normalization-0.1.0a4/CONTRIBUTING.md → gladia_normalization-0.1.1/docs/contributing-guide.md +46 -56
  7. gladia_normalization-0.1.1/normalization/pipeline/replacer.py +53 -0
  8. gladia_normalization-0.1.1/normalization/steps/text/convert_word_based_time_patterns.py +147 -0
  9. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/pyproject.toml +1 -1
  10. gladia_normalization-0.1.1/tests/e2e/latency_test.py +32 -0
  11. gladia_normalization-0.1.1/tests/unit/steps/text/convert_word_based_time_patterns_test.py +67 -0
  12. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/uv.lock +1 -1
  13. gladia_normalization-0.1.0a4/normalization/pipeline/replacer.py +0 -38
  14. gladia_normalization-0.1.0a4/normalization/steps/text/convert_word_based_time_patterns.py +0 -70
  15. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/.commitlintrc.json +0 -0
  16. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/.github/pull_request_template.md +0 -0
  17. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/.gitignore +0 -0
  18. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/.pre-commit-config.yaml +0 -0
  19. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/.python-version +0 -0
  20. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/AGENTS.md +0 -0
  21. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/CLAUDE.md +0 -0
  22. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/LICENSE +0 -0
  23. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/docs/steps.md +0 -0
  24. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/__init__.py +0 -0
  25. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/constants/__init__.py +0 -0
  26. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/constants/protectors.py +0 -0
  27. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/__init__.py +0 -0
  28. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/base/__init__.py +0 -0
  29. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/base/language_config.py +0 -0
  30. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/base/language_operator.py +0 -0
  31. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/english/__init__.py +0 -0
  32. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/english/number_normalizer.py +0 -0
  33. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/english/operators.py +0 -0
  34. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/english/replacements.py +0 -0
  35. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/english/sentence_replacements.py +0 -0
  36. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/french/__init__.py +0 -0
  37. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/french/operators.py +0 -0
  38. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/french/replacements.py +0 -0
  39. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/languages/registry.py +0 -0
  40. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/pipeline/__init__.py +0 -0
  41. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/pipeline/base.py +0 -0
  42. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/pipeline/loader.py +0 -0
  43. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/presets/gladia-3.yaml +0 -0
  44. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/__init__.py +0 -0
  45. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/base/__init__.py +0 -0
  46. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/base/protect_step.py +0 -0
  47. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/base/restore_step.py +0 -0
  48. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/base/text_step.py +0 -0
  49. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/base/word_step.py +0 -0
  50. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/registry.py +0 -0
  51. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/__init__.py +0 -0
  52. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/apply_sentence_level_replacements.py +0 -0
  53. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/casefold_text.py +0 -0
  54. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/convert_comparison_operators_to_words.py +0 -0
  55. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/convert_decimal_periods_to_decimal_word.py +0 -0
  56. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/convert_degree_symbols_to_words.py +0 -0
  57. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/convert_digit_word_sequences_to_digits.py +0 -0
  58. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/convert_dots_to_words_in_technical_contexts.py +0 -0
  59. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/convert_oclock_to_numeric_time.py +0 -0
  60. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/convert_roman_numerals_to_digits.py +0 -0
  61. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/expand_alphanumeric_codes.py +0 -0
  62. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/expand_contractions.py +0 -0
  63. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/expand_written_numbers_to_digits.py +0 -0
  64. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/expand_www_abbreviation.py +0 -0
  65. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/fix_ampm_letter_spacing.py +0 -0
  66. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/fix_dot_adjacent_number_words.py +0 -0
  67. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/fix_one_word_in_numeric_contexts.py +0 -0
  68. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/fix_version_number_v_prefix.py +0 -0
  69. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/format_time_patterns_with_ampm.py +0 -0
  70. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/normalize_numeric_time_formats.py +0 -0
  71. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/normalize_punctuation_between_number_words.py +0 -0
  72. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/placeholders.py +0 -0
  73. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/protect_plus_word_before_digit_words.py +0 -0
  74. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_acronym_periods.py +0 -0
  75. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_diacritics.py +0 -0
  76. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_filler_words.py +0 -0
  77. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_hash_before_numbers.py +0 -0
  78. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_non_numeric_trailing_dots.py +0 -0
  79. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_spaces_between_adjacent_digits.py +0 -0
  80. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_standalone_currency_symbols.py +0 -0
  81. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_symbols.py +0 -0
  82. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_thousand_separators.py +0 -0
  83. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_trailing_apostrophe_space.py +0 -0
  84. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_trailing_dot_word_from_emails.py +0 -0
  85. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_trailing_period.py +0 -0
  86. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/remove_zero_minutes_from_time.py +0 -0
  87. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/text/replace_currency.py +0 -0
  88. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/word/__init__.py +0 -0
  89. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/normalization/steps/word/apply_word_replacements.py +0 -0
  90. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/scripts/generate_step_docs.py +0 -0
  91. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/__init__.py +0 -0
  92. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/e2e/__init__.py +0 -0
  93. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/e2e/default_pipeline_test.py +0 -0
  94. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/e2e/files/gladia-3.csv +0 -0
  95. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/e2e/normalization_test.py +0 -0
  96. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/languages/__init__.py +0 -0
  97. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/languages/english_registry_test.py +0 -0
  98. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/languages/symbols_to_words_test.py +0 -0
  99. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/languages/word_replacement_test.py +0 -0
  100. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/__init__.py +0 -0
  101. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/text/__init__.py +0 -0
  102. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/text/conftest.py +0 -0
  103. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/text/convert_dots_to_words_in_technical_contexts_test.py +0 -0
  104. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/text/convert_oclock_to_numeric_time_test.py +0 -0
  105. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/text/convert_roman_numerals_to_digits_test.py +0 -0
  106. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/text/protect_decimal_separator_test.py +0 -0
  107. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/text/remove_diacritics_test.py +0 -0
  108. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/text/remove_zero_minutes_from_time_test.py +0 -0
  109. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/text/replace_currency_test.py +0 -0
  110. {gladia_normalization-0.1.0a4 → gladia_normalization-0.1.1}/tests/unit/steps/text/restore_decimal_separator_with_word_test.py +0 -0
@@ -16,7 +16,8 @@ jobs:
16
16
  environment: pypi
17
17
  permissions:
18
18
  contents: read
19
- id-token: write # required for Trusted Publisher (OIDC)
19
+ id-token: write # required for Trusted Publisher (OIDC) and Sigstore signing
20
+ attestations: write # required to persist build attestations
20
21
 
21
22
  steps:
22
23
  - uses: actions/checkout@v6
@@ -42,10 +43,12 @@ jobs:
42
43
  run: uv version ${{ steps.version.outputs.version }}
43
44
 
44
45
  - name: Build
45
- run: uv build --no-sources
46
+ run: uv build
46
47
 
47
- - name: Publish to PyPI
48
- run: uv publish
48
+ - name: Attest build artifacts
49
+ uses: actions/attest@v4
50
+ with:
51
+ subject-path: dist/*
49
52
 
50
- - name: Verify package installation
51
- run: uv pip install --prerelease=allow 'gladia-normalization==${{ steps.version.outputs.version }}'
53
+ - name: Publish to PyPI
54
+ run: uv publish --trusted-publishing automatic
@@ -46,7 +46,7 @@ jobs:
46
46
  runs-on: ubuntu-latest
47
47
  strategy:
48
48
  matrix:
49
- python-version: ["3.10", "3.11", "3.12", "3.13"]
49
+ python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
50
50
  steps:
51
51
  - uses: actions/checkout@v6
52
52
  - uses: astral-sh/setup-uv@v7
@@ -0,0 +1,86 @@
1
+ # Contributing to gladia-normalization
2
+
3
+ Thanks for your interest in contributing! Every contribution — whether it's a new language, a normalization step, or a bug report — makes the library more useful for the entire voice AI community.
4
+
5
+ ## Code of conduct
6
+
7
+ Please be respectful and welcoming in all interactions. We're committed to making this a safe and inclusive space for everyone. If you experience or witness unacceptable behavior, please open a GitHub issue or contact the maintainers directly.
8
+
9
+ ## How you can help
10
+
11
+ ### Add support for a new language
12
+
13
+ This is the most impactful contribution. We don't master every language — and that's exactly where the community can make a difference. If you speak a language not yet supported, you can add it in a self-contained folder with no changes to the core pipeline.
14
+
15
+ A new language requires:
16
+
17
+ 1. Create `normalization/languages/{lang}/` with `operators.py`, `replacements.py`, and `__init__.py`
18
+ 2. Put all word-level substitutions in `replacements.py`
19
+ 3. Instantiate a `LanguageConfig` and subclass `LanguageOperators` in `operators.py`
20
+ 4. Decorate with `@register_language` and add one import to `normalization/languages/__init__.py`
21
+ 5. Add tests under `tests/unit/languages/` and e2e fixture rows in `tests/e2e/files/`
22
+
23
+ See [docs/contributing-guide.md](docs/contributing-guide.md) for the full checklist and design rules.
24
+
25
+ ### Add a new normalization step
26
+
27
+ Steps are atomic, stateless transformations that plug into the pipeline via a decorator — no manual registry updates needed.
28
+
29
+ A new step requires:
30
+
31
+ 1. Create or extend a file under `normalization/steps/text/` or `normalization/steps/word/`
32
+ 2. Decorate the class with `@register_step` and set a unique `name` attribute
33
+ 3. Add an import to `steps/text/__init__.py` or `steps/word/__init__.py`
34
+ 4. Add unit tests under `tests/unit/steps/`
35
+ 5. Add the step name to the relevant preset YAML, or create a new preset version
36
+ 6. If you added or changed the class docstring, regenerate `docs/steps.md`: `uv run scripts/generate_step_docs.py`
37
+
38
+ See [docs/contributing-guide.md](docs/contributing-guide.md) for base class selection and test conventions.
39
+
40
+ ### Report a bug
41
+
42
+ Open an issue with steps to reproduce, expected vs. actual behavior, and your environment (Python version, OS, package version).
43
+
44
+ ### Ask a question
45
+
46
+ Open a GitHub issue with the `question` label. We're happy to help.
47
+
48
+ ---
49
+
50
+ ## Development setup
51
+
52
+ ```bash
53
+ git clone https://github.com/gladiaio/normalization.git
54
+ cd normalization
55
+ uv sync
56
+ uv run pre-commit install --install-hooks # install hooks once
57
+ ```
58
+
59
+ Run the checks at any time:
60
+
61
+ ```bash
62
+ uv run pytest # run tests
63
+ uv run ruff check . # lint
64
+ uv run ruff format . # format
65
+ uv run ty check # type-check
66
+ ```
67
+
68
+ ---
69
+
70
+ ## Submitting changes
71
+
72
+ 1. Fork the repo and create a branch: `git checkout -b feat/my-feature`
73
+ 2. Make your changes and add tests
74
+ 3. Run the checks (see above) and make sure everything passes
75
+ 4. Push your branch: `git push origin your-feature-branch`
76
+ 5. Open a pull request on GitHub with a clear description of your changes, filling the [template](./.github/pull_request_template.md)
77
+ 6. Address review feedback and push updates
78
+ 7. Once approved, your PR will be merged
79
+
80
+ ### Pre-commit hooks
81
+
82
+ The hooks run automatically on every commit: Ruff (lint + format), ty (type-check), and commit message validation. If a hook fails, fix the issue and commit again.
83
+
84
+ ## Commit style
85
+
86
+ We use [Conventional Commits](https://www.conventionalcommits.org/): prefix your commit with `feat:`, `fix:`, `docs:`, `chore:`, etc.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gladia-normalization
3
- Version: 0.1.0a4
3
+ Version: 0.1.1
4
4
  Summary: Normalize speech-to-text output for fair Word Error Rate (WER) comparison
5
5
  Project-URL: Homepage, https://github.com/gladiaio/normalization
6
6
  Project-URL: Repository, https://github.com/gladiaio/normalization
@@ -44,7 +44,7 @@ Description-Content-Type: text/markdown
44
44
  <p align="center">
45
45
  <h1 align="center">Normalization</h1>
46
46
  <p align="center">
47
- Text normalization for fair speech-to-text evaluation.
47
+ A lightweight library for normalizing speech transcripts before computing WER.
48
48
  <br />
49
49
  <a href="#quick-start">Quick Start</a> &middot; <a href="docs/steps.md">Step Reference</a> &middot; <a href="CONTRIBUTING.md">Contributing</a>
50
50
  </p>
@@ -1,7 +1,7 @@
1
1
  <p align="center">
2
2
  <h1 align="center">Normalization</h1>
3
3
  <p align="center">
4
- Text normalization for fair speech-to-text evaluation.
4
+ A lightweight library for normalizing speech transcripts before computing WER.
5
5
  <br />
6
6
  <a href="#quick-start">Quick Start</a> &middot; <a href="docs/steps.md">Step Reference</a> &middot; <a href="CONTRIBUTING.md">Contributing</a>
7
7
  </p>
@@ -1,65 +1,59 @@
1
- # Contributing
1
+ # Contributing guide
2
2
 
3
- Thanks for your interest in `gladia-normalization`! Here's how to get involved.
3
+ Detailed reference for contributors. Read this before adding a step or language.
4
4
 
5
- ## Reporting bugs
6
-
7
- Open an issue with steps to reproduce, expected vs actual behavior, and your environment (Python version, OS, package version).
8
-
9
- ## Submitting changes
5
+ ## Architecture at a glance
10
6
 
11
- 1. **Fork the repo and create a branch**: `git checkout -b feat/my-feature`
12
- 2. **Make your changes and add tests**
13
- 3. **Run the checks**:
14
- ```bash
15
- uv run pytest # run tests
16
- uv run ruff check . # lint
17
- uv run ruff format . # format
18
- uv run ty check # type-check
19
- ```
20
- 4. **Push your branch**: `git push origin your-feature-branch`
21
- 5. **Create a PR**: Go to GitHub and create a pull request
22
- 6. **Fill out the PR template**: Provide clear description of changes
23
- 7. **Wait for review**: Maintainers will review and provide feedback
24
- 8. **Address feedback**: Make requested changes and push updates
25
- 9. **Merge**: Once approved, your PR will be merged!
7
+ Every pipeline runs exactly **three stages**, always in this order:
26
8
 
27
- ### Pre-commit hooks
9
+ 1. **Text pre-processing** — full-text transforms before word splitting (placeholder protection, symbol conversion, contraction expansion, …)
10
+ 2. **Word processing** — per-token transforms after splitting on spaces (replacements, filler removal, …)
11
+ 3. **Text post-processing** — full-text cleanup after rejoining words (placeholder restoration, digit collapsing, …)
28
12
 
29
- The project uses [pre-commit](https://pre-commit.com/) to enforce linting, formatting, and commit message conventions automatically. Install the hooks once after cloning:
13
+ This ordering is a hard constraint — some steps depend on earlier steps having run. See the [README](../README.md) for more detail.
30
14
 
31
- ```bash
32
- uv run pre-commit install --install-hooks
33
- ```
15
+ ---
34
16
 
35
- This will run Ruff (lint + format) and ty (type-check) on every commit, and validate your commit message on `commit-msg`.
17
+ ## Adding a new language — checklist
36
18
 
37
- ## Commit style
19
+ - [ ] Create `languages/{lang}/` with `operators.py`, `replacements.py`, `__init__.py`
20
+ - [ ] Put all word-level substitutions in `replacements.py`; do not add inline entries in `operators.py`
21
+ - [ ] Instantiate a `LanguageConfig` in `operators.py`, filling in all required fields and any optional dict fields your language needs (`time_words`, `sentence_replacements`, etc.)
22
+ - [ ] Subclass `LanguageOperators`, overriding only methods where the _algorithm_ differs (not just the data)
23
+ - [ ] If the language has digit words, populate `digit_words` in `LanguageConfig`
24
+ - [ ] If the language uses spoken time patterns, populate `time_words` with all needed word→digit mappings; if it also uses compound minute expressions (e.g. "twenty-one"), override `get_compound_minutes()` — do **not** put this in config
25
+ - [ ] If number expansion is needed and the algorithm is complex, implement it in a `number_normalizer.py` file and override `expand_written_numbers`; otherwise do not create the file
26
+ - [ ] Decorate the class with `@register_language`
27
+ - [ ] Add one import to `languages/__init__.py`
28
+ - [ ] Add tests in `tests/unit/languages/`
29
+ - [ ] Add test rows to `tests/e2e/files/` for the new language
38
30
 
39
- We use [Conventional Commits](https://www.conventionalcommits.org/): pre-fix your commit with `feat:`, `fix:`, `docs:`, `chore:`, etc.
31
+ ### Language data vs. language behavior
40
32
 
41
- ## Architecture at a glance
33
+ This is the central design rule. Ask: "does the _logic_ change by language, or just the _values_?"
42
34
 
43
- Every pipeline runs exactly **three stages**, always in this order:
35
+ **`LanguageConfig` (data)** — everything that can be expressed as a value: strings, lists, dicts. Separator characters, currency words, filler words, digit words, number words, time words, sentence replacements. Optional fields default to `None`; steps that read them skip gracefully when `None`.
44
36
 
45
- 1. **Text pre-processing** — full-text transforms before word splitting (placeholder protection, symbol conversion, contraction expansion, …)
46
- 2. **Word processing** — per-token transforms after splitting on spaces (replacements, filler removal, …)
47
- 3. **Text post-processing** — full-text cleanup after rejoining words (placeholder restoration, digit collapsing, …)
37
+ **`LanguageOperators` (behavior)** — only methods where the _algorithm itself_ varies by language. Examples: `expand_contractions`, `expand_written_numbers`, `normalize_numeric_time_formats`, `get_compound_minutes`. If the algorithm is generic and only the _data_ differs, put the data in `LanguageConfig` and the algorithm in the step — not in the operator.
48
38
 
49
- This ordering is a hard constraint — some steps depend on earlier steps having run. See the [README](./README.md) for more detail.
39
+ ---
50
40
 
51
- ## Adding a new step
41
+ ## Adding a new step — checklist
52
42
 
53
- 1. Create or extend a file under `normalization/steps/text/` or `normalization/steps/word/`.
54
- 2. Decorate the class with `@register_step` and set a unique `name` attribute.
55
- 3. Add an import to `steps/text/__init__.py` or `steps/word/__init__.py`.
56
- 4. Add unit tests under `tests/unit/steps/`.
57
- 5. Add the step name to the relevant preset YAML, or create a new preset version.
58
- 6. If you added or changed the class docstring, regenerate `docs/steps.md` by running `uv run scripts/generate_step_docs.py`.
43
+ - [ ] Add the class to the appropriate file in `steps/text/` or `steps/word/`
44
+ - [ ] Set a unique `name` class attribute
45
+ - [ ] Decorate with `@register_step`
46
+ - [ ] Add one import to `steps/text/__init__.py` or `steps/word/__init__.py`
47
+ - [ ] Place the algorithm in `__call__`; read language data from `operators.config.*`; call operator methods only for genuinely behavioral differences
48
+ - [ ] If the step reads an optional `LanguageConfig` field, guard with `if operators.config.field is None: return text`
49
+ - [ ] Add unit tests in `tests/unit/steps/`
50
+ - [ ] If it involves placeholder protection, add both protect and restore to `steps/text/placeholders.py` and update `pipeline/base.py`'s `validate()` accordingly
51
+ - [ ] Add the step name to relevant preset YAMLs if needed (new preset version if existing presets are affected)
52
+ - [ ] If you added or changed the class docstring, run `uv run scripts/generate_step_docs.py` to regenerate `docs/steps.md`
59
53
 
60
54
  ### Choosing a base class
61
55
 
62
- There are four base classes. Pick the narrowest one that fits your step.
56
+ Pick the narrowest one that fits your step.
63
57
 
64
58
  **`WordStep`** — use when your transformation operates on a single token in isolation, with no knowledge of neighboring words. This is the only base class for Stage 2 steps.
65
59
 
@@ -83,7 +77,7 @@ class MyTextStep(TextStep):
83
77
  ...
84
78
  ```
85
79
 
86
- **`ProtectStep`** — a specialization of `TextStep` for the common case of replacing a character with a placeholder token. You only implement `_pattern`, which returns a compiled regex with **exactly two capture groups** (what comes before and after the character being replaced). The `__call__` is fixed: it applies the pattern as `\1{placeholder}\2`.
80
+ **`ProtectStep`** — a specialization of `TextStep` for replacing a character with a placeholder token. Implement `_pattern`, which returns a compiled regex with **exactly two capture groups** (what comes before and after the character being replaced). The `__call__` is fixed: it applies the pattern as `\1{placeholder}\2`.
87
81
 
88
82
  ```python
89
83
  @register_step
@@ -97,9 +91,9 @@ class MyProtectStep(ProtectStep):
97
91
 
98
92
  Use `ProtectStep` when: one regex pattern maps to exactly one placeholder substitution.
99
93
 
100
- Use `TextStep` directly instead when: a single pass must protect two different symbols (like email `@` and `.`), the replacement needs to absorb surrounding whitespace with `\s*`, or the replacement is a per-match function rather than a fixed template.
94
+ Use `TextStep` directly instead when: a single pass must protect two different symbols, the replacement needs to absorb surrounding whitespace with `\s*`, or the replacement is a per-match function rather than a fixed template.
101
95
 
102
- **`RestoreStep`** — a specialization of `TextStep` for restoring a placeholder back to a string. You only implement `_replacement`, which returns the string to substitute in. The `__call__` does a plain `str.replace` of the placeholder (and its case-folded form).
96
+ **`RestoreStep`** — a specialization of `TextStep` for restoring a placeholder back to a string. Implement `_replacement`, which returns the string to substitute in. The `__call__` does a plain `str.replace` of the placeholder (and its case-folded form).
103
97
 
104
98
  ```python
105
99
  @register_step
@@ -115,6 +109,8 @@ Use `RestoreStep` when: restoration is a straight token swap with no surrounding
115
109
 
116
110
  Use `TextStep` directly instead when: the placeholder was inserted with spaces around it (requiring `re.sub` with `\s*` to avoid double spaces), the marker should be deleted entirely rather than replaced, or post-replacement cleanup is needed.
117
111
 
112
+ ---
113
+
118
114
  ## Writing tests
119
115
 
120
116
  ### Unit tests for a step
@@ -200,18 +196,12 @@ def test_my_preset(test: NormalizationTest) -> None:
200
196
 
201
197
  Pipelines are cached per language inside `_MY_PRESET_PIPELINES` to avoid reloading for each parametrized case — follow the `_load_pipeline` helper pattern already in the file.
202
198
 
203
- Steps must be **language-agnostic** — delegate all language-specific logic to the `operators` argument or read data from `operators.config.*`.
204
-
205
- ## Adding a new language
206
-
207
- 1. Create `normalization/languages/{lang}/` with `operators.py`, `replacements.py`, and `__init__.py`.
208
- 2. Put all word-level substitutions in `replacements.py`.
209
- 3. Instantiate a `LanguageConfig` and subclass `LanguageOperators` in `operators.py`.
210
- 4. Decorate with `@register_language` and add one import to `normalization/languages/__init__.py`.
211
- 5. Add tests under `tests/unit/languages/` and e2e fixture rows in `tests/e2e/files/`.
199
+ ---
212
200
 
213
201
  ## Key design rules
214
202
 
215
203
  - **Data vs. behavior**: if only the _values_ change by language, put them in `LanguageConfig`. If the _algorithm_ changes, override a method in `LanguageOperators`.
204
+ - **Steps are language-agnostic**: a step must not contain any language-specific logic or string literals. Read from `operators.config.*` or call `operators.method()`.
216
205
  - **Presets are immutable**: never modify a published preset YAML — new behavior means a new preset file.
217
206
  - **Placeholder pairs**: every `protect_*` step in Stage 1 must have a matching `restore_*` in Stage 3. The pipeline validates this at load time.
207
+ - **Language folders are self-contained**: everything specific to a language lives inside its folder. Helpers used only by one language (e.g. `number_normalizer.py`) go in that language's folder, not in `steps/`.
@@ -0,0 +1,53 @@
1
+ import logging
2
+ import re
3
+
4
+ logger = logging.getLogger(__name__)
5
+
6
+
7
+ class Replacer:
8
+ """Stateful word-replacement engine.
9
+
10
+ Single-word keys are stored in a plain dict for O(1) lookup.
11
+ Multi-word keys (where the right side is a single word) fall back to
12
+ pre-compiled \\b-bounded regex patterns.
13
+
14
+ At least one side of each mapping pair must be a single word.
15
+ """
16
+
17
+ def __init__(self, mapping: dict[str, str]):
18
+ # Fast path: single-word key → direct dict lookup
19
+ self._word_map: dict[str, str] = {}
20
+ # Fallback: multi-word key (right side must be single word)
21
+ self._multi_patterns: list[tuple[re.Pattern[str], str]] = []
22
+
23
+ for left, right in mapping.items():
24
+ left_is_single = len(left.strip().split()) == 1
25
+ right_is_single = len(right.strip().split()) == 1
26
+
27
+ if not left_is_single and not right_is_single:
28
+ logger.warning(
29
+ f"At least one side must be a single word: '{left}' - '{right}'"
30
+ )
31
+ continue
32
+
33
+ if left_is_single:
34
+ self._word_map[left.strip()] = right
35
+ else:
36
+ # right is single word, left is multi-word: match multi-word phrase
37
+ self._multi_patterns.append(
38
+ (re.compile(rf"\b{re.escape(left.strip())}\b"), right)
39
+ )
40
+
41
+ # Keep a public alias for backwards compatibility
42
+ self.patterns = {
43
+ re.compile(rf"\b{re.escape(k)}\b"): v for k, v in self._word_map.items()
44
+ } | {p: r for p, r in self._multi_patterns}
45
+
46
+ def __call__(self, text: str) -> str:
47
+ # Fast path: single-word input (no spaces) → dict lookup
48
+ if " " not in text.strip():
49
+ return self._word_map.get(text.strip(), text)
50
+ # Multi-word input: apply regex patterns
51
+ for pattern, replacement in self._multi_patterns:
52
+ text = pattern.sub(replacement, text)
53
+ return text
@@ -0,0 +1,147 @@
1
+ import re
2
+ from collections.abc import Callable
3
+ from typing import NamedTuple
4
+
5
+ from normalization.languages.base import LanguageOperators
6
+ from normalization.steps.base import TextStep
7
+ from normalization.steps.registry import register_step
8
+
9
+ # Replacement type: either a pre-compiled string template or a callable.
10
+ # Callables avoid re._compile_template() being invoked on every .sub() call.
11
+ _Replacement = str | Callable[[re.Match[str]], str]
12
+
13
+
14
+ def _ampm_prefix_class(am_word: str, pm_word: str) -> str:
15
+ """Build a regex character class from the first characters of am/pm words."""
16
+ return f"[{re.escape(am_word[0])}{re.escape(pm_word[0])}]"
17
+
18
+
19
+ class _CompiledPatterns(NamedTuple):
20
+ # List of (compiled_pattern, replacement) for hour+compound_minute+ampm
21
+ compound_minute: list[tuple[re.Pattern[str], _Replacement]]
22
+ # List of (compiled_pattern, replacement) for hour+minute_word+ampm
23
+ minute_word: list[tuple[re.Pattern[str], _Replacement]]
24
+ # List of (compiled_pattern, replacement) for hour+ampm only
25
+ hour_only: list[tuple[re.Pattern[str], _Replacement]]
26
+ # (compiled_pattern, replacement) for digit+oclock, or None
27
+ oclock: tuple[re.Pattern[str], _Replacement] | None
28
+ # (compiled_pattern, replacement) for digit:00+ampm collapse, or None
29
+ oclock_ampm: tuple[re.Pattern[str], _Replacement] | None
30
+
31
+
32
+ def _build_compiled_patterns(operators: LanguageOperators) -> _CompiledPatterns:
33
+ time_words = operators.config.time_words
34
+ am_word = operators.config.am_word
35
+ pm_word = operators.config.pm_word
36
+ oclock_word = operators.config.oclock_word
37
+
38
+ compound_minute: list[tuple[re.Pattern[str], _Replacement]] = []
39
+ minute_word: list[tuple[re.Pattern[str], _Replacement]] = []
40
+ hour_only: list[tuple[re.Pattern[str], _Replacement]] = []
41
+ oclock: tuple[re.Pattern[str], _Replacement] | None = None
42
+ oclock_ampm: tuple[re.Pattern[str], _Replacement] | None = None
43
+
44
+ if time_words is not None and am_word is not None and pm_word is not None:
45
+ compound = operators.get_compound_minutes()
46
+ prefix_class = _ampm_prefix_class(am_word, pm_word)
47
+
48
+ for hour_word, hour_num in time_words.items():
49
+ for min_pattern, min_num in compound.items():
50
+ compound_minute.append(
51
+ (
52
+ re.compile(
53
+ rf"\b{hour_word}\s+{min_pattern}\s+({prefix_class})\.?m\.?\b",
54
+ re.IGNORECASE,
55
+ ),
56
+ lambda m, h=hour_num, mn=min_num: f"{h}:{mn} {m.group(1)}m",
57
+ )
58
+ )
59
+
60
+ for hour_word, hour_num in time_words.items():
61
+ for min_word, min_num in time_words.items():
62
+ minute_word.append(
63
+ (
64
+ re.compile(
65
+ rf"\b{hour_word}\s+{min_word}\s+({prefix_class})\.?m\.?\b",
66
+ re.IGNORECASE,
67
+ ),
68
+ lambda m, h=hour_num, mn=min_num: f"{h}:{mn} {m.group(1)}m",
69
+ )
70
+ )
71
+
72
+ for word, num in time_words.items():
73
+ hour_only.append(
74
+ (
75
+ re.compile(
76
+ rf"\b{word}\s+({prefix_class})\.?m\.?\b",
77
+ re.IGNORECASE,
78
+ ),
79
+ lambda m, n=num: f"{n} {m.group(1)}m",
80
+ )
81
+ )
82
+
83
+ if oclock_word is not None:
84
+ escaped = re.escape(oclock_word)
85
+ oclock = (
86
+ re.compile(rf"(\d{{1,2}})\s+{escaped}", re.IGNORECASE),
87
+ lambda m: f"{m.group(1)}:00",
88
+ )
89
+ if am_word is not None and pm_word is not None:
90
+ prefix_class = _ampm_prefix_class(am_word, pm_word)
91
+ oclock_ampm = (
92
+ re.compile(
93
+ rf"(\d{{1,2}}):00\s+({prefix_class})\.?m\.?\b", re.IGNORECASE
94
+ ),
95
+ lambda m: f"{m.group(1)} {m.group(2)}m",
96
+ )
97
+
98
+ return _CompiledPatterns(
99
+ compound_minute, minute_word, hour_only, oclock, oclock_ampm
100
+ )
101
+
102
+
103
+ @register_step
104
+ class ConvertWordBasedTimePatternsStep(TextStep):
105
+ """Convert word-based time patterns (two p.m -> 2 pm, two thirty p.m -> 2:30 pm).
106
+
107
+ Reads operators.config.time_words, operators.config.am_word,
108
+ operators.config.pm_word, operators.config.oclock_word, and
109
+ operators.get_compound_minutes().
110
+ No-op when required config is None.
111
+
112
+ Regex patterns are compiled once per operators config instance and cached
113
+ on the step to avoid recompilation on every call.
114
+ """
115
+
116
+ name = "convert_word_based_time_patterns"
117
+
118
+ def __init__(self) -> None:
119
+ self._cache: dict[int, _CompiledPatterns] = {}
120
+
121
+ def _get_patterns(self, operators: LanguageOperators) -> _CompiledPatterns:
122
+ key = id(operators.config)
123
+ if key not in self._cache:
124
+ self._cache[key] = _build_compiled_patterns(operators)
125
+ return self._cache[key]
126
+
127
+ def __call__(self, text: str, operators: LanguageOperators) -> str:
128
+ patterns = self._get_patterns(operators)
129
+
130
+ for compiled, repl in patterns.compound_minute:
131
+ text = compiled.sub(repl, text)
132
+
133
+ for compiled, repl in patterns.minute_word:
134
+ text = compiled.sub(repl, text)
135
+
136
+ for compiled, repl in patterns.hour_only:
137
+ text = compiled.sub(repl, text)
138
+
139
+ if patterns.oclock is not None:
140
+ compiled, repl = patterns.oclock
141
+ text = compiled.sub(repl, text)
142
+
143
+ if patterns.oclock_ampm is not None:
144
+ compiled, repl = patterns.oclock_ampm
145
+ text = compiled.sub(repl, text)
146
+
147
+ return text
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "gladia-normalization"
3
- version = "0.1.0a4"
3
+ version = "0.1.1"
4
4
  description = "Normalize speech-to-text output for fair Word Error Rate (WER) comparison"
5
5
  readme = "README.md"
6
6
  authors = [{ name = "Gladia", email = "support@gladia.io" }]
@@ -0,0 +1,32 @@
1
+ import time
2
+
3
+ import pytest
4
+
5
+ from normalization.languages.registry import get_language_registry
6
+ from normalization.pipeline.loader import load_pipeline
7
+
8
+ _ITERATIONS = 300
9
+ _MAX_TOTAL_SECONDS = 5.0
10
+ _SAMPLE_TEXT = "I love gladia S.T.T."
11
+
12
+ _LANGUAGE_CODES = sorted(code for code in get_language_registry())
13
+
14
+
15
+ @pytest.mark.parametrize("language", _LANGUAGE_CODES)
16
+ def test_gladia_3_latency(language: str) -> None:
17
+ pipeline = load_pipeline("gladia-3", language)
18
+
19
+ start_time = time.time()
20
+ for i in range(_ITERATIONS):
21
+ pipeline.normalize(_SAMPLE_TEXT)
22
+ if time.time() - start_time > _MAX_TOTAL_SECONDS:
23
+ print(
24
+ f"[{language}] {i} normalizations took more than {_MAX_TOTAL_SECONDS}s"
25
+ )
26
+ break
27
+ elapsed = time.time() - start_time
28
+
29
+ assert elapsed < _MAX_TOTAL_SECONDS, (
30
+ f"[{language}] {_ITERATIONS} normalizations took {elapsed:.2f}s "
31
+ f"(limit: {_MAX_TOTAL_SECONDS}s)"
32
+ )
@@ -0,0 +1,67 @@
1
+ from normalization.languages.english import EnglishOperators
2
+ from normalization.steps.text.convert_word_based_time_patterns import (
3
+ ConvertWordBasedTimePatternsStep,
4
+ )
5
+
6
+ from .conftest import assert_text_step_registered
7
+
8
+
9
+ def test_step_is_registered():
10
+ assert_text_step_registered(ConvertWordBasedTimePatternsStep)
11
+
12
+
13
+ def test_hour_with_ampm(english_operators: EnglishOperators):
14
+ step = ConvertWordBasedTimePatternsStep()
15
+ assert step("call at two p.m", english_operators) == "call at 2 pm"
16
+
17
+
18
+ def test_hour_with_am(english_operators: EnglishOperators):
19
+ step = ConvertWordBasedTimePatternsStep()
20
+ assert step("wake up at six a.m", english_operators) == "wake up at 6 am"
21
+
22
+
23
+ def test_hour_and_minute_word_with_ampm(english_operators: EnglishOperators):
24
+ step = ConvertWordBasedTimePatternsStep()
25
+ assert step("meeting at two thirty p.m", english_operators) == "meeting at 2:30 pm"
26
+
27
+
28
+ def test_hour_and_compound_minute_hyphenated(english_operators: EnglishOperators):
29
+ step = ConvertWordBasedTimePatternsStep()
30
+ assert step("at two twenty-one p.m", english_operators) == "at 2:21 pm"
31
+
32
+
33
+ def test_hour_and_compound_minute_spaced(english_operators: EnglishOperators):
34
+ step = ConvertWordBasedTimePatternsStep()
35
+ assert step("at two twenty one pm", english_operators) == "at 2:21 pm"
36
+
37
+
38
+ def test_oclock_with_digit(english_operators: EnglishOperators):
39
+ step = ConvertWordBasedTimePatternsStep()
40
+ assert step("it is 8 o'clock", english_operators) == "it is 8:00"
41
+
42
+
43
+ def test_oclock_ampm_dotted_am(english_operators: EnglishOperators):
44
+ # Mid-sentence so \b matches after the dotted meridiem (same as hour_only convention)
45
+ step = ConvertWordBasedTimePatternsStep()
46
+ assert (
47
+ step("wake up at 6:00 a.m today", english_operators) == "wake up at 6 am today"
48
+ )
49
+
50
+
51
+ def test_oclock_ampm_dotted_pm(english_operators: EnglishOperators):
52
+ step = ConvertWordBasedTimePatternsStep()
53
+ assert (
54
+ step("meeting at 8:00 p.m tomorrow", english_operators)
55
+ == "meeting at 8 pm tomorrow"
56
+ )
57
+
58
+
59
+ def test_oclock_ampm_no_dots(english_operators: EnglishOperators):
60
+ step = ConvertWordBasedTimePatternsStep()
61
+ assert step("meeting at 8:00 pm", english_operators) == "meeting at 8 pm"
62
+
63
+
64
+ def test_oclock_ampm_case_insensitive(english_operators: EnglishOperators):
65
+ step = ConvertWordBasedTimePatternsStep()
66
+ # re.IGNORECASE: uppercase letter is captured as-is; pipeline casefolding normalises before this step
67
+ assert step("call at 3:00 PM", english_operators) == "call at 3 Pm"
@@ -72,7 +72,7 @@ wheels = [
72
72
 
73
73
  [[package]]
74
74
  name = "gladia-normalization"
75
- version = "0.1.0a4"
75
+ version = "0.1.1"
76
76
  source = { editable = "." }
77
77
  dependencies = [
78
78
  { name = "contractions" },
@@ -1,38 +0,0 @@
1
- import logging
2
- import re
3
-
4
- logger = logging.getLogger(__name__)
5
-
6
-
7
- class Replacer:
8
- """Stateful compiled-regex engine for word-level replacements.
9
-
10
- Pre-compiles one \\b-bounded pattern per mapping entry at init time.
11
- At least one side of each mapping pair must be a single word.
12
- """
13
-
14
- def __init__(self, mapping: dict[str, str]):
15
- patterns: dict[re.Pattern, str] = {}
16
- for left, right in mapping.items():
17
- left_is_single = len(left.strip().split()) == 1
18
- right_is_single = len(right.strip().split()) == 1
19
-
20
- if not left_is_single and not right_is_single:
21
- logger.warning(
22
- f"At least one side must be a single word: '{left}' - '{right}'"
23
- )
24
- continue
25
-
26
- if left_is_single:
27
- bad, good = left, right
28
- else:
29
- bad, good = right, left
30
-
31
- patterns[re.compile(rf"\b{re.escape(bad)}\b")] = good
32
-
33
- self.patterns = patterns
34
-
35
- def __call__(self, text: str) -> str:
36
- for pattern, replacement in self.patterns.items():
37
- text = pattern.sub(replacement, text)
38
- return text
@@ -1,70 +0,0 @@
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