Skip to content
Open
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
9 changes: 5 additions & 4 deletions docs/USAGE_AND_CI.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,18 @@ 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:

- `"Hello, WORLD!"` and `"hello world"` compare equal.
- 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.

Expand Down
9 changes: 7 additions & 2 deletions splitcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions tests/test_splitcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
)
Loading