import os import re import sys from utils import read_txt_file, write_jsonl_file, parse def readfile(path): data = read_txt_file(path) return data def get_slot_value_table(decouple): svt = [] pattern = re.compile(r'[[](.*?)[]]', re.S) slot_list = re.findall(pattern, decouple) dic = {} for item in slot_list: slot_value = item.split(":")[-1] pair = slot_value.strip().split(' ', 1) dic["slot"] = pair[0] dic["value"] = pair[-1] svt.append(dic) dic = {} return svt def preprocess(args): filenames = os.listdir(args.input_dir) for filename in filenames: if len(filename) > 2: continue # Aiming at different path # path = os.path.join(args.input_dir, filename) # if not os.path.isdir(path): # continue # path = os.path.join(path, args.dataset + ".txt") path = os.path.join(args.input_dir, filename, args.dataset + ".txt") data = readfile(path) turns = [] for line in data: t = {} t["turn"] = "single" t["dialog"] = [] d = {} elem = line.split("\t") d["role"] = "ROLE" d["utterance"] = elem[3] d["slot_value_table"] = get_slot_value_table(elem[6]) d["summary"] = None d["locale"] = elem[5][:2] d["scenario"] = elem[4] d["intent"] = elem[1].split(":")[1] d["answer"] = None t["dialog"].append(d) t["knowledge"] = None t["goal"] = None turns.append(t) write_jsonl_file(turns, os.path.join(args.output_dir, filename + '_' + args.dataset + ".jsonl")) if __name__ == "__main__": args = parse() if args.dataset not in ["train", "test", "eval"]: print("Wrong dataset type") sys.exit(1) preprocess(args)