File size: 1,275 Bytes
a6326c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
30
31
32
33
34
35
36
37
38
39
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()