File size: 2,157 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
from utils import parse, write_jsonl_file
import os, json
from tqdm import tqdm

def replace(text):
    text = text.strip()
    if text:
        return text
    
    print("Replacing an utterance into `None`")
    
    return "None"


def preprocess(args):
    dialogs = []

    dial = {
        "turn": "multi",
        "locale": "en",
        "dialog": [],
    }

    pre_id = None
    dial = None
    roles = ["USER", "SYSTEM"]
    role_idx = 0
    with open(os.path.join(args.input_dir, "utterances.jsonl"), "r") as reader:
        for line in tqdm(reader):
            turn = json.loads(line)

            if dial is None:
                dial = {
                    "turn": "multi",
                    "locale": "en",
                    "dialog": [
                        {
                            "roles": ["USER"],
                            "utterance": replace(turn["text"]),
                        }
                    ],
                }
                role_idx += 1
                pre_id = turn["conversation_id"]
            elif turn["conversation_id"] == pre_id:
                dial["dialog"].append(
                    {
                        "roles": [roles[role_idx % 2]],
                        "utterance": replace(turn["text"]),
                    }
                )
                role_idx += 1
            else:
                dialogs.append(dial)
                dial = {
                    "turn": "multi",
                    "locale": "en",
                    "dialog": [
                        {
                            "roles": ["USER"],
                            "utterance": replace(turn["text"]),
                        }
                    ],
                }
                role_idx = 1
                pre_id = turn["conversation_id"]

        dialogs.append(dial)

    write_jsonl_file(dialogs[:-360], os.path.join(args.output_dir, "train.jsonl"))
    write_jsonl_file(dialogs[-360:-180], os.path.join(args.output_dir, "dev.jsonl"))
    write_jsonl_file(dialogs[-180:], os.path.join(args.output_dir, "test.jsonl"))


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