Download analysis/scripts/hf_rows_to_works.py from Urdatorn/Makron-code: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/Urdatorn/Makron-code/resolve/main/analysis/scripts/hf_rows_to_works.py
- Command line
-
hf download hf://Urdatorn/Makron-code/analysis/scripts/hf_rows_to_works.py
-
curl -L -o hf_rows_to_works.py https://huggingface.co/Urdatorn/Makron-code/resolve/main/analysis/scripts/hf_rows_to_works.py
1.21 kB
| """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}") | |