import os from utils import read_json_file, write_jsonl_file, parse def get_belief_state(metadata): bs = [] if not len(metadata): return bs for d_name, domain in metadata.items(): blf_stt = {"domain": d_name, "goal": []} for i_name, intent in domain.items(): goal = {"intent": i_name, "slot_value_table": []} for slot, value in intent.items(): if isinstance(value, (list, dict)) and len(value) > 0: for item in value: for inner_slot, inner_value in item.items(): real_slot = slot + '_' + inner_slot svt = {"slot": real_slot, "value": inner_value} goal["slot_value_table"].append(svt) else: svt = {"slot": slot, "value": value} goal["slot_value_table"].append(svt) blf_stt["goal"].append(goal) bs.append(blf_stt) return bs def preprocess(args): dirnames = [os.path.join(args.input_dir, dir) for dir in os.listdir(args.input_dir)] for dir in dirnames: ''' add train/eval/test instruction ''' filenames = os.listdir(dir) locale = dir[-2:] for filename in filenames: path = os.path.join(dir, filename) data = read_json_file(path) turns = [] for _, item in data.items(): t = {"turn": "multi", "dialog": [], "knowledge": None} tag = "log-cn" if locale == "zh" else ("log-" + locale) for obj in item[tag]: bs = get_belief_state(obj["metadata"]) role = "USER" if not len(bs) else "SYS" d = {"role": role, "utterance": obj["text"], "belief_state": bs, "querying_result": None, "summary": None, "locale": locale, "topic": None, "opinion": None, "answer": None} t["dialog"].append(d) turns.append(t) write_jsonl_file(turns, os.path.join(args.output_dir, filename[:-5]) + ".jsonl") if __name__ == "__main__": args = parse() preprocess(args)