Beispiel #1
0
    def test_get_tabs(self):
        tabs = TextUtils.get_tabs(0)
        self.assertEqual(tabs, "")

        tabs = TextUtils.get_tabs(1, tabs="\t")
        self.assertEqual(tabs, "\t")

        tabs = TextUtils.get_tabs(5, tabs="\t")
        self.assertEqual(tabs, "\t\t\t\t\t")
Beispiel #2
0
    def test_get_tabs(self):
        tabs = TextUtils.get_tabs(0)
        self.assertEqual(tabs, "")

        tabs = TextUtils.get_tabs(1, tabs="\t")
        self.assertEqual(tabs, "\t")

        tabs = TextUtils.get_tabs(5, tabs="\t")
        self.assertEqual(tabs, "\t\t\t\t\t")
Beispiel #3
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        if bot.configuration.tab_parse_output is True:
            tabs = TextUtils.get_tabs(depth)
        else:
            tabs = ""

        if context.search_depth_exceeded(depth) is True:
            #if depth > context.max_search_depth:
            if logging.getLogger().isEnabledFor(logging.ERROR):
                logging.error("%sMax search depth [%d]exceeded" %
                              (tabs, context.max_search_depth))
            return None

        if words.word(word_no) == PatternTopicNode.TOPIC:
            if logging.getLogger().isEnabledFor(logging.DEBUG):
                logging.debug("%sTopic matched %s" %
                              (tabs, words.word(word_no)))
            return super(PatternTopicNode,
                         self).consume(bot, clientid, context, words,
                                       word_no + 1, type, depth + 1)

        if logging.getLogger().isEnabledFor(logging.DEBUG):
            logging.debug("%sTopic NOT matched %s" %
                          (tabs, words.word(word_no)))
        return None
Beispiel #4
0
    def match_children(self, bot, clientid, children, child_type, words,
                       word_no, context, type, depth):

        if bot.configuration.tab_parse_output is True:
            tabs = TextUtils.get_tabs(depth)
        else:
            tabs = ""

        for child in children:

            result = child.equals(bot, clientid, words, word_no)
            if result.matched is True:
                word_no = result.word_no
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%s%s matched %s" %
                                  (tabs, child_type, result.matched_phrase))

                match_node = Match(type, child, result.matched_phrase)

                context.add_match(match_node)

                match = child.consume(bot, clientid, context, words,
                                      word_no + 1, type, depth + 1)
                if match is not None:
                    if logging.getLogger().isEnabledFor(logging.DEBUG):
                        logging.debug("%sMatched %s child, success!" %
                                      (tabs, child_type))
                    return match, word_no
                else:
                    context.pop_match()

        return None, word_no
Beispiel #5
0
 def consume_set_phrase(self, bot, clientid, context, child, match_node,
                        words, word_no):
     if child.is_set():
         tabs = TextUtils.get_tabs(word_no)
         phrase = words.word(word_no)
         full_phrase = phrase if child.is_full_phrase(
             bot, clientid, phrase) else None
         logging.debug(
             "%sChild %s is a set [%s], trying to consume more words..." %
             (tabs, child._word, child.set_name))
         phrase_words = [phrase]
         for cur_word_no in range(word_no + 1, words.num_words()):
             tabs = TextUtils.get_tabs(cur_word_no)
             cur_word = words.word(cur_word_no)
             phrase_words.append(cur_word)
             phrase = tuple(phrase_words)
             if child.equals(bot, clientid, phrase):
                 if child.is_full_phrase(bot, clientid, phrase):
                     logging.debug("%sSet %s consumed %s" %
                                   (tabs, child.set_name, cur_word))
                     logging.debug("%s*MATCH -> %s" % (tabs, cur_word))
                     match_node.add_word(cur_word)
                     full_phrase = phrase
                 else:
                     if full_phrase:
                         logging.debug("%sSet %s matched phrase %s" %
                                       (tabs, child.set_name, full_phrase))
                         word_no = cur_word_no - 1
                         break
             else:
                 break
         if not child.is_full_phrase(bot, clientid, full_phrase):
             logging.debug(
                 "%sSet %s did not find a full phrase match, continuing to the next child..."
                 % (tabs, child.set_name))
             context.pop_match()
             return None
     return word_no
