Exemple #1
0
 def add_const(self, slot, value, negate=False):
     """
     """
     if not negate:
         op = '='
     else:
         op = '!='
     item = dact.DactItem(slot, op, value)
     self.constraints.append(item)
Exemple #2
0
 def contains(self, slot, value, negate=False):
     '''
     :param slot: None
     :type slot: str
     :param value: None
     :type value: str
     :param negate: None
     :type negate: bool - default False
     :returns: (bool) is full semantic act in self.items?
     '''
     op = '='
     if negate:
         op = '!='
     item = dact.DactItem(slot, op, value)
     return item in self.items
Exemple #3
0
    def append_front(self, slot, value, negate=False):
        '''
        Add item to this act avoiding duplication

        :param slot: None
        :type slot: str
        :param value: None
        :type value: str
        :param negate: operation is '=' or not?  False by default.
        :type negate: bool
        :return:
        '''
        op = '='
        if negate:
            op = '!='
        self.items = [dact.DactItem(slot, op, value)] + self.items
Exemple #4
0
    def append(self, slot, value, negate=False):
        '''
        Add item to this act avoiding duplication

        :param slot: None
        :type slot: str
        :param value: None
        :type value: str
        :param negate: semantic operation is negation or not
        :type negate: bool [Default=False]
        :return:
        '''
        op = '='
        if negate:
            op = '!='
        self.items.append(dact.DactItem(slot, op, value))
Exemple #5
0
    def transform(self, sysAct):
        '''
        Transforms the sysAct from a semantic utterance form to a text form using the rules in the generator.
        This function will run the sysAct through all variable rules and will choose the best one according to the
        number of matched act types, matched items and missing items.

        :param sysAct: input system action (semantic form).
        :type sysAct: str
        :returns: (str) natural language 
        '''
        input_utt = DiaAct.DiaAct(sysAct)

        # FIXME hack to transform system acts with slot op "!=" to "=" and add slot-value pair other=true which is needed by NLG rule base
        # assumption: "!=" only appears if there are no further alternatives, ie, inform(name=none, name!=place!, ...)
        negFound = False
        for item in input_utt.items:
            if item.op == "!=":
                item.op = u"="
                negFound = True
        if negFound:
            otherTrue = dact.DactItem(u'other', u'=', u'true')
            input_utt.items.append(otherTrue)

        # Iterate over BasicTemplateRule rules.
        best_rule = None
        best = None
        best_matches = 0
        best_type_match = 0
        best_missing = 1000
        best_non_term_map = None
        for rule in self.rules:
            logger.debug('Checking Rule %s' % str(rule))
            out, matches, missing, type_match, non_term_map = rule.generate(
                input_utt)
            if type_match > 0:
                logger.debug(
                    'Checking Rule %s: type_match=%d, missing=%d, matches=%d, output=%s'
                    % (str(rule), type_match, missing, matches, ' '.join(out)))

            # Pick up the best rule.
            choose_this = False
            if type_match > 0:
                if missing < best_missing:
                    choose_this = True
                elif missing == best_missing:
                    if type_match > best_type_match:
                        choose_this = True
                    elif type_match == best_type_match and matches > best_matches:
                        choose_this = True

            if choose_this:
                best_rule = rule
                best = out
                best_missing = missing
                best_type_match = type_match
                best_matches = matches
                best_non_term_map = non_term_map

                if best_type_match == 1 and best_missing == 0 and best_matches == len(
                        input_utt.items):
                    break

        if best_rule is not None:
            if best_missing > 0:
                logger.warning(
                    'While transforming %s, there were missing items.' %
                    sysAct)
        else:
            logger.debug('No rule used.')

        best = self.compute_ftn(best, best_non_term_map)
        return ' '.join(best)