| from utils import read_json_file, write_jsonl_file, parse |
| import os |
|
|
|
|
| def preprocess(args, split): |
| path = os.path.join(args.input_dir, f"coqa-{split}-v1.0.json") |
| data = read_json_file(path) |
|
|
| outfile = os.path.join(args.output_dir, f"{split}.jsonl") |
|
|
| data = data["data"] |
|
|
| turns = [] |
| for i in range(len(data)): |
| t = { |
| "turn": "multi", |
| "locale": "en", |
| "dialog": [], |
| "knowledge": { |
| "type": "dict", |
| "value": {"source": data[i]["source"], "passage": data[i]["story"]}, |
| }, |
| } |
|
|
| cand_answers = [list(map(lambda x: x["input_text"], data[i]["answers"]))] |
|
|
| assert split != "train" or "additional_answers" not in data[i] |
| if "additional_answers" in data[i]: |
| for answers in data[i]["additional_answers"].values(): |
| cand_answers.append(list(map(lambda x: x["input_text"], answers))) |
|
|
| cand_answers = list(zip(*cand_answers)) |
|
|
| for q, answers in zip(data[i]["questions"], cand_answers): |
| dq = {"roles": ["USER"], "utterance": q["input_text"]} |
|
|
| t["dialog"].append(dq) |
| for answer in answers: |
| da = {"roles": ["SYSTEM"], "utterance": answer} |
| t["dialog"].append(da) |
|
|
| turns.append(t) |
|
|
| write_jsonl_file(turns, outfile) |
|
|
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| |
|
|
|
|
| if __name__ == "__main__": |
| args = parse() |
| preprocess(args, "train") |
| preprocess(args, "dev") |
|
|