From 67dc75ba1fe89bbce0dfba792fb5b9b20927657b Mon Sep 17 00:00:00 2001 From: Mohamed Ajab Date: Tue, 1 Dec 2026 20:51:16 +0000 Subject: [PATCH] fix: preserve Unicode text when checking split overlap Signed-off-by: Mohamed Ajab --- docs/USAGE_AND_CI.md | 9 +++++---- splitcheck.py | 9 +++++++-- tests/test_splitcheck.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/docs/USAGE_AND_CI.md b/docs/USAGE_AND_CI.md index 7094129..65d094b 100644 --- a/docs/USAGE_AND_CI.md +++ b/docs/USAGE_AND_CI.md @@ -89,8 +89,9 @@ repository. **Do not** rely on this gate alone for model validity. ## What "exact" means here -`normalise()` performs Unicode case folding, then keeps ASCII letter -and digit sequences matched by `[a-z0-9]+`, joined with single spaces. +`normalise()` performs Unicode case folding and NFC normalization. It keeps +letters, numbers and combining marks, replaces other characters with spaces, +and collapses repeated whitespace. Accented and non-Latin text is preserved. Consequences: @@ -98,8 +99,8 @@ Consequences: - Identical wording with punctuation/case changes is caught. - Paraphrases, translations, changed words, spelling mistakes and semantic near-duplicates are generally **not** caught. -- Some non-ASCII text can normalise poorly because the token pattern - accepts ASCII alphanumerics only. +- Accents remain significant: `"café"` and `"cafè"` are different, while + composed and decomposed forms of `"café"` compare equal. - Group equality does not detect related campaign IDs or indirect relationships. diff --git a/splitcheck.py b/splitcheck.py index c74dec7..0a4e881 100644 --- a/splitcheck.py +++ b/splitcheck.py @@ -4,14 +4,19 @@ import argparse import csv import json -import re +import unicodedata from collections.abc import Iterator from itertools import chain from pathlib import Path def normalise(text: str) -> str: - return " ".join(re.findall(r"[a-z0-9]+", text.casefold())) + text = unicodedata.normalize("NFC", text.casefold()) + words = "".join( + char if char.isalnum() or unicodedata.category(char).startswith("M") else " " + for char in text + ) + return " ".join(words.split()) def _iter_rows( diff --git a/tests/test_splitcheck.py b/tests/test_splitcheck.py index f91e1d3..5e661d1 100644 --- a/tests/test_splitcheck.py +++ b/tests/test_splitcheck.py @@ -30,3 +30,33 @@ def test_duplicate_ids_are_rejected(tmp_path: Path): write(test, [{"id": "b", "text": "three"}]) with pytest.raises(ValueError, match="unique"): audit(train, test) + + +@pytest.mark.parametrize("text,expected", [ + ("CAFÉ, déjà vu!", "café déjà vu"), + ("你好,世界!", "你好 世界"), + ("Hello_WORLD 123", "hello world 123"), + ("cafe\u0301", "café"), +]) +def test_normalise_preserves_unicode_letters(text, expected): + assert normalise(text) == expected + + +@pytest.mark.parametrize("train_text,test_text,overlap", [ + ("café", "cafè", False), + ("你好", "再见", False), + ("कि", "का", False), + ("CAFÉ!", "cafe\u0301", True), + ("你好!", "你好", True), +]) +def test_audit_compares_unicode_text(tmp_path, train_text, test_text, overlap): + train, test = tmp_path / "train.csv", tmp_path / "test.csv" + write(train, [{"id": "a", "text": train_text}]) + write(test, [{"id": "b", "text": test_text}]) + + result = audit(train, test) + + assert result["passed"] is not overlap + assert result["exact_text_overlaps"] == ( + [{"train_id": "a", "test_id": "b"}] if overlap else [] + )