fix: propagate recognized person names across local DOCX segments
The local DOCX flow analyzes each paragraph separately, so the backend's
per-text person propagation never crosses paragraph boundaries. A name
recognized once ("Frau Jorinde Quastenberg") stayed in clear text wherever
the model missed it in context ("bei Quastenberg nachfragen").
After all segment batches of a file, add the remaining occurrences of
confidently recognized full names, their parts (3+ characters) and names
after a salutation (including a genitive s) as reviewable PERSON hits.
Whitelist, placeholder filtering and the hit review still apply.
Synthetic reproduction with de_core_news_lg: 59 unmasked name occurrences
before, 0 after; comments, insertions, deletions and anchors verified.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
88
tests/person-propagation.test.ts
Normal file
88
tests/person-propagation.test.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { expect, it } from "bun:test";
|
||||
import { propagateDocxPersons } from "../src/lib/person-propagation";
|
||||
import type { Hit } from "../src/lib/project-work";
|
||||
import type { PlaceholderMapping } from "../src/types";
|
||||
|
||||
// Synthetic names only. The model recognizes them in the introduction, but
|
||||
// not in the later paragraphs (observed with de_core_news_lg).
|
||||
const segments = [
|
||||
{ text: "Projektleitung: Frau Jorinde Quastenberg, Fachbereich Planung.", source: "Absatz", kind: "text" },
|
||||
{ text: "Grüße Talvin", source: "Absatz", kind: "text" },
|
||||
...Array.from({ length: 26 }, () => [
|
||||
{ text: "bei Quastenberg nachfragen", source: "Absatz", kind: "text" },
|
||||
{ text: "Feedback Jorinde Quastenberg: passt. 😀 Quastenberg", source: "Absatz", kind: "text" },
|
||||
]).flat(),
|
||||
{ text: "Quastenbergstraße und Talvin bleiben", source: "Absatz", kind: "text" },
|
||||
{ text: "Jorinde Quastenberg", source: "Kommentar-Link", kind: "link" },
|
||||
];
|
||||
|
||||
function hit(segment: number, start: number, original: string, placeholder: string, score = 0.85): Hit {
|
||||
return { id: `a.docx:local-docx:${segment}:${start}:${start + original.length}`, file: "a.docx", source: "Absatz",
|
||||
original, placeholder, type: "PERSON", score, context: "", start, end: start + original.length,
|
||||
kind: "content", segment };
|
||||
}
|
||||
|
||||
it("masks every occurrence of a recognized person name across DOCX segments", () => {
|
||||
const mapping: PlaceholderMapping = {
|
||||
PERSON_01: { original: "Jorinde Quastenberg", type: "PERSON" },
|
||||
PERSON_02: { original: "Talvin", type: "PERSON" },
|
||||
};
|
||||
const detected = [hit(0, 21, "Jorinde Quastenberg", "PERSON_01"), hit(1, 6, "Talvin", "PERSON_02", 0.99)];
|
||||
const result = propagateDocxPersons(segments, detected, mapping, "a.docx");
|
||||
|
||||
const masked = segments.map((segment, index) => {
|
||||
const characters = Array.from(segment.text);
|
||||
for (const found of result.hits.filter((entry) => entry.segment === index)) {
|
||||
expect(characters.slice(found.start, found.end).join("")).toBe(found.original);
|
||||
for (let position = found.start; position < found.end; position++) characters[position] = "#";
|
||||
}
|
||||
return characters.join("");
|
||||
});
|
||||
const remaining = masked.slice(0, -1).join("\n").match(/(?<![\p{L}])Quastenberg(?![\p{L}])/gu) ?? [];
|
||||
expect(remaining).toHaveLength(0);
|
||||
expect(result.hits).toHaveLength(2 + 26 * 3);
|
||||
|
||||
const surname = result.hits.find((entry) => entry.original === "Quastenberg")!;
|
||||
expect(surname.placeholder).toBe("PERSON_03");
|
||||
expect(result.mapping.PERSON_03).toEqual({ original: "Quastenberg", type: "PERSON" });
|
||||
expect(result.hits.filter((entry) => entry.original === "Jorinde Quastenberg").every(
|
||||
(entry) => entry.placeholder === "PERSON_01")).toBe(true);
|
||||
// Unicode offsets count code points, like the Python codec.
|
||||
expect(result.hits.find((entry) => entry.segment === 3 && entry.start > 30)!.start).toBe(39);
|
||||
// No partial words, no lone first name without a full name, no link fields.
|
||||
expect(masked[54]).toBe("Quastenbergstraße und Talvin bleiben");
|
||||
expect(result.hits.some((entry) => entry.segment === 55)).toBe(false);
|
||||
expect(mapping.PERSON_03).toBeUndefined();
|
||||
});
|
||||
|
||||
it("does not seed propagation from weak or merged-away hits", () => {
|
||||
const mapping: PlaceholderMapping = {
|
||||
PERSON_04: { original: "Jorinde Quastenberg", type: "PERSON" },
|
||||
PERSON_07: { original: "Quastenberg", type: "PERSON", merged_into: "PERSON_04" },
|
||||
};
|
||||
const weak = propagateDocxPersons(segments, [hit(0, 21, "Jorinde Quastenberg", "PERSON_04", 0.6)], mapping, "a.docx");
|
||||
expect(weak.hits).toHaveLength(1);
|
||||
|
||||
const merged = propagateDocxPersons(segments, [hit(0, 21, "Jorinde Quastenberg", "PERSON_04")], mapping, "a.docx");
|
||||
expect(merged.hits.find((entry) => entry.original === "Quastenberg")!.placeholder).toBe("PERSON_04");
|
||||
expect(Object.keys(merged.mapping)).toEqual(["PERSON_04", "PERSON_07"]);
|
||||
});
|
||||
|
||||
it("seeds from salutation hits without the salutation and keeps the genitive s", () => {
|
||||
const texts = ["Projektleitung: Frau Jorinde Quastenberg, Planung.", "Dr. Ostermeier ist informiert.",
|
||||
"Quastenbergs Bericht und Ostermeiers Notiz liegen vor.", "Frau und Herr bleiben Wörter.", "Ostermeierhof"];
|
||||
const result = propagateDocxPersons(texts.map((text) => ({ text, source: "Absatz", kind: "text" })), [
|
||||
hit(0, 16, "Frau Jorinde Quastenberg", "PERSON_01", 0.7),
|
||||
hit(1, 0, "Dr. Ostermeier", "PERSON_02", 0.7),
|
||||
], {
|
||||
PERSON_01: { original: "Frau Jorinde Quastenberg", type: "PERSON" },
|
||||
PERSON_02: { original: "Dr. Ostermeier", type: "PERSON" },
|
||||
}, "a.docx");
|
||||
const added = result.hits.filter((entry) => !["PERSON_01", "PERSON_02"].includes(entry.placeholder));
|
||||
expect(added.map((entry) => [entry.segment, entry.original, texts[entry.segment!].slice(entry.start, entry.end)]))
|
||||
.toEqual([[2, "Quastenberg", "Quastenberg"], [2, "Ostermeier", "Ostermeier"]]);
|
||||
expect(result.hits.filter((entry) => entry.segment === 3 || entry.segment === 4)).toHaveLength(0);
|
||||
expect(Object.values(result.mapping).map((entry) => entry.original)).toEqual([
|
||||
"Frau Jorinde Quastenberg", "Dr. Ostermeier", "Quastenberg", "Ostermeier",
|
||||
]);
|
||||
});
|
||||
Reference in New Issue
Block a user