File size: 2,531 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from utils import read_jsonl_file, write_jsonl_file, parse, read_line_labels
import os
import copy

label2nl = {"1": "First", "2": "Second"}


def preprocess_for_train_and_dev(args, file):
    data_path = os.path.join(args.input_dir, f"{file}.jsonl")
    data = read_jsonl_file(data_path)

    label_path = os.path.join(args.input_dir, f"{file}-labels.lst")
    labels = read_line_labels(label_path)

    turns = []
    for idx, example in enumerate(data):
        turn = {
            "turn": "multi",
            "locale": "en",
            "dialog": [
                {"roles": ["First observation"], "utterance": example["obs1"]},
                {
                    "roles": ["Second observation"],
                    "utterance": example["obs2"],
                    "roles_to_select": [f"hypothesis candidate {labels[idx]}"],
                },
            ],
        }

        # turn["dialog"].append(
        #     {
        #         "roles": ["First hypothesis"],
        #         "utterance": example["hyp1"],
        #     }
        # )

        # turn["dialog"].append(
        #     {
        #         "roles": ["Second hypothesis"],
        #         "utterance": example["hyp2"],
        #         "roles_to_select": [label2nl[labels[idx]] + " hypothesis"],
        #     }
        # )

        turn["knowledge"] = {
            "type": "text",
            "value": {
                "hypothesis candidate 1": example["hyp1"],
                "hypothesis candidate 2": example["hyp2"],
            },
        }

        # turn["roles_to_select"] = ["HYPOTHESIS " + labels[idx]]
        turns.append(turn)

        # if labels[idx] == "1":
        #     pos_hyp = example["hyp1"]
        #     neg_hyp = example["hyp2"]
        # else:
        #     pos_hyp = example["hyp2"]
        #     neg_hyp = example["hyp1"]

        # # possitive hypothesis
        # pos_turn = copy.deepcopy(turn)
        # pos_turn["dialog"].append({"roles": ["HYPOTHESIS"], "utterance": pos_hyp, "class_label": True})

        # # negative hypothesis
        # neg_turn = copy.deepcopy(turn)
        # neg_turn["dialog"].append({"roles": ["HYPOTHESIS"], "utterance": neg_hyp, "class_label": False})

        # turns.append(pos_turn)
        # turns.append(neg_turn)

    write_jsonl_file(turns, os.path.join(args.output_dir, f"{file}.jsonl"))


def preprocess(args):
    preprocess_for_train_and_dev(args, "train")
    preprocess_for_train_and_dev(args, "dev")


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