|
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: |
|
|
|
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_", ","), |
|
} |
|
) |
|
|
|
else: |
|
|
|
if conv_id is not None: |
|
processed_data.append(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) |
|
|