Skip to content

Commit

Permalink
Merge pull request #1879 from krisgesling/bugfix/issue-1877
Browse files Browse the repository at this point in the history
Issue-1877 - fix ordinal followed by one
  • Loading branch information
forslund committed Nov 26, 2018
2 parents 3e6dedb + 77c7641 commit 169a55f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 10 additions & 0 deletions mycroft/util/lang/parse_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions test/unittests/util/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def test_extract_number(self):
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)
Expand Down Expand Up @@ -518,9 +521,7 @@ def test_multiple_numbers(self):
[20, 20, 20, 2])
self.assertEqual(extract_numbers("third one"),
[1 / 3, 1])
# NOTE ambiguous case, should return [3] or [3, 1] ?
self.assertEqual(extract_numbers("third one", ordinals=True),
[3, 1])
self.assertEqual(extract_numbers("third one", ordinals=True), [3])
self.assertEqual(extract_numbers("six trillion", short_scale=True),
[6e12])
self.assertEqual(extract_numbers("six trillion", short_scale=False),
Expand Down

0 comments on commit 169a55f

Please sign in to comment.