"""R5 GEN: put the train chunks of a chunk list in round-robin order over the ten languages (and over H and T), so that a partly finished stage has every language. Val and test chunks keep their order and stay first. The chunk tags (lines) are unchanged: only the line order changes. usage: python reorder_chunks.py IN.tsv OUT.tsv""" import collections import sys rows = [l.rstrip("\n").split("\t") for l in open(sys.argv[1], encoding="utf-8") if l.strip()] head = [r for r in rows if r[1] in ("val", "test")] train = [r for r in rows if r[1] == "train"] lists = collections.OrderedDict() for r in train: lists.setdefault((r[0], r[2]), []).append(r) # alternate H and T per language: order of keys (lang, pop) out = [] more = True while more: more = False for key in list(lists): if lists[key]: out.append(lists[key].pop(0)) more = True with open(sys.argv[2], "w", encoding="utf-8") as f: for r in head + out: f.write("\t".join(r) + "\n") print(len(head), "val/test chunks,", len(out), "train chunks")