Ganjoor-Rhythm-Bench / scripts /export_hf.py
farbodbij's picture
Add files using upload-large-folder tool
381fe1c verified
Raw
History Blame Contribute Delete
3.64 kB
#!/usr/bin/env python3
"""Package verse and benchmark JSONL files for the Hugging Face folder."""
from __future__ import annotations
import json
import re
from collections import Counter
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
HF = Path(__file__).resolve().parents[1]
DATA = ROOT / "data"
OUT = HF / "data"
BAHAR = re.compile(r"\s*\([^)]*\)\s*$")
def arkans(rhythm: str) -> str:
return BAHAR.sub("", rhythm or "").strip()
def leaked_keys(probe_path: Path, threshold: float) -> set[tuple]:
best: dict[tuple, float] = {}
with probe_path.open(encoding="utf-8") as handle:
for line in handle:
row = json.loads(line)
key = (row["poem_id"], row["couplet_index"])
score = float(row.get("score") or 0)
if score > best.get(key, 0):
best[key] = score
return {key for key, score in best.items() if score > threshold}
def export_verses(src: Path, dest: Path) -> dict:
dest.parent.mkdir(parents=True, exist_ok=True)
n = 0
metres = Counter()
with src.open(encoding="utf-8") as inp, dest.open("w", encoding="utf-8") as out:
for line in inp:
row = json.loads(line)
rhythm = arkans(row["rhythm"])
if not row.get("verse") or not rhythm:
continue
record = {
"verse": row["verse"],
"rhythm": rhythm,
"poem_id": row.get("poem_id"),
"poet_id": row.get("poet_id"),
"couplet_index": row.get("couplet_index"),
"mesra_index": row.get("verse_order"),
}
out.write(json.dumps(record, ensure_ascii=False))
out.write("\n")
metres[rhythm] += 1
n += 1
return {"rows": n, "metres": len(metres)}
def export_benchmark(src: Path, dest: Path, drop: set[tuple]) -> dict:
dest.parent.mkdir(parents=True, exist_ok=True)
n = 0
dropped = 0
metres = Counter()
with src.open(encoding="utf-8") as inp, dest.open("w", encoding="utf-8") as out:
for line in inp:
row = json.loads(line)
key = (row["poem_id"], row["couplet_index"])
if key in drop:
dropped += 1
continue
record = {
"first": row["first"],
"second": row["second"],
"rhythm": arkans(row["rhythm"]),
"poet": row.get("poet"),
"poet_id": row.get("poet_id"),
"book": row.get("book"),
"poem_id": row.get("poem_id"),
"couplet_index": row.get("couplet_index"),
}
out.write(json.dumps(record, ensure_ascii=False))
out.write("\n")
metres[record["rhythm"]] += 1
n += 1
return {"rows": n, "dropped": dropped, "metres": len(metres), "metre_counts": metres}
def main() -> None:
threshold = 0.6
drop = leaked_keys(DATA / "leakage_probe.jsonl", threshold)
verses = export_verses(DATA / "verses.jsonl", OUT / "verses.jsonl")
bench = export_benchmark(DATA / "leakage_pool.jsonl", OUT / "benchmark.jsonl", drop)
print(json.dumps({
"verses": verses,
"benchmark": {
"rows": bench["rows"],
"dropped_similarity_gt_0.6": bench["dropped"],
"metres": bench["metres"],
},
}, ensure_ascii=False, indent=2))
print("--- benchmark metres ---")
for rhythm, count in bench["metre_counts"].most_common():
print(f" {count:5d} {rhythm}")
if __name__ == "__main__":
main()