formalizing Named Entity Recognition types, Mask -> NER

This commit is contained in:
2026-10-28 12:41:31 +01:00
parent a007e8da25
commit 12fb3c78b2
11 changed files with 98 additions and 19 deletions
+25 -1
View File
@@ -26,7 +26,9 @@ from dspy._vendor.lm15 import Response, TextPart, Message, Usage
from dspy.utils.exceptions import AdapterParseError
from floop.dspy_modules import FloopModules
from floop.anonymize import NamedEntity, apply_mask, identity_answer, restore_mask
from floop.anonymize import (
ENTITY_TYPES, NamedEntity, apply_mask, identity_answer, restore_mask,
)
def _lm(script: list[str]) -> None:
@@ -75,6 +77,27 @@ def test_apply_restore_deterministic():
print("ok: apply/restore deterministic (longest-first, typed, reorder-safe)")
def test_entity_types_drive_schema_and_masks():
description = NamedEntity.model_fields["kind"].json_schema_extra["desc"]
for kind, definition in ENTITY_TYPES.items():
assert f"{kind}: {definition}" in description
entities = [NamedEntity(name=name, kind=kind) for kind, name in (
("PERSON", "Ada"), ("ORGANIZATION", "Acme"),
("LOCATION", "Paris"), ("DATE", "2026"),
("PRODUCT", "Widget"),
)]
entities.append(NamedEntity(name="Unknown", kind="OTHER"))
masked, restore = apply_mask("Ada Acme Paris 2026 Widget Unknown", entities)
assert masked == (
"PERSON_1 ORGANIZATION_1 LOCATION_1 DATE_1 PRODUCT_1 Unknown")
assert restore == {
"PERSON_1": "Ada", "ORGANIZATION_1": "Acme",
"LOCATION_1": "Paris", "DATE_1": "2026", "PRODUCT_1": "Widget",
}
print("ok: schema descriptions and mask prefixes use the shared entity types")
def test_init_decompose_masks_and_restores():
# script: first call = MASK, second = INIT
_lm([_MASKED_ENTS, _TWO_SUBS])
@@ -147,6 +170,7 @@ def test_mask_death_passthrough():
if __name__ == "__main__":
test_apply_restore_deterministic()
test_entity_types_drive_schema_and_masks()
test_init_decompose_masks_and_restores()
test_no_entities_passthrough()
test_identity_subquestion_seeded()