Esempio n. 1
0
    def answer_confirm(self, state, res0, slots_to_confirm):
        conflicts = []
        selects = []
        noclues = []
        for slot, slot_value in slots_to_confirm.items():
            state.user_state_confirm[slot] = []

            if not slot in res0:
                noclues += [slot]
            elif not res0[slot].lower() in slot_value:
                conflicts += [slot]
                if len(slot_value) > 1:
                    selects += [(slot, res0[slot], )]

        if len(noclues) > 0:
            return DialogueAct(
                "&".join(["noclue(%s)" % n for n in noclues]))
        elif len(conflicts) == 0:
            if len(selects) == 0:
                return DialogueAct("affirm()")
            else:
                return DialogueAct(
                    "&".join(["confirm(%s=%s)" % (n, v, ) \
                              for n, v in selects]))
        else:
            conf_acts = []
            for conflict in conflicts:
                conf_acts += ["inform(%s='%s')" % (conflict, res0[conflict], )]
            res = DialogueAct("negate()")
            # XXX I understood sorting was not desired.  Substituted with
            # the non-sorting version.
            # res.merge(DialogueAct("&".join(conf_acts)))
            res.extend(conf_acts)
            return res
Esempio n. 2
0
 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)
Esempio n. 3
0
    def _build_da_nbest_list(self, i, da, prob):
        '''Build all combination for the DialogueActItem and probs saved in self._sampled_da_item.

        Currently, not being used.
        '''
        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)
Esempio n. 4
0
    def compose_utterance_greedy(self, da):
        """\
        Compose an utterance from templates by iteratively looking for
        the longest (up to self.compose_greedy_lookahead) matching
        sub-utterance at the current position in the DA.

        Returns the composed utterance.
        """
        composed_utt = []
        sub_start = 0
        # pass through the dialogue act
        while sub_start < len(da):
            dax_utt = None
            dax_len = None
            # greedily look for the longest template that will cover the next
            # dialogue act items (try longer templates first, from maximum
            # length given in settings down to 1).
            for sub_len in xrange(self.compose_greedy_lookahead, 0, -1):
                dax = DialogueAct()
                dax.extend(da[sub_start:sub_start + sub_len])
                try:
                    # try to find an exact match
                    dax_utt = self.random_select(self.templates[unicode(dax)])
                    dax_len = sub_len
                    break
                except KeyError:
                    # try to find a relaxed match
                    svsx = dax.get_slots_and_values()
                    try:
                        dax_utt = self.match_and_fill_generic(dax, svsx)
                        dax_len = sub_len
                        break
                    except TemplateNLGException:
                        # nothing found: look for shorter templates
                        continue
            if dax_utt is None:  # dummy backoff
                dax_utt = unicode(da[sub_start])
                dax_len = 1
            composed_utt.append(dax_utt)
            sub_start += dax_len
        return ' '.join(composed_utt)
Esempio n. 5
0
    def compose_utterance_greedy(self, da):
        """\
        Compose an utterance from templates by iteratively looking for
        the longest (up to self.compose_greedy_lookahead) matching
        sub-utterance at the current position in the DA.

        Returns the composed utterance.
        """
        composed_utt = []
        sub_start = 0
        # pass through the dialogue act
        while sub_start < len(da):
            dax_utt = None
            dax_len = None
            # greedily look for the longest template that will cover the next
            # dialogue act items (try longer templates first, from maximum
            # length given in settings down to 1).
            for sub_len in xrange(self.compose_greedy_lookahead, 0, -1):
                dax = DialogueAct()
                dax.extend(da[sub_start:sub_start + sub_len])
                try:
                    # try to find an exact match
                    dax_utt = self.random_select(self.templates[unicode(dax)])
                    dax_len = sub_len
                    break
                except KeyError:
                    # try to find a relaxed match
                    svsx = dax.get_slots_and_values()
                    try:
                        dax_utt = self.match_and_fill_generic(dax, svsx)
                        dax_len = sub_len
                        break
                    except TemplateNLGException:
                        # nothing found: look for shorter templates
                        continue
            if dax_utt is None:  # dummy backoff
                dax_utt = unicode(da[sub_start])
                dax_len = 1
            composed_utt.append(dax_utt)
            sub_start += dax_len
        return ' '.join(composed_utt)
