import os from utils import read_csv_file, parse, write_jsonl_file def parse_knowledge(mr): svt = mr.split(",") knowledge = dict() for svp in svt: slot, value = svp.strip().split("[") assert value[-1] == "]" slot = slot.strip() value = value[:-1].strip() knowledge[slot] = value return {"type": "dict", "value": knowledge} def preprocess(args, split): if split != "test": infile = os.path.join(args.input_dir, f"{split}set.csv") else: infile = os.path.join(args.input_dir, "testset_w_refs.csv") outfile = os.path.join(args.output_dir, f"{split}.jsonl") processed_data = [] data = read_csv_file(infile) mrs = data["mr"].values refs = data["refs"] = data["ref"].values mr2refs = dict() for mr, ref in zip(mrs, refs): if mr not in mr2refs: mr2refs[mr] = [] mr2refs[mr].append(ref) for mr in mr2refs: refs = mr2refs[mr] dial = { "turn": "single", "locale": "en", "dialog": [], "knowledge": parse_knowledge(mr), } for ref in refs: dial["dialog"].append({"roles": ["SYSTEM"], "utterance": ref.strip()}) processed_data.append(dial) write_jsonl_file(processed_data, outfile) if __name__ == "__main__": args = parse() preprocess(args, "train") preprocess(args, "dev") preprocess(args, "test")