File size: 1,265 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
40
41
42
43
44
45
46
47
48
49
from utils import parse, write_jsonl_file
import xml.etree.ElementTree as ET
import os


def preprocess(args, split):
    infile = os.path.join(args.input_dir, f"{split}.xml")

    if split == "val":
        split = "dev"

    outfile = os.path.join(args.output_dir, f"{split}.jsonl")

    tree = ET.parse(infile)
    processed_data = []

    for sentence in tree.getroot():
        processed_data.append(
            {
                "turn": "single",
                "locale": "en",
                "dialog": [
                    {"roles": ["USER"], "utterance": sentence[0].text, "aspects": []}
                ],
            }
        )

        for aspect in sentence[1]:
            processed_data[-1]["dialog"][-1]["aspects"].append(
                {
                    "target": {
                        "value": aspect.attrib["term"],
                        "start": int(aspect.attrib["from"]),
                        "end": int(aspect.attrib["to"]),
                    },
                    "sentiment": aspect.attrib["polarity"],
                }
            )

    write_jsonl_file(processed_data, outfile)


if __name__ == "__main__":
    args = parse()

    preprocess(args, "train")
    preprocess(args, "val")
    preprocess(args, "test")