File size: 1,432 Bytes
c4e128a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
"""
scripts/export_jsonl.py
-----------------------
Export all metadata as a single JSONL file for downstream use.

Usage:
    python scripts/export_jsonl.py --meta-dir metadata/ --out corpus.jsonl
"""

from __future__ import annotations

import argparse
import json
from pathlib import Path

from rich.console import Console

console = Console()


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description="Export metadata as JSONL.")
    parser.add_argument("--meta-dir", default=Path("metadata"), type=Path)
    parser.add_argument("--out", default=Path("corpus.jsonl"), type=Path)
    return parser.parse_args()


def main() -> None:
    args = parse_args()
    meta_dir: Path = args.meta_dir.resolve()
    out_file: Path = args.out.resolve()

    files = sorted(meta_dir.glob("*.json"))
    if not files:
        console.print(f"[yellow]No metadata found in {meta_dir}[/yellow]")
        return

    with out_file.open("w", encoding="utf-8") as fout:
        for f in files:
            try:
                data = json.loads(f.read_text(encoding="utf-8"))
                fout.write(json.dumps(data, ensure_ascii=False) + "\n")
            except json.JSONDecodeError as exc:
                console.print(f"[red]Skipping {f.name}: {exc}[/red]")

    console.print(f"[green]✓ Exported {len(files)} records to {out_file}[/green]")


if __name__ == "__main__":
    main()