Beispiel #6
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        tabs = TextUtils.get_tabs(word_no)

        if depth > context.max_search_depth:
            logging.error("%sMax search depth [%d]exceeded" % (tabs, context.max_search_depth))
            return None

        if words.word(word_no) == PatternTopicNode.TOPIC:
            logging.debug("%sTopic matched %s" % (tabs, words.word(word_no)))
            return super(PatternTopicNode, self).consume(bot, clientid, context, words, word_no+1, type, depth+1)

        logging.debug("%sTopic NOT matched %s" % (tabs, words.word(word_no)))
        return None
Beispiel #7
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        tabs = TextUtils.get_tabs(word_no)

        if depth > context.max_search_depth:
            logging.error("%sMax search depth [%d]exceeded" % (tabs, context.max_search_depth))
            return None

        if words.word(word_no) == PatternThatNode.THAT:
            logging.debug("%sThat matched %s" % (tabs, words.word(word_no)))
            return super(PatternThatNode, self).consume(bot, clientid, context, words, word_no + 1, type, depth+1)

        logging.debug("%sTHAT NOT matched %s" % (tabs, words.word(word_no)))
        return None
Beispiel #8
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        tabs = TextUtils.get_tabs(word_no)

        if depth > context.max_search_depth:
            logging.error("%sMax search depth [%d]exceeded" %
                          (tabs, context.max_search_depth))
            return None

        if word_no >= words.num_words():
            logging.debug(
                "%sword_no %d exceeded words size %d when matching Topic",
                tabs, word_no, words.num_words())
            return None

        if words.word(word_no) == PatternTopicNode.TOPIC:
            logging.debug("%sTopic matched %s" % (tabs, words.word(word_no)))
            return super(PatternTopicNode,
                         self).consume(bot, clientid, context, words,
                                       word_no + 1, type, depth + 1)

        logging.debug("%sTopic NOT matched %s" % (tabs, words.word(word_no)))
        return None
Beispiel #9
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        tabs = TextUtils.get_tabs(word_no)

        if depth > context.max_search_depth:
            logging.error("%sMax search depth [%d]exceeded" % (tabs, context.max_search_depth))
            return None

        if word_no >= words.num_words():
            if self._template is not None:
                logging.debug("%sFound a template, success!" % (tabs))
                return self._template
            else:
                logging.debug("%sNo more words and no template, no match found!" % (tabs))
                #context.pop_match()
                return None

        if self._topic is not None:
            match = self._topic.consume(bot, clientid, context, words, word_no, Match.TOPIC, depth+1)
            if match is not None:
                return match
            if words.word(word_no) == PatternNode.TOPIC:
                logging.debug("%s Looking for a %s, none give, no match found!" % (tabs, PatternNode.TOPIC))
                #context.pop_match()
                return None

        if self._that is not None:
            match = self._that.consume(bot, clientid, context, words, word_no, Match.THAT, depth+1)
            if match is not None:
                return match
            if words.word(word_no) == PatternNode.THAT:
                logging.debug("%s Looking for a %s, none give, no match found!" % (tabs, PatternNode.THAT))
                #context.pop_match()
                return None

        for child in self._priority_words:
            if child.equals(bot, clientid, words.word(word_no)):
                logging.debug("%sPriority %s matched %s" % (tabs, child._word, words.word(word_no)))

                logging.debug("%sMATCH -> %s" % (tabs, words.word(word_no)))
                match_node = Match(type, child, words.word(word_no))
                context.add_match(match_node)

                match = child.consume(bot, clientid, context, words, word_no + 1, type, depth+1)
                if match is not None:
                    return match
                else:
                    context.pop_match ()

        if self._0ormore_hash is not None:
            match = self._0ormore_hash.consume(bot, clientid, context, words, word_no, type, depth+1)
            if match is not None:
                return match
            #else:
            #    context.pop_match ()

        if self._1ormore_underline is not None:
            match = self._1ormore_underline.consume(bot, clientid, context, words, word_no, type, depth+1)
            if match is not None:
                return match
            #else:
            #    context.pop_match ()

        for child in self._children:
            if child.equals(bot, clientid, words.word(word_no)):
                logging.debug("%sChild %s matched %s" % (tabs, child._word, words.word(word_no)))

                logging.debug("%sMATCH -> %s" % (tabs, words.word(word_no)))
                match_node = Match(type, child, words.word(word_no))
                context.add_match(match_node)

                match = child.consume(bot, clientid, context, words, word_no + 1, type, depth+1)
                if match is not None:
                    return match
                else:
                    context.pop_match ()

        if self._0ormore_arrow is not None:
            match = self._0ormore_arrow.consume(bot, clientid, context, words, word_no, type, depth+1)
            if match is not None:
                return match
            #else:
            #    context.pop_match ()

        if self._1ormore_star is not None:
            match = self._1ormore_star.consume(bot, clientid, context, words, word_no, type, depth+1)
            if match is not None:
                return match
            #else:
            #    context.pop_match ()

        logging.debug("%sNo match for %s, trying another path" % (tabs, words.word(word_no)))
        return None
