from utils import read_json_file, write_jsonl_file, parse, write_json_file import os def get_aligned_index(text, non_space_len, include_tail_space=True): cursor = 0 while non_space_len >= 0 and cursor < len(text): if not include_tail_space and non_space_len == 0: break if include_tail_space and non_space_len == 0 and text[cursor] != " ": break if text[cursor] != " ": non_space_len -= 1 cursor += 1 return cursor def parse_slot2index(slots, text, tokens): slot2index = dict() prefix_length = [0] for token in tokens: prefix_length.append(prefix_length[-1] + len(token)) for trip in slots: start = get_aligned_index(text, prefix_length[trip["start"]]) end = get_aligned_index(text, prefix_length[trip["exclusive_end"]], False) if trip["slot"] not in slot2index: slot2index[trip["slot"]] = dict() value = text[start:end] # if text not in [ # "there are none available for 8 pm , but there is one at 6.15 pm . would you like 6.15 pm instead ?", # "8 pm is unavailable at 8 pm has been declined by oren hummus . you can choose another time such as 7.15 pm , 7.30 pm , or 8.30 pm .", # "8 pm is not available on wednesday . i was unable to book your table on wednesday night at cetrella for 3 people .", # ]: # assert value not in slot2index[trip["slot"]], value if value not in slot2index[trip["slot"]]: slot2index[trip["slot"]][value] = (start, end) return slot2index def get_slot_index(slot2index, value, slot): if slot not in slot2index or value not in slot2index[slot]: # print(slot, value) return -1, -1 return slot2index[slot][value] def parse_dialogue_acts(acts, utter_dict, domain): dialog_acts = [] text = utter_dict["text"] # print(utter_dict) # print(acts) # print() slot2index = parse_slot2index(utter_dict["slots"], text, utter_dict["tokens"]) act_slot_values = dict() for act in acts: if act["type"] not in act_slot_values: act_slot_values[act["type"]] = dict() if "slot" in act: if act["slot"] not in act_slot_values[act["type"]]: act_slot_values[act["type"]][act["slot"]] = [] if "value" in act: act_slot_values[act["type"]][act["slot"]].append(act["value"]) for act in act_slot_values: svt = [] for slot, values in act_slot_values[act].items(): svp = { "slot": slot, } if values: svp["values"] = [] svp["relation"] = "=" for value in values: start, end = get_slot_index(slot2index, value, slot) if start != -1: assert value == text[start:end], f"{value} {text[start: end]}" svp["values"].append( {"value": value, "start": start, "end": end} ) else: svp["values"].append({"value": value}) svt.append(svp) dialog_acts.append({"act": act, "slot_value_table": svt, "domain": domain}) return dialog_acts def parse_dialogue_state(state, intent, domain, schema): svt = [] for pair in state: svt.append( { "slot": pair["slot"], "values": [{"value": pair["value"]}], "relation": "=", } ) schema[domain].add(pair["slot"]) dialog_state = [ {"intent": intent, "informed_slot_value_table": svt, "domain": domain} ] return dialog_state, schema def reformat(args, file, domain): path = args.input_dir + "/" + file + ".json" data = read_json_file(path) processed_data = [] schema = {domain: set()} for origin_dial in data: dialog = {"turn": "multi", "locale": "en", "dialog": []} origin_turns = origin_dial["turns"] intent = origin_turns[0]["user_intents"] assert len(intent) == 1 intent = intent[0] for origin_turn in origin_turns: # system if "system_utterance" in origin_turn: new_turn = { "roles": ["SYSTEM"], "utterance": origin_turn["system_utterance"]["text"], "dialog_acts": parse_dialogue_acts( origin_turn["system_acts"], origin_turn["system_utterance"], domain, ), } dialog["dialog"].append(new_turn) # user bs, schema = parse_dialogue_state( origin_turn["dialogue_state"], intent, domain, schema ) new_turn = { "roles": ["USER"], "utterance": origin_turn["user_utterance"]["text"], "dialog_acts": parse_dialogue_acts( origin_turn["user_acts"], origin_turn["user_utterance"], domain ), "belief_state": bs, } dialog["dialog"].append(new_turn) processed_data.append(dialog) write_jsonl_file(processed_data, args.output_dir + "/" + file + ".jsonl") schema[domain] = sorted(list(schema[domain])) ontology = {domain: {slot: True for slot in schema[domain]}} write_json_file(ontology, os.path.join(args.output_dir, f"{file}_ontology.json")) def preprocess(args, domain): reformat(args, "train", domain) reformat(args, "dev", domain) reformat(args, "test", domain) if __name__ == "__main__": args = parse() preprocess(args, args.domain)