From 5c424fecc130eca94e4f7f92d058656c3ca58e92 Mon Sep 17 00:00:00 2001 From: jarbasal Date: Tue, 19 Jun 2018 02:09:38 +0100 Subject: [PATCH] long scale --- mycroft/util/format.py | 7 ++++-- mycroft/util/lang/format_en.py | 34 +++++++++++++++++++++++++++--- test/unittests/util/test_format.py | 10 +++++++-- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/mycroft/util/format.py b/mycroft/util/format.py index 24fc64a247..a4f257b305 100755 --- a/mycroft/util/format.py +++ b/mycroft/util/format.py @@ -89,7 +89,7 @@ def nice_time(dt, lang="en-us", speech=True, use_24hour=False, return str(dt) -def pronounce_number(number, lang="en-us", places=2): +def pronounce_number(number, lang="en-us", places=2, short_scale=True): """ Convert a number to it's spoken equivalent @@ -97,12 +97,15 @@ def pronounce_number(number, lang="en-us", places=2): Args: number: the number to pronounce + short_scale (bool) : use short (True) or long scale (False) + https://en.wikipedia.org/wiki/Names_of_large_numbers Returns: (str): The pronounced number """ lang_lower = str(lang).lower() if lang_lower.startswith("en"): - return pronounce_number_en(number, places=places) + return pronounce_number_en(number, places=places, + short_scale=short_scale) elif lang_lower.startswith("it"): return pronounce_number_it(number, places=places) elif lang_lower.startswith("fr"): diff --git a/mycroft/util/lang/format_en.py b/mycroft/util/lang/format_en.py index 82d32d5791..0b884b7266 100644 --- a/mycroft/util/lang/format_en.py +++ b/mycroft/util/lang/format_en.py @@ -223,7 +223,7 @@ def nice_number_en(number, speech, denominators): return return_string -def pronounce_number_en(num, places=2, short_scale=False): +def pronounce_number_en(num, places=2, short_scale=True): """ Convert a number to it's spoken equivalent @@ -232,6 +232,8 @@ def pronounce_number_en(num, places=2, short_scale=False): Args: num(float or int): the number to pronounce (under 100) places(int): maximum decimal places to speak + short_scale (bool) : use short (True) or long scale (False) + https://en.wikipedia.org/wiki/Names_of_large_numbers Returns: (str): The pronounced number """ @@ -287,11 +289,37 @@ def pronounce_number_en(num, places=2, short_scale=False): res.append(r) return res + def _split_by_millions(n): + assert (0 <= n) + res = [] + while n: + n, r = divmod(n, 1000) + res.append(r) + return res + + def _long_scale(n): + if n >= 10e153: + return "infinity" + n = int(n) + assert (0 <= n) + res = [] + for i, z in enumerate(_split_by_millions(n)): + if not z: + continue + number = pronounce_number_en(z, places, True) + if i % 2 != 0 and i > 1: + number += " " + "thousand" + elif i > 0 and i < 3: + number += " " + hundreds[i] + "," + elif i: + number += " " + hundreds[i - 1] + "," + res.append(number) + return " ".join(reversed(res)) + if short_scale: result += _short_scale(num) else: - # TODO long scale - result += _short_scale(num) + result += _long_scale(num) # Deal with fractional part if not num == int(num) and places > 0: diff --git a/test/unittests/util/test_format.py b/test/unittests/util/test_format.py index 730d5eb764..00e5c77a20 100755 --- a/test/unittests/util/test_format.py +++ b/test/unittests/util/test_format.py @@ -140,8 +140,8 @@ class TestPronounceNumber(unittest.TestCase): def test_convert_hundreds(self): self.assertEqual(pronounce_number(100), "one hundred") self.assertEqual(pronounce_number(666), "six hundred and sixty six") - # self.assertEqual(pronounce_number(1456), "one thousand, four hundred " - # "and fifty six") + self.assertEqual(pronounce_number(1456), "one thousand, four hundred " + "and fifty six") self.assertEqual(pronounce_number(103254654), "one hundred and three " "million, two hundred " "and fifty four " @@ -159,6 +159,12 @@ class TestPronounceNumber(unittest.TestCase): "trillion, eight hundred and ninety six billion, six " "hundred and thirty nine million, six hundred and " "thirty one thousand, eight hundred and ninety three") + self.assertEqual(pronounce_number(95505896639631893, + short_scale=False), + "ninety five thousand five hundred and five billion, " + "eight hundred and ninety six thousand six hundred " + "and thirty nine million, six hundred and thirty one " + "thousand, eight hundred and ninety three") # def nice_time(dt, lang="en-us", speech=True, use_24hour=False,