Beispiel #10
0
 def get_tabs(self, client_context, depth):
     if client_context.bot.configuration.tab_parse_output is True:
         return TextUtils.get_tabs(depth)
     return ""
Beispiel #11
0
 def get_tabs(self, bot, depth):
     if bot.configuration.tab_parse_output is True:
         return TextUtils.get_tabs(depth)
     return ""
Beispiel #12
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        if bot.configuration.tab_parse_output is True:
            tabs = TextUtils.get_tabs(depth)
        else:
            tabs = ""

        if context.search_time_exceeded() is True:
            if logging.getLogger().isEnabledFor(logging.ERROR):
                logging.error("%sMax search time [%d]secs exceeded" %
                              (tabs, context.max_search_timeout))
            return None

        if context.search_depth_exceeded(depth) is True:
            if logging.getLogger().isEnabledFor(logging.ERROR):
                logging.error("%sMax search depth [%d] exceeded" %
                              (tabs, context.max_search_depth))
            return None

        if word_no >= words.num_words():
            return None

        word = words.word(word_no)
        if logging.getLogger().isEnabledFor(logging.DEBUG):
            logging.debug("%sWildcard %s matched %s" %
                          (tabs, self._wildcard, word))
        context_match = Match(type, self, word)
        context.add_match(context_match)
        matches_added = 1

        match = self.check_child_is_wildcard(tabs, bot, clientid, context,
                                             words, word_no, type, depth)
        if match is not None:
            return match

        if self._topic is not None:
            match = self._topic.consume(bot, clientid, context, words,
                                        word_no + 1, Match.TOPIC, depth + 1)
            if match is not None:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%sMatched topic, success!" % (tabs))
                return match
            if words.word(word_no) == PatternNode.TOPIC:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug(
                        "%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(bot, clientid, context, words,
                                       word_no + 1, Match.THAT, depth + 1)
            if match is not None:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%sMatched that, success!" % (tabs))
                return match
            if words.word(word_no) == PatternNode.THAT:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug(
                        "%s Looking for a %s, none give, no match found!" %
                        (tabs, PatternNode.THAT))
                return None

        # TODO Add priority words first

        word_no += 1
        if word_no >= words.num_words():
            if logging.getLogger().isEnabledFor(logging.DEBUG):
                logging.debug("%sNo more words" % (tabs))
            return super(PatternOneOrMoreWildCardNode,
                         self).consume(bot, clientid, context, words, word_no,
                                       type, depth + 1)
        word = words.word(word_no)

        if len(self._children) > 0:
            for child in self._children:

                result = child.equals(bot, clientid, words, word_no)
                if result.matched is True:
                    word_no = result.word_no
                    if logging.getLogger().isEnabledFor(logging.DEBUG):
                        logging.debug("%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(bot, clientid, context, words,
                                          word_no + 1, type, depth + 1)
                    if match is not None:
                        return match

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

            if logging.getLogger().isEnabledFor(logging.DEBUG):
                logging.debug("%sWildcard %s matched %s" %
                              (tabs, self._wildcard, word))
            context_match.add_word(word)

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

        if logging.getLogger().isEnabledFor(logging.DEBUG):
            logging.debug(
                "%sNo children, consume words until next break point" % (tabs))
        while word_no < words.num_words() - 1:
            match = super(PatternOneOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words, word_no,
                                        type, depth + 1)
            if match is not None:
                return match

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

            if logging.getLogger().isEnabledFor(logging.DEBUG):
                logging.debug("%sWildcard %s matched %s" %
                              (tabs, self._wildcard, word))
            context_match.add_word(word)

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

        if logging.getLogger().isEnabledFor(logging.DEBUG):
            logging.debug("%sWildcard %s matched %s" %
                          (tabs, self._wildcard, word))
        context_match.add_word(word)

        if word_no == words.num_words() - 1:
            match = super(PatternOneOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words,
                                        word_no + 1, type, depth + 1)
        else:
            match = super(PatternOneOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words, word_no,
                                        type, depth + 1)

        if match is not None:
            return match
        else:
            context.pop_matches(matches_added)
            return None
