diff --git a/pipeline/ab2026_harvest_contract.py b/pipeline/ab2026_harvest_contract.py index 10c8b77e7..be5753787 100644 --- a/pipeline/ab2026_harvest_contract.py +++ b/pipeline/ab2026_harvest_contract.py @@ -446,8 +446,10 @@ def _read(raw, normalization=""): _CHAIN = re.compile(rf"(?:^|(?<=[\s(\[]))({_STEP}(?:\s+{_STEP})*)") _OPERAND = re.compile(rf"10(?:\^|\*\*)(?P[0-9]+)|{_DIGITS}") # What would carry on a number the chain seems to end: "x 1 000 000", "x 1 million", -# "x 1 hundred" -_CONTINUES = re.compile(rf"\s+(?:[0-9]|(?:{_either(MULTIPLIERS)})\b)") +# "x 1 hundred", "x 2 and a half" +_CONTINUES = re.compile( + rf"\s+(?:[0-9]|(?:{_either(MULTIPLIERS)}|and\s+(?:half|{_PART}))\b)" +) # An operation on what the checker can't read as a number: a sign, bracket, quote # or currency sign first ("x (1e6)", "x +1e6", "x '1e6'", "x £100"), or number words # ("x one million", "x (a million)", "x half", "/ a quarter"). Also an operation in words ("times 1,000", @@ -468,11 +470,16 @@ def _read(raw, normalization=""): ) +def _collapsed(text): + """Text in lower case, with each run of whitespace as one space.""" + return " ".join((text or "").lower().split()) + + def _factors(normalization): """The multipliers a normalization states ("x 1,000,000", "/ 100", "(x 10^6)"), or None if it states one the checker can't read whole. Operations in a row make one multiplier: "x 1,000,000 / 100" is 10,000.""" - text = re.sub(r"\s*(\^|\*\*)\s*", r"\1", (normalization or "").lower()) + text = re.sub(r"\s*(\^|\*\*)\s*", r"\1", _collapsed(normalization)) if _UNREAD.search(text): return None out = [] @@ -574,7 +581,7 @@ def value_problems(r): ): return [f"value {v!r} is not value_raw {raw!r} at a stated scale"] if ( - WORDED_SIGN.search(r.get("normalization") or "") + WORDED_SIGN.search(_collapsed(r.get("normalization"))) or any(_same(v, p) for p in exact) or any(_rounded(v, f) for f in near) ): diff --git a/sources/harvest-uk-ab2026-2026-10-29/README.md b/sources/harvest-uk-ab2026-2026-10-29/README.md index d3868bce5..08096da8b 100644 --- a/sources/harvest-uk-ab2026-2026-10-29/README.md +++ b/sources/harvest-uk-ab2026-2026-10-29/README.md @@ -72,7 +72,7 @@ We follow the AB2025 harvest README's rules 1–10 and its row contract, with th - a range: two figures joined by a dash, with no spaces or a space on each side ("£4-5 million", "4 – 5%"), or two numbers joined by "to" ("four to five", "4bn to 5bn"); - "nil", "none", "nothing", "no change", "no impact" or "no overall impact", as zero. It also reads a dash as zero where we say, in one whole clause of `normalization`, that the producer prints it for nil. It accepts forms such as "dash for nil" and "the table prints nil as a dash". It ends a clause at a comma, semicolon, full stop or bracket. - It also reads a qualifier before the number ("about", "up to" and the others in `QUALIFIERS`), "a" before digits ("less than a 0.1 percentage point"), and "a year" or "of GDP" after the number. It reads a ratio or worded fraction with its percentage in brackets, in either order, if the two agree to the nearest percentage point: "one third (33%)", "25% (1 in 4)". It matches `value` to either number there or in a range. It reports a range that joins a percentage to a multiplier ("4% to 2 million") or a fraction to a percentage ("a third to 40%"). It also reports a range whose first number exceeds its second, unless neither is positive: it reports "2025-26" and "2 to -3", and reads "-4 to -5". After a bare "in", it reports a year from 2010 to 2099 ("5 in 2027"). -- It takes each number's scale from its own word (bn, million, %). When only a range's second number has one, it gives that word to the first number too. When digits, number words or a whole number with a fraction have no word of their own, it uses the factor in `normalization`, failing that 1. It reads a factor whole ("x 1,000,000", "/ 100", "(x 10^6)"), multiplies operations in a row ("x 1,000,000 / 100" as 10,000), and takes either factor where a semicolon or a word separates two. It reads x, ×, *, / or ÷ as a factor wherever a number follows, at the note's start or after a space or bracket. It ignores a slash between digits ("2/3", "2026/27") but reads "page 3 / 4" as a factor of 0.25, so we write "page 3 of 4". At any value, it reports "x 1 million", "x (1e6)", "x one million", "x half", "times 1,000", "multiply by 1,000,000" and "£m×1e6". It reads a percentage as a share or in percent units, and requires digits and number words to match `value` within four units in the last place. +- It takes each number's scale from its own word (bn, million, %). When only a range's second number has one, it gives that word to the first number too. When digits, number words or a whole number with a fraction have no word of their own, it uses the factor in `normalization`, failing that 1. It reads a factor whole ("x 1,000,000", "/ 100", "(x 10^6)"), multiplies operations in a row ("x 1,000,000 / 100" as 10,000), and takes either factor where a semicolon or a word separates two. It reads x, ×, *, / or ÷ as a factor wherever a number follows, at the note's start or after a space or bracket, and it reads any run of whitespace in the note as one space. It ignores a slash between digits ("2/3", "2026/27") but reads "page 3 / 4" as a factor of 0.25, so we write "page 3 of 4". At any value, it reports "x 1 million", "x (1e6)", "x one million", "x half", "x 2 and a half", "times 1,000", "multiply by 1,000,000" and "£m×1e6". It reads a percentage as a share or in percent units, and requires digits and number words to match `value` within four units in the last place. - It reads a ratio or a worded fraction on its own as a share, and accepts a `value` within 0.1% of that share. On a `unit_concept: percent` row, it requires 100 times the share instead ("1 in 4" as 25). It won't match `value` to either of a ratio's numbers alone, and it reads "-1 in 4" and "1 in -4" as −0.25. It also allows 0.1% on a whole number with a fraction, and on a fraction of a multiplier or percentage. - It requires `value` to share `value_raw`'s sign unless we note in `normalization` that we took the sign from the wording. - It treats an HM Treasury Impact on households row as a bar reading (rule 4) if the row has `value_kind: approx_chart_reading`. It does the same if we mention a bar, chart, graph, plot, diagram or visual in the locator, in any spacing and with or without hyphens. It also counts "off" or "from" up to four words before a figure there. It does the same where we write in the locator that we read, took, estimated or measured the number using, on, in or of a figure, or read it by eye. It reads "Fig.", "Figs." and "figures" as figures, including where "figures" means numbers ("from HMT's figures"), and looks past a colon, brackets or quotes before them. It counts "read off Fig. 1.A", "read off: Fig. 1.A", "read from [Figure 1.A]" and "column 3 of Figure 1.A", and leaves "Table 2.1, column 3" alone. It ignores such a phrase right after one "no", "not", "never", "without", "cannot" or "n't" ("not a bar reading"). It counts the phrase after two negations in the same clause ("not possible without reading off the chart"). diff --git a/tests/test_ab2026_harvest_contract.py b/tests/test_ab2026_harvest_contract.py index 2439f430f..4d6b80609 100644 --- a/tests/test_ab2026_harvest_contract.py +++ b/tests/test_ab2026_harvest_contract.py @@ -23,6 +23,7 @@ from pipeline.ab2026_harvest_contract import ( OGL, + _factors, duplicate_problems, enums, family_problems, @@ -2063,6 +2064,7 @@ def test_rule_4_holds_whatever_the_title_case(): ('paragraph 1.7; read off the "Impact on households" Figure 1.A', True), ("paragraph 1.7; read off the “Impact on households” Figure 1.A", True), ("paragraph 1.7; read from ((HMT)) Figure 1.A", True), + ("paragraph 1.7; read from [[HMT]] Figure 1.A", True), ('paragraph 1.7; read from "HMT" Figure 1.A', True), ("paragraph 1.7; read from (“HMT”) Figure 1.A", True), # Up to four words stand between "from" and the figure. @@ -2072,6 +2074,7 @@ def test_rule_4_holds_whatever_the_title_case(): ('paragraph 1.7; not read from "HMT" Figure 1.A', False), ("paragraph 1.7; not read from (“HMT”) Figure 1.A", False), ("paragraph 1.7; not read from ((HMT)) Figure 1.A", False), + ("paragraph 1.7; not read from [[HMT]] Figure 1.A", False), # a comma ends the words before a figure ("paragraph 1.7; figures from Table 2.C, Figure 1.A data label", False), ("paragraph 1.7; not read from [Figure 1.A]", False), @@ -2721,6 +2724,121 @@ def test_an_operand_the_checker_cant_read_is_a_problem_at_any_value(op, operand) assert any("a factor in normalization" in p for p in found), (note, value) +@pytest.mark.parametrize( + "note", + [ + "x 2 and a half", + "/ 2 and a half", + "x 2 and half", + "x 2 and three-quarters", + "x 2 and three quarters", + "(x 2 and a half)", + "as printed; x 2 and a half", + "x 1e6 / 2 and a half", + "x 2 and a half", + "x 2 and a\thalf", + "x 2 and a\u00a0half", + ], +) +def test_a_numeric_mixed_fraction_is_a_problem_at_any_value(note): + """Sol's review of 4dd5281d: the checker read "x 2 and a half" as a factor of 2, + so "4" under it passed at 8.""" + for value in (4, 8, 10, 1.6, 2e6): + found = problems(_share("4", value, note)) + assert any("a factor in normalization" in p for p in found), (note, value) + + +@pytest.mark.parametrize("note", ["x 2 and rounded", "x 2 and halfway", "x 2 and a"]) +def test_a_word_after_and_leaves_the_factor_alone(note): + """Only a fraction after "and" carries on the number.""" + assert not any( + "a factor in normalization" in p for p in problems(_share("4", 8, note)) + ) + + +SPACED_NOTES = [ + "x 2", + "x 2.5", + "/ 100", + "x 1,000,000 / 100", + "x 10 ^ 6", + "times 1,000", + "x 1 million", + "as printed; x 2", + "as printed", + "x 2 and a half", + "x 2 and three quarters", + "x 2 and rounded", + "x 2 and halfway", + "x 2 and a", + "sign from the wording; x 2", + "carried negative; x 2", + "x 2 -> -8", + "x 2 → negative", + "dash for nil", + "the table prints nil as a dash", +] +WHITESPACE = [chr(c) for c in range(0x110000) if chr(c).isspace()] + + +def _stretch(note, at, run): + return note[:at] + run + note[at + 1 :] + + +@pytest.mark.parametrize("note", SPACED_NOTES) +def test_a_run_of_whitespace_in_a_note_reads_as_one_space(note): + """Sol's reviews of 8200b418 and c9027fe2: two spaces or a tab hid the fraction + in "x 2 and a half" and the sign in "sign from the wording", so the checker + gave another reading of the same note.""" + spaces = [i for i, c in enumerate(note) if c == " "] + for at, run in product(spaces, (" ", " ", "\t", "\n", " \t ", *WHITESPACE)): + stretched = _stretch(note, at, run) + assert _factors(stretched) == _factors(note), stretched + for raw, value in product( + ("4", "-"), (4, -8, 8, 10, 40, 400, 0.04, 40000, 4e6, 0) + ): + assert problems(_share(raw, value, stretched)) == problems( + _share(raw, value, note) + ), (stretched, raw, value) + + +@settings(max_examples=300, deadline=None) +@given( + note=st.sampled_from(SPACED_NOTES), + run=st.text(st.sampled_from(WHITESPACE), min_size=1, max_size=4), + gap=st.integers(0, 20), + raw=st.sampled_from(["4", "-"]), + value=st.sampled_from([4, -8, 8, 40000, 0.04, 0]), +) +def test_any_whitespace_character_reads_as_one_space(note, run, gap, raw, value): + spaces = [i for i, c in enumerate(note) if c == " "] + stretched = _stretch(note, spaces[gap % len(spaces)], run) + assert _factors(stretched) == _factors(note), stretched + assert problems(_share(raw, value, stretched)) == problems(_share(raw, value, note)) + + +@pytest.mark.parametrize( + "note", + [ + "sign from the wording; x 2", + "sign from the wording; x 2", + "sign\tfrom the wording; x 2", + "sign\u00a0from the wording; x 2", + "Sign from the wording; x 2", + "SIGN FROM THE WORDING; X 2", + "carried negative; x 2", + "x 2 -> -8", + "x 2 → negative", + ], +) +def test_a_worded_sign_reads_in_any_spacing_or_case(note): + assert not problems(_share("4", -8, note)) + + +def test_a_note_with_no_worded_sign_keeps_the_printed_sign(): + assert any("opposite sign" in p for p in problems(_share("4", -8, "x 2"))) + + def test_a_long_run_of_quotes_stays_quick_to_read(): """If a quote could both open a word and stand as a bracket, the reading's time would grow with about the fifth power of the locator's length."""