Add Common Corpus English filter and held-out split
This commit is contained in:
1 parent
a7db571513
commit
6965ab25db
4 files changed
+249
No files matched your search
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Write a training corpus with the held-out eval documents removed.
|
||||
|
||||
Documents are matched by identifier, which is how the held-out sample was drawn.
|
||||
The script fails if any held-out identifier is missing from the input, because that
|
||||
would mean the held-out list and the corpus have drifted apart.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--input-file", required=True, type=Path)
|
||||
parser.add_argument("--heldout-ids", required=True, type=Path)
|
||||
parser.add_argument("--output-file", required=True, type=Path)
|
||||
args = parser.parse_args()
|
||||
if args.output_file.exists():
|
||||
parser.error(f"refusing to overwrite: {args.output_file}")
|
||||
heldout = {line.strip() for line in args.heldout_ids.read_text(encoding="utf-8").splitlines()
|
||||
if line.strip()}
|
||||
found, kept, removed = set(), 0, 0
|
||||
with args.input_file.open(encoding="utf-8") as source, \
|
||||
args.output_file.open("x", encoding="utf-8") as target:
|
||||
for line in source:
|
||||
identifier = json.loads(line)["identifier"]
|
||||
if identifier in heldout:
|
||||
found.add(identifier)
|
||||
removed += 1
|
||||
continue
|
||||
target.write(line)
|
||||
kept += 1
|
||||
missing = heldout - found
|
||||
if missing:
|
||||
args.output_file.unlink()
|
||||
parser.error(f"{len(missing)} held-out identifiers not found in the corpus")
|
||||
print(json.dumps({"kept": kept, "removed": removed, "heldout_ids": len(heldout)}, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user