File size: 2,209 Bytes
a6326c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
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)
|