| 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) | |