Ejemplo n.º 1
0
    def process_sentence(self, client_context, sentence, srai, responselogger):

        assert (client_context is not None)
        assert (client_context.brain is not None)

        try:
            client_context.check_max_recursion()
            client_context.check_max_timeout()
        except LimitOverException:
            raise

        if srai is False:
            self.check_spelling_before(client_context, sentence)
            self._srai_count = 0
        else:
            self._srai_count += 1
            if self._srai_count > client_context.bot.configuration.max_search_srai:
                raise LimitOverException(
                    "Max search srai [%d] exceeded: [%s]" %
                    (client_context.bot.configuration.max_search_srai,
                     client_context.brain.tokenizer.words_to_texts(
                         sentence.words)))

        response = client_context.brain.ask_question(client_context, sentence,
                                                     srai)

        if response is None and srai is False:
            response = self.check_spelling_and_retry(client_context, sentence)

        if response is not None:
            return self.handle_response(client_context, sentence, response,
                                        srai, responselogger)
        else:
            return self.handle_none_response(client_context, sentence,
                                             responselogger)
Ejemplo n.º 2
0
    def _create_new_category(self, client_context, category, userid="*"):
        graph = client_context.brain.aiml_parser.pattern_parser

        new_pattern = self.resolve_element_evals(client_context, category.pattern)
        new_topic = self.resolve_element_evals(client_context, category.topic)
        new_that = self.resolve_element_evals(client_context, category.that)

        is_replace = graph.check_learn_pattern_in_graph(new_pattern, new_topic, new_that, userid=client_context.userid)

        conversation = client_context.bot.get_conversation(client_context)
        if (conversation.num_categories + conversation.user_categories) >= client_context.bot.configuration.max_categories:
            if is_replace is False:
                raise LimitOverException("Max categories [%d] exceeded: [%s]" % (client_context.bot.configuration.max_categories, new_pattern.text))

        new_template = self.evaluate_eval_nodes(client_context, category.template)

        graph.add_pattern_to_graph(new_pattern, new_topic, new_that, new_template, learn=True, userid=client_context.userid)

        YLogger.debug(client_context, "[%s] resolved to new pattern [[%s] [%s] [%s]", self.to_string(),
                      ET.tostring(new_pattern, 'utf-8').decode('utf-8'),
                      ET.tostring(new_topic, 'utf-8').decode('utf-8'),
                      ET.tostring(new_that, 'utf-8').decode('utf-8'))

        if is_replace is False:
            conversation.user_categories += 1

        return LearnCategory(new_pattern, new_topic, new_that, new_template), is_replace
Ejemplo n.º 3
0
 def check_max_timeout(self):
     if self.bot.configuration.max_question_timeout != -1:
         if self.total_search_time(
         ) >= self.bot.configuration.max_question_timeout:
             raise LimitOverException(
                 "Maximum question time limit [%d]secs exceeded" %
                 self.bot.configuration.max_question_timeout)
Ejemplo n.º 4
0
    def _check_properties_count(self):
        used_count = len(self._properties) + len(self._data_properties)

        except_properties = ['positivity', 'subjectivity']
        for key in self._properties.keys():
            if key in except_properties:
                used_count -= 1

        if used_count >= self._max_properties:
            raise LimitOverException("Max properties count [%d] exceeded" %
                                     self._max_properties)
Ejemplo n.º 5
0
    def _resolve_sub(self, client_context):
        self._loop_count += 1
        if self._loop_count > self._max_search_condition:
            raise LimitOverException(
                "Max search condition [%d] exceeded: %s" %
                (self._max_search_condition, self.to_string()))

        if self._condition_type == TemplateConditionNode.BLOCK:
            return self.resolve_type1_condition(client_context)
        elif self._condition_type == TemplateConditionNode.SINGLE:
            return self.resolve_type2_condition(client_context)
        elif self._condition_type == TemplateConditionNode.MULTIPLE:
            return self.resolve_type3_condition(client_context)
        return None
