Esempio n. 1
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. 2
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. 3
0
    def compose_utterance_single(self, da):
        """\
        Compose an utterance from templates for single dialogue act items.
        Returns the composed utterance.
        """
        composed_utt = []
        # try to find a template for each single dialogue act item
        for dai in da:
            try:
                # look for an exact match
                dai_utt = self.random_select(self.templates[unicode(dai)])
            except KeyError:
                # try to find a relaxed match
                dax = DialogueAct()
                dax.append(dai)
                svsx = dax.get_slots_and_values()
                try:
                    dai_utt = self.match_and_fill_generic(dax, svsx)
                except TemplateNLGException:
                    dai_utt = unicode(dai)

            composed_utt.append(dai_utt)
        return ' '.join(composed_utt)
Esempio n. 4
0
    def compose_utterance_single(self, da):
        """\
        Compose an utterance from templates for single dialogue act items.
        Returns the composed utterance.
        """
        composed_utt = []
        # try to find a template for each single dialogue act item
        for dai in da:
            try:
                # look for an exact match
                dai_utt = self.random_select(self.templates[unicode(dai)])
            except KeyError:
                # try to find a relaxed match
                dax = DialogueAct()
                dax.append(dai)
                svsx = dax.get_slots_and_values()
                try:
                    dai_utt = self.match_and_fill_generic(dax, svsx)
                except TemplateNLGException:
                    dai_utt = unicode(dai)

            composed_utt.append(dai_utt)
        return ' '.join(composed_utt)