From 0cba4024154114f9946cdaa4e4fb81e8d1cd9ec8 Mon Sep 17 00:00:00 2001 From: Benjamin Schwing <63053132+BenjiAvH@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:31:35 +0000 Subject: [PATCH] fix(learning): ignore the abbreviation dot in unit words "4,5 Mio" and "4,5 Mio." were different units and so not comparable, and the quiz viewer marked the dotless answer wrong. Both ports now drop a trailing dot from the unit word before comparing. Signed-off-by: Benjamin Schwing <63053132+BenjiAvH@users.noreply.github.com> --- deeptutor/learning/grading.py | 5 +++-- tests/fixtures/numeric_answers.json | 3 +++ web/lib/numeric-answer.ts | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/deeptutor/learning/grading.py b/deeptutor/learning/grading.py index 23a581aa3..194c7f3b8 100644 --- a/deeptutor/learning/grading.py +++ b/deeptutor/learning/grading.py @@ -78,7 +78,7 @@ _PREFIX = re.compile( _CURRENCY = {"€": "eur", "euro": "eur", "$": "usd", "£": "gbp", "eur": "eur", "usd": "usd"} _CURRENCY |= {"gbp": "gbp", "chf": "chf"} # Words that change the magnitude or meaning and so may never be dropped. -_FIXED_UNITS = {"tsd", "tsd.", "teur", "mio", "mio.", "mrd", "mrd.", "bn", "pi", "dozen", "dutzend"} +_FIXED_UNITS = {"tsd", "teur", "mio", "mrd", "bn", "pi", "dozen", "dutzend"} _FIXED_UNITS |= {"hundred", "hundert", "thousand", "tausend", "million", "millionen"} _FIXED_UNITS |= {"milliarde", "milliarden", "billion", "billionen"} # A part outcome that matches only once the key is rounded to the answer's @@ -186,7 +186,8 @@ def _parse_number(text: str) -> _Number | None: sign = -1 if "-" in match["pre"] else 1 scale = Fraction(1, 100) if percent else 1 readings = {k: (sign * v * scale, d) for k, (v, d) in readings.items()} - return _Number(readings, implied, percent, "".join(currencies), "".join(units), text.strip()) + unit = "".join(units).rstrip(".") # "Mio." is "Mio" + return _Number(readings, implied, percent, "".join(currencies), unit, text.strip()) def _numeric_parts(text: str) -> list[tuple[str | None, _Number]] | None: diff --git a/tests/fixtures/numeric_answers.json b/tests/fixtures/numeric_answers.json index e6fa66924..c1cdbc1a0 100644 --- a/tests/fixtures/numeric_answers.json +++ b/tests/fixtures/numeric_answers.json @@ -165,6 +165,9 @@ {"user": "1.251.000", "expected": "1.250", "comparable": true, "correct": false, "note": "a scale mismatch must be exact"}, {"user": "1.250.400", "expected": "1.250", "comparable": true, "correct": false}, {"user": "1.250.000 %", "expected": "1.250 %", "comparable": true, "correct": false, "note": "rates have no unit scale"}, + {"user": "4,5 Mio", "expected": "4,5 Mio.", "comparable": true, "correct": true, "note": "the abbreviation dot does not change a unit"}, + {"user": "12 Tsd.", "expected": "12 Tsd", "comparable": true, "correct": true}, + {"user": "5 Stk", "expected": "5 Stk.", "comparable": true, "correct": true}, {"user": "photosynthesis", "expected": "photosynthesis", "comparable": false, "correct": false}, {"user": "12", "expected": "twelve", "comparable": false, "correct": false}, {"user": "", "expected": "5", "comparable": false, "correct": false} diff --git a/web/lib/numeric-answer.ts b/web/lib/numeric-answer.ts index ba4b42467..534f0f031 100644 --- a/web/lib/numeric-answer.ts +++ b/web/lib/numeric-answer.ts @@ -52,7 +52,7 @@ CURRENCY.set('gbp', 'gbp').set('chf', 'chf') // Words that change the magnitude or meaning and so may never be dropped. const FIXED_UNITS = new Set( ( - 'tsd tsd. teur mio mio. mrd mrd. bn pi dozen dutzend hundred hundert thousand tausend ' + + 'tsd teur mio mrd bn pi dozen dutzend hundred hundert thousand tausend ' + 'million millionen milliarde milliarden billion billionen' ).split(' ') ) @@ -147,7 +147,7 @@ function parseNumber(text: string): NumberToken | null { implied, percent, currency: [...currencies].join(''), - unit: units.join(''), + unit: units.join('').replace(/\.$/, ''), // "Mio." is "Mio" text: text.trim(), } }