def main(): import autopath cfg = Config.load_configs(['resources/default-lz.cfg'], use_default=False, project_root=True) #cfg = {'DM': {'UfalRuleDM': # {'ontology':"/xdisk/devel/vystadial/alex/applications/" + \ # "CamInfoRest/ontology.cfg", # 'db_cfg': "/xdisk/devel/vystadial/alex/applications/" + \ # "CamInfoRest/cued_data/CIRdbase_V7_noloc.txt"}}} u = UfalRuleDM(cfg) # ufal_ds = u.create_ds() while 1: curr_acts = DialogueActNBList() for ln in sys.stdin: if len(ln.strip()) == 0: break ln = ln.strip() print ln score, act = ln.split(" ", 1) score = float(score) curr_acts.add(score, DialogueActItem(dai=act)) u.da_in(curr_acts) print " >", u.da_out()
def filter(self, in_da): """Go through the input dialogue acts and pick only the ones that we can understand and that have good enough confidence.""" new_nblist = DialogueActNBList() # for each dialogue act item check if it is of known type # and if it has good probability for item in in_da: da = item[1] new_da = DialogueAct() for dai in da: if dai.dat in ["inform", "request"]: if dai.value is not None and not dai.value in self.policy.values: continue if dai.dat in ["inform", "request", "confirm"]: if not dai.name in self.policy.slots: continue # check if the value is in our ontology #if type(dai.value) is str and \ # self.ontology_unknown_re.match(dai.value): # continue if dai.dat in ["inform", "request", "other", "confirm", "reqalts", "bye", "restart"]: new_da.append(dai) if item[0] >= 0.3: # do not consider things bellow 0.3 if len(new_da) > 0: new_nblist.add(item[0], new_da) return new_nblist
def input_da_nblist(self): """Reads an N-best list of dialogue acts from the input. :rtype : confusion network """ self.init_readline() nblist = DialogueActNBList() i = 1 while i < 100: l = raw_input("User DA %d: " % i).decode('utf8') if len(l) == 1 and l.startswith("."): print break try: prob, da = self.parse_input_da(l) except SemHubException as e: print e continue nblist.add(prob, da) i += 1 nblist.merge() nblist.scale() return nblist.get_confnet()
def simulate_asr(self, user_da): """Returns N-best list ASR hypothesis given correct user_da and grammar or data etc.""" self._sampled_da_items = [] for da_item in user_da: self._sampled_da_items.append(self.simulate_one_da_item(da_item)) print "get sample da_items", self._sampled_da_items self._nbest_list = DialogueActNBList() self._build_da_nbest_list(0, None, None) print "get nbest-listobject" return self._nbest_list
def main(): cfg = Config.load_configs(['resources/default-lz.cfg'], use_default=False, project_root=True) #cfg = {'DM': {'UfalRuleDM': # {'ontology':"/xdisk/devel/vystadial/alex/applications/" + \ # "CamInfoRest/ontology.cfg", # 'db_cfg': "/xdisk/devel/vystadial/alex/applications/" + \ # "CamInfoRest/cued_data/CIRdbase_V7_noloc.txt"}}} u = UfalRuleDM(cfg) # ufal_ds = u.create_ds() while 1: curr_acts = DialogueActNBList() for ln in sys.stdin: if len(ln.strip()) == 0: break ln = ln.strip() print ln score, act = ln.split(" ", 1) score = float(score) curr_acts.add(score, DialogueActItem(dai=act)) u.da_in(curr_acts) print " >", u.da_out()
def test_swapping_merge_normalise(self): nblist1 = DialogueActNBList() nblist1.add(0.7, DialogueAct("hello()")) nblist1.add(0.2, DialogueAct("bye()")) nblist2 = deepcopy(nblist1) nblist1.merge().normalise() nblist2.normalise().merge() s = [] s.append("") s.append("Using merge().normalise():") s.append(unicode(nblist1)) s.append("") s.append("Using normalise().merge():") s.append(unicode(nblist2)) s.append("") self.assertEqual(nblist1, nblist2)
class SimpleASRSimulator(ASRSimulator): """Simple ASR simulator""" def __init__(self, cfg, db): self.full_config = cfg self._name = self.__class__.__name__ self.config = cfg["asr_simulator"][self._name] self.system_logger = self.full_config["Logging"]["system_logger"] self.domain = self.full_config["domain"] self.db = db def simulate_asr(self, user_da): """Returns N-best list ASR hypothesis given correct user_da and grammar or data etc.""" self._sampled_da_items = [] for da_item in user_da: self._sampled_da_items.append(self.simulate_one_da_item(da_item)) print "get sample da_items", self._sampled_da_items self._nbest_list = DialogueActNBList() self._build_da_nbest_list(0, None, None) print "get nbest-listobject" return self._nbest_list def _build_da_nbest_list(self, i, da, prob): if i < len(self._sampled_da_items): da_items, probs = self._sampled_da_items[i] for dai_index in range(len(da_items)): if da is None: da_new = DialogueAct() da_new.append(da_items[dai_index]) self._build_da_nbest_list(i + 1, da_new, probs[dai_index]) else: da_new = DialogueAct() da_new.extend(da) da_new.append(da_items[dai_index]) self._build_da_nbest_list( i + 1, da_new, prob * probs[dai_index] ) # TODO check the equation and fix it when we there is more types of confusion else: self._nbest_list.add(da, prob) def simulate_one_da_item(self, da_item): # TODO: The current way of sample prob is not really fit with action and slot confusion, just for value confusion print "--Simulate da_item:", da_item original_da_type = da_item.dat original_slot = da_item.name original_value = da_item.value # get the da_types/acts will be confused act_confusion = self.config["act_confusion"] da_types = original_da_type if original_da_type in act_confusion.keys(): da_types = sample_from_dict(act_confusion[original_da_type]) if isinstance(da_types, str): # only one datype will be generate da_types = [da_types] # Confusing things da_items = [] # all item confused for this da_item da_items_probs = [] # Each confused action have a prob of 1 for its all confused elements and will be combined/recalculate later for ( da_type ) in da_types: # for every dalougue act (type) which will be considerd or confused, such as inform, affirm # get the act_out definitions and check is this actionneed slot act_out_des = self.domain["dialogue_act_definitions"][da_type] if "act_without_slot" in act_out_des.keys() and act_out_des["act_without_slot"]: da_item = DialogueActItem(da_type) da_items.append(da_item) da_items_probs.append(1.0) continue # get slots will be confused slot_confusion = self.config["slot_confusion"] slots = original_slot if original_slot in slot_confusion.keys(): slots = sample_from_dict(slot_confusion[original_slot]) if isinstance(slots, str): # only one slot slots = [slots] for slot in slots: # for every slot confusion_des = self._get_confusion_description(slot, da_type) confusion_type = self._sample_confusion_type(confusion_des["confusion_types"]) correct_position = 0 print "---", da_type, slot, confusion_type if confusion_type == "silence": # can be never used since it is similar to the act confusion da_item = DialogueActItem("silence") da_items.append(da_item) else: length = confusion_des["max_length"] length = random.randint(1, length) correct_position = 0 # this postion for the confusion type = correct if confusion_type == "correct": correct_position = 0 elif confusion_type == "offlist": correct_position = -1 elif confusion_type == "onlist": correct_position = self._sample_onlist_position(confusion_des, length) # sample else: raise NotImplementedError("confusion_type=%s was not implemented." % confusion_type) # print da_type, slot, confusion_type, correct_position items, probs = self._sample_nbest_list_hypotheses( da_type, slot, original_value, correct_position, length, confusion_des, confusion_type ) da_items.extend(items) da_items_probs.extend(probs) # print 'Get final resul for a dialogue_item:', da_items return da_items, da_items_probs def _sample_confusion_type(self, confusion_des): return sample_from_dict(confusion_des) def _get_confusion_description(self, slot, da_type): slot_confusion_des = self.config["default"] if slot in self.config.keys(): slot_confusion_des = self.config[slot] refine_key = da_type + "_confusion_matrix" if refine_key in slot_confusion_des.keys(): # this da_type is refined for this slot refine = slot_confusion_des[refine_key] slot_confusion_des = override_dicts(refine, slot_confusion_des["default_confusion_matrix"]) else: slot_confusion_des = slot_confusion_des["default_confusion_matrix"] return slot_confusion_des def _sample_onlist_position(self, confusion_des, length): alpha = confusion_des["onlist_fraction_alpha"] beta = confusion_des["onlist_fraction_beta"] x = random.betavariate(alpha, beta) position = int((length - 1) * x) + 1 if position == length: position -= 1 return position def _sample_nbest_list_hypotheses( self, da_type, slot, value, correct_position, length, confusion_des, confusion_type ): """Sample nbest list hypotheses for on dialogue act item.""" """Sample n=length of dialogue act items with information slot and value, and correct_position for the truth.""" # TODO check some condtion for parameters such as length vs. cardinality of the filed and the correct position da_items = [] values = self._get_slot_values(slot) sample_length = length if correct_position == -1: # Plus one in the case of sampled value equal to correct value sample_length = length + 1 print "length=", length row_ids = random.sample(xrange(len(values)), sample_length) for i in range(length): da_item = None if i == correct_position: da_item = DialogueActItem(da_type, slot, value) else: while True: row_id = row_ids.pop(0) if values[row_id] != value: da_item = DialogueActItem(da_type, slot, values[row_id]) break da_items.append(da_item) da_items_probs = self._sample_nbest_list_probs(confusion_des, confusion_type, length) return da_items, da_items_probs def _sample_nbest_list_probs(self, confusion_des, confusion_type, length): """Sample probabilities for n-bestlist hypotehsis.""" sample_probs = sample_dirichlet_from_dict(confusion_des["probability_generator"][confusion_type]) probs = [sample_probs["correct"]] if length == 1: pass else: if length == 2: onlist_fractions = [1.0] else: onlist_fractions = self._get_onlist_fractions(confusion_des, length) for i in range(1, length): probs.append(sample_probs["onlist"] * onlist_fractions[i - 1]) return probs def _get_onlist_fractions(self, confusion_des, length): frac = [] prev = 0.0 step_size = 1.0 / (length - 1.0) alpha = confusion_des["onlist_fraction_alpha"] beta = confusion_des["onlist_fraction_beta"] for i in range(length - 1): x = step_size * (i + 1) b = lbetai(alpha, beta, x) frac.append(b - prev) prev = b return frac # -----------------------------General function for processing domain def _get_slot_values(self, slot): """Get a all distinct values for the given slot.""" values = set() tables_fields = self._get_slot_mapping(slot) for tbf in tables_fields: # sample a value of each table which the slot is connected if iscallable(tbf): # slot have values generated dynamic from one or several funs values.append(tbf(self.goal, slot)) values.append(v) else: tb, f = tbf values = values.union(self.db.get_field_values(tb, f)) return list(values) # -----------------------------General function for domain, which is currently copy from user simulator def _get_slot_mapping(self, slot): """Return a full set of table field mapping for the given slot.""" assert ( slot in self.domain["slot_table_field_mapping"].keys() ), "Have not defined the slot_table_field_mapping for the slot=%s" % (slot) return self.domain["slot_table_field_mapping"][slot]
def test_merge_slu_nblists_full_nbest_lists(self): # make sure the alex.components.slu.da.merge_slu_nblists merges nblists correctly nblist1 = DialogueActNBList() nblist1.add(0.7, DialogueAct("hello()")) nblist1.add(0.2, DialogueAct("bye()")) nblist1.merge().normalise() # nblist1.normalise() nblist2 = DialogueActNBList() nblist2.add(0.6, DialogueAct("hello()")) nblist2.add(0.3, DialogueAct("restart()")) nblist2.merge().normalise() # nblist2.normalise() nblists = [[0.7, nblist1], [0.3, nblist2]] merged_nblists = merge_slu_nblists(nblists) correct_merged_nblists = DialogueActNBList() correct_merged_nblists.add(0.7 * 0.7, DialogueAct("hello()")) correct_merged_nblists.add(0.7 * 0.2, DialogueAct("bye()")) correct_merged_nblists.add(0.7 * 0.1, DialogueAct("other()")) correct_merged_nblists.add(0.3 * 0.6, DialogueAct("hello()")) correct_merged_nblists.add(0.3 * 0.3, DialogueAct("restart()")) correct_merged_nblists.add(0.3 * 0.1, DialogueAct("other()")) correct_merged_nblists.merge().normalise() # correct_merged_nblists.normalise() s = [] s.append("") s.append("Merged nblists:") s.append(unicode(merged_nblists)) s.append("") s.append("Correct merged results:") s.append(unicode(correct_merged_nblists)) s.append("") self.assertEqual(unicode(merged_nblists), unicode(correct_merged_nblists))
def test_merge_slu_nblists_full_nbest_lists(self): # make sure the alex.components.slu.da.merge_slu_nblists merges nblists correctly nblist1 = DialogueActNBList() nblist1.add(0.7, DialogueAct("hello()")) nblist1.add(0.2, DialogueAct("bye()")) nblist1.merge().normalise() # nblist1.normalise() nblist2 = DialogueActNBList() nblist2.add(0.6, DialogueAct("hello()")) nblist2.add(0.3, DialogueAct("restart()")) nblist2.merge().normalise() # nblist2.normalise() nblists = [[0.7, nblist1], [0.3, nblist2]] merged_nblists = merge_slu_nblists(nblists) correct_merged_nblists = DialogueActNBList() correct_merged_nblists.add(0.7 * 0.7, DialogueAct("hello()")) correct_merged_nblists.add(0.7 * 0.2, DialogueAct("bye()")) correct_merged_nblists.add(0.7 * 0.1, DialogueAct("other()")) correct_merged_nblists.add(0.3 * 0.6, DialogueAct("hello()")) correct_merged_nblists.add(0.3 * 0.3, DialogueAct("restart()")) correct_merged_nblists.add(0.3 * 0.1, DialogueAct("other()")) correct_merged_nblists.merge().normalise() # correct_merged_nblists.normalise() s = [] s.append("") s.append("Merged nblists:") s.append(unicode(merged_nblists)) s.append("") s.append("Correct merged results:") s.append(unicode(correct_merged_nblists)) s.append("") print '\n'.join(s) self.assertEqual(unicode(merged_nblists), unicode(correct_merged_nblists))