def test_match_multi_word(self): topic = PatternOneOrMoreWildCardNode("*") match = Match(Match.TOPIC, topic, None) match.add_word("Hello") match.add_word("World") self.assertEqual(Match.TOPIC, match.matched_node_type) self.assertTrue(match.matched_node_multi_word) self.assertTrue(match.matched_node_wildcard) self.assertEqual("ONEORMORE [*]", match.matched_node_str) self.assertEqual( "Match=(Topic) Node=(ONEORMORE [*]) Matched=(Hello World)", match.to_string(self._client_context)) self.assertEqual(["Hello", "World"], match.matched_node_words) self.assertEqual("Hello World", match.joined_words(self._client_context))
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 given, 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 given, 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._priority_words or self._children: ################################################################################################################ # Priority nodes for child in self._priority_words: 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 ################################################################################################################ # Children nodes 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