#!/usr/bin/env python3 """ 从 NiuTrans/Classical-Modern 导入古文原文,生成检索补充 jsonl。 设计原则: 1. 只读取 `古文原文/**/text.txt` 2. 不读取 `双语数据`、`target.txt`、`bitext.txt`、README 等说明文本 3. 以原始文本行/段为单位输出,供评估脚本直接读取原文库,或按需再做轻量预处理 """ import argparse import json from pathlib import Path SCRIPT_DIR = Path(__file__).resolve().parent DEFAULT_REPO_ROOT = SCRIPT_DIR / "retrieval_extra" / "vendor" / "Classical-Modern" DEFAULT_SOURCE_ROOT = DEFAULT_REPO_ROOT / "古文原文" DEFAULT_OUTPUT_FILE = SCRIPT_DIR / "retrieval_extra" / "classical_modern_originals.jsonl" def parse_csv_set(spec): if not spec: return set() return {item.strip() for item in str(spec).split(",") if item.strip()} def iter_source_files(source_root, include_books=None, exclude_books=None): include_books = include_books or set() exclude_books = exclude_books or set() for path in sorted(source_root.rglob("text.txt")): rel_parts = path.relative_to(source_root).parts if not rel_parts: continue book = rel_parts[0] if include_books and book not in include_books: continue if book in exclude_books: continue yield path def iter_text_units(path): with open(path, "r", encoding="utf-8") as f: for line in f: text = line.strip() if text: yield text def main(): parser = argparse.ArgumentParser(description="导入 Classical-Modern 古文原文为检索补充库") parser.add_argument( "--repo_root", default=str(DEFAULT_REPO_ROOT), type=str, help="Classical-Modern 仓库根目录", ) parser.add_argument( "--source_root", default=str(DEFAULT_SOURCE_ROOT), type=str, help="古文原文目录,默认使用 repo_root/古文原文", ) parser.add_argument( "--output_file", default=str(DEFAULT_OUTPUT_FILE), type=str, help="输出 jsonl 文件路径", ) parser.add_argument( "--include_books", default="", type=str, help="只导入这些书名,逗号分隔;默认空表示全部导入", ) parser.add_argument( "--exclude_books", default="", type=str, help="排除这些书名,逗号分隔", ) parser.add_argument( "--progress_every", default=5000, type=int, help="每处理多少条文本打印一次进度", ) args = parser.parse_args() repo_root = Path(args.repo_root) source_root = Path(args.source_root) output_file = Path(args.output_file) output_file.parent.mkdir(parents=True, exist_ok=True) if not repo_root.exists(): raise FileNotFoundError(f"repo root not found: {repo_root}") if not source_root.exists(): raise FileNotFoundError(f"source root not found: {source_root}") include_books = parse_csv_set(args.include_books) exclude_books = parse_csv_set(args.exclude_books) progress_every = max(1, args.progress_every) file_count = 0 text_count = 0 book_counter = {} with open(output_file, "w", encoding="utf-8") as fout: for path in iter_source_files( source_root, include_books=include_books, exclude_books=exclude_books, ): rel = path.relative_to(source_root) parts = rel.parts book = parts[0] section = "/".join(parts[1:-1]) if len(parts) > 2 else "" file_count += 1 for text in iter_text_units(path): record = { "source": "Classical-Modern", "book": book, "section": section, "path": str(rel), "text": text, } fout.write(json.dumps(record, ensure_ascii=False) + "\n") text_count += 1 book_counter[book] = book_counter.get(book, 0) + 1 if text_count % progress_every == 0: print( f"[progress] files={file_count}, texts={text_count}, last={rel}", flush=True, ) print("=" * 70) print("Classical-Modern import finished") print("=" * 70) print(f"Repo root: {repo_root}") print(f"Source root: {source_root}") print(f"Output file: {output_file}") print(f"Imported files: {file_count}") print(f"Imported texts: {text_count}") print("Top books:") for book, cnt in sorted(book_counter.items(), key=lambda x: x[1], reverse=True)[:20]: print(f" - {book}: {cnt}") if __name__ == "__main__": main()