doc-extract
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesedoc-extract
文档文本提取
Shared document-to-text extraction. One script, no state: reads an input file, prints JSON to stdout, writes nothing to disk (PHI-safe — no caches, no temp files; callers own any caching).
共享式文档转文本提取工具。单脚本、无状态:读取输入文件,将JSON输出到标准输出,不向磁盘写入任何内容(符合PHI安全要求——无缓存、无临时文件;缓存由调用方自行管理)。
Setup (once)
初始化(仅需一次)
bash
cd <this skill dir> && bun installThis pulls liteparse (the bin, used for PDF/DOCX/XLSX/PPTX, OCR included) and rtf-to-text (RTF). Without it, PDFs still work via a fallback if poppler is installed; other binary formats require liteparse.
litpdftotext -layoutbash
cd <this skill dir> && bun install该命令会拉取liteparse(可执行文件,支持PDF/DOCX/XLSX/PPTX格式,内置OCR功能)和rtf-to-text(用于RTF格式)。如果未安装这些依赖,若系统已安装poppler,PDF仍可通过作为备选方案处理;其他二进制格式则必须依赖liteparse。
litpdftotext -layoutUse
使用方法
bash
bun <this skill dir>/scripts/extract.ts <input-file> [--content-type <mime>]Output on stdout:
json
{ "text": "...", "method": "liteparse | pdftotext | rtf-to-text | passthrough", "pages": 12 }- is page-anchored for paged formats:
textmarkers between pages.=== [page N] === - is present when page markers exist.
pages - is the extractor that actually produced the text.
method - Format is taken from the file extension; pass (e.g.
--content-type) when the file has no useful extension, as with downloaded EHR attachments. Note liteparse refuses extension-less files, so those PDFs go through theapplication/pdffallback.pdftotext - Errors print to stderr and exit 1.
{"error": "..."}
bash
bun <this skill dir>/scripts/extract.ts <input-file> [--content-type <mime>]标准输出内容:
json
{ "text": "...", "method": "liteparse | pdftotext | rtf-to-text | passthrough", "pages": 12 }- 对于分页格式,包含页面锚点:页面之间会有
text标记。=== [page N] === - 当存在页面标记时,会输出字段。
pages - 字段表示实际生成文本所使用的提取器。
method - 格式由文件扩展名识别;当文件无有效扩展名时(如下载的EHR附件),需传入参数(例如
--content-type)。注意liteparse不处理无扩展名的文件,此类PDF会通过application/pdf备选方案处理。pdftotext - 错误信息会以格式输出到标准错误流,并以状态码1退出。
{"error": "..."}
Table caveat
表格提取注意事项
Tables with multiple value columns (option A vs option B, in-tier vs out-of-tier) can interleave columns line-by-line in the extracted text: fragments of adjacent cells alternate, and a cell's text can even land mid-sentence inside a neighboring column. Values usually survive, but which column a value belongs to can become ambiguous. When an answer comes from one column of a multi-column table and the document has no redundant restatement of the value elsewhere, verify it by reading the original page directly before treating it as ground truth. The extracted text's anchor tells you which page: pass it to the Read tool's parameter (e.g. pages: "37") to render just that page to vision instead of the whole document.
=== [page N] ===pages包含多值列的表格(如选项A vs 选项B、计划内 vs 计划外)在提取后的文本中可能会逐行交错显示列内容:相邻单元格的文本片段交替出现,甚至某个单元格的文本可能会插入到相邻列的句子中间。数值通常会保留,但数值所属的列可能会变得模糊。当答案来自多列表格中的某一列,且文档其他位置未重复该数值时,在将其视为真实数据前,请直接查看原始页面进行验证。提取文本中的锚点会告知对应的页码:可将该页码传入Read工具的参数(例如pages: "37"),仅渲染该页面而非整个文档。
=== [page N] ===pagesFor other skills
供其他技能调用
Import the functions instead of shelling out when you're already in bun TS:
ts
import { extract, resolveLit } from "../doc-extract/scripts/extract";
const lit = resolveLit([myRoot]); // also checks myRoot/node_modules/.bin/lit
const text = extract(lit, "/path/to/file.pdf"); // string | nullThe contracts skill consumes it this way (its ingest caching stays on the contracts side).
当您已在bun TS环境中时,可直接导入函数而非通过shell调用:
ts
import { extract, resolveLit } from "../doc-extract/scripts/extract";
const lit = resolveLit([myRoot]); // also checks myRoot/node_modules/.bin/lit
const text = extract(lit, "/path/to/file.pdf"); // string | nullcontracts技能就是通过这种方式调用的(其导入缓存由contracts侧自行管理)。