File size: 1,391 Bytes
0109502 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | from __future__ import annotations
import json
from pathlib import Path
from .utils import append_jsonl
_DECISION_DIRS = {
"include": "accepted",
"manual_review": "manual_review",
"reject": "rejected",
"skip": "skipped",
"retry": "retry",
}
class DatasetWriter:
def __init__(self, output_dir: Path):
self.output_dir = output_dir
def write(self, decision: str, record: dict, tier: str | None = None,
tier_library: str | None = None) -> Path:
"""Write a record under data/<tier>/[<library>/]<decision>/records.jsonl.
`tier` defaults to record["tier"] if not passed explicitly, then to
"unclassified" for records that never made it far enough to be
classified (e.g. empty source rows).
"""
tier = tier or record.get("tier") or "unclassified"
parts = [tier]
if tier_library:
parts.append(tier_library)
parts.append(_DECISION_DIRS[decision])
path = self.output_dir.joinpath(*parts) / "records.jsonl"
append_jsonl(path, record)
return path
def write_raw_nim(self, record_id: str, raw: dict) -> Path:
path = self.output_dir / "nim_raw" / f"{record_id}.json"
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(raw, ensure_ascii=False, indent=2), encoding="utf-8")
return path
|