File size: 1,206 Bytes
7e0a2ec | 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 | """Map each row of the HF data (final/data, file order) to its OGA work by walking the
macronizer parts (parts/*.txt, sorted, = the order of oga_macronized.txt) in step with the
rows; cleaning only removed lines, so a greedy in-order match is exact.
Usage: python3 hf_rows_to_works.py PARTS_DIR HF_DATA_DIR OUT_TSV"""
import glob, os, sys
import pyarrow.parquet as pq
parts, data, out = sys.argv[1:4]
rows = []
for f in sorted(glob.glob(os.path.join(data, "*.parquet"))):
rows += pq.read_table(f, columns=["macronized"]).column("macronized").to_pylist()
def stream():
for f in sorted(glob.glob(os.path.join(parts, "*.txt"))):
w = os.path.basename(f).split(".tok01")[0]
for l in open(f, encoding="utf-8"):
l = l.rstrip("\n")
if l.strip():
yield w, l
it = stream(); skipped = 0
with open(out, "w", encoding="utf-8") as fh:
fh.write("idx\twork\n")
for i, r in enumerate(rows):
for w, l in it:
if l == r:
fh.write(f"{i}\t{w}\n"); break
skipped += 1
else:
sys.exit(f"ran out of source lines at row {i}")
print(f"rows {len(rows)} matched, source lines skipped {skipped}")
|