ovos-number-parser 0.0.1__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.
@@ -0,0 +1,274 @@
1
+ from typing import Union
2
+
3
+ from ovos_number_parser.numbers_az import numbers_to_digits_az, extract_number_az, is_fractional_az, pronounce_number_az
4
+ from ovos_number_parser.numbers_ca import pronounce_number_ca, is_fractional_ca, extract_number_ca
5
+ from ovos_number_parser.numbers_cs import numbers_to_digits_cs, pronounce_number_cs, is_fractional_cs, extract_number_cs
6
+ from ovos_number_parser.numbers_da import is_fractional_da, is_ordinal_da, pronounce_number_da, \
7
+ pronounce_ordinal_da, extract_number_da
8
+ from ovos_number_parser.numbers_de import numbers_to_digits_de, pronounce_number_de, pronounce_ordinal_de, \
9
+ is_ordinal_de, is_fractional_de, extract_number_de
10
+ from ovos_number_parser.numbers_en import numbers_to_digits_en, is_ordinal_en, pronounce_number_en, extract_number_en, \
11
+ is_fractional_en
12
+ from ovos_number_parser.numbers_es import numbers_to_digits_es, pronounce_number_es, extract_number_es, is_fractional_es
13
+ from ovos_number_parser.numbers_eu import pronounce_number_eu, extract_number_eu, is_fractional_eu
14
+ from ovos_number_parser.numbers_fa import pronounce_number_fa, extract_number_fa
15
+ from ovos_number_parser.numbers_fr import (pronounce_number_fr, extract_number_fr, is_fractional_fr)
16
+ from ovos_number_parser.numbers_hu import pronounce_number_hu, pronounce_ordinal_hu
17
+ from ovos_number_parser.numbers_it import (extract_number_it, pronounce_number_it, is_fractional_it)
18
+ from ovos_number_parser.numbers_nl import numbers_to_digits_nl, pronounce_number_nl, pronounce_ordinal_nl, \
19
+ extract_number_nl, is_fractional_nl
20
+ from ovos_number_parser.numbers_pl import numbers_to_digits_pl, pronounce_number_pl, extract_number_pl, is_fractional_pl
21
+ from ovos_number_parser.numbers_pt import numbers_to_digits_pt, pronounce_number_pt, is_fractional_pt, extract_number_pt
22
+ from ovos_number_parser.numbers_ru import numbers_to_digits_ru, pronounce_number_ru, extract_number_ru, is_fractional_ru
23
+ from ovos_number_parser.numbers_sv import pronounce_number_sv, pronounce_ordinal_sv, extract_number_sv, \
24
+ is_fractional_sv
25
+ from ovos_number_parser.numbers_uk import numbers_to_digits_uk, pronounce_number_uk, extract_number_uk, is_fractional_uk
26
+ from ovos_number_parser.numbers_sl import nice_number_sl, pronounce_number_sl
27
+
28
+
29
+ def numbers_to_digits(utterance: str, lang: str) -> str:
30
+ """
31
+ Replace written numbers in text with their digit equivalents.
32
+
33
+ Args:
34
+ utterance (str): Input string possibly containing written numbers.
35
+
36
+ Returns:
37
+ str: Text with written numbers replaced by digits.
38
+ """
39
+ if lang.startswith("az"):
40
+ return numbers_to_digits_az(utterance)
41
+ if lang.startswith("cs"):
42
+ return numbers_to_digits_cs(utterance)
43
+ if lang.startswith("de"):
44
+ return numbers_to_digits_de(utterance)
45
+ if lang.startswith("en"):
46
+ return numbers_to_digits_en(utterance)
47
+ if lang.startswith("es"):
48
+ return numbers_to_digits_es(utterance)
49
+ if lang.startswith("nl"):
50
+ return numbers_to_digits_nl(utterance)
51
+ if lang.startswith("pl"):
52
+ return numbers_to_digits_pl(utterance)
53
+ if lang.startswith("pt"):
54
+ return numbers_to_digits_pt(utterance)
55
+ if lang.startswith("ru"):
56
+ return numbers_to_digits_ru(utterance)
57
+ if lang.startswith("uk"):
58
+ return numbers_to_digits_uk(utterance)
59
+ raise NotImplementedError(f"Unsupported language: '{lang}'")
60
+
61
+
62
+ def pronounce_number(number: Union[int, float], lang: str, places: int = 2, short_scale: bool = True,
63
+ scientific: bool = False, ordinals: bool = False) -> str:
64
+ """
65
+ Convert a number to it's spoken equivalent
66
+
67
+ For example, '5' would be 'five'
68
+
69
+ Args:
70
+ number: the number to pronounce
71
+ lang (str, optional): an optional BCP-47 language code, if omitted
72
+ the default language will be used.
73
+ places (int): number of decimal places to express, default 2
74
+ short_scale (bool) : use short (True) or long scale (False)
75
+ https://en.wikipedia.org/wiki/Names_of_large_numbers
76
+ scientific (bool) : convert and pronounce in scientific notation
77
+ ordinals (bool): pronounce in ordinal form "first" instead of "one"
78
+ Returns:
79
+ (str): The pronounced number
80
+ """
81
+ if lang.startswith("en"):
82
+ return pronounce_number_en(number, places, short_scale, scientific, ordinals)
83
+ if lang.startswith("az"):
84
+ return pronounce_number_az(number, places, short_scale, scientific, ordinals)
85
+ if lang.startswith("ca"):
86
+ return pronounce_number_ca(number, places)
87
+ if lang.startswith("cs"):
88
+ return pronounce_number_en(number, places, short_scale, scientific, ordinals)
89
+ if lang.startswith("da"):
90
+ return pronounce_number_da(number, places, short_scale, scientific, ordinals)
91
+ if lang.startswith("de"):
92
+ return pronounce_number_de(number, places, short_scale, scientific, ordinals)
93
+ if lang.startswith("es"):
94
+ return pronounce_number_es(number, places)
95
+ if lang.startswith("eu"):
96
+ return pronounce_number_eu(number, places)
97
+ if lang.startswith("fa"):
98
+ return pronounce_number_fa(number, places, scientific, ordinals)
99
+ if lang.startswith("fr"):
100
+ return pronounce_number_fr(number, places)
101
+ if lang.startswith("hu"):
102
+ return pronounce_number_hu(number, places, short_scale, scientific, ordinals)
103
+ if lang.startswith("it"):
104
+ return pronounce_number_it(number, places, short_scale, scientific)
105
+ if lang.startswith("nl"):
106
+ return pronounce_number_nl(number, places, short_scale, scientific, ordinals)
107
+ if lang.startswith("pl"):
108
+ return pronounce_number_pl(number, places, short_scale, scientific, ordinals)
109
+ if lang.startswith("pt"):
110
+ return pronounce_number_pt(number, places)
111
+ if lang.startswith("ru"):
112
+ return pronounce_number_ru(number, places, short_scale, scientific, ordinals)
113
+ if lang.startswith("sl"):
114
+ return pronounce_number_sl(number, places, short_scale, scientific, ordinals)
115
+ if lang.startswith("sv"):
116
+ return pronounce_number_sv(number, places, short_scale, scientific, ordinals)
117
+ if lang.startswith("uk"):
118
+ return pronounce_number_uk(number, places, short_scale, scientific, ordinals)
119
+ raise NotImplementedError(f"Unsupported language: '{lang}'")
120
+
121
+
122
+ def pronounce_ordinal(number: Union[int, float], lang: str, short_scale: bool = True) -> str:
123
+ """
124
+ Convert an ordinal number to it's spoken equivalent
125
+
126
+ For example, '5' would be 'fifth'
127
+
128
+ Args:
129
+ number: the number to pronounce
130
+ lang (str, optional): an optional BCP-47 language code, if omitted
131
+ the default language will be used.
132
+ short_scale (bool) : use short (True) or long scale (False)
133
+ https://en.wikipedia.org/wiki/Names_of_large_numbers
134
+ Returns:
135
+ (str): The pronounced number
136
+ """
137
+ if lang.startswith("da"):
138
+ return pronounce_ordinal_da(number)
139
+ if lang.startswith("de"):
140
+ return pronounce_ordinal_de(number)
141
+ if lang.startswith("hu"):
142
+ return pronounce_ordinal_hu(number)
143
+ if lang.startswith("nl"):
144
+ return pronounce_ordinal_nl(number)
145
+ if lang.startswith("sv"):
146
+ return pronounce_ordinal_sv(number)
147
+ raise NotImplementedError(f"Unsupported language: '{lang}'")
148
+
149
+
150
+ def extract_number(text: str, lang: str, short_scale: bool = True, ordinals: bool = False) -> Union[int, float, bool]:
151
+ """Takes in a string and extracts a number.
152
+
153
+ Assumes only 1 number is in the string, does NOT handle multiple numbers
154
+
155
+ Args:
156
+ text (str): the string to extract a number from
157
+ short_scale (bool): Use "short scale" or "long scale" for large
158
+ numbers -- over a million. The default is short scale, which
159
+ is now common in most English speaking countries.
160
+ See https://en.wikipedia.org/wiki/Names_of_large_numbers
161
+ ordinals (bool): consider ordinal numbers, e.g. third=3 instead of 1/3
162
+ lang (str, optional): an optional BCP-47 language code, if omitted
163
+ the default language will be used.
164
+ Returns:
165
+ (int, float or False): The number extracted or False if the input
166
+ text contains no numbers
167
+ """
168
+ if lang.startswith("en"):
169
+ return extract_number_en(text, short_scale, ordinals)
170
+ if lang.startswith("az"):
171
+ return extract_number_az(text, short_scale, ordinals)
172
+ if lang.startswith("ca"):
173
+ return extract_number_ca(text, short_scale, ordinals)
174
+ if lang.startswith("cs"):
175
+ return extract_number_cs(text, short_scale, ordinals)
176
+ if lang.startswith("da"):
177
+ return extract_number_da(text, short_scale, ordinals)
178
+ if lang.startswith("de"):
179
+ return extract_number_de(text, short_scale, ordinals)
180
+ if lang.startswith("es"):
181
+ return extract_number_es(text, short_scale, ordinals)
182
+ if lang.startswith("eu"):
183
+ return extract_number_eu(text, short_scale, ordinals)
184
+ if lang.startswith("fa"):
185
+ return extract_number_fa(text, ordinals)
186
+ if lang.startswith("fr"):
187
+ return extract_number_fr(text, short_scale, ordinals)
188
+ if lang.startswith("it"):
189
+ return extract_number_it(text, short_scale, ordinals)
190
+ if lang.startswith("nl"):
191
+ return extract_number_nl(text, short_scale, ordinals)
192
+ if lang.startswith("pl"):
193
+ return extract_number_pl(text, short_scale, ordinals)
194
+ if lang.startswith("pt"):
195
+ return extract_number_pt(text, short_scale, ordinals)
196
+ if lang.startswith("ru"):
197
+ return extract_number_ru(text, short_scale, ordinals)
198
+ if lang.startswith("sv"):
199
+ return extract_number_sv(text, short_scale, ordinals)
200
+ if lang.startswith("uk"):
201
+ return extract_number_uk(text, short_scale, ordinals)
202
+ raise NotImplementedError(f"Unsupported language: '{lang}'")
203
+
204
+
205
+ def is_fractional(input_str: str, lang: str, short_scale: bool = True) -> Union[bool, float]:
206
+ """
207
+ This function takes the given text and checks if it is a fraction.
208
+ Used by most of the number exractors.
209
+
210
+ Will return False on phrases that *contain* a fraction. Only detects
211
+ exact matches. To pull a fraction from a string, see extract_number()
212
+
213
+ Args:
214
+ input_str (str): the string to check if fractional
215
+ short_scale (bool): use short scale if True, long scale if False
216
+ lang (str, optional): an optional BCP-47 language code, if omitted
217
+ the default language will be used.
218
+ Returns:
219
+ (bool) or (float): False if not a fraction, otherwise the fraction
220
+ """
221
+ if lang.startswith("en"):
222
+ return is_fractional_en(input_str, short_scale)
223
+ if lang.startswith("az"):
224
+ return is_fractional_az(input_str, short_scale)
225
+ if lang.startswith("ca"):
226
+ return is_fractional_ca(input_str, short_scale)
227
+ if lang.startswith("cs"):
228
+ return is_fractional_cs(input_str, short_scale)
229
+ if lang.startswith("da"):
230
+ return is_fractional_da(input_str, short_scale)
231
+ if lang.startswith("de"):
232
+ return is_fractional_de(input_str, short_scale)
233
+ if lang.startswith("es"):
234
+ return is_fractional_es(input_str, short_scale)
235
+ if lang.startswith("eu"):
236
+ return is_fractional_eu(input_str)
237
+ if lang.startswith("fr"):
238
+ return is_fractional_fr(input_str)
239
+ if lang.startswith("it"):
240
+ return is_fractional_it(input_str, short_scale)
241
+ if lang.startswith("nl"):
242
+ return is_fractional_pl(input_str, short_scale)
243
+ if lang.startswith("pl"):
244
+ return is_fractional_pl(input_str, short_scale)
245
+ if lang.startswith("pt"):
246
+ return is_fractional_pt(input_str, short_scale)
247
+ if lang.startswith("ru"):
248
+ return is_fractional_ru(input_str, short_scale)
249
+ if lang.startswith("sv"):
250
+ return is_fractional_sv(input_str, short_scale)
251
+ if lang.startswith("uk"):
252
+ return is_fractional_uk(input_str, short_scale)
253
+ raise NotImplementedError(f"Unsupported languags: '{lang}'")
254
+
255
+
256
+ def is_ordinal(input_str: str, lang: str) -> Union[bool, float]:
257
+ """
258
+ This function takes the given text and checks if it is an ordinal number.
259
+
260
+ Args:
261
+ input_str (str): the string to check if ordinal
262
+ lang (str, optional): an optional BCP-47 language code, if omitted
263
+ the default language will be used.
264
+ Returns:
265
+ (bool) or (float): False if not an ordinal, otherwise the number
266
+ corresponding to the ordinal
267
+ """
268
+ if lang.startswith("en"):
269
+ return is_ordinal_en(input_str)
270
+ if lang.startswith("de"):
271
+ return is_ordinal_de(input_str)
272
+ if lang.startswith("da"):
273
+ return is_ordinal_da(input_str)
274
+ raise NotImplementedError(f"Unsupported languags: '{lang}'")