Example #1
0
 def test_match_multi_word(self):
     topic = PatternOneOrMoreWildCardNode("*")
     match = Match(Match.TOPIC, topic, None)
     match.add_word("Hello")
     match.add_word("World")
     self.assertEquals(["Hello", "World"], match.matched_words)
     self.assertEquals("Hello World", match.joined_words(self._bot.brain.tokenizer))
     self.assertEquals("Match=(Topic) Node=(ONEORMORE [*]) Matched=(Hello World)", match.to_string(self._bot.brain.tokenizer))
Example #2
0
 def test_match_multi_word(self):
     topic = PatternOneOrMoreWildCardNode("*")
     match = Match(Match.TOPIC, topic, None)
     match.add_word("Hello")
     match.add_word("World")
     self.assertEquals(["Hello", "World"], match.matched_words)
     self.assertEquals("Hello World", match.joined_words())
     self.assertEquals("Match=(Topic) Node=(ONEORMORE [*]) Matched=(Hello World)", match.to_string())
Example #3
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:
            YLogger.error(client_context,
                          "%sMax search time [%d]secs exceeded", tabs,
                          context.max_search_timeout)
            return None

        if context.search_depth_exceeded(depth) is True:
            YLogger.error(client_context, "%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)
        YLogger.debug(client_context, "%sWildcard %s matched %s", tabs,
                      self._wildcard, word)
        context_match = Match(match_type, self, word)
        context.add_match(context_match)
        matches_added = 1

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

        if self._topic is not None:
            match = self._topic.consume(client_context, context, words,
                                        word_no + 1, 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 + 1, 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

        word_no += 1
        if word_no >= words.num_words():
            YLogger.debug(client_context, "%sNo more words", tabs)
            return super(PatternOneOrMoreWildCardNode,
                         self).consume(client_context, context, words, word_no,
                                       match_type, depth + 1)
        word = words.word(word_no)

        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)

            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(PatternOneOrMoreWildCardNode,
                          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)

        YLogger.debug(client_context, "%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(client_context, context, words,
                                        word_no + 1, match_type, depth + 1)
        else:
            match = super(PatternOneOrMoreWildCardNode,
                          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
Example #4
0
    def consume(self, client_context, context, words, word_no, match_type,
                depth):

        tabs = self.get_tabs(client_context, depth)

        #TODO uncomment this section
        # if context.search_time_exceeded() is True:
        #     YLogger.error(client_context, "%sMax search time [%d]secs exceeded", tabs, context.max_search_timeout)
        #     return None

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

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

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

        word = words.word(word_no)

        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)

        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
Example #5
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
Example #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

        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
Example #7
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:
            YLogger.error(client_context, "%sMax search time [%d]secs exceeded", tabs, context.max_search_timeout)
            return None

        if context.search_depth_exceeded(depth) is True:
            YLogger.error(client_context, "%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)
        YLogger.debug(client_context, "%sWildcard %s matched %s", tabs, self._wildcard, word)
        context_match = Match(match_type, self, word)
        context.add_match(context_match)
        matches_added = 1

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

        if self._topic is not None:
            match = self._topic.consume(client_context, context, words, word_no+1, 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+1, 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

        word_no += 1
        if word_no >= words.num_words():
            YLogger.debug(client_context, "%sNo more words", tabs)
            return super(PatternOneOrMoreWildCardNode, self).consume(client_context, context, words, word_no, match_type, depth+1)
        word = words.word(word_no)

        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)

            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(PatternOneOrMoreWildCardNode, 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)

        YLogger.debug(client_context, "%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(client_context, context, words, word_no+1, match_type, depth+1)
        else:
            match = super(PatternOneOrMoreWildCardNode, 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
Example #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():
            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
    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
Example #10
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