Exemple #1
0
 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())
Exemple #2
0
 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)
Exemple #3
0
 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())
Exemple #4
0
 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())
Exemple #5
0
 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())
Exemple #6
0
    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)
Exemple #7
0
 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