Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/loraft/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ def _norm_amount(v) -> float | None:
if "," in s and "." in s:
s = s.replace(",", "") if s.rfind(".") > s.rfind(",") else s.replace(".", "").replace(",", ".")
elif "," in s:
s = s.replace(",", ".") if len(s.split(",")[-1]) == 2 else s.replace(",", "")
# Only commas that split off groups of exactly three digits are
# thousands separators ("1,204", "12,000,000"). "12,5" and "12,50" are
# decimal commas.
s = s.replace(",", "") if re.fullmatch(r"-?\d{1,3}(,\d{3})+", s) else s.replace(",", ".")
try:
return round(float(s), 2)
except ValueError:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@ def test_generator_produces_null_examples():
rows = data.generate(n=200)
n_null = sum(1 for r in rows if all(r["gold"][f] is None for f in config.FIELDS))
assert n_null > 0, "without all-null training rows the model never learns to abstain"


@pytest.mark.parametrize("raw,want", [
("12,5", 12.5), ("12,50", 12.5), ("1,204", 1204.0), ("12,000,000", 12000000.0),
("1.204,55", 1204.55), ("1,204.55", 1204.55), ("€12,5", 12.5), ("-3,75", -3.75),
])
def test_norm_amount_comma(raw, want):
assert task._norm_amount(raw) == want
17 changes: 15 additions & 2 deletions verify/score.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,10 +506,23 @@ static Norm norm_amount(const JVal *v)
}
}
} else if (last_comma) {
size_t tail = k - (size_t)(last_comma - buf) - 1;
/* thousands only for -?d{1,3}(,ddd)+ ; any other comma is a decimal
comma, so "12,5" is 12.5 and "1,204" is 1204 */
int thousands = 1;
size_t lead = 0, p0 = buf[0] == '-' ? 1 : 0;
for (j = p0; j < k && buf[j] != ','; j++)
lead++;
if (lead < 1 || lead > 3)
thousands = 0;
for (; thousands && j < k; j += 4) {
if (buf[j] != ',' || j + 3 >= k ||
!isdigit((unsigned char)buf[j + 1]) || !isdigit((unsigned char)buf[j + 2]) ||
!isdigit((unsigned char)buf[j + 3]))
thousands = 0;
}
for (j = 0; j < k; j++) {
if (buf[j] == ',') {
if (tail == 2)
if (!thousands)
tmp[w++] = '.';
} else {
tmp[w++] = buf[j];
Expand Down
Loading