from utils import read_json_file, write_jsonl_file, parse, choices import os import copy def reformat(args, split): infile = os.path.join(args.input_dir, f"{split}.json") outfile = os.path.join(args.output_dir, f"{split}.jsonl") data = read_json_file(infile) processed_data = [] for example in data: knowledge_dial = [] for turn in example[0]: role = turn[0] role = "woman" if role == "W" else "man" text = turn[2:].strip() knowledge_dial.append({"roles": [role], "utterance": text}) _dial = { "turn": "single", "locale": "en", "dialog": [], "knowledge": {"type": "list", "value": {}}, } for turn in example[1]: dialogue = copy.deepcopy(_dial) dialogue["dialog"].append( {"roles": ["USER"], "utterance": turn["question"]} ) label = -1 for idx, choice in enumerate(turn["choice"]): # dialogue["dialog"].append( # {"roles": [f"{choices[idx]} choice"], "utterance": choice} # ) dialogue["knowledge"]["value"][chr(ord('A') + idx)] = choice if choice == turn["answer"]: label = idx assert label >= 0 dialogue["dialog"][-1]["roles_to_select"] = [chr(ord('A') + label)] dialogue["knowledge"]["value"]["dialogue"] = knowledge_dial processed_data.append(dialogue) write_jsonl_file(processed_data, outfile) def preprocess(args): reformat(args, "train") reformat(args, "dev") reformat(args, "test") if __name__ == "__main__": args = parse() preprocess(args)