| |
|
|
| from utils import parse, read_json_file, read_line_labels, write_json_file |
| import os, re, json, spacy |
| from collections import OrderedDict |
| from tqdm import tqdm |
| import shutil |
|
|
| all_domains = [ |
| "restaurant", |
| "hotel", |
| "attraction", |
| "train", |
| "taxi", |
| "police", |
| "hospital", |
| ] |
|
|
| mapping_pairs = { |
| "it 's": "it is", |
| "don 't": "do not", |
| "doesn 't": "does not", |
| "didn 't": "did not", |
| "you 'd": "you would", |
| "you 're": "you are", |
| "you 'll": "you will", |
| "i 'm": "i am", |
| "they 're": "they are", |
| "that 's": "that is", |
| "what 's": "what is", |
| "couldn 't": "could not", |
| "i 've": "i have", |
| "we 've": "we have", |
| "can 't": "cannot", |
| "i 'd": "i would", |
| "aren 't": "are not", |
| "isn 't": "is not", |
| "wasn 't": "was not", |
| "weren 't": "were not", |
| "won 't": "will not", |
| "there 's": "there is", |
| "there 're": "there are", |
| ". .": ".", |
| "restaurants": "restaurant -s", |
| "hotels": "hotel -s", |
| "laptops": "laptop -s", |
| "cheaper": "cheap -er", |
| "dinners": "dinner -s", |
| "lunches": "lunch -s", |
| "breakfasts": "breakfast -s", |
| "expensively": "expensive -ly", |
| "moderately": "moderate -ly", |
| "cheaply": "cheap -ly", |
| "prices": "price -s", |
| "places": "place -s", |
| "venues": "venue -s", |
| "ranges": "range -s", |
| "meals": "meal -s", |
| "locations": "location -s", |
| "areas": "area -s", |
| "policies": "policy -s", |
| "children": "child -s", |
| "kids": "kid -s", |
| "kidfriendly": "kid friendly", |
| "cards": "card -s", |
| "upmarket": "expensive", |
| "inpricey": "cheap", |
| "inches": "inch -s", |
| "uses": "use -s", |
| "dimensions": "dimension -s", |
| "driverange": "drive range", |
| "includes": "include -s", |
| "computers": "computer -s", |
| "machines": "machine -s", |
| "families": "family -s", |
| "ratings": "rating -s", |
| "constraints": "constraint -s", |
| "pricerange": "price range", |
| "batteryrating": "battery rating", |
| "requirements": "requirement -s", |
| "drives": "drive -s", |
| "specifications": "specification -s", |
| "weightrange": "weight range", |
| "harddrive": "hard drive", |
| "batterylife": "battery life", |
| "businesses": "business -s", |
| "hours": "hour -s", |
| "one": "1", |
| "two": "2", |
| "three": "3", |
| "four": "4", |
| "five": "5", |
| "six": "6", |
| "seven": "7", |
| "eight": "8", |
| "nine": "9", |
| "ten": "10", |
| "eleven": "11", |
| "twelve": "12", |
| "anywhere": "any where", |
| "good bye": "goodbye", |
| } |
|
|
| normlize_slot_names = { |
| "car type": "car", |
| "entrance fee": "price", |
| "duration": "time", |
| "leaveat": "leave", |
| "arriveby": "arrive", |
| "trainid": "id", |
| } |
|
|
|
|
| def clean_time(utter): |
| utter = re.sub( |
| r"(\d+) ([ap]\.?m)", lambda x: x.group(1) + x.group(2), utter |
| ) |
| utter = re.sub(r"((?<!\d)\d:\d+)(am)?", r"0\1", utter) |
| utter = re.sub(r"((?<!\d)\d)am", r"0\1:00", utter) |
| utter = re.sub(r"((?<!\d)\d)pm", lambda x: str(int(x.group(1)) + 12) + ":00", utter) |
| utter = re.sub( |
| r"(\d+)(:\d+)pm", lambda x: str(int(x.group(1)) + 12) + x.group(2), utter |
| ) |
| utter = re.sub(r"(\d+)a\.?m", r"\1", utter) |
| return utter |
|
|
|
|
| def clean_text(text): |
| text = text.strip() |
| text = text.lower() |
| text = text.replace("’", "'") |
| text = text.replace("‘", "'") |
| text = text.replace(";", ",") |
| text = text.replace('"', " ") |
| text = text.replace("/", " and ") |
| text = text.replace("don't", "do n't") |
| text = clean_time(text) |
| baddata = { |
| r"c\.b (\d), (\d) ([a-z])\.([a-z])": r"cb\1\2\3\4", |
| "c.b. 1 7 d.y": "cb17dy", |
| "c.b.1 7 d.y": "cb17dy", |
| "c.b 25, 9 a.q": "cb259aq", |
| "isc.b 25, 9 a.q": "is cb259aq", |
| "c.b2, 1 u.f": "cb21uf", |
| "c.b 1,2 q.a": "cb12qa", |
| "0-122-336-5664": "01223365664", |
| "postcodecb21rs": "postcode cb21rs", |
| r"i\.d": "id", |
| " i d ": "id", |
| "Telephone:01223358966": "Telephone: 01223358966", |
| "depature": "departure", |
| "depearting": "departing", |
| "-type": " type", |
| r"b[\s]?&[\s]?b": "bed and breakfast", |
| "b and b": "bed and breakfast", |
| r"guesthouse[s]?": "guest house", |
| r"swimmingpool[s]?": "swimming pool", |
| "wo n't": "will not", |
| " 'd ": " would ", |
| " 'm ": " am ", |
| " 're' ": " are ", |
| " 'll' ": " will ", |
| " 've ": " have ", |
| r"^\'": "", |
| r"\'$": "", |
| } |
| for tmpl, good in baddata.items(): |
| text = re.sub(tmpl, good, text) |
|
|
| text = re.sub(r"([a-zT]+)\.([a-z])", r"\1 . \2", text) |
| text = re.sub(r"(\w+)\.\.? ", r"\1 . ", text) |
|
|
| for fromx in mapping_pairs: |
| tox = mapping_pairs[fromx] |
| text = " " + text + " " |
| text = text.replace(" " + fromx + " ", " " + tox + " ")[1:-1] |
|
|
| return text |
|
|
|
|
| def clean_slot_values(domain, slot, value): |
| value = clean_text(value) |
| if not value: |
| value = "" |
| elif value == "not mentioned": |
| value = "" |
| elif domain == "attraction": |
| if slot == "name": |
| if value == "t": |
| value = "" |
| if value == "trinity": |
| value = "trinity college" |
| elif slot == "area": |
| if value in ["town centre", "cent", "center", "ce"]: |
| value = "centre" |
| elif value in ["ely", "in town", "museum", "norwich", "same area as hotel"]: |
| value = "" |
| elif value in ["we"]: |
| value = "west" |
| elif slot == "type": |
| if value in ["m", "mus", "musuem"]: |
| value = "museum" |
| elif value in ["art", "architectural"]: |
| value = "architecture" |
| elif value in ["churches"]: |
| value = "church" |
| elif value in ["coll"]: |
| value = "college" |
| elif value in ["concert", "concerthall"]: |
| value = "concert hall" |
| elif value in ["night club"]: |
| value = "nightclub" |
| elif value in ["mutiple sports", "mutliple sports", "sports", "galleria"]: |
| value = "multiple sports" |
| elif value in ["ol", "science", "gastropub", "la raza"]: |
| value = "" |
| elif value in ["swimmingpool", "pool"]: |
| value = "swimming pool" |
| elif value in ["fun"]: |
| value = "entertainment" |
|
|
| elif domain == "hotel": |
| if slot == "area": |
| if value in ["cen", "centre of town", "near city center", "center"]: |
| value = "centre" |
| elif value in ["east area", "east side"]: |
| value = "east" |
| elif value in ["in the north", "north part of town"]: |
| value = "north" |
| elif value in ["we"]: |
| value = "west" |
| elif slot == "day": |
| if value == "monda": |
| value = "monday" |
| elif value == "t": |
| value = "tuesday" |
| elif slot == "name": |
| if value == "uni": |
| value = "university arms hotel" |
| elif value == "university arms": |
| value = "university arms hotel" |
| elif value == "acron": |
| value = "acorn guest house" |
| elif value == "ashley": |
| value = "ashley hotel" |
| elif value == "arbury lodge guesthouse": |
| value = "arbury lodge guest house" |
| elif value == "la": |
| value = "la margherit" |
| elif value == "no": |
| value = "" |
| elif slot == "internet": |
| if value == "does not": |
| value = "no" |
| elif value in ["y", "free", "free internet"]: |
| value = "yes" |
| elif value in ["4"]: |
| value = "" |
| elif slot == "parking": |
| if value == "n": |
| value = "no" |
| elif value in ["free parking"]: |
| value = "yes" |
| elif value in ["y"]: |
| value = "yes" |
| elif slot in ["pricerange", "price range"]: |
| slot = "pricerange" |
| if value == "moderately": |
| value = "moderate" |
| elif value in ["any"]: |
| value = "do n't care" |
| elif value in ["any"]: |
| value = "do n't care" |
| elif value in ["inexpensive"]: |
| value = "cheap" |
| elif value in ["2", "4"]: |
| value = "" |
| elif slot == "stars": |
| if value == "two": |
| value = "2" |
| elif value == "three": |
| value = "3" |
| elif value in ["4-star", "4 stars", "4 star", "four star", "four stars"]: |
| value = "4" |
| elif slot == "type": |
| if value == "0 star rarting": |
| value = "" |
| elif value == "guesthouse": |
| value = "guest house" |
| elif value not in ["hotel", "guest house", "do n't care"]: |
| value = "" |
| elif domain == "restaurant": |
| if slot == "area": |
| if value in [ |
| "center", |
| "scentre", |
| "center of town", |
| "city center", |
| "cb30aq", |
| "town center", |
| "centre of cambridge", |
| "city centre", |
| ]: |
| value = "centre" |
| elif value == "west part of town": |
| value = "west" |
| elif value == "n": |
| value = "north" |
| elif value in ["the south"]: |
| value = "south" |
| elif value not in [ |
| "centre", |
| "south", |
| "do n't care", |
| "west", |
| "east", |
| "north", |
| ]: |
| value = "" |
| elif slot == "day": |
| if value == "monda": |
| value = "monday" |
| elif value == "t": |
| value = "tuesday" |
| elif slot in ["pricerange", "price range"]: |
| slot = "pricerange" |
| if value in ["moderately", "mode", "mo"]: |
| value = "moderate" |
| elif value in ["not"]: |
| value = "" |
| elif value in ["inexpensive", "ch"]: |
| value = "cheap" |
| elif slot == "food": |
| if value == "barbecue": |
| value = "barbeque" |
| elif slot == "pricerange": |
| if value == "moderately": |
| value = "moderate" |
| elif slot == "time": |
| if value == "9:00": |
| value = "09:00" |
| elif value == "9:45": |
| value = "09:45" |
| elif value == "1330": |
| value = "13:30" |
| elif value == "1430": |
| value = "14:30" |
| elif value == "9:15": |
| value = "09:15" |
| elif value == "9:30": |
| value = "09:30" |
| elif value == "1830": |
| value = "18:30" |
| elif value == "9": |
| value = "09:00" |
| elif value == "2:00": |
| value = "14:00" |
| elif value == "1:00": |
| value = "13:00" |
| elif value == "3:00": |
| value = "15:00" |
| elif domain == "taxi": |
| if slot in ["arriveBy", "arrive by"]: |
| slot = "arriveby" |
| if value == "1530": |
| value = "15:30" |
| elif value == "15 minutes": |
| value = "" |
| elif slot in ["leaveAt", "leave at"]: |
| slot = "leaveat" |
| if value == "1:00": |
| value = "01:00" |
| elif value == "21:4": |
| value = "21:04" |
| elif value == "4:15": |
| value = "04:15" |
| elif value == "5:45": |
| value = "05:45" |
| elif value == "0700": |
| value = "07:00" |
| elif value == "4:45": |
| value = "04:45" |
| elif value == "8:30": |
| value = "08:30" |
| elif value == "9:30": |
| value = "09:30" |
| value = value.replace(".", ":") |
|
|
| elif domain == "train": |
| if slot in ["arriveBy", "arrive by"]: |
| slot = "arriveby" |
| if value == "1": |
| value = "01:00" |
| elif value in ["does not care", "doesnt care", "doesn't care"]: |
| value = "do n't care" |
| elif value == "8:30": |
| value = "08:30" |
| elif value == "not 15:45": |
| value = "" |
| value = value.replace(".", ":") |
| elif slot == "day": |
| if value == "doesnt care" or value == "doesn't care": |
| value = "do n't care" |
| elif slot in ["leaveAt", "leave at"]: |
| slot = "leaveat" |
| if value == "2:30": |
| value = "02:30" |
| elif value == "7:54": |
| value = "07:54" |
| elif value == "after 5:45 pm": |
| value = "17:45" |
| elif value in ["early evening", "friday", "sunday", "tuesday", "afternoon"]: |
| value = "" |
| elif value == "12": |
| value = "12:00" |
| elif value == "1030": |
| value = "10:30" |
| elif value == "1700": |
| value = "17:00" |
| elif value in [ |
| "does not care", |
| "doesnt care", |
| "do nt care", |
| "doesn't care", |
| ]: |
| value = "do n't care" |
|
|
| value = value.replace(".", ":") |
| if value in ["dont care", "don't care", "do nt care", "doesn't care"]: |
| value = "do n't care" |
| if normlize_slot_names.get(slot): |
| slot = normlize_slot_names[slot] |
| return slot, value |
|
|
|
|
| def parse_belief_state(raw, dial_domains, constraint_dict, model): |
| for domain in dial_domains: |
| if not constraint_dict.get(domain): |
| constraint_dict[domain] = OrderedDict() |
| info_sv = raw[domain]["semi"] |
| for s, v in info_sv.items(): |
| s, v = clean_slot_values(domain, s, v) |
| if len(v.split()) > 1: |
| v = " ".join([token.text for token in model(v)]).strip() |
| if v != "": |
| constraint_dict[domain][s] = v |
| book_sv = raw[domain]["book"] |
| for s, v in book_sv.items(): |
| if s == "booked": |
| continue |
| s, v = clean_slot_values(domain, s, v) |
| if len(v.split()) > 1: |
| v = " ".join([token.text for token in model(v)]).strip() |
| if v != "": |
| constraint_dict[domain][s] = v |
|
|
| belief_state = [] |
| for domain in constraint_dict: |
| cur_domain_bs = { |
| "intent": "", |
| "informed_slot_value_table": [], |
| "requested_slots": [], |
| "domain": domain, |
| } |
| for slot in constraint_dict[domain]: |
| cur_domain_bs["informed_slot_value_table"].append( |
| { |
| "slot": slot, |
| "values": [ |
| { |
| "value": constraint_dict[domain][slot], |
| "cononical_value": constraint_dict[domain][slot], |
| } |
| ], |
| "relation": "=", |
| } |
| ) |
|
|
| belief_state.append(cur_domain_bs) |
|
|
| return constraint_dict, belief_state |
|
|
|
|
| def preprocess(args): |
| if not os.path.exists(args.output_dir): |
| os.makedirs(args.output_dir) |
| data = read_json_file(os.path.join(args.input_dir, "data.json")) |
| test_split = read_line_labels(os.path.join(args.input_dir, "testListFile.txt")) |
| dev_split = read_line_labels(os.path.join(args.input_dir, "valListFile.txt")) |
|
|
| model = spacy.load("en_core_web_sm") |
|
|
| with open(os.path.join(args.output_dir, "train.jsonl"), "w") as train_writer, open( |
| os.path.join(args.output_dir, "dev.jsonl"), "w" |
| ) as dev_writer, open( |
| os.path.join(args.output_dir, "test.jsonl"), "w" |
| ) as test_writer: |
| for dial_id in tqdm(data): |
| origin_dialog = data[dial_id] |
|
|
| constraint_dict = OrderedDict() |
|
|
| |
| dialog = { |
| "turn": "multi", |
| "domain": [], |
| "dialog": [], |
| } |
|
|
| |
| dial_domains = [] |
| for dom, g in origin_dialog["goal"].items(): |
| if dom != "topic" and dom != "message" and g: |
| if dom in all_domains: |
| dial_domains.append(dom) |
|
|
| for _, dial_turn in enumerate(origin_dialog["log"]): |
| dial_state = dial_turn["metadata"] |
| utterance = " ".join(clean_text(dial_turn["text"]).split()) |
| if not dial_state: |
| new_turn = { |
| "roles": ["USER"], |
| "utterance": utterance, |
| "dialog_acts": [], |
| } |
|
|
| else: |
| constraint_dict, new_bf_state = parse_belief_state( |
| dial_state, dial_domains, constraint_dict, model |
| ) |
| dialog["dialog"][-1]["belief_state"] = new_bf_state |
| new_turn = { |
| "roles": ["SYSTEM"], |
| "utterance": utterance, |
| "dialog_acts": [], |
| } |
|
|
| dialog["dialog"].append(new_turn) |
|
|
| |
| if dial_id in test_split: |
| test_writer.write(json.dumps(dialog) + "\n") |
| elif dial_id in dev_split: |
| dev_writer.write(json.dumps(dialog) + "\n") |
| else: |
| train_writer.write(json.dumps(dialog) + "\n") |
|
|
|
|
| if __name__ == "__main__": |
| args = parse() |
| preprocess(args) |
|
|
| schema = { |
| "taxi": ["leave", "destination", "departure", "arrive"], |
| "police": [], |
| "hospital": ["department"], |
| "hotel": [ |
| "type", |
| "parking", |
| "pricerange", |
| "internet", |
| "stay", |
| "day", |
| "people", |
| "area", |
| "stars", |
| "name", |
| ], |
| "attraction": ["area", "type", "name"], |
| "train": ["destination", "day", "arrive", "departure", "people", "leave"], |
| "restaurant": ["food", "pricerange", "area", "name", "time", "day", "people"], |
| } |
|
|
| ontology = {domain: {slot: True for slot in schema[domain]} for domain in schema} |
|
|
| write_json_file(ontology, os.path.join(args.output_dir, "train_ontology.json")) |
| shutil.copyfile( |
| os.path.join(args.output_dir, "train_ontology.json"), |
| os.path.join(args.output_dir, "dev_ontology.json"), |
| ) |
| shutil.copyfile( |
| os.path.join(args.output_dir, "train_ontology.json"), |
| os.path.join(args.output_dir, "test_ontology.json"), |
| ) |
|
|