def process_input(self, statement): """ Process input from the HipChat room. """ new_message = False response_statement = self.chatbot.storage.get_latest_response( self.session_id) if response_statement: last_message_id = response_statement.extra_data.get( 'hipchat_message_id', None) if last_message_id: self.recent_message_ids.add(last_message_id) while not new_message: data = self.get_most_recent_message(self.hipchat_room) if data and data['id'] not in self.recent_message_ids: self.recent_message_ids.add(data['id']) new_message = True else: pass sleep(3.5) text = data['message'] statement = Statement(text) statement.add_extra_data('hipchat_message_id', data['id']) return statement
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.logger.info( u'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(tweet.text) if tweet.in_reply_to_status_id: try: status = self.api.GetStatus(tweet.in_reply_to_status_id) statement.add_response(Response(status.text)) statements.append(statement) except TwitterError as error: self.logger.warning(str(error)) self.logger.info('Adding {} tweets with responses'.format( len(statements))) return statements
def process(self, statement): """ Takes a statement string. Returns the equation from the statement with the mathematical terms solved. """ from mathparse import mathparse input_text = statement.text # Use the result cached by the process method if it exists if input_text in self.cache: cached_result = self.cache[input_text] self.cache = {} return cached_result # Getting the mathematical terms within the input statement expression = mathparse.extract_expression(input_text, language=self.language) response = Statement(text=expression) try: response.text += ' = ' + str( mathparse.parse(expression, language=self.language)) # The confidence is 1 if the expression could be evaluated response.confidence = 1 except mathparse.PostfixTokenEvaluationException: response.confidence = 0 return response
def process(self, statement): from chatter.chatterbot.conversation import Statement now = datetime.now() time_features = self.time_question_features(statement.text.lower()) confidence = self.classifier.classify(time_features) response = Statement('The current time is ' + now.strftime('%I:%M %p')) response.confidence = confidence return response
def get_statement(self): from chatter.chatterbot.conversation import Statement as StatementObject from chatter.chatterbot.conversation import Response as ResponseObject statement = StatementObject(self.text, tags=[tag.name for tag in self.tags], extra_data=self.extra_data) for response in self.in_response_to: statement.add_response( ResponseObject(text=response.text, occurrence=response.occurrence)) return statement
def get_or_create(self, statement_text): """ Return a statement if it exists. Create and return the statement if it does not exist. """ temp_statement = self.get_preprocessed_statement( Statement(text=statement_text)) statement = self.storage.find(temp_statement.text) if not statement: statement = Statement(temp_statement.text) return statement
def deserialize_responses(self, response_list): """ Takes the list of response items and returns the list converted to Response objects. """ Statement = self.get_model('statement') Response = self.get_model('response') proxy_statement = Statement('') for response in response_list: text = response['text'] del response['text'] proxy_statement.add_response(Response(text, **response)) return proxy_statement.in_response_to
def __init__(self, **kwargs): super(SpecificResponseAdapter, self).__init__(**kwargs) from chatter.chatterbot.conversation import Statement self.input_text = kwargs.get('input_text') output_text = kwargs.get('output_text') self.response_statement = Statement(output_text)
def process_input(self, statement): input_type = self.detect_type(statement) # Return the statement object without modification if input_type == self.OBJECT: return statement # Convert the input string into a statement object if input_type == self.TEXT: return Statement(statement) # Convert input dictionary into a statement object if input_type == self.JSON: input_json = dict(statement) text = input_json['text'] del input_json['text'] return Statement(text, **input_json)
def process_input(self, statement): urls = self.get_stored_email_urls() url = list(urls)[0] response = self.get_message(url) message = response.json() text = message.get('stripped-text') return Statement(text)
def mongo_to_object(self, statement_data): """ Return Statement object when given data returned from Mongo DB. """ Statement = self.get_model('statement') statement_text = statement_data['text'] del statement_data['text'] statement_data['in_response_to'] = self.deserialize_responses( statement_data.get('in_response_to', [])) return Statement(statement_text, **statement_data)
def process_input(self, statement): new_message = False while not new_message: data = self.get_most_recent_message() if self.should_respond(data): self.mark_messages_as_read([data['id']]) new_message = True sleep(self.sleep_time) text = self.remove_mentions(data['text']) statement = Statement(text) return statement
def __init__(self, **kwargs): super(LowConfidenceAdapter, self).__init__(**kwargs) self.confidence_threshold = kwargs.get('threshold', 0.65) default_responses = kwargs.get('default_response', "I'm sorry, I do not understand.") # Convert a single string into a list if isinstance(default_responses, str): default_responses = [default_responses] self.default_responses = [ Statement(text=default) for default in default_responses ]
def find(self, statement_text): Statement = self.get_model('statement') query = self.base_query.statement_text_equals(statement_text) values = self.statements.find_one(query.value()) if not values: return None del values['text'] # Build the objects for the response list values['in_response_to'] = self.deserialize_responses( values.get('in_response_to', [])) return Statement(statement_text, **values)
def process_input(self, statement): new_message = False data = None while not new_message: data = self.get_most_recent_message() if data and data['id']: new_message = True else: pass sleep(3.5) text = data['text'] statement = Statement(text) self.logger.info('processing user statement {}'.format(statement)) return statement
def process_input(self, *args, **kwargs): """ Read the user's input from the terminal. """ user_input = input_function() return Statement(user_input)