text2spoken 0.0.2a1__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.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 TigreGotico Lda
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,100 @@
1
+ Metadata-Version: 2.4
2
+ Name: text2spoken
3
+ Version: 0.0.2a1
4
+ Summary: Rewrite written text into the words a voice speaks: numbers, dates, times, units, titles and contractions
5
+ Author-email: JarbasAI <jarbasai@mailfence.com>
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/TigreGotico/text2spoken
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: Apache Software License
10
+ Classifier: Operating System :: OS Independent
11
+ Classifier: Topic :: Text Processing :: Linguistic
12
+ Requires-Python: >=3.11
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+ Requires-Dist: ovos-number-parser>=0.4.0
16
+ Requires-Dist: ovos-date-parser>=0.6.4a1
17
+ Requires-Dist: unicode_rbnf>=2.0.0
18
+ Provides-Extra: test
19
+ Requires-Dist: pytest; extra == "test"
20
+ Dynamic: license-file
21
+
22
+ # text2spoken
23
+
24
+ text2spoken rewrites written text into the words a voice speaks. It expands numbers, dates, times, units, titles and contractions. A phonemizer then reads words, not digits or symbols.
25
+
26
+ ## Quickstart
27
+
28
+ ```bash
29
+ pip install --pre text2spoken
30
+ ```
31
+
32
+ ```python
33
+ from text2spoken import normalize
34
+
35
+ print(normalize("Dr. Smith paid 3/4 of the bill at 25°C.", "en"))
36
+ print(normalize("O Sr. Silva pagou 10kg às 14h30.", "pt-PT"))
37
+ ```
38
+
39
+ ```text
40
+ Doctor Smith paid three quarters of the bill at twenty five degrees celsius.
41
+ O Senhor Silva pagou dez quilogramas às catorze e trinta.
42
+ ```
43
+
44
+ ## Where this fits
45
+
46
+ Alone: it turns written text into spoken words, with `from text2spoken import normalize`.
47
+ In the stack: it is the normalisation stage. A frontend or a TTS library calls it before phonemization, and it calls `ovos-number-parser`, `ovos-date-parser` and `unicode_rbnf`.
48
+ Through scriptconv: scriptconv has no `normalize` extra. Install `text2spoken` directly.
49
+
50
+ ## What `normalize` does
51
+
52
+ `normalize(text, lang)` runs four passes in this order:
53
+
54
+ 1. It expands dates and times, such as `03/08/2025` and `14h30`.
55
+ 2. It splits a word from a digit that a hyphen joins, such as `sub-23`. A range such as `3-2` stays as it is.
56
+ 3. It expands units and ordinal indicators, such as `10kg`, `25°C` and `1º`.
57
+ 4. It reads each word, and expands contractions, titles, fractions and numbers.
58
+
59
+ Each pass is also a plain function, so a frontend can compose them and add its own rules. See [docs/api.md](docs/api.md).
60
+
61
+ The language tag selects the rules. The tables use the part before the first hyphen, so `pt-BR` and `pt-PT` share one table. `en-US` reads an ambiguous date as month first, and every other tag reads it as day first.
62
+
63
+ ## Languages
64
+
65
+ These languages have tables in `text2spoken/tables.py`:
66
+
67
+ | Table | Languages |
68
+ | --- | --- |
69
+ | `CONTRACTIONS` | en |
70
+ | `TITLES` | ca, de, en, es, fr, gl, it, nl, pt |
71
+ | `UNITS` | de, en, es, fr, pt |
72
+
73
+ This command prints the same list from the installed package:
74
+
75
+ ```bash
76
+ python -c "from text2spoken import CONTRACTIONS, TITLES, UNITS; [print(sorted(t)) for t in (CONTRACTIONS, TITLES, UNITS)]"
77
+ ```
78
+
79
+ Numbers, dates and times come from `ovos-number-parser` and `ovos-date-parser`, so those packages set their language coverage.
80
+
81
+ To add a language, add its entries to `text2spoken/tables.py`. A new language needs no code change.
82
+
83
+ ## Behaviour to know
84
+
85
+ - The library uses no network and loads no model, so it works offline.
86
+ - A language that the parsers do not support keeps its digits. The library logs an error and returns the text.
87
+ - A `text` or `lang` value of the wrong type raises `TypeError` or `AttributeError` from the code that reads it.
88
+ - Importing the library imports `ovos-date-parser`, which sets the import time.
89
+ - `ovos-config`, a dependency of `ovos-date-parser`, creates `~/.config/mycroft` at import.
90
+
91
+ ## Development
92
+
93
+ ```bash
94
+ uv pip install --prerelease=allow -e ".[test]"
95
+ pytest tests
96
+ ```
97
+
98
+ ## Licence
99
+
100
+ Apache-2.0. See [LICENSE](LICENSE).
@@ -0,0 +1,79 @@
1
+ # text2spoken
2
+
3
+ text2spoken rewrites written text into the words a voice speaks. It expands numbers, dates, times, units, titles and contractions. A phonemizer then reads words, not digits or symbols.
4
+
5
+ ## Quickstart
6
+
7
+ ```bash
8
+ pip install --pre text2spoken
9
+ ```
10
+
11
+ ```python
12
+ from text2spoken import normalize
13
+
14
+ print(normalize("Dr. Smith paid 3/4 of the bill at 25°C.", "en"))
15
+ print(normalize("O Sr. Silva pagou 10kg às 14h30.", "pt-PT"))
16
+ ```
17
+
18
+ ```text
19
+ Doctor Smith paid three quarters of the bill at twenty five degrees celsius.
20
+ O Senhor Silva pagou dez quilogramas às catorze e trinta.
21
+ ```
22
+
23
+ ## Where this fits
24
+
25
+ Alone: it turns written text into spoken words, with `from text2spoken import normalize`.
26
+ In the stack: it is the normalisation stage. A frontend or a TTS library calls it before phonemization, and it calls `ovos-number-parser`, `ovos-date-parser` and `unicode_rbnf`.
27
+ Through scriptconv: scriptconv has no `normalize` extra. Install `text2spoken` directly.
28
+
29
+ ## What `normalize` does
30
+
31
+ `normalize(text, lang)` runs four passes in this order:
32
+
33
+ 1. It expands dates and times, such as `03/08/2025` and `14h30`.
34
+ 2. It splits a word from a digit that a hyphen joins, such as `sub-23`. A range such as `3-2` stays as it is.
35
+ 3. It expands units and ordinal indicators, such as `10kg`, `25°C` and `1º`.
36
+ 4. It reads each word, and expands contractions, titles, fractions and numbers.
37
+
38
+ Each pass is also a plain function, so a frontend can compose them and add its own rules. See [docs/api.md](docs/api.md).
39
+
40
+ The language tag selects the rules. The tables use the part before the first hyphen, so `pt-BR` and `pt-PT` share one table. `en-US` reads an ambiguous date as month first, and every other tag reads it as day first.
41
+
42
+ ## Languages
43
+
44
+ These languages have tables in `text2spoken/tables.py`:
45
+
46
+ | Table | Languages |
47
+ | --- | --- |
48
+ | `CONTRACTIONS` | en |
49
+ | `TITLES` | ca, de, en, es, fr, gl, it, nl, pt |
50
+ | `UNITS` | de, en, es, fr, pt |
51
+
52
+ This command prints the same list from the installed package:
53
+
54
+ ```bash
55
+ python -c "from text2spoken import CONTRACTIONS, TITLES, UNITS; [print(sorted(t)) for t in (CONTRACTIONS, TITLES, UNITS)]"
56
+ ```
57
+
58
+ Numbers, dates and times come from `ovos-number-parser` and `ovos-date-parser`, so those packages set their language coverage.
59
+
60
+ To add a language, add its entries to `text2spoken/tables.py`. A new language needs no code change.
61
+
62
+ ## Behaviour to know
63
+
64
+ - The library uses no network and loads no model, so it works offline.
65
+ - A language that the parsers do not support keeps its digits. The library logs an error and returns the text.
66
+ - A `text` or `lang` value of the wrong type raises `TypeError` or `AttributeError` from the code that reads it.
67
+ - Importing the library imports `ovos-date-parser`, which sets the import time.
68
+ - `ovos-config`, a dependency of `ovos-date-parser`, creates `~/.config/mycroft` at import.
69
+
70
+ ## Development
71
+
72
+ ```bash
73
+ uv pip install --prerelease=allow -e ".[test]"
74
+ pytest tests
75
+ ```
76
+
77
+ ## Licence
78
+
79
+ Apache-2.0. See [LICENSE](LICENSE).
@@ -0,0 +1,38 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "text2spoken"
7
+ dynamic = ["version"]
8
+ description = "Rewrite written text into the words a voice speaks: numbers, dates, times, units, titles and contractions"
9
+ license = {text = "Apache-2.0"}
10
+ classifiers = [
11
+ "Programming Language :: Python :: 3",
12
+ "License :: OSI Approved :: Apache Software License",
13
+ "Operating System :: OS Independent",
14
+ "Topic :: Text Processing :: Linguistic",
15
+ ]
16
+ authors = [{name = "JarbasAI", email = "jarbasai@mailfence.com"}]
17
+ readme = "README.md"
18
+ requires-python = ">=3.11"
19
+ dependencies = [
20
+ "ovos-number-parser>=0.4.0",
21
+ "ovos-date-parser>=0.6.4a1",
22
+ "unicode_rbnf>=2.0.0",
23
+ ]
24
+
25
+ [project.urls]
26
+ Homepage = "https://github.com/TigreGotico/text2spoken"
27
+
28
+ [project.optional-dependencies]
29
+ test = [
30
+ "pytest",
31
+ ]
32
+
33
+ [tool.setuptools.dynamic]
34
+ version = {attr = "text2spoken.version.__version__"}
35
+
36
+ [tool.setuptools.packages.find]
37
+ where = ["."]
38
+ include = ["text2spoken*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,93 @@
1
+ import logging
2
+ import unittest
3
+
4
+ import text2spoken as normalization
5
+ from text2spoken import datetimes, numbers, tables, text
6
+ from text2spoken import units as units_module
7
+
8
+ # Every name the package exports from its modules, with the module that owns it.
9
+ MOVED_NAMES = {
10
+ "CONTRACTIONS": tables, "TITLES": tables, "UNITS": tables,
11
+ "normalize": text, "is_fraction": numbers,
12
+ "pronounce_date": datetimes, "pronounce_time": datetimes,
13
+ "_get_number_separators": numbers, "_normalize_number_word": numbers,
14
+ "_normalize_dates_and_times": datetimes, "_normalize_word_hyphen_digit": text,
15
+ "_normalize_units": units_module, "_normalize_word": text,
16
+ }
17
+
18
+
19
+ class TestPackageExports(unittest.TestCase):
20
+
21
+ def test_every_moved_name_is_importable_from_the_package(self):
22
+ for name, owner in MOVED_NAMES.items():
23
+ with self.subTest(name=name):
24
+ self.assertIs(getattr(normalization, name), getattr(owner, name))
25
+
26
+ def test_each_module_logs_to_the_library_logger(self):
27
+ for module in (datetimes, numbers, text, units_module):
28
+ with self.subTest(module=module.__name__):
29
+ self.assertIs(module.LOG, logging.getLogger("text2spoken"))
30
+
31
+ def test_lang_identity_helpers_stay_outside_the_library(self):
32
+ for name in ("normalize_lang", "match_lang"):
33
+ with self.subTest(name=name):
34
+ self.assertFalse(hasattr(normalization, name))
35
+
36
+
37
+ class TestPackageLayout(unittest.TestCase):
38
+ """Tables are data, apart from the code that applies them."""
39
+
40
+ def test_tables_module_holds_only_data(self):
41
+ self.assertEqual(
42
+ {"CONTRACTIONS", "TITLES", "UNITS"},
43
+ {n for n in vars(tables) if not n.startswith("__")},
44
+ )
45
+
46
+ def test_each_concern_owns_its_transformations(self):
47
+ self.assertTrue(hasattr(numbers, "_normalize_number_word"))
48
+ self.assertTrue(hasattr(datetimes, "_normalize_dates_and_times"))
49
+ self.assertTrue(hasattr(units_module, "_normalize_units"))
50
+ self.assertTrue(hasattr(text, "normalize"))
51
+
52
+
53
+ class TestPipelineStillNormalizes(unittest.TestCase):
54
+ """A spot check on the whole pipeline, independent of the unit tests."""
55
+
56
+ def test_english_sentence_leaves_no_digits_or_symbols(self):
57
+ out = normalization.normalize("Dr. Smith owes me 12.5% of 3/4 at 25°C", "en")
58
+ self.assertFalse(any(c.isdigit() for c in out), out)
59
+ self.assertIn("Doctor", out)
60
+ self.assertIn("per cent", out)
61
+ self.assertIn("degrees celsius", out)
62
+
63
+ def test_portuguese_uses_the_portuguese_tables(self):
64
+ out = normalization.normalize("Sr. Silva pagou 10kg", "pt")
65
+ self.assertIn("Senhor", out)
66
+ self.assertIn("quilogramas", out)
67
+
68
+ def test_digit_hyphen_digit_score_is_left_intact_pt(self):
69
+ # A score is a range, not a word glued to a digit: the phonemizer,
70
+ # not this pipeline, is responsible for reading "3-2" aloud.
71
+ out = normalization.normalize("O jogo é 3-2.", "pt-PT")
72
+ self.assertEqual(out, "O jogo é 3-2.")
73
+
74
+ def test_digit_hyphen_digit_score_is_left_intact_en(self):
75
+ out = normalization.normalize("The score is 3-2.", "en-US")
76
+ self.assertEqual(out, "The score is 3-2.")
77
+
78
+ def test_digit_hyphen_digit_page_range_is_left_intact(self):
79
+ out = normalization.normalize("páginas 1139-1185", "pt-PT")
80
+ self.assertEqual(out, "páginas 1139-1185")
81
+
82
+ def test_digit_en_dash_digit_range_is_left_intact(self):
83
+ out = normalization.normalize("páginas 1139–1185", "pt-PT")
84
+ self.assertEqual(out, "páginas 1139–1185")
85
+
86
+ def test_word_hyphen_digit_still_normalizes(self):
87
+ out = normalization.normalize("sub-23", "en-US")
88
+ self.assertNotIn("-", out)
89
+ self.assertIn("twenty", out.lower())
90
+
91
+
92
+ if __name__ == "__main__":
93
+ unittest.main()
@@ -0,0 +1,110 @@
1
+ import unittest
2
+
3
+ from text2spoken import _normalize_dates_and_times
4
+
5
+
6
+ class TestAmPmNormalization(unittest.TestCase):
7
+ """Regression tests for digit-anchored am/pm normalization.
8
+
9
+ Previously a global text.replace("am", "A M").replace("pm", "P M")
10
+ corrupted any ordinary word containing "am" or "pm", e.g. "am", "spam",
11
+ "program", "campus". The fix anchors the substitution to a preceding
12
+ digit so only actual time expressions are affected.
13
+ """
14
+
15
+ def test_am_word_untouched(self):
16
+ result = _normalize_dates_and_times("I am happy today", "en-US")
17
+ self.assertEqual(result, "I am happy today")
18
+
19
+ def test_spam_untouched(self):
20
+ result = _normalize_dates_and_times("this is spam", "en-US")
21
+ self.assertEqual(result, "this is spam")
22
+
23
+ def test_campus_program_untouched(self):
24
+ result = _normalize_dates_and_times("campus program schedule", "en-US")
25
+ self.assertEqual(result, "campus program schedule")
26
+
27
+ def test_am_radio_casing_untouched(self):
28
+ result = _normalize_dates_and_times("tune in to AM radio", "en-US")
29
+ self.assertEqual(result, "tune in to AM radio")
30
+
31
+ def test_combined_words_and_real_time(self):
32
+ result = _normalize_dates_and_times(
33
+ "I am happy this is spam, meet me at 9am", "en-US"
34
+ )
35
+ self.assertEqual(result, "I am happy this is spam, meet me at 9 A M")
36
+
37
+ def test_time_9am(self):
38
+ result = _normalize_dates_and_times("the train leaves at 9am", "en-US")
39
+ self.assertEqual(result, "the train leaves at 9 A M")
40
+
41
+ def test_time_with_space(self):
42
+ result = _normalize_dates_and_times("lunch is at 10 pm", "en-US")
43
+ self.assertEqual(result, "lunch is at 10 P M")
44
+
45
+ def test_time_uppercase(self):
46
+ result = _normalize_dates_and_times("wake up at 7AM sharp", "en-US")
47
+ self.assertEqual(result, "wake up at 7 A M sharp")
48
+
49
+ def test_time_dotted_pm(self):
50
+ result = _normalize_dates_and_times("arrive by 12p.m.", "en-US")
51
+ self.assertTrue(result.startswith("arrive by 12 P M"))
52
+
53
+ def test_multiple_times_in_one_sentence(self):
54
+ result = _normalize_dates_and_times(
55
+ "open from 9am to 5pm daily", "en-US"
56
+ )
57
+ self.assertEqual(result, "open from 9 A M to 5 P M daily")
58
+
59
+ def test_non_english_language_untouched(self):
60
+ # am/pm normalization is English-only; other languages must be
61
+ # unaffected even when the substrings "am"/"pm" appear.
62
+ text = "Je suis am spam et le programme est fantastique"
63
+ result = _normalize_dates_and_times(text, "fr-FR")
64
+ self.assertEqual(result, text)
65
+
66
+
67
+ class TestMultiDateExpansion(unittest.TestCase):
68
+ """Regression tests for expanding every date in the text.
69
+
70
+ Previously date_pattern.search(text) only located and expanded the
71
+ FIRST date, leaving any subsequent dates in the text unspoken.
72
+ """
73
+
74
+ def test_single_date_expanded(self):
75
+ result = _normalize_dates_and_times(
76
+ "The event is on 12/25/2024", "en-US", "MDY"
77
+ )
78
+ self.assertNotIn("12/25/2024", result)
79
+ self.assertIn("december", result.lower())
80
+
81
+ def test_two_dates_both_expanded(self):
82
+ result = _normalize_dates_and_times(
83
+ "From 01/02/2025 until 03/04/2025", "en-US", "MDY"
84
+ )
85
+ self.assertNotIn("01/02/2025", result)
86
+ self.assertNotIn("03/04/2025", result)
87
+ self.assertIn("january", result.lower())
88
+ self.assertIn("march", result.lower())
89
+
90
+ def test_three_dates_all_expanded(self):
91
+ result = _normalize_dates_and_times(
92
+ "Dates: 01/02/2025, 03/04/2025 and 05/06/2025", "en-US", "MDY"
93
+ )
94
+ for raw_date in ("01/02/2025", "03/04/2025", "05/06/2025"):
95
+ self.assertNotIn(raw_date, result)
96
+
97
+ def test_identical_dates_both_expanded(self):
98
+ # Regression: the old implementation used text.replace(match, ...)
99
+ # which would replace ALL occurrences of an identical date string
100
+ # from a single match, but relied on search() to only ever find
101
+ # the first one. Confirm repeated identical dates are handled.
102
+ result = _normalize_dates_and_times(
103
+ "Reminder on 01/02/2025 and again 01/02/2025", "en-US", "MDY"
104
+ )
105
+ self.assertNotIn("01/02/2025", result)
106
+ self.assertEqual(result.lower().count("january"), 2)
107
+
108
+
109
+ if __name__ == "__main__":
110
+ unittest.main()
@@ -0,0 +1,50 @@
1
+ """The plain functions that a frontend composes around its own rules."""
2
+ import unittest
3
+
4
+ import text2spoken
5
+ from text2spoken import (
6
+ expand_dates_and_times,
7
+ expand_hyphen_digits,
8
+ expand_units,
9
+ expand_word,
10
+ normalize,
11
+ )
12
+
13
+
14
+ class TestPublicFunctions(unittest.TestCase):
15
+
16
+ def test_all_names_resolve(self):
17
+ for name in text2spoken.__all__:
18
+ with self.subTest(name=name):
19
+ self.assertTrue(hasattr(text2spoken, name))
20
+
21
+ def test_version_is_the_version_module_value(self):
22
+ from text2spoken.version import __version__
23
+ self.assertEqual(text2spoken.__version__, __version__)
24
+
25
+ def test_expand_hyphen_digits_splits_a_word_from_a_digit(self):
26
+ self.assertEqual(expand_hyphen_digits("sub-23"), "sub 23")
27
+
28
+ def test_expand_hyphen_digits_keeps_a_range(self):
29
+ self.assertEqual(expand_hyphen_digits("3-2"), "3-2")
30
+
31
+ def test_expand_units_reads_the_portuguese_table(self):
32
+ self.assertEqual(expand_units("10kg", "pt"), "dez quilogramas")
33
+
34
+ def test_expand_dates_and_times_leaves_plain_words(self):
35
+ self.assertEqual(expand_dates_and_times("I am happy", "en"), "I am happy")
36
+
37
+ def test_expand_word_reads_a_contraction(self):
38
+ self.assertEqual(expand_word("can't", "en"), "can not")
39
+
40
+ def test_expand_word_reads_a_number_and_keeps_its_punctuation(self):
41
+ self.assertEqual(expand_word("23,", "en"), "twenty three,")
42
+
43
+ def test_expand_word_equals_the_word_pass_of_normalize(self):
44
+ for word in ("Dr.", "doesn't", "12", "3/4", "hello"):
45
+ with self.subTest(word=word):
46
+ self.assertEqual(expand_word(word, "en"), normalize(word, "en"))
47
+
48
+
49
+ if __name__ == "__main__":
50
+ unittest.main()