Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/doc-scaffold-fp-guards.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stll/anonymize": patch
---

Reject section-marker addresses and generic-role page-number organizations as false positives.
97 changes: 97 additions & 0 deletions crates/anonymize-core/src/false_positives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ fn should_reject_entity(
if exceeds_open_ended_word_count(entity) {
return Ok(true);
}
// Section markers (`6.1`, `3.2.4`) are never addresses, including when an
// address cue (place-of-performance) extracts them as trigger values.
if entity.label == ADDRESS_LABEL && is_section_number(text) {
return Ok(true);
}
if entity.label != IP_ADDRESS_LABEL
&& entity.label != ADDRESS_LABEL
&& is_section_number(text)
&& entity.source != DetectionSource::Trigger
{
Expand Down Expand Up @@ -206,6 +212,14 @@ fn should_reject_entity(
{
return Ok(true);
}
// Municipality / party-role prefix cues can capture page footers such as
// "Strana 7" / "Page 3". A generic-role head plus only a number is scaffolding,
// not an organization name.
if entity.label == ORGANIZATION_LABEL
&& filters.is_some_and(|filters| is_generic_role_plus_number(text, filters))
{
return Ok(true);
}
if entity.label == ADDRESS_LABEL && should_reject_address(entity, filters) {
return Ok(true);
}
Expand Down Expand Up @@ -444,6 +458,24 @@ fn role_exact_match(
.contains(&entity.text.trim().to_lowercase())
}

/// True when the span is a generic role word followed only by a bare number
/// (`Strana 7`, `Page 3`, `Contractor 1`). Uses the shared role vocabulary so
/// the guard is not tied to any one language's page-footer spelling.
fn is_generic_role_plus_number(
text: &str,
filters: &DenyListFilterData,
) -> bool {
let trimmed = text.trim();
let Some((word_end, word)) = first_word(trimmed) else {
return false;
};
if !filters.generic_roles.contains(&word.to_lowercase()) {
return false;
}
let rest = trimmed.get(word_end..).unwrap_or("").trim();
!rest.is_empty() && rest.chars().all(|ch| ch.is_ascii_digit())
}

fn is_all_caps_candidate(text: &str) -> bool {
let mut has_upper = false;
for ch in text.chars().filter(|ch| ch.is_alphabetic()) {
Expand Down Expand Up @@ -1829,6 +1861,71 @@ mod tests {
assert!(entities.is_empty());
}

#[test]
fn rejects_trigger_address_section_numbers() {
let entities = filter_entity_false_positives(
vec![entity(
"6.1",
"6.1",
ADDRESS_LABEL,
DetectionSource::Trigger,
)],
"6.1. Place of performance",
Some(&DenyListFilterData::default()),
)
.unwrap();

assert!(entities.is_empty());
}

#[test]
fn rejects_generic_role_plus_number_organizations() {
let filters = DenyListFilterData {
// Vocabulary-driven: any generic role, not a Czech-only page word.
generic_roles: set(["contractor", "strana"]),
..DenyListFilterData::default()
};
let text = "Contractor 1\nStrana 7\nAcme Industries";
let contractor = text.find("Contractor 1").unwrap();
let strana = text.find("Strana 7").unwrap();
let acme = text.find("Acme Industries").unwrap();
let entities = filter_entity_false_positives(
vec![
PipelineEntity::detected(
u32::try_from(contractor).unwrap(),
u32::try_from(contractor.saturating_add("Contractor 1".len()))
.unwrap(),
ORGANIZATION_LABEL,
"Contractor 1",
0.8,
DetectionSource::Trigger,
),
PipelineEntity::detected(
u32::try_from(strana).unwrap(),
u32::try_from(strana.saturating_add("Strana 7".len())).unwrap(),
ORGANIZATION_LABEL,
"Strana 7",
0.8,
DetectionSource::Trigger,
),
PipelineEntity::detected(
u32::try_from(acme).unwrap(),
u32::try_from(acme.saturating_add("Acme Industries".len())).unwrap(),
ORGANIZATION_LABEL,
"Acme Industries",
0.8,
DetectionSource::Trigger,
),
],
text,
Some(&filters),
)
.unwrap();

assert_eq!(entities.len(), 1);
assert_eq!(entities[0].text, "Acme Industries");
}

#[test]
fn keeps_ipv4_addresses_that_resemble_section_numbers() {
let text = "192.0.2.1";
Expand Down
81 changes: 81 additions & 0 deletions packages/anonymize/src/__test__/document-scaffold-fp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* False-positive guards for document scaffolding mistaken as PII:
* section-marker addresses and generic-role + page-number organizations.
* Motivated by Czech Registr smluv mandate contracts; rules are vocabulary-
* driven / structural, not Czech-token checks in core logic.
*/
import { describe, expect, setDefaultTimeout, test } from "bun:test";
import { DEFAULT_ENTITY_LABELS } from "../constants";
import type { Dictionaries, PipelineConfig } from "../types";
import { detectNative } from "./native-detect";
import { loadTestDictionaries } from "./load-dictionaries";

setDefaultTimeout(60_000);

const CONFIG: PipelineConfig = {
threshold: 0.3,
enableTriggerPhrases: true,
enableRegex: true,
enableLegalForms: true,
enableNameCorpus: true,
enableDenyList: true,
enableGazetteer: false,
enableConfidenceBoost: true,
enableCoreference: true,
enableHotwordRules: true,
denyListCountries: ["CZ"],
nameCorpusLanguages: ["cs"],
labels: [...DEFAULT_ENTITY_LABELS],
workspaceId: "doc-scaffold-fp-test",
};

let cachedDictionaries: Dictionaries | undefined;
const detect = async (text: string) => {
cachedDictionaries ??= await loadTestDictionaries();
return detectNative({ ...CONFIG, dictionaries: cachedDictionaries }, text);
};

describe("document scaffolding false positives", () => {
test("place-of-performance cue does not keep a section marker as address", async () => {
const text =
"Místo plnění\n\n6.1. Místem plnění se rozumí: Technická správa města";
const entities = await detect(text);
expect(
entities.some((e) => e.label === "address" && e.text.trim() === "6.1"),
).toBe(false);
});

test("generic-role page footer is not an organization (vocabulary-driven)", async () => {
// Role word is English `contractor` from generic-roles.json, not hardcoded
// in detector logic.
const text = "starosta města\n\nContractor 1 (of 3)\n";
const entities = await detect(text);
expect(
entities.some(
(e) => e.label === "organization" && /^Contractor\s+1$/u.test(e.text),
),
).toBe(false);
});

test("ordinary municipality organization trigger is unchanged", async () => {
const text = "město Brandýs nad Labem, IČO: 00240066";
const entities = await detect(text);
expect(
entities.some(
(e) =>
e.label === "organization" && e.text.includes("Brandýs nad Labem"),
),
).toBe(true);
});

test("Czech page footer Strana N is not an organization", async () => {
const text =
"Mgr. et Bc. Milan Rychtařík\n starosta města\n\n Strana 7 (celkem 7)\n";
const entities = await detect(text);
expect(
entities.some(
(e) => e.label === "organization" && /^Strana\s+7$/u.test(e.text),
),
).toBe(false);
});
});
Loading