Beispiel #13
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        if bot.configuration.tab_parse_output is True:
            tabs = TextUtils.get_tabs(depth)
        else:
            tabs = ""

        if context.search_time_exceeded() is True:
            if logging.getLogger().isEnabledFor(logging.ERROR):
                logging.error("%sMax search time [%d]secs exceeded" %
                              (tabs, context.max_search_timeout))
            return None

        if context.search_depth_exceeded(depth) is True:
            if logging.getLogger().isEnabledFor(logging.ERROR):
                logging.error("%sMax search depth [%d] exceeded" %
                              (tabs, context.max_search_depth))
            return None

        if word_no >= words.num_words():
            if self._template is not None:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%sFound a template, success!" % (tabs))
                return self._template
            else:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug(
                        "%sNo more words and no template, no match found!" %
                        (tabs))
                #context.pop_match()
                return None

        if self._topic is not None:
            match = self._topic.consume(bot, clientid, context, words, word_no,
                                        Match.TOPIC, depth + 1)
            if match is not None:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%sMatched topic, success!" % (tabs))
                return match
            if words.word(word_no) == PatternNode.TOPIC:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug(
                        "%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(bot, clientid, context, words, word_no,
                                       Match.THAT, depth + 1)
            if match is not None:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%sMatched that, success!" % (tabs))
                return match
            if words.word(word_no) == PatternNode.THAT:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug(
                        "%s Looking for a %s, none give, no match found!" %
                        (tabs, PatternNode.THAT))
                return None

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

        if self._0ormore_hash is not None:
            match = self._0ormore_hash.consume(bot, clientid, context, words,
                                               word_no, type, depth + 1)
            if match is not None:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%sMatched 0 or more hash, success!" %
                                  (tabs))
                return match

        if self._1ormore_underline is not None:
            match = self._1ormore_underline.consume(bot, clientid, context,
                                                    words, word_no, type,
                                                    depth + 1)
            if match is not None:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%sMatched 1 or more underline, success!" %
                                  (tabs))
                return match

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

        if self._0ormore_arrow is not None:
            match = self._0ormore_arrow.consume(bot, clientid, context, words,
                                                word_no, type, depth + 1)
            if match is not None:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%sMatched 0 or more arrow, success!" %
                                  (tabs))
                return match

        if self._1ormore_star is not None:
            match = self._1ormore_star.consume(bot, clientid, context, words,
                                               word_no, type, depth + 1)
            if match is not None:
                if logging.getLogger().isEnabledFor(logging.DEBUG):
                    logging.debug("%sMatched 1 or more star, success!" %
                                  (tabs))
                return match

        if logging.getLogger().isEnabledFor(logging.DEBUG):
            logging.debug("%sNo match for %s, trying another path" %
                          (tabs, words.word(word_no)))
        return None
