| import os |
| import json |
| import sys |
|
|
|
|
| def transform(): |
| input_dir, output_dir = sys.argv[1:] |
| if not os.path.exists(output_dir): |
| os.makedirs(output_dir) |
| for filename in os.listdir(input_dir): |
| filepath = os.path.join(input_dir, filename) |
| if os.path.isfile(filepath): |
| split = filename.split(".")[0] |
|
|
| src_file = os.path.join(output_dir, f"{split}.src") |
| tgt_file = os.path.join( |
| output_dir, f"{split}.tgt" if split == "train" else f"{split}.gold" |
| ) |
|
|
| with open(filepath, "r", encoding="UTF-8") as reader, open( |
| src_file, "w" |
| ) as src_writer, open(tgt_file, "w") as tgt_writer: |
| for line in reader: |
| if line.strip(): |
| example = json.loads(line.strip()) |
| src_writer.write(f"{example['src']}\n") |
| tgt_writer.write(f"{example['tgt']}") |
|
|
| if "db_id" in example and split != "train": |
| tgt_writer.write(f"\t{example['db_id']}") |
|
|
| tgt_writer.write("\n") |
| elif line: |
| tgt_writer.write("\n") |
|
|
|
|
| if __name__ == "__main__": |
| transform() |
|
|