Read keeps the file and says its format; extract takes fields from columns; EEG batches carry their recording, on Dask where it is installed
This commit is contained in:
+33
-4
@@ -50,9 +50,22 @@ READ_COLUMNS = { # declared, so a load where every file read cleanly still has
|
||||
"text": {"data_type": "text", "nullable": True},
|
||||
"ocr": {"data_type": "bool"},
|
||||
"error": {"data_type": "text", "nullable": True},
|
||||
"format": {"data_type": "text", "nullable": True},
|
||||
}
|
||||
|
||||
|
||||
def file_format(content: bytes) -> str:
|
||||
"""What kind of file this is, from its first bytes, so a route can tell a PDF from an
|
||||
EEG recording in the same column: pdf, edf (EDF and EDF+: a fixed ASCII header whose
|
||||
version is "0"), or other (whatever MarkItDown reads)."""
|
||||
if content.startswith(b"%PDF"):
|
||||
return "pdf"
|
||||
head = content[:256]
|
||||
if len(head) == 256 and head[:8] == b"0 " and head[184:192].strip().isdigit():
|
||||
return "edf"
|
||||
return "other"
|
||||
|
||||
|
||||
@cache
|
||||
def _reader() -> MarkItDown:
|
||||
return MarkItDown()
|
||||
@@ -73,7 +86,15 @@ def _text(content: bytes) -> str:
|
||||
|
||||
|
||||
def read_file(content: bytes) -> dict:
|
||||
"""Text of one file, and whether OCR was needed. An unreadable file keeps its error."""
|
||||
"""Text of one file, its format, and whether OCR was needed. An unreadable file keeps
|
||||
its error; a signal recording (EDF) has no text to read."""
|
||||
kind = file_format(content)
|
||||
if kind == "edf":
|
||||
return {"text": None, "ocr": False, "error": None, "format": kind}
|
||||
return {**_read_text(content), "format": kind}
|
||||
|
||||
|
||||
def _read_text(content: bytes) -> dict:
|
||||
try:
|
||||
found = _text(content)
|
||||
ocr = content.startswith(b"%PDF") and len(found.split()) < MIN_WORDS
|
||||
@@ -91,12 +112,19 @@ def read_file(content: bytes) -> dict:
|
||||
|
||||
|
||||
def run_read(p: str, node: Read):
|
||||
"""The input's rows with the file column replaced by its text; each file read once."""
|
||||
"""The input's rows with their file's text and format beside the file column (which
|
||||
keeps the file, for a study that takes the file itself: an EEG's recording); each file
|
||||
read once."""
|
||||
rows = list(nodes.rows(p, node.inputs[0]))
|
||||
for row in rows: # the source kept the file; the row carries its hash
|
||||
row["key"] = row.pop(node.column) or None
|
||||
row["key"] = row.get(node.column) or None
|
||||
keys = {r["key"] for r in rows} - {None}
|
||||
found = nodes.cached("read_cache", keys)
|
||||
legacy = [k for k, r in found.items() if r.get("format") is None] # read before formats
|
||||
for k in legacy:
|
||||
found[k]["format"] = file_format(files.content(k) or b"")
|
||||
nodes.remember("read_cache", [{"key": k, **{c: found[k][c] for c in READ_COLUMNS}}
|
||||
for k in legacy], READ_COLUMNS) # fmt: skip
|
||||
new = {k: files.content(k) for k in keys - set(found)}
|
||||
with ProcessPoolExecutor() as pool:
|
||||
read = dict(zip(new, pool.map(read_file, new.values()), strict=True))
|
||||
@@ -191,8 +219,9 @@ def run_extract(p: str, node: Extract):
|
||||
found = apply_patterns(node, text, codes)
|
||||
else:
|
||||
found = [({}, {}, ["no LLM answering and no patterns set"])]
|
||||
taken = {f: row[c] for f, c in node.columns.items() if row.get(c) is not None}
|
||||
for index, (values, quotes, held) in enumerate(found or [({}, {}, ["nothing found"])]):
|
||||
out.append({**values, RECORD: row[RECORD], "_index": index,
|
||||
out.append({**values, **taken, RECORD: row[RECORD], "_index": index,
|
||||
QUOTES: json.dumps(quotes, ensure_ascii=False),
|
||||
HELD: json.dumps(held) if held else None}) # fmt: skip
|
||||
return nodes.write(p, node.id, out)
|
||||
|
||||
Reference in New Issue
Block a user