import { getDocument, GlobalWorkerOptions } from './pdf.min.mjs'; import './tesseract.min.js'; const assets = new URL('./', import.meta.url); GlobalWorkerOptions.workerSrc = new URL('pdf.worker.min.mjs', assets).href; const files = document.getElementById('files'); const photo = document.getElementById('document-photo'); const output = document.getElementById('report-brief'); const status = document.getElementById('report-brief-status'); const pageLimit = 5; let latestReview = 0; const asset = name => new URL(name, assets).href; function summarize(text) { const lines = text.split(/\r?\n/).map(line => line.replace(/\s+/g, ' ').trim()).filter(Boolean); const excerpts = []; const headings = /^(?:reason for referral|reported symptoms|symptoms|findings|impression|conclusion|diagnosis|recommendations?|treatment plan)\b\s*[:–-]?\s*/i; for (const [index, line] of lines.entries()) { const heading = line.match(headings); if (!heading) continue; const content = line.slice(heading[0].length).trim() || lines[index + 1]; if (content && !headings.test(content)) excerpts.push(heading[0].replace(/[:–-]\s*$/, '').trim() + ': ' + content); } return { labeled: excerpts.length > 0, lines: [...new Set(excerpts.length ? excerpts : lines)].slice(0, 3).map(line => line.length > 320 ? line.slice(0, 317) + '…' : line) }; } async function readPdf(file, recognize) { const task = getDocument({ data: new Uint8Array(await file.arrayBuffer()), cMapUrl: asset('cmaps/'), cMapPacked: true, standardFontDataUrl: asset('standard_fonts/'), wasmUrl: asset('wasm/'), iccUrl: asset('iccs/'), isEvalSupported: false, enableXfa: false }); try { const pdf = await task.promise; const chunks = []; for (let i = 1; i <= Math.min(pdf.numPages, pageLimit); i++) { const page = await pdf.getPage(i); const content = await page.getTextContent(); const text = content.items.map(item => item.str ? item.str + (item.hasEOL ? '\n' : ' ') : '').join(''); if (text.trim().length >= 30) { chunks.push(text); } else { const size = page.getViewport({ scale: 1 }); const viewport = page.getViewport({ scale: Math.min(2.5, 2000 / Math.max(size.width, size.height)) }); const canvas = document.createElement('canvas'); canvas.width = Math.ceil(viewport.width); canvas.height = Math.ceil(viewport.height); await page.render({ canvasContext: canvas.getContext('2d'), viewport }).promise; chunks.push(await recognize(canvas)); } } return { text: chunks.join('\n'), limited: pdf.numPages > pageLimit }; } finally { await task.destroy(); } } async function review() { const revision = ++latestReview; const selected = [...files.files]; output.replaceChildren(); status.textContent = ''; if (!selected.length || selected.length > 10 || selected.some(file => file.size > 5 * 1048576) || selected.reduce((bytes, file) => bytes + file.size, 0) > 10 * 1048576) return; status.textContent = 'Reading selected documents on this device…'; let worker; async function recognize(image) { worker ??= await globalThis.Tesseract.createWorker('eng', 1, { workerPath: asset('worker.min.js'), workerBlobURL: false, corePath: asset('tesseract-core-lstm.js'), langPath: assets.href, gzip: false, cacheMethod: 'none' }); return (await worker.recognize(image)).data.text; } let readCount = 0; try { for (const file of selected) { if (revision !== latestReview) return; const item = document.createElement('details'); item.open = true; const title = document.createElement('summary'); title.textContent = file.name; item.append(title); let brief, limited; try { const result = /\.pdf$/i.test(file.name) ? await readPdf(file, recognize) : { text: await recognize(file) }; brief = summarize(result.text); limited = result.limited; } catch { brief = { lines: [] }; } if (revision !== latestReview) return; const caption = document.createElement('p'); caption.textContent = brief.lines.length ? (brief.labeled ? 'Key fields copied from the report.' : 'Opening text copied from the report.') + (limited ? ' Only the first five pages were checked.' : '') : 'No readable text was found. You can still attach this file and describe it below.'; item.append(caption); if (brief.lines.length) { readCount++; const list = document.createElement('ul'); for (const line of brief.lines) { const row = document.createElement('li'); row.textContent = line; list.append(row); } item.append(list); } output.append(item); } status.textContent = 'Readable text found in ' + readCount + ' of ' + selected.length + ' files. Check the excerpts against your originals before continuing.'; } finally { if (worker) await worker.terminate(); } } files?.addEventListener('change', review); photo?.addEventListener('change', () => queueMicrotask(review)); document.getElementById('skip-documents')?.addEventListener('click', () => queueMicrotask(review));