| import os |
| from utils import parse, read_json_file, write_jsonl_file, choices |
| import re |
| from tqdm import tqdm |
| import random |
|
|
| options = list(range(4)) |
|
|
|
|
| def refine_roles_of_dialog(example): |
| final = example["options"][0][0] |
|
|
| assert final.lower() in ["m", "f"] |
|
|
| lowercase = True |
| if final in ["M", "F"]: |
| lowercase = False |
|
|
| if lowercase: |
| turns = example["article"].split("m ; f :") |
| roles = ["m :", "f :"] |
| else: |
| turns = example["article"].split("M;F:") |
| roles = ["M:", "F:"] |
|
|
| role_idx = 0 if final.lower() == "m" else 1 |
| role_idx = (role_idx + (len(turns) % 2) + 1) % 2 |
|
|
| new_turns = [] |
|
|
| for turn in turns: |
| if not turn: |
| continue |
| new_turns.append(roles[role_idx]) |
| new_turns.append(turn) |
| role_idx = 1 - role_idx |
|
|
| return new_turns |
|
|
|
|
| def get_an_order_of_choices(label, sep): |
| wrong_choices = options[:label] + options[label + 1 :] |
| random.shuffle(wrong_choices) |
|
|
| choices_order = [label] + wrong_choices |
|
|
| return sep.join([choices[idx] for idx in choices_order]) |
|
|
|
|
| def preprocess(args, split, part): |
| indir = os.path.join(os.path.join(args.input_dir, part), split) |
| outfile = os.path.join(os.path.join(args.output_dir, part), f"{split}.jsonl") |
|
|
| processed_data = [] |
| for filename in tqdm(os.listdir(indir)): |
| filepath = os.path.join(indir, filename) |
| example = read_json_file(filepath) |
|
|
| dial = {"turn": "multi", "locale": "en", "dialog": []} |
|
|
| if "plus" not in part: |
| turns = re.split("([mf] :)", example["article"]) |
| else: |
| turns = re.split("([MF]:)", example["article"]) |
| if not turns[0]: |
| turns = turns[1:] |
| assert len(turns) % 2 == 0, example |
| else: |
| turns = refine_roles_of_dialog(example) |
| |
| |
| for i in range(0, len(turns), 2): |
| role = turns[i] |
| utterance = turns[i + 1] |
|
|
| if "plus" not in part: |
| assert ( |
| len(role) == 3 |
| and role[0] in ["m", "f"] |
| and role[1] == " " |
| and role[2] == ":" |
| ) |
| else: |
| assert len(role) == 2 and role[0] in ["M", "F"] and role[1] == ":" |
|
|
| if role[0].lower() == "m": |
| role = "male" |
| else: |
| role = "female" |
|
|
| dial["dialog"].append({"roles": [role], "utterance": utterance.strip()}) |
| |
| dial["knowledge"] = {"type": "dict", "value": {}} |
|
|
| for idx, option in enumerate(example["options"]): |
| role, utterance = option.split(":", 1) |
| role = role.strip().lower() |
|
|
| assert role in ["m", "f"] |
|
|
| if role == "m": |
| role = "male" |
| else: |
| role = "female" |
|
|
| |
|
|
| |
| |
| |
| dial["knowledge"]["value"][chr(ord("A") + idx)] = utterance.strip() |
|
|
| |
| |
|
|
| |
| |
| |
| dial["dialog"][-1]["roles_to_select"] = [example["answers"]] |
|
|
| processed_data.append(dial) |
|
|
| write_jsonl_file(processed_data, outfile) |
|
|
|
|
| if __name__ == "__main__": |
| args = parse() |
| random.seed(args.seed) |
| preprocess(args, "train", "mutual") |
| preprocess(args, "dev", "mutual") |
| preprocess(args, "train", "mutual_plus") |
| preprocess(args, "dev", "mutual_plus") |
|
|