dlp-lab / demo /src /baseline.py
alirezaaminzadeh's picture
Publish AriaDLP synthetic Persian/English redaction lab
eef4eed verified
Raw History Blame Contribute Delete
2.17 kB
from __future__ import annotations
import re
from src.domain import EntityType, Method, Workflow
from src.schemas import Span
EMAIL_RE = re.compile(r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b")
PHONE_RE = re.compile(r"(?<!\d)(?:\+98|0)9\d{9}(?!\d)")
TEN_DIGIT_RE = re.compile(r"(?<!\d)\d{10}(?!\d)")
SIXTEEN_DIGIT_RE = re.compile(r"(?<!\d)\d{16}(?!\d)")
IBAN_RE = re.compile(r"\bIR\d{24}\b")
EMPLOYEE_RE = re.compile(r"\b(?:ARIA|EMP)-\d{4,6}\b", re.I)
HONORIFIC_RE = re.compile(r"\b(?:Mr|Ms|Mrs|Dr)\.?\s+[A-Z][A-Za-z]+(?:\s+[A-Z][A-Za-z]+)?")
def _span(text: str, start: int, end: int, entity_type: EntityType, rule_id: str) -> Span:
return Span(
start=start,
end=end,
entity_type=entity_type,
surface=text[start:end],
confidence=0.4,
method=Method.BASELINE.value,
rule_id=rule_id,
workflow=Workflow.DETECTED,
reason="Regex-only baseline without checksum or digit fold",
)
def analyze_regex_only(text: str) -> list[Span]:
"""Naive recognizer: ASCII regex, no Persian digits, no checksum, no hard-negative filter."""
found: list[Span] = []
for match in EMAIL_RE.finditer(text):
found.append(_span(text, match.start(), match.end(), EntityType.EMAIL, "base.email"))
for match in PHONE_RE.finditer(text):
found.append(_span(text, match.start(), match.end(), EntityType.PHONE, "base.phone"))
for match in TEN_DIGIT_RE.finditer(text):
found.append(_span(text, match.start(), match.end(), EntityType.IR_NATIONAL_ID, "base.ten_digit"))
for match in SIXTEEN_DIGIT_RE.finditer(text):
found.append(_span(text, match.start(), match.end(), EntityType.CREDIT_CARD, "base.sixteen_digit"))
for match in IBAN_RE.finditer(text):
found.append(_span(text, match.start(), match.end(), EntityType.IBAN, "base.iban"))
for match in EMPLOYEE_RE.finditer(text):
found.append(_span(text, match.start(), match.end(), EntityType.EMPLOYEE_ID, "base.employee"))
for match in HONORIFIC_RE.finditer(text):
found.append(_span(text, match.start(), match.end(), EntityType.PERSON, "base.honorific"))
return found