Skip to content

Commit

Permalink
Merge pull request #141 from nielstron/negative-parens
Browse files Browse the repository at this point in the history
Introduce parsing of parens in currencies as proposed by #103
  • Loading branch information
nielstron authored Sep 16, 2020
2 parents e5cbaff + ec22237 commit 4747e27
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
13 changes: 13 additions & 0 deletions quantulum3/_lang/en_US/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ def build_quantity(orig_text, text, item, values, unit, surface, span, uncert):
_LOGGER.debug("\tCorrect for am/pm time pattern")
return

# When it comes to currencies, some users prefer the format ($99.99) instead of -$99.99
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
while pruned_common_word:
Expand Down
50 changes: 50 additions & 0 deletions quantulum3/_lang/en_US/tests/quantities.json
Original file line number Diff line number Diff line change
Expand Up @@ -1266,5 +1266,55 @@
{
"req": "The bank robbery begun at 2pm and was over until 2.17pm.",
"res": []
},
{
"req": "The annual revenue is ($99.99).",
"res": [
{
"value": -99.99,
"unit": "dollar",
"surface": "($99.99)",
"entity": "currency",
"uncertainty": null
}
]
},
{
"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": "mass",
"uncertainty": null
},
{
"value": 25,
"unit": "pound-mass",
"surface": "25lbs",
"entity": "mass",
"uncertainty": null
}
]
}
]

0 comments on commit 4747e27

Please sign in to comment.