Handle percent/decimal convention in Number-Match
This commit is contained in:
parent
c2a48b55c0
commit
80594cd38c
3 changed files with 14 additions and 2 deletions
|
|
@ -47,6 +47,10 @@ def extract_numbers(text: str) -> list[float]:
|
||||||
value = _to_float(token)
|
value = _to_float(token)
|
||||||
if value is not None:
|
if value is not None:
|
||||||
numbers.append(value)
|
numbers.append(value)
|
||||||
|
if "%" in token:
|
||||||
|
# Gold answers store ratios as either a percent (24.69) or a
|
||||||
|
# decimal (0.935); offer both readings of a percent figure.
|
||||||
|
numbers.append(value / 100)
|
||||||
for number, scale in _SCALED_RE.findall(text):
|
for number, scale in _SCALED_RE.findall(text):
|
||||||
value = _to_float(number)
|
value = _to_float(number)
|
||||||
if value is not None:
|
if value is not None:
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,14 @@ class TestNumberMatchEvaluator:
|
||||||
ctx = self._make_ctx("50.3", "In 2008 it grew from 27.0 to 50.3 percent")
|
ctx = self._make_ctx("50.3", "In 2008 it grew from 27.0 to 50.3 percent")
|
||||||
assert self.evaluator.evaluate(ctx) == 1.0
|
assert self.evaluator.evaluate(ctx) == 1.0
|
||||||
|
|
||||||
|
def test_percent_answer_matches_decimal_gold(self) -> None:
|
||||||
|
ctx = self._make_ctx("0.935", "the cumulative total return was 93.5%")
|
||||||
|
assert self.evaluator.evaluate(ctx) == 1.0
|
||||||
|
|
||||||
|
def test_percent_answer_matches_percent_gold(self) -> None:
|
||||||
|
ctx = self._make_ctx("24.691358024691358", "approximately 24.69% of production")
|
||||||
|
assert self.evaluator.evaluate(ctx) == 1.0
|
||||||
|
|
||||||
def test_non_numeric_prediction(self) -> None:
|
def test_non_numeric_prediction(self) -> None:
|
||||||
ctx = self._make_ctx("127.4", "I cannot determine the value")
|
ctx = self._make_ctx("127.4", "I cannot determine the value")
|
||||||
assert self.evaluator.evaluate(ctx) == 0.0
|
assert self.evaluator.evaluate(ctx) == 0.0
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ class TestExtractNumbers:
|
||||||
def test_currency_and_thousands(self) -> None:
|
def test_currency_and_thousands(self) -> None:
|
||||||
assert extract_numbers("$1,234.5") == [1234.5]
|
assert extract_numbers("$1,234.5") == [1234.5]
|
||||||
|
|
||||||
def test_percent_stripped(self) -> None:
|
def test_percent_yields_both_readings(self) -> None:
|
||||||
assert extract_numbers("margin was 50.3%") == [50.3]
|
assert extract_numbers("margin was 50.3%") == [50.3, 0.503]
|
||||||
|
|
||||||
def test_parenthesised_negative(self) -> None:
|
def test_parenthesised_negative(self) -> None:
|
||||||
assert extract_numbers("loss of (123)") == [-123.0]
|
assert extract_numbers("loss of (123)") == [-123.0]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue