From 32a3860bce07c8e647310ebc68b51c325f92f02c Mon Sep 17 00:00:00 2001 From: Kris Gesling Date: Sun, 18 Nov 2018 11:53:28 +0930 Subject: [PATCH] Issue-1877 - fix ordinal followed by one "Third one" will now return the expected 3 instead of 1. --- mycroft/util/lang/parse_en.py | 10 ++++++++++ test/unittests/util/test_parse.py | 3 +++ 2 files changed, 13 insertions(+) diff --git a/mycroft/util/lang/parse_en.py b/mycroft/util/lang/parse_en.py index 27c4c35714..ac16fee0d0 100644 --- a/mycroft/util/lang/parse_en.py +++ b/mycroft/util/lang/parse_en.py @@ -138,6 +138,8 @@ def extractnumber_en(text, short_scale=True, ordinals=False): "million": 1000000, 'millions': 1000000} + string_num_ordinal_en = {} + for num in NUM_STRING_EN: num_string = NUM_STRING_EN[num] string_num_en[num_string] = num @@ -147,10 +149,12 @@ def extractnumber_en(text, short_scale=True, ordinals=False): if short_scale: for num in SHORT_ORDINAL_STRING_EN: num_string = SHORT_ORDINAL_STRING_EN[num] + string_num_ordinal_en[num_string] = num string_num_en[num_string] = num else: for num in LONG_ORDINAL_STRING_EN: num_string = LONG_ORDINAL_STRING_EN[num] + string_num_ordinal_en[num_string] = num string_num_en[num_string] = num # negate next number (-2 = 0 - 2) @@ -229,6 +233,11 @@ def extractnumber_en(text, short_scale=True, ordinals=False): if word in string_num_en: val = string_num_en[word] + # is the prev word an ordinal number and current word is one? + # second one, third one + if ordinals and prev_word in string_num_ordinal_en and val is 1: + val = prev_val + # is the prev word a number and should we sum it? # twenty two, fifty six if prev_word in sums and word in string_num_en: @@ -268,6 +277,7 @@ def extractnumber_en(text, short_scale=True, ordinals=False): else: prev_val = val + # handle long numbers # six hundred sixty six # two million five hundred thousand diff --git a/test/unittests/util/test_parse.py b/test/unittests/util/test_parse.py index 262aa75d68..7433dbb674 100644 --- a/test/unittests/util/test_parse.py +++ b/test/unittests/util/test_parse.py @@ -68,6 +68,9 @@ class TestNormalize(unittest.TestCase): self.assertEqual(extract_number("this is the third test"), 1.0 / 3.0) self.assertEqual(extract_number("this is the third test", ordinals=True), 3.0) + self.assertEqual(extract_number("the fourth one", ordinals=True), 4.0) + self.assertEqual(extract_number("the thirty sixth one", + ordinals=True), 36.0) self.assertEqual(extract_number("this is test number 4"), 4) self.assertEqual(extract_number("one third of a cup"), 1.0 / 3.0) self.assertEqual(extract_number("three cups"), 3)