| import os | |
| from utils import read_jsonl_file, write_jsonl_file, parse, choices | |
| def preprocess(args, split): | |
| infile = os.path.join(args.input_dir, f"CSQA2_{split}.json") | |
| outfile = os.path.join(args.output_dir, f"{split}.jsonl") | |
| data = read_jsonl_file(infile) | |
| processed_data = [] | |
| for example in data: | |
| knowledge = dict() | |
| if example["relational_prompt_used"]: | |
| knowledge["relational prompt"] = example["relational_prompt"] | |
| if example["topic_prompt_used"]: | |
| knowledge["topic prompt"] = example["topic_prompt"] | |
| dial = { | |
| "turn": "single", | |
| "locale": "en", | |
| "dialog": [], | |
| "knowledge": {"type": "dict", "value": knowledge}, | |
| } | |
| dial["dialog"].append({"roles": ["USER"], "utterance": example["question"]}) | |
| answer = example["answer"] | |
| label = -1 | |
| for idx, choice in enumerate(["yes", "no"]): | |
| dial["knowledge"]["value"][chr(ord('A') + idx)] = choice | |
| if choice == answer: | |
| assert label == -1 | |
| label = idx | |
| assert label >= 0 | |
| dial["dialog"][-1]["roles_to_select"] = chr(ord('A') + label) | |
| processed_data.append(dial) | |
| write_jsonl_file(processed_data, outfile) | |
| if __name__ == "__main__": | |
| args = parse() | |
| preprocess(args, "train") | |
| preprocess(args, "dev") | |