|
from utils import read_json_file, write_jsonl_file, parse |
|
import os |
|
|
|
|
|
def preprocess(args, split): |
|
path = os.path.join(args.input_dir, f"doqa-cooking-{split}-v2.1.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)): |
|
title = data[i]["title"] |
|
background = data[i]["background"] |
|
|
|
for example in data[i]["paragraphs"]: |
|
passage = example["context"] |
|
if passage.endswith("CANNOTANSWER"): |
|
passage = passage[: -len("CANNOTANSWER")].strip() |
|
knowledge = { |
|
"type": "dict", |
|
"value": {"title": title, "background": background, "passage": passage}, |
|
} |
|
|
|
t = {"turn": "multi", "locale": "en", "dialog": [], "knowledge": knowledge} |
|
|
|
for qa in example["qas"]: |
|
t["dialog"].append({"roles": ["USER"], "utterance": qa["question"]}) |
|
|
|
assert split != "train" or len(qa["answers"]) == 1 |
|
|
|
for answer in qa["answers"]: |
|
text = ( |
|
answer["input_text"] |
|
if "input_text" in answer |
|
else answer["text"] |
|
) |
|
if text == "CANNOTANSWER": |
|
text = "None" |
|
|
|
t["dialog"].append({"roles": ["SYSTEM"], "utterance": text}) |
|
|
|
turns.append(t) |
|
|
|
write_jsonl_file(turns, outfile) |
|
|
|
|
|
if __name__ == "__main__": |
|
args = parse() |
|
preprocess(args, "train") |
|
preprocess(args, "dev") |
|
preprocess(args, "test") |
|
|