Ejemplo n.º 6
0
    def consume(self, client_context, context, words, word_no, match_type, depth):

        tabs = self.get_tabs(client_context, depth)

        if context.search_depth_exceeded(depth) is True:
            if context.is_error is False:
                context.is_error = True
                raise LimitOverException("%sMax search depth [%d] exceeded (topic)" % (tabs, context.max_search_depth))
            return None

        if words.word(word_no) == PatternTopicNode.TOPIC:
            YLogger.debug(client_context, "%sTopic matched %s", tabs, words.word(word_no))
            return super(PatternTopicNode, self).consume(client_context, context, words, word_no+1, match_type, depth+1)

        YLogger.debug(client_context, "%sTopic NOT matched %s", tabs, words.word(word_no))
        return None
Ejemplo n.º 7
0
 def check_max_recursion(self):
     if self.bot.configuration.max_question_recursion != -1:
         if self._question_depth > self.bot.configuration.max_question_recursion:
             raise LimitOverException("Maximum recursion limit [%d] exceeded" % self.bot.configuration.max_question_recursion)
Ejemplo n.º 8
0
    def consume(self, client_context, context, words, word_no, match_type, depth):

        tabs = self.get_tabs(client_context, depth)

        if context.search_time_exceeded() is True:
            if context.is_error is False:
                context.is_error = True
                raise LimitOverException("%sMax search time [%d]secs exceeded" % (tabs, context.max_search_timeout))
            return None

        if context.search_depth_exceeded(depth) is True:
            if context.is_error is False:
                context.is_error = True
                raise LimitOverException("%sMax search depth [%d] exceeded" % (tabs, context.max_search_depth))
            return None

        if word_no >= words.num_words():
            if self._0ormore_hash is not None:
                child_template = self._0ormore_hash.template
            elif self._0ormore_arrow is not None:
                child_template = self._0ormore_arrow.template
            else:
                child_template = self._template

            if child_template is not None:
                YLogger.debug(client_context, "%sFound a template, success!", tabs)
                return child_template
            else:
                YLogger.debug(client_context, "%sNo more words and no template, no match found!", tabs)
                return None

        if self._topic is not None:
            match = self._topic.consume(client_context, context, words, word_no, Match.TOPIC, depth+1)
            if match is not None:
                YLogger.debug(client_context, "%sMatched topic, success!", tabs)
                return match
            if words.word(word_no) == PatternNode.TOPIC:
                YLogger.debug(client_context, "%s Looking for a %s, none give, no match found!", tabs, PatternNode.TOPIC)
                return None

        if self._that is not None:
            match = self._that.consume(client_context, context, words, word_no, Match.THAT, depth+1)
            if match is not None:
                YLogger.debug(client_context, "%sMatched that, success!", tabs)
                return match
            if words.word(word_no) == PatternNode.THAT:
                YLogger.debug(client_context, "%s Looking for a %s, none give, no match found!", tabs, PatternNode.THAT)
                return None

        match, word_no = self.match_children(client_context, self._priority_words, "Priority", words, word_no, context, match_type, depth)
        if match is not None:
            return match

        if self._0ormore_hash is not None:
            match = self._0ormore_hash.consume(client_context, context, words, word_no, match_type, depth+1)
            if match is not None:
                YLogger.debug(client_context, "%sMatched 0 or more hash, success!", tabs)
                return match

        if self._1ormore_underline is not None:
            match = self._1ormore_underline.consume(client_context, context, words, word_no, match_type, depth+1)
            if match is not None:
                YLogger.debug(client_context, "%sMatched 1 or more underline, success!", tabs)
                return match

        match, word_no = self.match_children(client_context, self._children, "Word", words, word_no, context, match_type, depth)
        if match is not None:
            return match

        if self._0ormore_arrow is not None:
            match = self._0ormore_arrow.consume(client_context, context, words, word_no, match_type, depth+1)
            if match is not None:
                YLogger.debug(client_context, "%sMatched 0 or more arrow, success!", tabs)
                return match

        if self._1ormore_star is not None:
            match = self._1ormore_star.consume(client_context, context, words, word_no, match_type, depth+1)
            if match is not None:
                YLogger.debug(client_context, "%sMatched 1 or more star, success!", tabs)
                return match

        YLogger.debug(client_context, "%sNo match for %s, trying another path", tabs, words.word(word_no))
        return None
