DialogZoo / src /preprocess /FriendsQA.py
AnAutomaticPencil's picture
data preprocessing update
a6326c7
import os
from utils import read_json_file, parse, write_jsonl_file
def parse_dialogue_knowledge(utterances):
dialogue = []
for utter in utterances:
dialogue.append({"roles": utter["speakers"], "utterance": utter["utterance"]})
knowledge = {"type": "dict", "value": {"dialogue": dialogue}}
return knowledge
def preprocess(args, split):
infile = os.path.join(args.input_dir, f"friendsqa_{split}.json")
if split == "trn":
split = "train"
elif split == "tst":
split = "test"
outfile = os.path.join(args.output_dir, f"{split}.jsonl")
data = read_json_file(infile)["data"]
processed_data = []
for part in data:
for example in part["paragraphs"]:
assert "utterances:" in example, example.keys()
knowledge = parse_dialogue_knowledge(example["utterances:"])
for qa in example["qas"]:
dial = {
"turn": "single",
"locale": "en",
"dialog": [],
"knowledge": knowledge,
}
dial["dialog"].append({"roles": ["USER"], "utterance": qa["question"]})
for answer in qa["answers"]:
dial["dialog"].append(
{
"roles": ["SYSTEM"],
"utterance": answer["answer_text"],
"start": answer["inner_start"],
"end": answer["inner_end"],
"dialog_turn": answer["utterance_id"],
}
)
processed_data.append(dial)
write_jsonl_file(processed_data, outfile)
if __name__ == "__main__":
args = parse()
preprocess(args, "trn")
preprocess(args, "dev")
preprocess(args, "tst")