DialogZoo / src /preprocess /EmpathicDialogue.py
AnAutomaticPencil's picture
data preprocessing update
a6326c7
import os
from utils import write_jsonl_file, parse
def reformat(args, split):
infile = os.path.join(args.input_dir, f"{split}.csv")
if split == "valid":
split = "dev"
outfile = os.path.join(args.output_dir, f"{split}.jsonl")
processed_data = []
conv_id = None
dialogue = {"turn": "multi", "locale": "en", "dialog": []}
roles = ["Speaker", "Listener"]
turn_idx = -1
with open(infile, "r", encoding="UTF-8") as reader:
# skip the first header line
reader.readline()
for line in reader:
if not line.strip():
continue
parts = line.strip().split(",")
if conv_id is not None and parts[0] == conv_id:
turn_idx += 1
dialogue["dialog"].append(
{
"roles": [roles[turn_idx % 2]],
"utterance": parts[5].replace("_comma_", ","),
}
)
# next dialogue
else:
# save previous dialogue
if conv_id is not None:
processed_data.append(dialogue)
# current dialogue
turn_idx = 0
conv_id = parts[0]
dialogue = {
"turn": "multi",
"locale": "en",
"dialog": [
{
"roles": [roles[turn_idx % 2]],
"utterance": parts[5].replace("_comma_", ","),
}
],
"knowledge": {
"type": "dict",
"value": {
"emotion": parts[2],
"passage": parts[3].replace("_comma_", ","),
},
},
}
if conv_id is not None:
processed_data.append(dialogue)
write_jsonl_file(processed_data, outfile)
def preprocess(args):
reformat(args, "train")
reformat(args, "valid")
reformat(args, "test")
if __name__ == "__main__":
args = parse()
preprocess(args)