Ejemplo n.º 9
0
    def consume(self, client_context, context, words, word_no, match_type, depth, parent_wildcard=False):

        tabs = self.get_tabs(client_context, depth)

        if context.search_time_exceeded() is True:
            if context.is_error is False:
                context.is_error = True
                raise LimitOverException("%sMax search time [%d]secs exceeded (zeroormore)" % (tabs, context.max_search_timeout))
            return None

        if context.search_depth_exceeded(depth) is True:
            if context.is_error is False:
                context.is_error = True
                raise LimitOverException("%sMax search depth [%d] exceeded (zeroormore)" % (tabs, context.max_search_depth))
            return None

        context_match = Match(match_type, self, None)
        context.add_match(context_match)
        matches_added = 1

        word = words.word(word_no)

        if self._template is not None:
            YLogger.debug(client_context, "%sFound a template, success!", tabs)
            context_match.add_word(word)
            return self._template

        match = self.check_child_is_wildcard(tabs, client_context, context, words, word_no, match_type, depth)
        if match is not None:
            context_match.add_word(word)
            return match

        if self._topic is not None:
            match = self._topic.consume(client_context, context, words, word_no, Match.TOPIC, depth+1)
            if match is not None:
                YLogger.debug(client_context, "%sMatched topic, success!", tabs)
                return match
            if words.word(word_no) == PatternNode.TOPIC:
                YLogger.debug(client_context, "%s Looking for a %s, none give, no match found!", tabs, PatternNode.TOPIC)
                return None

        if self._children:
            for child in self._children:

                result = child.equals(client_context, words, word_no)
                if result.matched is True:
                    word_no = result.word_no
                    YLogger.debug(client_context, "%sWildcard child matched %s", tabs, result.matched_phrase)

                    context_match2 = Match(Match.WORD, child, result.matched_phrase)

                    context.add_match(context_match2)
                    matches_added += 1

                    match = child.consume(client_context, context, words, word_no+1, match_type, depth+1)
                    if match is not None:
                        return match

                    if self.invalid_topic_or_that(tabs, client_context, word, context, matches_added) is True:
                        return None

            YLogger.debug(client_context, "%sWildcard %s matched %s", tabs, self._wildcard, word)
            context_match.add_word(word)

            match = super(PatternZeroOrMoreWildCardNode, self).consume(client_context, context, words, word_no + 1, match_type, depth+1)
            if match is not None:
                return match

            word_no += 1
            word = words.word(word_no)

            if self.invalid_topic_or_that(tabs, client_context, word, context, matches_added) is True:
                return None

            YLogger.debug(client_context, "%sWildcard %s matched %s", tabs, self._wildcard, word)
            context_match.add_word(word)

            match = super(PatternZeroOrMoreWildCardNode, self).consume(client_context, context, words, word_no + 1, match_type, depth+1)
            if match is not None:
                return match

            word_no += 1
            if word_no >= words.num_words():
                context.pop_matches(matches_added)
                return None
            word = words.word(word_no)

        YLogger.debug(client_context, "%sNo children, consume words until next break point", tabs)
        while word_no < words.num_words() - 1:
            match = super(PatternZeroOrMoreWildCardNode, self).consume(client_context, context, words, word_no, match_type, depth+1)
            if match is not None:
                return match

            if self.invalid_topic_or_that(tabs, client_context, word, context, matches_added) is True:
                return None

            YLogger.debug(client_context, "%sWildcard %s matched %s", tabs, self._wildcard, word)
            context_match.add_word(word)

            word_no += 1
            word = words.word(word_no)

        if parent_wildcard is True:
            match = super(PatternZeroOrMoreWildCardNode, self).consume(client_context, context, words, word_no+1, match_type,
                                                                       depth + 1)
        else:
            match = super(PatternZeroOrMoreWildCardNode, self).consume(client_context, context, words, word_no, match_type,
                                                                       depth + 1)
        if match is not None:
            return match
        context.pop_matches(matches_added)
        return None