-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathfileProcessor.js
291 lines (251 loc) · 9.08 KB
/
fileProcessor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import fs from 'fs';
import path from 'path';
import ExcelJS from 'exceljs';
import mammoth from 'mammoth';
import { Document, Packer, Paragraph } from 'docx';
import pdfParse from 'pdf-parse';
import { PDFDocument } from 'pdf-lib';
import { pipeline, env } from '@xenova/transformers';
import { fileURLToPath } from 'url';
// ES module paths
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Transformers.js environment
env.localModelPath = path.join(__dirname, 'models');
env.allowRemoteModels = false;
env.quantized = false;
// Toggle whether we use LLM-based anonymization
const useLLM = true;
// Pipeline reference
let nerPipeline = null;
// Pseudonym counters/mappings
const pseudonymCounters = {};
const pseudonymMapping = {};
/**
* Returns a consistent pseudonym for a given entity text + type.
*/
function getPseudonym(entityText, entityType) {
if (pseudonymMapping[entityText]) {
return pseudonymMapping[entityText];
}
if (!pseudonymCounters[entityType]) {
pseudonymCounters[entityType] = 1;
}
const pseudonym = `${entityType}_${pseudonymCounters[entityType]++}`;
pseudonymMapping[entityText] = pseudonym;
return pseudonym;
}
/**
* Aggressively merges consecutive tokens of the same entity type,
* removing whitespace/punctuation from each token, then concatenating.
* e.g. “Bay,” + “ona,” + “Wil” + “ber” => “BayonaWilber”
*/
function aggressiveMergeTokens(predictions) {
if (!predictions || predictions.length === 0) return [];
const merged = [];
let current = null;
for (const pred of predictions) {
const type = pred.entity.replace(/^(B-|I-)/, '');
// Remove whitespace/punctuation from each token
let word = pred.word.replace(/\s+/g, '').replace(/[^\w\s.,'-]/g, '');
word = word.trim();
if (!word) continue;
if (!current) {
current = { type, text: word };
} else if (current.type === type) {
// Same entity => unify
current.text += word;
} else {
// Different entity => push old one, start new
merged.push(current);
current = { type, text: word };
}
}
if (current) {
merged.push(current);
}
return merged;
}
/**
* Safely escapes all regex meta-characters in a string.
*/
function escapeRegexChars(str) {
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
}
/**
* Builds a fuzzy regex (with 'g' + 'i') that matches the merged string ignoring spacing/punctuation.
*/
function buildFuzzyRegex(mergedString) {
// Remove punctuation from mergedString
let noPunc = mergedString.replace(/[^\w]/g, '');
if (!noPunc) {
return null;
}
// Escape special regex chars
noPunc = escapeRegexChars(noPunc);
// Build a pattern that allows any non-alphanumeric between letters
let pattern = '';
for (const char of noPunc) {
pattern += `${char}[^a-zA-Z0-9]*`;
}
// No trailing slice, to avoid bracket issues.
if (!pattern) {
return null;
}
try {
return new RegExp(pattern, 'ig');
} catch (err) {
console.warn(`Regex build failed for pattern="${pattern}". Error: ${err.message}`);
return null;
}
}
/**
* Loads the PII detection model from local files, if not already loaded.
*/
async function loadNERModel() {
if (!nerPipeline) {
console.log("Loading PII detection model from local files...");
nerPipeline = await pipeline('token-classification', 'protectai/lakshyakh93-deberta_finetuned_pii-onnx');
console.log("Model loaded.");
}
return nerPipeline;
}
/**
* The main anonymization function.
* 1) Runs the pipeline
* 2) Merges partial tokens
* 3) Uses a fuzzy global regex to replace each merged token with a pseudonym
*/
async function anonymizeText(text) {
let processedText = String(text);
const ner = await loadNERModel();
console.log("Internal LLM processing...");
const predictions = await ner(processedText);
console.log("Raw predicted tokens:", predictions);
const merged = aggressiveMergeTokens(predictions);
console.log("Aggressively merged tokens:", merged);
for (const obj of merged) {
const entityType = obj.type;
const mergedString = obj.text;
if (!mergedString) continue;
const pseudonym = getPseudonym(mergedString, entityType);
const fuzzyRegex = buildFuzzyRegex(mergedString);
if (!fuzzyRegex) {
console.log(`Skipping zero-length or invalid pattern for mergedString="${mergedString}"`);
continue;
}
console.log(`Replacing fuzzy match of "${mergedString}" => regex ${fuzzyRegex} with "${pseudonym}"`);
// Single-pass global replace
processedText = processedText.replace(fuzzyRegex, pseudonym);
}
console.log("LLM processing complete.");
return processedText;
}
export class FileProcessor {
static async processFile(filePath, outputPath) {
return new Promise(async (resolve, reject) => {
try {
const ext = path.extname(filePath).toLowerCase();
console.log(`Processing file: ${filePath}`);
if (ext === '.txt' || ext === '.csv') {
// Text-based approach
console.log(`Processing text file: ${filePath}`);
const content = fs.readFileSync(filePath, 'utf8');
let newContent;
if (useLLM) {
console.log("LLM anonymization enabled. Processing text...");
const anonymizedText = await anonymizeText(content);
newContent = "Anonymized\n\n" + anonymizedText;
} else {
console.log("LLM anonymization disabled. Using default processing.");
newContent = "Anonymized\n\n" + content;
}
fs.writeFileSync(outputPath, newContent, 'utf8');
console.log(`Text file processed and saved to: ${outputPath}`);
resolve(true);
} else if (ext === '.xlsx') {
// Excel partial coverage
console.log(`Processing Excel file: ${filePath}`);
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(filePath);
for (const worksheet of workbook.worksheets) {
for (let i = 1; i <= worksheet.rowCount; i++) {
const row = worksheet.getRow(i);
for (let j = 1; j <= row.cellCount; j++) {
const cell = row.getCell(j);
if (typeof cell.value === 'string') {
console.log(`Anonymizing cell [Row ${i}, Col ${j}] with value: ${cell.value}`);
cell.value = await anonymizeText(cell.value);
}
}
}
}
await workbook.xlsx.writeFile(outputPath);
console.log(`Excel file processed and saved to: ${outputPath}`);
resolve(true);
} else if (ext === '.docx') {
// DOCX: mammoth + docx approach
console.log(`Processing DOCX file: ${filePath}`);
const { value: docxText } = await mammoth.extractRawText({ path: filePath });
console.log("Extracted DOCX text:", docxText);
let anonymizedDocxText = docxText;
if (useLLM) {
anonymizedDocxText = await anonymizeText(docxText);
}
// Create minimal docx with 'docx' library
const doc = new Document({
sections: [
{
children: [ new Paragraph(anonymizedDocxText) ],
},
],
});
const buffer = await Packer.toBuffer(doc);
fs.writeFileSync(outputPath, buffer);
console.log(`DOCX file processed and saved to: ${outputPath}`);
resolve(true);
} else if (ext === '.pdf') {
// PDF: pdf-parse + pdf-lib approach
console.log(`Processing PDF file: ${filePath}`);
const dataBuffer = fs.readFileSync(filePath);
const data = await pdfParse(dataBuffer);
const pdfText = data.text;
console.log("Extracted PDF text:", pdfText);
let anonymizedPdfText = pdfText;
if (useLLM) {
anonymizedPdfText = await anonymizeText(pdfText);
}
// Create a minimal PDF with pdf-lib
const doc = await PDFDocument.create();
const page = doc.addPage();
page.drawText(anonymizedPdfText, { x: 50, y: 700, size: 12 });
const pdfBytes = await doc.save();
fs.writeFileSync(outputPath, pdfBytes);
console.log(`PDF file processed and saved to: ${outputPath}`);
resolve(true);
} else {
// For other file types, just copy
console.log(`Processing binary file: ${filePath}`);
fs.copyFileSync(filePath, outputPath);
console.log(`Binary file copied to: ${outputPath}`);
resolve(true);
}
} catch (error) {
console.error("Error in processFile:", error);
reject(error);
}
});
}
static generateOutputFileName(originalName) {
const ext = path.extname(originalName);
const baseName = path.basename(originalName, ext);
return `${baseName}-anon${ext}`;
}
static validateFileType(filePath) {
const supportedTypes = [
'.doc', '.docx', '.xls', '.xlsx', '.csv', '.pdf', '.txt'
];
const ext = path.extname(filePath).toLowerCase();
return supportedTypes.includes(ext);
}
}