polish-dynaword / src /test_scrub_entities.py
Maggio33's picture
Add in-tree PII scrub (no new parquet) (#20)
a916ede
Raw
History Blame
4.15 kB
"""PERSON-only NER replace: allowlist + whole-word guard. No model download."""
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from scrub_entities import (
NER_SOURCES,
apply_person_spans,
source_allows_ner,
scrub_entities,
)
SEJM = (
"Marszałek Sejmu Szymon Hołownia otworzył posiedzenie. "
"Głos zabrał poseł Donald Tusk w imieniu Klubu Koalicja Obywatelska."
)
HRUB = "Hrubieszów położony w województwie lubelskim."
def _span(label, text, start, end=None):
end = end if end is not None else start + len(text)
return {"label": label, "text": text, "start": start, "end": end, "score": 1.0}
class AllowlistTest(unittest.TestCase):
def test_web_sources_allowed(self):
for key in ("european_hplt_v3_pl", "govpl", "samorzad_gov_pl"):
self.assertTrue(source_allows_ner(key), key)
self.assertEqual(
NER_SOURCES,
frozenset({"european_hplt_v3_pl", "govpl", "samorzad_gov_pl"}),
)
def test_official_sources_blocked(self):
for key in (
"parlamint_pl", "sejm_api", "sejm_interpellations",
"parliamentary", "eurlex", "dziennik_ustaw", "wikipedia",
):
self.assertFalse(source_allows_ner(key), key)
def test_blocked_source_does_not_replace_even_with_spans(self):
spans = [_span("PERSON", "Szymon Hołownia", SEJM.index("Szymon Hołownia"))]
out, counts = scrub_entities(SEJM, source="parlamint_pl", spans=spans)
self.assertEqual(out, SEJM)
self.assertIn("Szymon Hołownia", out)
self.assertEqual(counts["person"], 0)
def test_blocked_sejm_api_same(self):
spans = [_span("PERSON", "Donald Tusk", SEJM.index("Donald Tusk"))]
out, counts = scrub_entities(SEJM, source="sejm_api", spans=spans)
self.assertEqual(out, SEJM)
self.assertEqual(counts["person"], 0)
class ApplyPersonSpansTest(unittest.TestCase):
def test_person_becomes_pii_keeps_sentence(self):
text = "Kontakt: Anna Nowak, ul. 3 Maja."
spans = [_span("PERSON", "Anna Nowak", text.index("Anna Nowak"))]
out, n = apply_person_spans(text, spans)
self.assertEqual(n, 1)
self.assertIn("[PII]", out)
self.assertNotIn("Anna Nowak", out)
self.assertIn("Kontakt:", out)
self.assertIn("ul. 3 Maja.", out)
def test_city_and_org_are_ignored(self):
text = HRUB + " Sony i Ministerstwo Finansów."
spans = [
_span("CITY", "H", 0, 1),
_span("CITY", "bieszów", 3, 10),
_span("ORG", "Sony", text.index("Sony")),
_span("ORG", "Ministerstwo Finansów", text.index("Ministerstwo")),
]
out, n = apply_person_spans(text, spans)
self.assertEqual(n, 0)
self.assertEqual(out, text)
self.assertIn("Hrubieszów", out)
def test_gapped_person_subwords_expand_to_whole_word(self):
# Same hole as Hrubieszów: middle letters tagged O.
text = "Hrubieszów"
spans = [
_span("PERSON", "H", 0, 1),
_span("PERSON", "bieszów", 3, 10),
]
out, n = apply_person_spans(text, spans)
self.assertEqual(n, 1)
self.assertEqual(out, "[PII]")
self.assertNotIn("ru", out)
def test_partial_person_expands_to_word(self):
text = "Widziałem Kowalskiego wczoraj."
spans = [_span("PERSON", "Kowal", text.index("Kowal"), text.index("Kowal") + 5)]
out, n = apply_person_spans(text, spans)
self.assertEqual(n, 1)
self.assertNotIn("Kowalskiego", out)
self.assertEqual(out, "Widziałem [PII] wczoraj.")
def test_allowed_source_uses_injected_spans(self):
text = "Zgłosiła to Anna Nowak."
spans = [_span("PERSON", "Anna Nowak", text.index("Anna Nowak"))]
out, counts = scrub_entities(
text, source="european_hplt_v3_pl", spans=spans,
)
self.assertEqual(counts["person"], 1)
self.assertNotIn("Anna Nowak", out)
if __name__ == "__main__":
unittest.main()