DialogZoo / src /preprocess /Molweni.py
AnAutomaticPencil's picture
data preprocessing update
a6326c7
import os
from utils import parse, read_json_file, write_jsonl_file
def parse_dialogue_knowledge(utterances):
dialogue = []
for utter in utterances:
dialogue.append({"roles": [utter["speaker"]], "utterance": utter["text"]})
return {"type": "dict", "value": {"dialogue": dialogue}}
def preprocess(args, split):
infile = os.path.join(args.input_dir, f"{split}.json")
outfile = os.path.join(args.output_dir, f"{split}.jsonl")
data = read_json_file(infile)["data"]["dialogues"]
processed_data = []
for example in data:
knowledge = parse_dialogue_knowledge(example["edus"])
for qa in example["qas"]:
dial = {
"turn": "single",
"locale": "en",
"dialog": [],
"knowledge": knowledge,
}
dial["dialog"].append({"roles": ["USER"], "utterance": qa["question"]})
if not qa["answers"]:
dial["dialog"].append({"roles": ["SYSTEM"], "utterance": "None"})
else:
for answer in qa["answers"]:
dial["dialog"].append(
{"roles": ["SYSTEM"], "utterance": answer["text"]}
)
processed_data.append(dial)
write_jsonl_file(processed_data, outfile)
if __name__ == "__main__":
args = parse()
preprocess(args, "train")
preprocess(args, "dev")
preprocess(args, "test")