Beispiel #14
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        tabs = TextUtils.get_tabs(word_no)

        if depth > context.max_search_depth:
            logging.error("%sMax search depth [%d]exceeded" % (tabs, context.max_search_depth))
            return None

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

        if self._0ormore_hash is not None:
            logging.debug("%sWildcard is next node, moving on!"%(tabs))
            match = self._0ormore_hash.consume(bot, clientid, context, words, word_no+1, type, depth+1)
            if match is not None:
                return match

        if self._1ormore_underline is not None:
            logging.debug("%sWildcard is next node, moving on!"%(tabs))
            match = self._1ormore_underline.consume(bot, clientid, context, words, word_no+1, type, depth+1)
            if match is not None:
                return match

        if self._0ormore_arrow is not None:
            logging.debug("%sWildcard is next node, moving on!"%(tabs))
            match = self._0ormore_arrow.consume(bot, clientid, context, words, word_no+1, type, depth+1)
            if match is not None:
                return match

        if self._1ormore_star is not None:
            logging.debug("%sWildcard is next node, moving on!"%(tabs))
            match = self._1ormore_star.consume(bot, clientid, context, words, word_no+1, type, depth+1)
            if match is not None:
                return match

        # TODO Add priority words first

        word = words.word(word_no)

        if len(self._children) > 0:
            for child in self._children:
                if child.equals(bot, clientid, word):
                    logging.debug ("%sWildcard child %s matched %s"%(tabs, child._word, word))

                    logging.debug("%s*MATCH -> %s" % (tabs, word))
                    context_match2 = Match(Match.WORD, child, word)
                    context.add_match(context_match2)
                    matches_added += 1

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

                    if word == PatternTopicNode.TOPIC or word == PatternThatNode.THAT:
                        logging.debug ("Found a topic or that ar the wrong place....")
                        context.pop_matches(matches_added)
                        return None

            logging.debug("%sWildcard %s consumed %s" % (tabs, self._wildcard, word))

            logging.debug("%s*MATCH -> %s" % (tabs, word))
            context_match.add_word(word)

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

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

            if word == PatternTopicNode.TOPIC or word == PatternThatNode.THAT:
                logging.debug("Found a topic or that ar the wrong place....")
                context.pop_matches(matches_added)
                return None

            logging.debug("%sWildcard %s consumed %s" % (tabs, self._wildcard, word))
            logging.debug("%s*MATCH -> %s" % (tabs, word))
            context_match.add_word(word)

            match = super(PatternZeroOrMoreWildCardNode, self).consume(bot, clientid, context, words, word_no + 1, 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)

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

            if word == PatternTopicNode.TOPIC or word == PatternThatNode.THAT:
                logging.debug("Found a topic or that ar the wrong place....")
                context.pop_matches(matches_added)
                return None

            logging.debug("%s*MATCH -> %s" % (tabs, word))
            context_match.add_word(word)

            word_no += 1
            word = words.word(word_no)
            logging.debug("%sWildcard %s consumed %s" % (tabs, self._wildcard, word))

        logging.debug("%sWildcard %s consumed %s" % (tabs, self._wildcard, word))

        match = super(PatternZeroOrMoreWildCardNode, self).consume(bot, clientid, context, words, word_no, type, depth+1)

        if match is not None:
            return match
        else:
            context.pop_matches(matches_added)
            return None
Beispiel #15
0
 def get_tabs(self, client_context, depth):
     if client_context.bot.configuration.tab_parse_output is True:
         return TextUtils.get_tabs(depth)
     return ""
Beispiel #16
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        tabs = TextUtils.get_tabs(word_no)

        if depth > context.max_search_depth:
            logging.error("%sMax search depth [%d]exceeded" %
                          (tabs, context.max_search_depth))
            return None

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

        if self._0ormore_hash is not None:
            logging.debug("%sWildcard is next node, moving on!" % (tabs))
            match = self._0ormore_hash.consume(bot, clientid, context, words,
                                               word_no + 1, type, depth + 1)
            if match is not None:
                return match

        if self._1ormore_underline is not None:
            logging.debug("%sWildcard is next node, moving on!" % (tabs))
            match = self._1ormore_underline.consume(bot, clientid, context,
                                                    words, word_no + 1, type,
                                                    depth + 1)
            if match is not None:
                return match

        if self._0ormore_arrow is not None:
            logging.debug("%sWildcard is next node, moving on!" % (tabs))
            match = self._0ormore_arrow.consume(bot, clientid, context, words,
                                                word_no + 1, type, depth + 1)
            if match is not None:
                return match

        if self._1ormore_star is not None:
            logging.debug("%sWildcard is next node, moving on!" % (tabs))
            match = self._1ormore_star.consume(bot, clientid, context, words,
                                               word_no + 1, type, depth + 1)
            if match is not None:
                return match

        # TODO Add priority words first

        word = words.word(word_no)

        if len(self._children) > 0:
            for child in self._children:
                if child.equals(bot, clientid, word):
                    logging.debug("%sWildcard child %s matched %s" %
                                  (tabs, child._word, word))

                    logging.debug("%s*MATCH -> %s" % (tabs, word))
                    context_match2 = Match(Match.WORD, child, word)
                    context.add_match(context_match2)
                    matches_added += 1

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

                    if word == PatternTopicNode.TOPIC:
                        logging.debug("1 Found a topic at the wrong place....")
                        context.pop_matches(matches_added)
                        return None

                    if word == PatternThatNode.THAT:
                        logging.debug("Found a that at the wrong place....")
                        context.pop_matches(matches_added)
                        return None

            logging.debug("%sWildcard %s consumed %s" %
                          (tabs, self._wildcard, word))

            logging.debug("%s*MATCH -> %s" % (tabs, word))
            context_match.add_word(word)

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

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

            if word == PatternTopicNode.TOPIC:
                logging.debug("2 Found a topic at the wrong place....")
                context.pop_matches(matches_added)
                return None

            if word == PatternThatNode.THAT:
                logging.debug("Found a that at the wrong place....")
                context.pop_matches(matches_added)
                return None

            logging.debug("%sWildcard %s consumed %s" %
                          (tabs, self._wildcard, word))
            logging.debug("%s*MATCH -> %s" % (tabs, word))
            context_match.add_word(word)

            match = super(PatternZeroOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words,
                                        word_no + 1, 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)

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

            if word == PatternTopicNode.TOPIC:
                logging.debug("3 Found a topic at the wrong place....")
                context.pop_matches(matches_added)
                return None

            if word == PatternThatNode.THAT:
                logging.debug("Found a that at the wrong place....")
                context.pop_matches(matches_added)
                return None

            logging.debug("%s*MATCH -> %s" % (tabs, word))
            context_match.add_word(word)

            word_no += 1
            word = words.word(word_no)
            logging.debug("%sWildcard %s consumed %s" %
                          (tabs, self._wildcard, word))

        logging.debug("%sWildcard %s consumed %s" %
                      (tabs, self._wildcard, word))

        match = super(PatternZeroOrMoreWildCardNode,
                      self).consume(bot, clientid, context, words, word_no,
                                    type, depth + 1)

        if match is not None:
            return match
        else:
            context.pop_matches(matches_added)
            return None
