class TwitterTrainerTestCase(ChatBotTestCase): def setUp(self): """ Instantiate the trainer class for testing. """ super(TwitterTrainerTestCase, self).setUp() self.trainer = TwitterTrainer( self.chatbot, twitter_consumer_key='twitter_consumer_key', twitter_consumer_secret='twitter_consumer_secret', twitter_access_token_key='twitter_access_token_key', twitter_access_token_secret='twitter_access_token_secret', show_training_progress=False) self.trainer.api = Mock() self.trainer.api.GetSearch = MagicMock( side_effect=get_search_side_effect) self.trainer.api.GetStatus = MagicMock( side_effect=get_status_side_effect) def test_random_word(self): word = self.trainer.random_word('random') self.assertTrue(len(word) > 3) def test_get_words_from_tweets(self): tweets = get_search_side_effect() words = self.trainer.get_words_from_tweets(tweets) self.assertIn('about', words) self.assertIn('favorite', words) self.assertIn('things', words) def test_get_statements(self): statements = self.trainer.get_statements() self.assertEqual(len(statements), 1) def test_train(self): self.trainer.train() statements = list(self.trainer.chatbot.storage.filter()) self.assertGreater(len(statements), 1) def test_train_sets_search_text(self): self.trainer.train() statements = list(self.trainer.chatbot.storage.filter()) self.assertGreater(len(statements), 1) self.assertEqual(statements[0].search_text, 'PRP:sure IN:jewel') def test_train_sets_search_in_response_to(self): self.trainer.train() statements = list(self.trainer.chatbot.storage.filter()) self.assertGreater(len(statements), 1) self.assertEqual(statements[0].search_in_response_to, 'PRP:sure IN:jewel')
class TwitterTrainerTestCase(ChatBotTestCase): def setUp(self): """ Instantiate the trainer class for testing. """ super(TwitterTrainerTestCase, self).setUp() self.trainer = TwitterTrainer( self.chatbot, twitter_consumer_key='twitter_consumer_key', twitter_consumer_secret='twitter_consumer_secret', twitter_access_token_key='twitter_access_token_key', twitter_access_token_secret='twitter_access_token_secret', show_training_progress=False ) self.trainer.api = Mock() self.trainer.api.GetSearch = MagicMock(side_effect=get_search_side_effect) self.trainer.api.GetStatus = MagicMock(side_effect=get_status_side_effect) def test_random_word(self): word = self.trainer.random_word('random') self.assertTrue(len(word) > 3) def test_get_words_from_tweets(self): tweets = get_search_side_effect() words = self.trainer.get_words_from_tweets(tweets) self.assertIn('about', words) self.assertIn('favorite', words) self.assertIn('things', words) def test_get_statements(self): statements = self.trainer.get_statements() self.assertEqual(len(statements), 1) def test_train(self): self.trainer.train() statements = list(self.trainer.chatbot.storage.filter()) self.assertGreater(len(statements), 1) def test_train_sets_search_text(self): self.trainer.train() statements = list(self.trainer.chatbot.storage.filter()) self.assertGreater(len(statements), 1) self.assertEqual(statements[0].search_text, 'urub') def test_train_sets_search_in_response_to(self): self.trainer.train() statements = list(self.trainer.chatbot.storage.filter()) self.assertGreater(len(statements), 1) self.assertEqual(statements[0].search_in_response_to, 'urub')
def setUp(self): """ Instantiate the trainer class for testing. """ super(TwitterTrainerTestCase, self).setUp() self.trainer = TwitterTrainer( self.chatbot.storage, twitter_consumer_key='twitter_consumer_key', twitter_consumer_secret='twitter_consumer_secret', twitter_access_token_key='twitter_access_token_key', twitter_access_token_secret='twitter_access_token_secret' ) self.trainer.api = Mock() self.trainer.api.GetSearch = MagicMock(side_effect=get_search_side_effect) self.trainer.api.GetStatus = MagicMock(side_effect=get_status_side_effect)
class TwitterTrainerTestCase(ChatBotTestCase): def setUp(self): """ Instantiate the trainer class for testing. """ super(TwitterTrainerTestCase, self).setUp() self.trainer = TwitterTrainer( self.chatbot.storage, twitter_consumer_key='twitter_consumer_key', twitter_consumer_secret='twitter_consumer_secret', twitter_access_token_key='twitter_access_token_key', twitter_access_token_secret='twitter_access_token_secret') self.trainer.api = Mock() self.trainer.api.GetSearch = MagicMock( side_effect=get_search_side_effect) self.trainer.api.GetStatus = MagicMock( side_effect=get_status_side_effect) def test_random_word(self): word = self.trainer.random_word('random') self.assertTrue(len(word) > 3) def test_get_words_from_tweets(self): tweets = get_search_side_effect() words = self.trainer.get_words_from_tweets(tweets) self.assertIn('about', words) self.assertIn('favorite', words) self.assertIn('things', words) def test_get_statements(self): statements = self.trainer.get_statements() self.assertEqual(len(statements), 1) def test_train(self): self.trainer.train() statement_created = self.trainer.storage.filter() self.assertTrue(len(statement_created))
TWITTER = { "CONSUMER_KEY": "my-twitter-consumer-key", "CONSUMER_SECRET": "my-twitter-consumer-secret", "ACCESS_TOKEN": "my-access-token", "ACCESS_TOKEN_SECRET": "my-access-token-secret" } ''' # Comment out the following line to disable verbose logging logging.basicConfig(level=logging.INFO) chatbot = ChatBot( "TwitterBot", logic_adapters=[ "chatterbot.logic.BestMatch" ], input_adapter="chatterbot.input.TerminalAdapter", output_adapter="chatterbot.output.TerminalAdapter", database_uri="./twitter-database.db", twitter_consumer_key=TWITTER["CONSUMER_KEY"], twitter_consumer_secret=TWITTER["CONSUMER_SECRET"], twitter_access_token_key=TWITTER["ACCESS_TOKEN"], twitter_access_token_secret=TWITTER["ACCESS_TOKEN_SECRET"] ) trainer = TwitterTrainer(chatbot) trainer.train() chatbot.logger.info('Trained database generated successfully!')
To use this example, create a new file called settings.py. In settings.py define the following: TWITTER = { "CONSUMER_KEY": "my-twitter-consumer-key", "CONSUMER_SECRET": "my-twitter-consumer-secret", "ACCESS_TOKEN": "my-access-token", "ACCESS_TOKEN_SECRET": "my-access-token-secret" } ''' # Comment out the following line to disable verbose logging logging.basicConfig(level=logging.INFO) chatbot = ChatBot("TwitterBot", logic_adapters=["chatterbot.logic.BestMatch"], input_adapter="chatterbot.input.TerminalAdapter", output_adapter="chatterbot.output.TerminalAdapter", database_uri="./twitter-database.db", twitter_consumer_key=TWITTER["CONSUMER_KEY"], twitter_consumer_secret=TWITTER["CONSUMER_SECRET"], twitter_access_token_key=TWITTER["ACCESS_TOKEN"], twitter_access_token_secret=TWITTER["ACCESS_TOKEN_SECRET"]) trainer = TwitterTrainer(chatbot) trainer.train() chatbot.logger.info('Trained database generated successfully!')