build_documents.py

파일 다운로드

"""원문에서 문서·조각과 분할 비교를 생성합니다."""
import argparse
import hashlib
import json
from pathlib import Path
from documents import load_documents, chunk_documents, write_jsonl


def build(data_dir, max_chars=360):
    root = Path(data_dir)
    documents = load_documents(root / "source/documents.json")
    chunks = chunk_documents(documents, max_chars)
    write_jsonl(root / "documents.jsonl", documents)
    write_jsonl(root / "chunks.jsonl", chunks)
    comparison = {}
    for size in (180, 360):
        rows = chunk_documents(documents, size)
        comparison[str(size)] = {"chunk_count": len(rows),
                                "longest_chars": max(len(row["text"]) for row in rows),
                                "over_soft_limit": sum(len(row["text"]) > size for row in rows)}
    meta = {"dataset_id": "moa_data_r01", "schema_version": 1,
            "chunk_version": "sections_v1", "max_chars": max_chars,
            "document_count": len(documents), "chunk_count": len(chunks),
            "source_sha256": hashlib.sha256((root / "source/documents.json").read_bytes()).hexdigest(),
            "chunks_sha256": hashlib.sha256((root / "chunks.jsonl").read_bytes()).hexdigest(),
            "comparison": comparison}
    (root / "document_build.json").write_text(json.dumps(meta, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
    return meta


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--data", default=str(Path(__file__).parent / "data"))
    parser.add_argument("--max-chars", type=int, default=360)
    args = parser.parse_args()
    print(json.dumps(build(args.data, args.max_chars), ensure_ascii=False, indent=2))