Beispiel #17
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        tabs = TextUtils.get_tabs(word_no)

        if depth > context.max_search_depth:
            logging.error("%sMax search depth [%d]exceeded" %
                          (tabs, context.max_search_depth))
            return None

        if word_no >= words.num_words():
            if self._template is not None:
                logging.debug("%sFound a template, success!" % (tabs))
                return self._template
            else:
                logging.debug(
                    "%sNo more words and no template, no match found!" %
                    (tabs))
                #context.pop_match()
                return None

        if self._topic is not None:
            match = self._topic.consume(bot, clientid, context, words, word_no,
                                        Match.TOPIC, depth + 1)
            if match is not None:
                logging.debug("%sMatched topic, success!" % (tabs))
                return match
            if words.word(word_no) == PatternNode.TOPIC:
                logging.debug(
                    "%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(bot, clientid, context, words, word_no,
                                       Match.THAT, depth + 1)
            if match is not None:
                logging.debug("%sMatched that, success!" % (tabs))
                return match
            if words.word(word_no) == PatternNode.THAT:
                logging.debug(
                    "%s Looking for a %s, none give, no match found!" %
                    (tabs, PatternNode.THAT))
                return None

        for child in self._priority_words:
            if child.equals(bot, clientid, words.word(word_no)):
                logging.debug("%sPriority %s matched %s" %
                              (tabs, child._word, words.word(word_no)))
                match_node = Match(type, child, words.word(word_no))
                context.add_match(match_node)

                match = child.consume(bot, clientid, context, words,
                                      word_no + 1, type, depth + 1)
                if match is not None:
                    logging.debug("%sMatched child, success!" % (tabs))
                    return match
                else:
                    context.pop_match()

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

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

        for child in self._children:
            if child.equals(bot, clientid, words.word(word_no)):
                logging.debug("%sChild %s matched %s" %
                              (tabs, child._word, words.word(word_no)))
                match_node = Match(type, child, words.word(word_no))
                context.add_match(match_node)

                skip_to = self.consume_set_phrase(bot, clientid, context,
                                                  child, match_node, words,
                                                  word_no)
                if skip_to is None:
                    continue
                else:
                    word_no = skip_to

                match = child.consume(bot, clientid, context, words,
                                      word_no + 1, type, depth + 1)
                if match is not None:
                    logging.debug("%sMatched child, success!" % (tabs))
                    return match
                else:
                    context.pop_match()

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

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

        logging.debug("%sNo match for %s, trying another path" %
                      (tabs, words.word(word_no)))
        return None
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        tabs = TextUtils.get_tabs(depth)

        if depth > context.max_search_depth:
            logging.error("%sMax search depth [%d] exceeded" %
                          (tabs, context.max_search_depth))
            return None

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

        match = self.check_child_is_wildcard(tabs, bot, clientid, context,
                                             words, word_no, type, depth)
        if match is not None:
            return match

        # TODO Add priority words first

        word = words.word(word_no)

        if len(self._children) > 0:
            for child in self._children:

                result = child.equals(bot, clientid, words, word_no)
                if result.matched is True:
                    word_no = result.word_no
                    logging.debug("%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(bot, clientid, context, words,
                                          word_no + 1, type, depth + 1)
                    if match is not None:
                        return match

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

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

            match = super(PatternZeroOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words,
                                        word_no + 1, 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, word, context,
                                          matches_added) is True:
                return None

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

            match = super(PatternZeroOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words,
                                        word_no + 1, 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)

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

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

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

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

        if word_no == words.num_words() - 1:
            match = super(PatternZeroOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words,
                                        word_no + 1, type, depth + 1)
        else:
            match = super(PatternZeroOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words, word_no,
                                        type, depth + 1)

        if match is not None:
            return match
        else:
            context.pop_matches(matches_added)
            return None