Esempio n. 6
0
def generate_task():

    task = []
    da = DialogueAct()

    # indicate that we're looking for connection
    da.append(DialogueActItem('inform', 'task', 'find_connection'))

    # get two distinct stops
    from_stop = random.choice(STOPS)
    to_stop = from_stop
    while to_stop == from_stop:
        to_stop = random.choice(STOPS)
    da.append(DialogueActItem('inform', 'from_stop', from_stop))
    da.append(DialogueActItem('inform', 'to_stop', to_stop))
    task.append(da)

    # generate random subsequent questions
    questions = random.sample(range(6), random.randint(5, 6) - len(task))

    query_change = False
    da = DialogueAct()
    for question in sorted(questions):
        dais = QUESTIONS[question]

        if dais[0].name in ['alternative', 'vehicle', 'time', 'to_stop'
                            ] and not query_change:
            query_change = True
            task.append(da)
            da = DialogueAct()

        if dais[0].name == 'to_stop':
            new_to_stop = random.choice(STOPS)
            while new_to_stop == from_stop or new_to_stop == to_stop:
                new_to_stop = random.choice(STOPS)
            dais[0].value = new_to_stop

        da.extend(dais)

    task.append(da)
    return task
Esempio n. 7
0
    def answer_confirm(self, state, res0, slots_to_confirm):
        conflicts = []
        selects = []
        noclues = []
        for slot, slot_value in slots_to_confirm.items():
            state.user_state_confirm[slot] = []

            if not slot in res0:
                noclues += [slot]
            elif not res0[slot].lower() in slot_value:
                conflicts += [slot]
                if len(slot_value) > 1:
                    selects += [(
                        slot,
                        res0[slot],
                    )]

        if len(noclues) > 0:
            return DialogueAct("&".join(["noclue(%s)" % n for n in noclues]))
        elif len(conflicts) == 0:
            if len(selects) == 0:
                return DialogueAct("affirm()")
            else:
                return DialogueAct(
                    "&".join(["confirm(%s=%s)" % (n, v, ) \
                              for n, v in selects]))
        else:
            conf_acts = []
            for conflict in conflicts:
                conf_acts += [
                    "inform(%s='%s')" % (
                        conflict,
                        res0[conflict],
                    )
                ]
            res = DialogueAct("negate()")
            # XXX I understood sorting was not desired.  Substituted with
            # the non-sorting version.
            # res.merge(DialogueAct("&".join(conf_acts)))
            res.extend(conf_acts)
            return res
Esempio n. 8
0
def generate_task():

    task = []
    da = DialogueAct()

    # indicate that we're looking for connection
    da.append(DialogueActItem('inform', 'task', 'find_connection'))

    # get two distinct stops
    from_stop = random.choice(STOPS)
    to_stop = from_stop
    while to_stop == from_stop:
        to_stop = random.choice(STOPS)
    da.append(DialogueActItem('inform', 'from_stop', from_stop))
    da.append(DialogueActItem('inform', 'to_stop', to_stop))
    task.append(da)

    # generate random subsequent questions
    questions = random.sample(range(6), random.randint(5, 6) - len(task))

    query_change = False
    da = DialogueAct()
    for question in sorted(questions):
        dais = QUESTIONS[question]

        if dais[0].name in ['alternative', 'vehicle', 'time', 'to_stop'] and not query_change:
            query_change = True
            task.append(da)
            da = DialogueAct()

        if dais[0].name == 'to_stop':
            new_to_stop = random.choice(STOPS)
            while new_to_stop == from_stop or new_to_stop == to_stop:
                new_to_stop = random.choice(STOPS)
            dais[0].value = new_to_stop

        da.extend(dais)

    task.append(da)
    return task