forked from r.muellner/velum-raycast
feat: run Velum project jobs in the background and process DOCX locally
This commit is contained in:
99
src/lib/project-text-mapping.ts
Normal file
99
src/lib/project-text-mapping.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import type { PlaceholderMapping, PseudonymizeResponse } from "../types";
|
||||
|
||||
type Analyze = (text: string, mapping: PlaceholderMapping, types: string[],
|
||||
masks: Record<string, string[]>) => Promise<PseudonymizeResponse>;
|
||||
|
||||
/** Keep project identities locally; the API sees only rules needed by this text. */
|
||||
export function projectTextAnalyzer(initial: PlaceholderMapping, analyze: Analyze): Analyze {
|
||||
const mapping: PlaceholderMapping = { ...initial };
|
||||
const identities = new Map<string, string>();
|
||||
const counters = new Map<string, number>();
|
||||
const identity = (type: string, original: string) => JSON.stringify([type, original.trim().toLowerCase()]);
|
||||
const roots = new Set(Object.values(initial).flatMap((entry) => entry.merged_into ? [entry.merged_into] : []));
|
||||
// Ordinary historical NER mappings do not force recognition. Only custom
|
||||
// terms and user-confirmed person aliases have recognition semantics.
|
||||
const rules: PlaceholderMapping = {};
|
||||
for (const [key, entry] of Object.entries(initial)) {
|
||||
identities.set(identity(entry.type, entry.original), key);
|
||||
const number = Number(key.replace(/[<>]/g, "").match(/_(\d+)$/)?.[1] ?? 0);
|
||||
counters.set(entry.type, Math.max(counters.get(entry.type) ?? 0, number));
|
||||
if (entry.type === "BEGRIFF" || entry.merged_into || roots.has(key)) rules[key] = entry;
|
||||
}
|
||||
const canonical = (key: string): string => {
|
||||
const seen = new Set<string>();
|
||||
while (mapping[key]?.merged_into) {
|
||||
if (seen.has(key)) throw new Error("Zyklische Personen-Zusammenführung.");
|
||||
seen.add(key);
|
||||
key = mapping[key].merged_into!;
|
||||
if (!mapping[key]) throw new Error("Ziel einer Personen-Zusammenführung fehlt.");
|
||||
}
|
||||
return key;
|
||||
};
|
||||
return async (text, _previous, types, masks) => {
|
||||
// Other project phases (for example Office analysis) may have extended
|
||||
// the mapping since the previous text request. Keep those identities too.
|
||||
if (_previous !== mapping) {
|
||||
const addedRoots = new Set(Object.values(_previous).flatMap((entry) => entry.merged_into ? [entry.merged_into] : []));
|
||||
for (const [key, entry] of Object.entries(_previous)) {
|
||||
if (mapping[key] && identity(mapping[key].type, mapping[key].original) !== identity(entry.type, entry.original)) {
|
||||
throw new Error("Widersprüchliche Platzhalter in der Projektzuordnung.");
|
||||
}
|
||||
mapping[key] = entry;
|
||||
identities.set(identity(entry.type, entry.original), key);
|
||||
const number = Number(key.replace(/[<>]/g, "").match(/_(\d+)$/)?.[1] ?? 0);
|
||||
counters.set(entry.type, Math.max(counters.get(entry.type) ?? 0, number));
|
||||
if (entry.type === "BEGRIFF" || entry.merged_into || addedRoots.has(key)) rules[key] = entry;
|
||||
}
|
||||
}
|
||||
const lower = text.toLowerCase().replace(/\s+/gu, " ");
|
||||
const window: PlaceholderMapping = {};
|
||||
for (const [key, entry] of Object.entries(rules)) {
|
||||
if (!lower.includes(entry.original.trim().toLowerCase().replace(/\s+/gu, " "))) continue;
|
||||
let dependency: string | null | undefined = key;
|
||||
const seen = new Set<string>();
|
||||
while (dependency) {
|
||||
if (seen.has(dependency) || !mapping[dependency]) throw new Error("Ungültige Personen-Zusammenführung.");
|
||||
seen.add(dependency);
|
||||
window[dependency] = mapping[dependency];
|
||||
dependency = mapping[dependency].merged_into;
|
||||
}
|
||||
}
|
||||
const response = await analyze(text, window, types, masks);
|
||||
const translated = new Map<string, string>();
|
||||
for (const [remote, entry] of Object.entries(response.mapping)) {
|
||||
const id = identity(entry.type, entry.original);
|
||||
let local = identities.get(id);
|
||||
if (!local) {
|
||||
const number = (counters.get(entry.type) ?? 0) + 1;
|
||||
counters.set(entry.type, number);
|
||||
local = `${entry.type}_${String(number).padStart(2, "0")}`;
|
||||
if (mapping[local]) throw new Error("Doppelte Platzhalternummer in der Projektzuordnung.");
|
||||
mapping[local] = { original: entry.original, type: entry.type };
|
||||
identities.set(id, local);
|
||||
if (entry.type === "BEGRIFF") rules[local] = mapping[local];
|
||||
}
|
||||
translated.set(remote, canonical(local));
|
||||
}
|
||||
const spans = response.spans.map((span) => {
|
||||
const placeholder = translated.get(span.placeholder);
|
||||
if (!placeholder) throw new Error("Velum-Treffer ohne passende Zuordnung.");
|
||||
return { ...span, placeholder };
|
||||
});
|
||||
// Rebuild from original positions so existing literal placeholders are
|
||||
// never accidentally renumbered alongside newly detected entities.
|
||||
const characters = Array.from(text);
|
||||
const pieces: string[] = [];
|
||||
let offset = 0;
|
||||
for (const span of [...spans].sort((a, b) => a.start - b.start)) {
|
||||
if (!Number.isInteger(span.start) || !Number.isInteger(span.end)
|
||||
|| span.start < offset || span.start >= span.end || span.end > characters.length) {
|
||||
throw new Error("Ungültige Trefferposition der Velum-API.");
|
||||
}
|
||||
pieces.push(characters.slice(offset, span.start).join(""), span.placeholder);
|
||||
offset = span.end;
|
||||
}
|
||||
pieces.push(characters.slice(offset).join(""));
|
||||
return { ...response, pseudonymized_text: pieces.join(""), mapping, spans,
|
||||
entity_count: identities.size };
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user