Beispiel #19
0
    def consume(self, bot, clientid, context, words, word_no, type, depth):

        tabs = TextUtils.get_tabs(word_no)

        if depth > context.max_search_depth:
            logging.error("%sMax search depth [%d]exceeded" %
                          (tabs, context.max_search_depth))
            return None

        if word_no >= words.num_words():
            return None

        word = words.word(word_no)
        logging.debug("%sWildcard %s matched %s" %
                      (tabs, self._wildcard, word))
        context_match = Match(type, self, word)
        context.add_match(context_match)
        matches_add = 1

        match = self.check_child_is_wildcard(tabs, bot, clientid, context,
                                             words, word_no, type, depth)
        if match is not None:
            return match

        if self._topic is not None:
            match = self._topic.consume(bot, clientid, context, words,
                                        word_no + 1, Match.TOPIC, depth + 1)
            if match is not None:
                logging.debug("%sMatched topic, success!" % (tabs))
                return match
            if words.word(word_no) == PatternNode.TOPIC:
                logging.debug(
                    "%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(bot, clientid, context, words,
                                       word_no + 1, Match.THAT, depth + 1)
            if match is not None:
                logging.debug("%sMatched that, success!" % (tabs))
                return match
            if words.word(word_no) == PatternNode.THAT:
                logging.debug(
                    "%s Looking for a %s, none give, no match found!" %
                    (tabs, PatternNode.THAT))
                return None

        # TODO Add priority words first

        word_no += 1
        if word_no >= words.num_words():
            logging.debug("%sNo more words" % (tabs))
            return super(PatternOneOrMoreWildCardNode,
                         self).consume(bot, clientid, context, words, word_no,
                                       type, depth + 1)
        word = words.word(word_no)

        if len(self._children) > 0:
            for child in self._children:
                if child.equals(bot, clientid, word):
                    logging.debug("%sWildcard child %s matched %s" %
                                  (tabs, child._word, word))
                    context_match2 = Match(Match.WORD, child, word)
                    context.add_match(context_match2)
                    matches_add += 1
                    skip_to = self.consume_set_phrase(bot, clientid, context,
                                                      child, context_match2,
                                                      words, word_no)
                    if skip_to is None:
                        continue
                    else:
                        word_no = skip_to
                    match = child.consume(bot, clientid, context, words,
                                          word_no + 1, type, depth + 1)
                    if match is not None:
                        return match

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

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

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

        logging.debug("%sNo children, consume words until next break point" %
                      (tabs))
        while word_no < words.num_words() - 1:
            match = super(PatternOneOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words, word_no,
                                        type, depth + 1)
            if match is not None:
                return match

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

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

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

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

        if word_no == words.num_words() - 1:
            match = super(PatternOneOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words,
                                        word_no + 1, type, depth + 1)
        else:
            match = super(PatternOneOrMoreWildCardNode,
                          self).consume(bot, clientid, context, words, word_no,
                                        type, depth + 1)

        if match is not None:
            return match
        else:
            context.pop_matches(matches_add)
            return None