Skip to content

Commit

Permalink
https://github.com/MycroftAI/lingua-franca/pull/150
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed May 9, 2021
1 parent cfbbd19 commit 34d3f7d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 7 deletions.
29 changes: 23 additions & 6 deletions lingua_nostra/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from warnings import warn
from os.path import join


from lingua_nostra.bracket_expansion import SentenceTreeParser
from lingua_nostra.internal import localized_function, \
populate_localized_function_dict, get_active_langs, \
Expand All @@ -30,10 +29,10 @@
UnsupportedLanguageError, NoneLangWarning, InvalidLangWarning, \
FunctionNotLocalizedError


_REGISTERED_FUNCTIONS = ("nice_number",
"nice_time",
"pronounce_number",
"pronounce_digits",
"nice_response",
"nice_duration")

Expand Down Expand Up @@ -119,12 +118,12 @@ def _number_strings(self, number, lang):
str(int(number % 100 / 10))) or str(int(number % 100 / 10))
x0 = (self.lang_config[lang]['number'].get(
str(int(number % 100 / 10) * 10)) or
str(int(number % 100 / 10) * 10))
str(int(number % 100 / 10) * 10))
xxx = (self.lang_config[lang]['number'].get(str(number % 1000)) or
str(number % 1000))
x00 = (self.lang_config[lang]['number'].get(str(int(
number % 1000 / 100) * 100)) or
str(int(number % 1000 / 100) * 100))
str(int(number % 1000 / 100) * 100))
x_in_x00 = self.lang_config[lang]['number'].get(str(int(
number % 1000 / 100))) or str(int(number % 1000 / 100))
xx00 = self.lang_config[lang]['number'].get(str(int(
Expand All @@ -134,11 +133,12 @@ def _number_strings(self, number, lang):
number % 10000 / 100))) or str(int(number % 10000 / 100))
x000 = (self.lang_config[lang]['number'].get(str(int(
number % 10000 / 1000) * 1000)) or
str(int(number % 10000 / 1000) * 1000))
str(int(number % 10000 / 1000) * 1000))
x_in_x000 = self.lang_config[lang]['number'].get(str(int(
number % 10000 / 1000))) or str(int(number % 10000 / 1000))
x0_in_x000 = self.lang_config[lang]['number'].get(str(int(
number % 10000 / 1000) * 10)) or str(int(number % 10000 / 1000) * 10)
number % 10000 / 1000) * 10)) or str(
int(number % 10000 / 1000) * 10)
x_in_0x00 = self.lang_config[lang]['number'].get(str(int(
number % 1000 / 100)) or str(int(number % 1000 / 100)))

Expand Down Expand Up @@ -305,6 +305,23 @@ def pronounce_number(number, lang='', places=2, short_scale=True,
"""


@localized_function()
def pronounce_digits(number, lang=None, places=2, all_digits=False):
"""
Pronounce a number's digits, either colloquially or in full
In English, the colloquial way is usually to read two digits at a time,
treating each pair as a single number.
Examples:
>>> pronounce_number(127, all_digits=False)
'one twenty seven'
>>> pronounce_number(127, all_digits=True)
'one two seven'
Args:
number (int|float)
all_digits (bool): read every digit, rather than two digits at a time
"""


def nice_date(dt, lang='', now=None):
"""
Format a datetime to a pronounceable date
Expand Down
37 changes: 36 additions & 1 deletion lingua_nostra/lang/format_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

from math import modf
from lingua_nostra.lang.format_common import convert_to_mixed_fraction
from lingua_nostra.lang.common_data_en import _NUM_STRING_EN, \
_FRACTION_STRING_EN, _LONG_SCALE_EN, _SHORT_SCALE_EN, _SHORT_ORDINAL_EN, _LONG_ORDINAL_EN
Expand Down Expand Up @@ -65,6 +65,41 @@ def nice_number_en(number, speech=True, denominators=range(1, 21)):
return return_string


def pronounce_digits_en(number, places=2, all_digits=False):
decimal_part = ""
op_val = ""
result = []
is_float = isinstance(number, float)
if is_float:
op_val, decimal_part = [part for part in str(number).split(".")]
decimal_part = pronounce_number_en(
float("." + decimal_part), places=places).replace("zero ", "")
else:
op_val = str(number)

if all_digits:
result = [pronounce_number_en(int(i)) for i in op_val]
if is_float:
result.append(decimal_part)
result = " ".join(result)
else:
while len(op_val) > 1:
idx = -2 if len(op_val) in [2, 4] else -3
back_digits = op_val[idx:]
op_val = op_val[:idx]
result = pronounce_number_en(
int(back_digits)).split(" ") + result
if op_val:
result.insert(0, pronounce_number_en(int(op_val)))
if is_float:
result.append(decimal_part)
no_no_words = list(_SHORT_SCALE_EN.values())[:5]
no_no_words.append('and')
result = [word for word in result if word.strip() not in no_no_words]
result = " ".join(result)
return result


def pronounce_number_en(number, places=2, short_scale=True, scientific=False,
ordinals=False):
"""
Expand Down

0 comments on commit 34d3f7d

Please sign in to comment.