| 64 | } |
| 65 | |
| 66 | function loadDocumentText(fileName: string, dataSet: string): string | null { |
| 67 | // Search in extracted directory for matching JSON file |
| 68 | const dsDir = path.join(EXTRACTED_DIR, `ds${dataSet}`); |
| 69 | if (!fs.existsSync(dsDir)) return null; |
| 70 | |
| 71 | // Try exact match first |
| 72 | const exactPath = path.join(dsDir, `${fileName}.json`); |
| 73 | if (fs.existsSync(exactPath)) { |
| 74 | try { |
| 75 | const data = JSON.parse(fs.readFileSync(exactPath, "utf-8")); |
| 76 | return data.text && data.text.length >= MIN_TEXT_LENGTH ? data.text : null; |
| 77 | } catch { |
| 78 | return null; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Try fuzzy match (fileName might not include .pdf extension in JSON filename) |
| 83 | try { |
| 84 | const entries = fs.readdirSync(dsDir); |
| 85 | const base = fileName.replace(/\.pdf$/i, ""); |
| 86 | for (const entry of entries) { |
| 87 | if (entry.startsWith(base) && entry.endsWith(".json")) { |
| 88 | const data = JSON.parse(fs.readFileSync(path.join(dsDir, entry), "utf-8")); |
| 89 | return data.text && data.text.length >= MIN_TEXT_LENGTH ? data.text : null; |
| 90 | } |
| 91 | } |
| 92 | } catch { |
| 93 | // fall through |
| 94 | } |
| 95 | |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | // --- Queue Management --- |
| 100 | |