def test_check_child_is_wildcard_hash(self): wildcard = MockPatternWildCardNode("*") self.assertIsNotNone(wildcard) wildcard._0ormore_hash = PatternZeroOrMoreWildCardNode('#') wildcard._0ormore_hash._template = PatternTemplateNode(TemplateNode()) context = MatchContext( max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.nlp.tokenizer) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "TEST SENTENCE") match = wildcard.check_child_is_wildcard("", self._client_context, context, sentence, 1, Match.WORD, 0) self.assertIsNotNone(match) context = MatchContext( max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.nlp.tokenizer) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "TEST") match = wildcard.check_child_is_wildcard("", self._client_context, context, sentence, 0, Match.WORD, 0) self.assertIsNotNone(match)
def test_equals_attribs(self): loader = SetLoader() self._client_context.brain._sets_collection._sets["TEST1"] = loader.load_from_text(""" VALUE1 VALUE2 VALUE3 VALUE4 """) node1 = PatternSetNode({"name": "test1"}, "") node2 = PatternSetNode({"name": "test1"}, "", userid="testid") node3 = PatternSetNode({"name": "test1"}, "", userid="testid2") match1 = node1.equals(self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'VALUE1'), 0) self.assertIsNotNone(match1) self.assertTrue(match1.matched) match2 = node2.equals(self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'VALUE1'), 0) self.assertIsNotNone(match2) self.assertTrue(match2.matched) match3 = node3.equals(self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'VALUE1'), 0) self.assertIsNotNone(match3) self.assertFalse(match3.matched)
def test_sentence_creation_one_word(self): sentence = Sentence(self._bot.brain.nlp.tokenizer, "One") self.assertIsNotNone(sentence) self.assertEqual(1, sentence.num_words()) with self.assertRaises(Exception): sentence.sentence.word(1) self.assertEqual("One", sentence.text())
def test_equals_template(self): self._client_context.brain.regex_templates["LEGION"] = re.compile( "^LEGION$", re.IGNORECASE) node1 = PatternRegexNode({"template": "LEGION"}, "") node2 = PatternRegexNode({"template": "LEGION"}, "", userid="testid") node3 = PatternRegexNode({"template": "LEGION"}, "", userid="testid2") match1 = node1.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'LEGION'), 0) self.assertIsNotNone(match1) self.assertTrue(match1.matched) match2 = node2.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'LEGION'), 0) self.assertIsNotNone(match2) self.assertTrue(match2.matched) match3 = node3.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'LEGION'), 0) self.assertIsNotNone(match3) self.assertFalse(match3.matched)
def test_question_create_from_sentence(self): sentence = Sentence(self._bot.brain.nlp.tokenizer, "One Two Three") question = Question.create_from_sentence(sentence) self.assertIsNotNone(question) self.assertEqual(1, len(question.sentences)) self.assertEqual(sentence.text(), question.sentence(0).text()) with self.assertRaises(Exception): question.sentence(1)
def test_sentence_creation_two_words_diff_split_char(self): tokenizer = Tokenizer(",") sentence = Sentence( tokenizer, "One,Two", ) self.assertIsNotNone(sentence) self.assertEqual(2, sentence.num_words()) self.assertEqual("One", sentence.word(0)) self.assertEqual("Two", sentence.word(1)) with self.assertRaises(Exception): sentence.sentence.word(2) self.assertEqual("One,Two", sentence.text())
def setUp(self): self._client_context = ClientContext(TestClient(), "testid") self._client_context.bot = Bot(BotConfiguration()) self._client_context.brain = self._client_context.bot.brain self._graph = self._client_context.bot.brain.aiml_parser.template_parser self.test_sentence = Sentence(self._client_context.brain.nlp.tokenizer, "test sentence") test_node = PatternOneOrMoreWildCardNode("*") self.test_sentence._matched_context = MatchContext( max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.nlp.tokenizer) self.test_sentence._matched_context._matched_nodes = [ Match(Match.WORD, test_node, 'one'), Match(Match.WORD, test_node, 'two'), Match(Match.WORD, test_node, 'three'), Match(Match.WORD, test_node, 'four'), Match(Match.WORD, test_node, 'five'), Match(Match.WORD, test_node, 'six'), Match(Match.TOPIC, test_node, '*'), Match(Match.THAT, test_node, '*') ] conversation = self._client_context.bot.get_conversation( self._client_context) question = Question.create_from_sentence(self.test_sentence) conversation._questions.append(question)
def test_equals_userid(self): word1 = PatternPriorityWordNode("word") word2 = PatternPriorityWordNode("word", userid="testid") word3 = PatternPriorityWordNode("word", userid="testid2") match1 = word1.equals(self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'word'), 0) self.assertIsNotNone(match1) self.assertTrue(match1.matched) match2 = word2.equals(self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'word'), 0) self.assertIsNotNone(match2) self.assertTrue(match2.matched) match3 = word3.equals(self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'word'), 0) self.assertIsNotNone(match3) self.assertFalse(match3.matched)
def test_arrow(self): node = PatternZeroOrMoreWildCardNode("^") self.assertFalse(node.is_root()) self.assertFalse(node.is_priority()) self.assertTrue(node.is_zero_or_more()) self.assertFalse(node.is_one_or_more()) self.assertFalse(node.is_set()) self.assertFalse(node.is_bot()) self.assertFalse(node.is_template()) self.assertFalse(node.is_that()) self.assertFalse(node.is_topic()) self.assertTrue(node.is_wildcard()) self.assertIsNotNone(node.children) self.assertFalse(node.has_children()) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "*") self.assertEqual(node.wildcard, "^") self.assertTrue(node.equivalent(PatternZeroOrMoreWildCardNode("^"))) result = node.equals(self._client_context, sentence, 0) self.assertFalse(result.matched) self.assertEqual( node.to_string(), "ZEROORMORE [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] wildcard=[^]" ) self.assertEqual('<zerormore wildcard="^">\n</zerormore>\n', node.to_xml(self._client_context)) self.assertFalse(node.equivalent(PatternWordNode("test")))
def test_init(self): node = PatternPriorityWordNode("test1") self.assertIsNotNone(node) self.assertFalse(node.is_root()) self.assertTrue(node.is_priority()) self.assertFalse(node.is_wildcard()) self.assertFalse(node.is_zero_or_more()) self.assertFalse(node.is_one_or_more()) self.assertFalse(node.is_set()) self.assertFalse(node.is_bot()) self.assertFalse(node.is_template()) self.assertFalse(node.is_that()) self.assertFalse(node.is_topic()) self.assertFalse(node.is_wildcard()) self.assertIsNotNone(node.children) self.assertFalse(node.has_children()) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "test1 test2") self.assertTrue(node.equivalent(PatternPriorityWordNode("test1"))) result = node.equals(self._client_context, sentence, 0) self.assertTrue(result.matched) result = node.equals(self._client_context, sentence, 1) self.assertFalse(result.matched) self.assertEqual(node.to_string(), "PWORD [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]") self.assertEqual('<priority word="test1"></priority>\n', node.to_xml(self._client_context)) node.add_child(PatternWordNode("test2")) self.assertEqual(len(node.children), 1) self.assertEqual(node.to_string(), "PWORD [*] [P(0)^(0)#(0)C(1)_(0)*(0)To(0)Th(0)Te(0)] word=[test1]") self.assertEqual('<priority word="test1"><word word="test2"></word>\n</priority>\n', node.to_xml(self._client_context))
def handle_none_response(self, client_context, sentence, responselogger, options=[]): sentence.response = self.get_default_response(client_context) non_hanlde_sentence = Sentence(client_context.brain.nlp, sentence.response) non_hanlde_sentence._no_response = True if responselogger is not None: responselogger.log_unknown_response(non_hanlde_sentence) if len(options) == 0: return non_hanlde_sentence else: return non_hanlde_sentence, options
def test_bot_init_with_spellchecker(self): bot_config = BotConfiguration() bot_config.spelling._classname = "programr.spelling.norvig.NorvigSpellingChecker" bot_config.spelling._corpus = os.path.dirname(__file__) + os.sep + "test_corpus.txt" bot_config.spelling._check_before = True bot_config.spelling._check_and_retry = True bot = Bot(bot_config) self.assertIsNotNone(bot) test_sentence = Sentence(bot.brain.nlp.tokenizer, "locetion") bot.check_spelling_before(test_sentence) self.assertIsNotNone(test_sentence) self.assertEqual("LOCATION", test_sentence.text()) test_sentence = Sentence(bot.brain.nlp.tokenizer, "locetion") response = bot.check_spelling_and_retry(self._client_context, test_sentence) self.assertIsNone(response)
def test_words_from_current_pos(self): sentence = Sentence(self._bot.brain.nlp.tokenizer, "One Two Three") self.assertIsNotNone(sentence) self.assertEqual("One Two Three", sentence.words_from_current_pos(0)) self.assertEqual("Two Three", sentence.words_from_current_pos(1)) self.assertEqual("Three", sentence.words_from_current_pos(2)) with self.assertRaises(Exception): self.assertEqual("Three", sentence.words_from_current_pos(3)) self.assertEqual("One Two Three", sentence.text())
def get_exit_response(self, client_context): if self.exit_response_srai is not None: sentence = Sentence(client_context.brain.nlp, self.exit_response_srai) exit_response = client_context.brain.ask_question( client_context, sentence) if exit_response is None or not exit_response: exit_response = self.exit_response return exit_response else: return self.exit_response
def get_initial_question(self, client_context): if self.initial_question_srai is not None: sentence = Sentence(client_context.brain.nlp, self.initial_question_srai) initial_question = client_context.brain.ask_question( client_context, sentence) if initial_question is None or not initial_question: initial_question = self.initial_question return initial_question else: return self.initial_question
def test_equals_pattern(self): node1 = PatternRegexNode({}, "^LEGION$") node2 = PatternRegexNode({}, "^LEGION$", userid="testid") node3 = PatternRegexNode({}, "^LEGION$", userid="testid2") match1 = node1.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'LEGION'), 0) self.assertIsNotNone(match1) self.assertTrue(match1.matched) match2 = node2.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'LEGION'), 0) self.assertIsNotNone(match2) self.assertTrue(match2.matched) match3 = node3.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'LEGION'), 0) self.assertIsNotNone(match3) self.assertFalse(match3.matched)
def test_equals(self): node1 = PatternISetNode([], "test1, test2, test3") node2 = PatternISetNode([], "test1, test2, test3", userid="testid") node3 = PatternISetNode([], "test1, test2, test3", userid="testid2") match1 = node1.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'test1'), 0) self.assertIsNotNone(match1) self.assertTrue(match1.matched) match2 = node2.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'test1'), 0) self.assertIsNotNone(match2) self.assertTrue(match2.matched) match3 = node3.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'test1'), 0) self.assertIsNotNone(match3) self.assertFalse(match3.matched)
def test_number(self): self._client_context.brain.dynamics.add_dynamic_set('number', "programr.dynamic.sets.numeric.IsNumeric", None) node = PatternSetNode([], "NUMBER") self.assertIsNotNone(node) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "12 XY") result = node.equals(self._client_context, sentence, 0) self.assertTrue(result.matched) result = node.equals(self._client_context, sentence, result.word_no+1) self.assertFalse(result.matched)
def test_split_into_words(self): sentence = Sentence(self._bot.brain.nlp.tokenizer, "HELLO") self.assertIsNotNone(sentence) self.assertEqual(1, sentence.num_words()) self.assertEqual("HELLO", sentence.word(0)) self.assertEqual("HELLO", sentence.words_from_current_pos(0)) with self.assertRaises(Exception): sentence.sentence.word(1) self.assertEqual("HELLO", sentence.text())
def test_equivalent_property(self): self._client_context.brain.properties.add_property("test1", "value1") bot1 = PatternBotNode({"property": "test1"}, None) bot2 = PatternBotNode({"property": "test1"}, None, userid="testid") bot3 = PatternBotNode({"property": "test1"}, None, userid="testid2") match1 = bot1.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'value1'), 0) self.assertIsNotNone(match1) self.assertTrue(match1.matched) match2 = bot2.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'value1'), 0) self.assertIsNotNone(match2) self.assertTrue(match2.matched) match3 = bot3.equals( self._client_context, Sentence(self._client_context.brain.nlp.tokenizer, 'value1'), 0) self.assertIsNotNone(match3) self.assertFalse(match3.matched)
def test_check_child_is_wildcard_no_wildcard_children(self): wildcard = MockPatternWildCardNode("*") self.assertIsNotNone(wildcard) context = MatchContext( max_search_depth=100, max_search_timeout=-1, tokenizer=self._client_context.brain.nlp.tokenizer) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "TEST SENTENCE") match = wildcard.check_child_is_wildcard("", self._client_context, context, sentence, 0, Match.WORD, 0) self.assertIsNone(match)
def handle_response(self, client_context, sentence, response, srai, responselogger, options=[]): YLogger.debug(client_context, "Raw Response (%s): %s", client_context.userid, response) YLogger.debug(client_context, "Options (%s)", options) sentence.response = response post_processed_response = self.post_process_response( client_context, response, srai) response_sentence = Sentence(client_context.brain.nlp, post_processed_response) response_text = response_sentence.text() self.log_answer(client_context, sentence.text, response_text, responselogger) if len(options) == 0: return response_sentence, None else: YLogger.debug(client_context, "Returning response_sentence and options.") return response_sentence, options
def test_transcripts_questions_without_props(self): client = TranscriptAdminExtensionClient() client_context = client.create_client_context("testid") question = Question.create_from_sentence( Sentence(client_context.brain.nlp.tokenizer, "Hello World")) conversation = client_context.bot.get_conversation(client_context) conversation.record_question(question) extension = TranscriptAdminExtension() self.assertIsNotNone(extension) result = extension.execute(client_context, "") self.assertIsNotNone(result) self.assertEquals( "Questions:<br /><ul><li>Hello World - </li></ul><br />", result)
def test_multi_node_set(self): loader = SetLoader() self._client_context.brain._sets_collection._sets["TEST1"] = loader.load_from_text(""" Red Red Amber Red Brown """) node = PatternSetNode([], "test1") self.assertIsNotNone(node) self.assertFalse(node.is_root()) self.assertFalse(node.is_priority()) self.assertFalse(node.is_wildcard()) self.assertFalse(node.is_zero_or_more()) self.assertFalse(node.is_one_or_more()) self.assertTrue(node.is_set()) self.assertFalse(node.is_bot()) self.assertFalse(node.is_template()) self.assertFalse(node.is_that()) self.assertFalse(node.is_topic()) self.assertFalse(node.is_wildcard()) self.assertTrue(node.equivalent(PatternSetNode([], "TEST1"))) self.assertIsNotNone(node.children) self.assertFalse(node.has_children()) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "RED Red BROWN red AMBER") result = node.equals(self._client_context, sentence, 0) self.assertTrue(result.matched) self.assertEquals(result.matched_phrase, "Red") self.assertEquals(result.word_no, 0) result = node.equals(self._client_context, sentence, result.word_no+1) self.assertTrue(result.matched) self.assertEquals(result.matched_phrase, "Red Brown") result = node.equals(self._client_context, sentence, result.word_no+1) self.assertTrue(result.matched) self.assertEquals(result.matched_phrase, "Red Amber") self.assertEqual(node.to_string(), "SET [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] name=[TEST1]") self.assertEqual('<set name="TEST1">\n</set>', node.to_xml(self._client_context))
def failed_authentication(self, client_context): YLogger.error(client_context, "[%s] failed authentication!") # If we have an SRAI defined, then use that if self.authentication.configuration.denied_srai is not None: match_context = self._aiml_parser.match_sentence( client_context, Sentence(self._bot.brain.nlp.tokenizer, self.authentication.configuration.denied_srai), topic_pattern="*", that_pattern="*") # If the SRAI matched then return the result if match_context is not None: return self.resolve_matched_template(client_context, match_context) # Otherswise return the static text, which is either # User defined via config.yaml # Or use the default value BrainSecurityConfiguration.DEFAULT_ACCESS_DENIED return self.authentication.configuration.denied_text
def test_init(self): loader = SetLoader() self._client_context.brain._sets_collection._sets["TEST1"] = loader.load_from_text(""" VALUE1 VALUE2 VALUE3 VALUE4 """) node = PatternSetNode([], "test1") self.assertIsNotNone(node) self.assertFalse(node.is_root()) self.assertFalse(node.is_priority()) self.assertFalse(node.is_wildcard()) self.assertFalse(node.is_zero_or_more()) self.assertFalse(node.is_one_or_more()) self.assertTrue(node.is_set()) self.assertFalse(node.is_bot()) self.assertFalse(node.is_template()) self.assertFalse(node.is_that()) self.assertFalse(node.is_topic()) self.assertFalse(node.is_wildcard()) self.assertIsNotNone(node.children) self.assertFalse(node.has_children()) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "VALUE1 VALUE2 VALUE3 VALUE4") self.assertTrue(node.equivalent(PatternSetNode([], "TEST1"))) result = node.equals(self._client_context, sentence, 0) self.assertTrue(result.matched) result = node.equals(self._client_context, sentence, 1) self.assertTrue(result.matched) result = node.equals(self._client_context, sentence, 2) self.assertTrue(result.matched) result = node.equals(self._client_context, sentence, 3) self.assertTrue(result.matched) self.assertEqual(node.to_string(), "SET [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] name=[TEST1]") self.assertEqual('<set name="TEST1">\n</set>', node.to_xml(self._client_context))
def test_init(self): node = PatternISetNode([], "test1, test2, test3") self.assertIsNotNone(node) self.assertFalse(node.is_root()) self.assertFalse(node.is_priority()) self.assertFalse(node.is_wildcard()) self.assertFalse(node.is_zero_or_more()) self.assertFalse(node.is_one_or_more()) self.assertFalse(node.is_set()) self.assertFalse(node.is_bot()) self.assertFalse(node.is_template()) self.assertFalse(node.is_that()) self.assertFalse(node.is_topic()) self.assertFalse(node.is_wildcard()) self.assertTrue(node.is_iset()) self.assertIsNotNone(node.children) self.assertFalse(node.has_children()) self.assertIsNotNone(node.words) self.assertEquals(3, len(node.words)) self.assertEquals("TEST1", node.words[0]) self.assertEquals("TEST2", node.words[1]) self.assertEquals("TEST3", node.words[2]) self.assertTrue( node.equivalent(PatternISetNode([], "test1, test2, test3"))) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "TEST1 TEST2 TEST3") result = node.equals(self._client_context, sentence, 0) self.assertTrue(result.matched) result = node.equals(self._client_context, sentence, 1) self.assertTrue(result.matched) result = node.equals(self._client_context, sentence, 2) self.assertTrue(result.matched) result = node.equals(self._client_context, sentence, 3) self.assertFalse(result.matched)
def test_init(self): self._client_context.brain.properties.add_property("test1", "value1") node = PatternBotNode({}, "test1") self.assertIsNotNone(node) self.assertFalse(node.is_root()) self.assertFalse(node.is_priority()) self.assertFalse(node.is_wildcard()) self.assertFalse(node.is_zero_or_more()) self.assertFalse(node.is_one_or_more()) self.assertFalse(node.is_set()) self.assertTrue(node.is_bot()) self.assertFalse(node.is_template()) self.assertFalse(node.is_that()) self.assertFalse(node.is_topic()) self.assertFalse(node.is_wildcard()) self.assertTrue(node.equivalent(PatternBotNode([], "test1"))) self.assertFalse(node.equivalent(PatternBotNode([], "test2"))) sentence = Sentence(self._client_context.brain.nlp.tokenizer, "value1 value2") result = node.equals(self._client_context, sentence, 0) self.assertTrue(result.matched) self.assertEquals(0, result.word_no) result = node.equals(self._client_context, sentence, 1) self.assertFalse(result.matched) self.assertEquals(1, result.word_no) self.assertEqual( node.to_string(), "BOT [*] [P(0)^(0)#(0)C(0)_(0)*(0)To(0)Th(0)Te(0)] property=[test1]" ) self.assertEqual('<bot property="test1">\n</bot>', node.to_xml(self._client_context))
def test_match_sentence(self): self.parser.parse_from_text("""<?xml version="1.0" encoding="UTF-8"?> <aiml> <category> <pattern>HELLO</pattern> <template>Hiya</template> </category> </aiml> """) self.parser.pattern_parser.dump() bot = Bot(BotConfiguration()) context = self.parser.match_sentence( self._client_context, Sentence(bot.brain.nlp.tokenizer, "HELLO"), "*", "*") self.assertIsNotNone(context) self.assertEqual( "Hiya", context.template_node().template.resolve(self._client_context))
def test_combine_answers(self): question = Question() sentence1 = Sentence(self._bot.brain.nlp.tokenizer, "Hi") sentence1._response = "Hello" question._sentences.append(sentence1) sentence2 = Sentence(self._bot.brain.nlp.tokenizer, "Hi Again") question._sentences.append(sentence2) sentence2._response = "World" self.assertEqual(2, len(question._sentences)) self.assertEqual(question._sentences[0]._response, "Hello") self.assertEqual(question._sentences[1]._response, "World") sentences = question.combine_sentences() self.assertEqual("Hi. Hi Again", sentences) combined = question.combine_answers() self.assertIsNotNone(combined) self.assertEqual(combined, "Hello. World")