def get_statements(self): """ Returns list of random statements from the API. """ from twitter import TwitterError statements = [] # Generate a random word random_word = self.random_word(self.random_seed_word, self.lang) self.chatbot.logger.info('Requesting 50 random tweets containing the word {}'.format(random_word)) tweets = self.api.GetSearch(term=random_word, count=50, lang=self.lang) for tweet in tweets: statement = Statement(text=tweet.text) if tweet.in_reply_to_status_id: try: status = self.api.GetStatus(tweet.in_reply_to_status_id) statement.in_response_to = status.text statements.append(statement) except TwitterError as error: self.chatbot.logger.warning(str(error)) self.chatbot.logger.info('Adding {} tweets with responses'.format(len(statements))) return statements
def test_update_does_not_modify_existing_statement(self): statement = Statement(text="New statement") self.adapter.update(statement) self.adapter.read_only = True statement.in_response_to = "New statement" self.adapter.update(statement) results = self.adapter.filter(text="New statement") self.assertEqual(len(results), 1) self.assertEqual(results[0].text, statement.text) self.assertEqual(results[0].in_response_to, None)
def test_update_modifies_existing_statement(self): statement = Statement(text="New statement") self.adapter.update(statement) # Check the initial values results = list(self.adapter.filter(text=statement.text)) self.assertEqual(len(results), 1) self.assertEqual(results[0].in_response_to, None) # Update the statement value statement.in_response_to = "New response" self.adapter.update(statement) # Check that the values have changed results = list(self.adapter.filter(text=statement.text)) self.assertEqual(len(results), 1) self.assertEqual(results[0].in_response_to, "New response")
def test_update_modifies_existing_statement(self): statement = Statement(text="New statement") self.adapter.update(statement) # Check the initial values results = self.adapter.filter(text=statement.text) self.assertEqual(len(results), 1) self.assertEqual(results[0].in_response_to, None) # Update the statement value statement.in_response_to = "New response" self.adapter.update(statement) # Check that the values have changed results = self.adapter.filter(text=statement.text) self.assertEqual(len(results), 1) self.assertEqual(results[0].in_response_to, "New response")
def train(self, *corpus_paths): from chatterbot.corpus import load_corpus, list_corpus_files data_file_paths = [] # Get the paths to each file the bot will be trained with for corpus_path in corpus_paths: data_file_paths.extend(list_corpus_files(corpus_path)) for corpus, categories, file_path in load_corpus(*data_file_paths): statements_to_create = [] # Train the chat bot with each statement and response pair for conversation_count, conversation in enumerate(corpus): if self.show_training_progress: utils.print_progress_bar( 'Training ' + str(os.path.basename(file_path)), conversation_count + 1, len(corpus)) previous_statement_text = None previous_statement_search_text = '' for text in conversation: suggestion_tags = [] if text.strip('.?!/;:\'\"') in constants.AFFIRMATIVES: text = 'AFF' elif text.strip('.?!/;:\'\"') in constants.NEGATIVES: text = 'NEG' elif text[0] is '^': (suggestion, text) = text.split(maxsplit=1) suggestion = suggestion[1:] if not suggestion.find('/'): suggestion_tags.append(suggestion) else: for suggestion in suggestion.split('/'): suggestion_tags.append(suggestion) statement_search_text = self.chatbot.storage.tagger.get_bigram_pair_string( text) statement = Statement( text=text, search_text=statement_search_text, in_response_to=previous_statement_text, search_in_response_to=previous_statement_search_text, conversation='training') # YesNoLogicAdapter deals with responses to AFF/NEG via statement tags. # No need for statements in_response_to = AFF/NEG In fact, it was causing # erroneous responses if statement.in_response_to in ['AFF', 'NEG']: statement.in_response_to = None statement.search_in_response_to = None statement.add_tags(*categories) if suggestion_tags: for suggestion in suggestion_tags: statement.add_tags('SUGGESTION:' + suggestion) if previous_statement_text: if previous_statement_text == 'AFF': statements_to_create[-2].add_tags('AFF:' + statement.text) elif previous_statement_text == 'NEG': statements_to_create[-2].add_tags('NEG:' + statement.text) statement = self.get_preprocessed_statement(statement) previous_statement_text = statement.text previous_statement_search_text = statement_search_text statements_to_create.append(statement) # Using update() because create_many() makes duplicate statements. AFF/NEG tag data was lost on some. for stmnts in statements_to_create: self.chatbot.storage.update(stmnts)