MCPcopy Create free account
hub / github.com/Donnadieu/Epstein-File-Explorer / processDocuments

Function processDocuments

scripts/pipeline/pdf-processor.ts:238–401  ·  view source on GitHub ↗
(options: {
  inputDir?: string;
  dataSetIds?: number[];
  fileTypes?: string[];
  maxFiles?: number;
  outputDir?: string;
  reprocessEmpty?: boolean;
})

Source from the content-addressed store, hash-verified

236}
237
238export async function processDocuments(options: {
239 inputDir?: string;
240 dataSetIds?: number[];
241 fileTypes?: string[];
242 maxFiles?: number;
243 outputDir?: string;
244 reprocessEmpty?: boolean;
245}): Promise<ExtractedDocument[]> {
246 console.log("\n=== PDF Text Extractor ===\n");
247
248 const {
249 inputDir = DOWNLOADS_DIR,
250 maxFiles = Infinity,
251 outputDir = EXTRACTED_DIR,
252 } = options;
253
254 if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
255 if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true });
256
257 // Collect files to process per data set
258 const dirs = fs.existsSync(inputDir)
259 ? fs.readdirSync(inputDir)
260 .filter(d => d.startsWith("data-set-") && fs.statSync(path.join(inputDir, d)).isDirectory())
261 .sort()
262 : [];
263
264 const log = loadLog();
265
266 // Pre-pass: remove low-quality extractions so they get re-extracted with OCR
267 if (options.reprocessEmpty) {
268 console.log(" Scanning for low-quality extractions to reprocess...");
269 let removedCount = 0;
270
271 for (const dir of dirs) {
272 const dsMatch = dir.match(/data-set-(\d+)/);
273 if (!dsMatch) continue;
274 const dsId = parseInt(dsMatch[1], 10);
275 if (options.dataSetIds && !options.dataSetIds.includes(dsId)) continue;
276
277 const dsOutputDir = path.join(outputDir, `ds${dsId}`);
278 if (!fs.existsSync(dsOutputDir)) continue;
279
280 const jsonFiles = fs.readdirSync(dsOutputDir).filter(f => f.endsWith(".json"));
281 for (const jsonFile of jsonFiles) {
282 const jsonPath = path.join(dsOutputDir, jsonFile);
283 try {
284 const doc: ExtractedDocument = JSON.parse(fs.readFileSync(jsonPath, "utf-8"));
285 if (doc.method === "ocr") continue; // Already OCR'd, skip
286 if (isTextQualityPoor(doc.text, doc.pageCount)) {
287 log.totalPages -= doc.pageCount;
288 log.totalChars -= doc.text.length;
289 log.totalProcessed--;
290 fs.unlinkSync(jsonPath);
291 removedCount++;
292 }
293 } catch {
294 // Skip malformed JSON
295 }

Callers 2

runStageFunction · 0.90
pdf-processor.tsFile · 0.85

Calls 7

loadLogFunction · 0.85
isTextQualityPoorFunction · 0.85
getOutputPathFunction · 0.85
extractPdfTextFunction · 0.85
extractPdfTextWithOCRFunction · 0.85
saveLogFunction · 0.85
terminateTesseractWorkerFunction · 0.85

Tested by

no test coverage detected