Skip to content

Commit

Permalink
Merge pull request #139 from nielstron/patch-negative-parens
Browse files Browse the repository at this point in the history
Fix parsing and add tests
  • Loading branch information
nielstron committed Oct 5, 2019
2 parents 1b14a52 + 81ae1c4 commit ec22237
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
17 changes: 11 additions & 6 deletions quantulum3/_lang/en_US/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,17 @@ def build_quantity(orig_text, text, item, values, unit, surface, span, uncert):
return

# When it comes to currencies, some users prefer the format ($99.99) instead of -$99.99
if unit.entity.name == "currency" and values[0] < 0:
surface_elements = surface.split(
"-"
) # Get currnecy sign and its value (surface_elements[0] = '$' and surface_elements[0] = '12' )
span = (span[0] - 1, span[1] + 1)
surface = "({0}{1})".format(surface_elements[0], surface_elements[1])
try:
if (
len(values) == 1 and unit.entity.name == "currency"
and orig_text[span[0]-1] == "(" and orig_text[span[1]] == ")"
and values[0] >= 0
):
span = (span[0] - 1, span[1] + 1)
surface = "({})".format(surface)
values[0] = -values[0]
except IndexError:
pass

# check if a unit without operators, actually is a common word
pruned_common_word = unit.original_dimensions
Expand Down
25 changes: 22 additions & 3 deletions quantulum3/_lang/en_US/tests/quantities.json
Original file line number Diff line number Diff line change
Expand Up @@ -1267,21 +1267,40 @@
}
]
},
{
"req": "The annual revenue is -15€ ($-99.99).",
"res": [
{
"value": -15,
"unit": "euro",
"surface": "-15€",
"entity": "currency",
"uncertainty": null
},
{
"value": -99.99,
"unit": "dollar",
"surface": "$-99.99",
"entity": "currency",
"uncertainty": null
}
]
},
{
"req": "The average weight is 10kg (25lbs).",
"res": [
{
"value": 10,
"unit": "kilogram",
"surface": "10kg",
"entity": "weight",
"entity": "mass",
"uncertainty": null
},
{
"value": 25,
"unit": "dollar",
"unit": "pound-mass",
"surface": "25lbs",
"entity": "weight",
"entity": "mass",
"uncertainty": null
}
]
Expand Down

0 comments on commit ec22237

Please sign in to comment.