From f3c2cb6ff5a899cc25563dc5c1774c5918f821e9 Mon Sep 17 00:00:00 2001 From: nBiqoz Date: Sun, 7 Sep 2025 13:36:00 +0200 Subject: [PATCH] fr --- app/api/process-document/route.ts | 79 +++++++++++++++---------------- 1 file changed, 39 insertions(+), 40 deletions(-) diff --git a/app/api/process-document/route.ts b/app/api/process-document/route.ts index fbf5756..795045e 100644 --- a/app/api/process-document/route.ts +++ b/app/api/process-document/route.ts @@ -174,8 +174,7 @@ export async function POST(req: NextRequest) { console.log("🔍 Appel à Presidio Analyzer..."); // ✅ Définir l'URL AVANT de l'utiliser - const presidioAnalyzerUrl = - "http://analyzer.151.80.20.211.sslip.io/analyze"; + const presidioAnalyzerUrl = "http://localhost:5001/analyze"; try { const analyzeResponse = await fetch(presidioAnalyzerUrl, { @@ -212,8 +211,7 @@ export async function POST(req: NextRequest) { }; console.log("🔍 Appel à Presidio Anonymizer..."); - const presidioAnonymizerUrl = - "http://analyzer.151.80.20.211.sslip.io/anonymize"; + const presidioAnonymizerUrl = "http://localhost:5001/anonymize"; const anonymizeResponse = await fetch(presidioAnonymizerUrl, { method: "POST", @@ -251,46 +249,47 @@ export async function POST(req: NextRequest) { analyzerResults: AnalyzerResult[] ) => { const replacementMap: Record = {}; - - // Créer une copie du texte anonymisé pour le traitement - const workingText = anonymizedText; // ✅ Changé de 'let' à 'const' - // Supprimer workingOriginal car elle n'est jamais utilisée - - // Trier les résultats par position (du plus grand au plus petit pour éviter les décalages) - const sortedResults = [...analyzerResults].sort( - (a, b) => b.start - a.start - ); - + + // Approche simple : comparer caractère par caractère + let originalIndex = 0; + let anonymizedIndex = 0; + + // Trier les résultats par position + const sortedResults = [...analyzerResults].sort((a, b) => a.start - b.start); + for (const result of sortedResults) { - const originalValue = originalText.substring( - result.start, - result.end - ); - - // Extraire les parties avant et après l'entité dans le texte original - const beforeOriginal = originalText.substring(0, result.start); - const afterOriginal = originalText.substring(result.end); - - // Trouver les mêmes parties dans le texte anonymisé - const beforeIndex = workingText.indexOf(beforeOriginal); - const afterIndex = workingText.lastIndexOf(afterOriginal); - - if (beforeIndex !== -1 && afterIndex !== -1) { - // Extraire la valeur de remplacement entre ces deux parties - const startPos = beforeIndex + beforeOriginal.length; - const endPos = afterIndex; - const replacementValue = workingText.substring(startPos, endPos); - - // Vérifier que c'est bien un remplacement (commence par [ et finit par ]) - if ( - replacementValue.startsWith("[") && - replacementValue.endsWith("]") - ) { - replacementMap[originalValue] = replacementValue; + const originalValue = originalText.substring(result.start, result.end); + + // Avancer jusqu'à la position de l'entité dans le texte original + while (originalIndex < result.start) { + originalIndex++; + anonymizedIndex++; + } + + // Maintenant on est au début de l'entité + // Dans le texte anonymisé, on doit avoir un remplacement qui commence par '[' + if (anonymizedText[anonymizedIndex] === '[') { + // Trouver la fin du remplacement (le ']') + let endBracket = anonymizedIndex; + while (endBracket < anonymizedText.length && anonymizedText[endBracket] !== ']') { + endBracket++; } + endBracket++; // Inclure le ']' + + const replacementValue = anonymizedText.substring(anonymizedIndex, endBracket); + replacementMap[originalValue] = replacementValue; + + // Avancer les index + originalIndex = result.end; + anonymizedIndex = endBracket; + } else { + // Si pas de '[', avancer normalement + originalIndex = result.end; + anonymizedIndex += (result.end - result.start); } } - + + console.log("🔧 Valeurs de remplacement extraites:", replacementMap); return replacementMap; };