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 process_input(self, statement): new_message = False input_statement = self.context.get_last_input_statement() response_statement = self.context.get_last_response_statement() if input_statement: last_message_id = input_statement.extra_data.get("hipchat_message_id", None) if last_message_id: self.recent_message_ids.add(last_message_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 process_input(self, statement): new_message = False input_statement = self.chatbot.get_last_input_statement() response_statement = self.chatbot.get_last_response_statement() if input_statement: last_message_id = input_statement.extra_data.get( 'hipchat_message_id', None) if last_message_id: self.recent_message_ids.add(last_message_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 process_input(self, statement, conversation): """ Process input from the HipChat room. """ new_message = False conversation = self.chatbot.storage.filter(text=conversation, order_by=['id']) response_statement = conversation[-1] if conversation else None 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_or_create(self, text, ext_id): statement = self.storage.find_by_id(ext_id) if not statement: statement = Statement(text) statement.add_extra_data('ext_id', ext_id) return statement
def create_statement(data): text = data['text'] statement = Statement(text) statement.add_extra_data('ext_id', data['extra_data']['ext_id']) for response in data['in_response_to']: statement.add_response(Response(response['text'])) return statement
class StatementIntegrationTestCase(TestCase): """ Test case to make sure that the Django Statement model and ChatterBot Statement object have a common interface. """ def setUp(self): super(StatementIntegrationTestCase, self).setUp() from datetime import datetime from pytz import UTC now = datetime(2020, 2, 15, 3, 14, 10, 0, UTC) self.object = StatementObject(text='_', created_at=now) self.model = StatementModel(text='_', created_at=now) def test_text(self): self.assertTrue(hasattr(self.object, 'text')) self.assertTrue(hasattr(self.model, 'text')) def test_in_response_to(self): self.assertTrue(hasattr(self.object, 'in_response_to')) self.assertTrue(hasattr(self.model, 'in_response_to')) def test_extra_data(self): self.assertTrue(hasattr(self.object, 'extra_data')) self.assertTrue(hasattr(self.model, 'extra_data')) def test__str__(self): self.assertTrue(hasattr(self.object, '__str__')) self.assertTrue(hasattr(self.model, '__str__')) self.assertEqual(str(self.object), str(self.model)) def test_add_extra_data(self): self.object.add_extra_data('key', 'value') self.model.add_extra_data('key', 'value') def test_serialize(self): object_data = self.object.serialize() model_data = self.model.serialize() self.assertEqual(object_data, model_data)
def process_input(self, statement): """ Process input from the HipChat room. """ new_message = False input_statement = self.chatbot.conversation_sessions.get( self.session_id).conversation.get_last_input_statement() response_statement = self.chatbot.conversation_sessions.get( self.session_id).conversation.get_last_response_statement() if input_statement: last_message_id = input_statement.extra_data.get( 'hipchat_message_id', None ) if last_message_id: self.recent_message_ids.add(last_message_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 process_input(self, input_dict): st = Statement(input_dict['msg']) # 令牌 st.add_extra_data("token", input_dict['token']) # 分词 st.add_extra_data("statement_cut", [(t_word.word, t_word.flag) for t_word in pseg.lcut(input_dict['msg'])]) logging.debug(st.extra_data['statement_cut']) # 意图 log_intention = get_cache(input_dict['token'], 'intention') ai_intention = predict(input_dict['msg']) intention = ai_intention if log_intention is None else IntentionType( int(log_intention)) st.add_extra_data("statement_intention", intention) logging.debug( 'log_intention is: %s, ai_intention is: %s, the end intention is: %s', log_intention, ai_intention, st.extra_data['statement_intention']) # 记忆话题 if intention != IntentionType.Other: set_cache(input_dict['token'], 'intention', intention.value) return st
def process_input(self, *args, **kwargs): new_message = False result = None while not new_message: payload = self.sc.rtm_read() if payload: #print payload for item in payload: #{u'text': u'hi', u'ts': u'1472181456.000131', u'user': u'U027M0MDZ', u'team': u'T027EHDJB', u'type': u'message', u'channel': u'C03U0R1MB'} if "channel" in item and item["channel"] in self.monitor: if item["type"] == "message": if self.use_message(item) and "text" in item: input_text = item["text"] is_q = self.is_question(input_text) is_bot_ask = self.is_bot_ask(input_text) if is_bot_ask: input_text = input_text.replace(self.bot_name, "").strip() #Create the statement new_message = True result = Statement(input_text) result.add_extra_data("is_question", is_q) result.add_extra_data("is_bot_ask", is_bot_ask) result.add_extra_data("slack_channel", item["channel"]) result.add_extra_data("slack_team", item["team"]) result.add_extra_data("slack_user", item["user"]) result.add_extra_data("slack_time_stamp", item["ts"]) break elif item["type"] == "reaction_added": #{u'event_ts': u'1472227659.147767', u'item': {u'type': u'message', u'ts': u'1472227650.000003', u'channel': u'C2550PQD9'}, u'type': u'reaction_added', u'user': u'U027M0MDZ', u'reaction': u'+1'} sub_item = item["item"] new_message = False if sub_item["channel"] in self.monitor: #if we have a positive reaction, let's do something smart to save the message print item #print "q: {0} data: {1}".format(result, result.extra_data) return result
class ChatterBotResponseTests(ChatBotTestCase): def setUp(self): super(ChatterBotResponseTests, self).setUp() response_list = [Response("Hi")] self.test_statement = Statement("Hello", in_response_to=response_list) def test_empty_database(self): """ If there is no statements in the database, then the user's input is the only thing that can be returned. """ response = self.chatbot.get_response("How are you?") self.assertEqual("How are you?", response) def test_statement_saved_empty_database(self): """ Test that when database is empty, the first statement is saved and returned as a response. """ statement_text = "Wow!" response = self.chatbot.get_response(statement_text) saved_statement = self.chatbot.storage.find(statement_text) self.assertIsNotNone(saved_statement) self.assertEqual(response, statement_text) def test_statement_added_to_recent_response_list(self): """ An input statement should be added to the recent response list. """ statement_text = "Wow!" response = self.chatbot.get_response(statement_text) self.assertIn(statement_text, self.chatbot.recent_statements[0]) self.assertEqual(response, statement_text) def test_response_known(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response("Hi") self.assertEqual(response, self.test_statement.text) def test_response_format(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response("Hi") statement_object = self.chatbot.storage.find(response.text) self.assertEqual(response, self.test_statement.text) self.assertEqual(len(statement_object.in_response_to), 1) self.assertIn("Hi", statement_object.in_response_to) def test_second_response_format(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response("Hi") # response = "Hello" second_response = self.chatbot.get_response("How are you?") statement = self.chatbot.storage.find(second_response.text) # Make sure that the second response was saved to the database self.assertIsNotNone(self.chatbot.storage.find("How are you?")) self.assertEqual(second_response, self.test_statement.text) self.assertEqual(len(statement.in_response_to), 1) self.assertIn("Hi", statement.in_response_to) def test_response_extra_data(self): """ If an input statement has data contained in the `extra_data` attribute of a statement object, that data should saved with the input statement. """ self.test_statement.add_extra_data("test", 1) self.chatbot.get_response(self.test_statement) saved_statement = self.chatbot.storage.find(self.test_statement.text) self.assertIn("test", saved_statement.extra_data) self.assertEqual(1, saved_statement.extra_data["test"])
class ChatterBotResponseTestCase(ChatBotTestCase): def setUp(self): super(ChatterBotResponseTestCase, self).setUp() response_list = [Response('Hi')] self.test_statement = Statement('Hello', in_response_to=response_list) def test_empty_database(self): """ If there is no statements in the database, then the user's input is the only thing that can be returned. """ response = self.chatbot.get_response('How are you?') self.assertEqual('How are you?', response) def test_statement_saved_empty_database(self): """ Test that when database is empty, the first statement is saved and returned as a response. """ statement_text = 'Wow!' response = self.chatbot.get_response(statement_text) saved_statement = self.chatbot.storage.find(statement_text) self.assertIsNotNone(saved_statement) self.assertEqual(response, statement_text) def test_statement_added_to_recent_response_list(self): """ An input statement should be added to the recent response list. """ statement_text = 'Wow!' response = self.chatbot.get_response(statement_text) session = self.chatbot.conversation_sessions.get( self.chatbot.default_session.id_string) self.assertIn(statement_text, session.conversation[0]) self.assertEqual(response, statement_text) def test_response_known(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response('Hi') self.assertEqual(response, self.test_statement.text) def test_response_format(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response('Hi') statement_object = self.chatbot.storage.find(response.text) self.assertEqual(response, self.test_statement.text) self.assertIsLength(statement_object.in_response_to, 1) self.assertIn('Hi', statement_object.in_response_to) def test_second_response_format(self): self.chatbot.storage.update(self.test_statement) self.chatbot.get_response('Hi') # >>> 'Hello' second_response = self.chatbot.get_response('How are you?') statement = self.chatbot.storage.find(second_response.text) # Make sure that the second response was saved to the database self.assertIsNotNone(self.chatbot.storage.find('How are you?')) self.assertEqual(second_response, self.test_statement.text) self.assertIsLength(statement.in_response_to, 1) self.assertIn('Hi', statement.in_response_to) def test_get_response_unicode(self): """ Test the case that a unicode string is passed in. """ response = self.chatbot.get_response(u'سلام') self.assertGreater(len(response.text), 0) def test_get_response_emoji(self): """ Test the case that the input string contains an emoji. """ response = self.chatbot.get_response(u'💩 ') self.assertGreater(len(response.text), 0) def test_get_response_non_whitespace(self): """ Test the case that a non-whitespace C1 control string is passed in. """ response = self.chatbot.get_response(u'') self.assertGreater(len(response.text), 0) def test_get_response_two_byte_characters(self): """ Test the case that a string containing two-byte characters is passed in. """ response = self.chatbot.get_response(u'田中さんにあげて下さい') self.assertGreater(len(response.text), 0) def test_get_response_corrupted_text(self): """ Test the case that a string contains "corrupted" text. """ response = self.chatbot.get_response( u'Ṱ̺̺̕h̼͓̲̦̳̘̲e͇̣̰̦̬͎ ̢̼̻̱̘h͚͎͙̜̣̲ͅi̦̲̣̰̤v̻͍e̺̭̳̪̰-m̢iͅn̖̺̞̲̯̰d̵̼̟͙̩̼̘̳.̨̹͈̣' ) self.assertGreater(len(response.text), 0) def test_response_extra_data(self): """ If an input statement has data contained in the `extra_data` attribute of a statement object, that data should saved with the input statement. """ self.test_statement.add_extra_data('test', 1) self.chatbot.get_response(self.test_statement) saved_statement = self.chatbot.storage.find(self.test_statement.text) self.assertIn('test', saved_statement.extra_data) self.assertEqual(1, saved_statement.extra_data['test']) def test_generate_response(self): statement = Statement( 'Many insects adopt a tripedal gait for rapid yet stable walking.') input_statement, response = self.chatbot.generate_response( statement, self.chatbot.default_session.id) self.assertEqual(input_statement, statement) self.assertEqual(response, statement) self.assertEqual(response.confidence, 1) def test_learn_response(self): previous_response = Statement('Define Hemoglobin.') statement = Statement( 'Hemoglobin is an oxygen-transport metalloprotein.') self.chatbot.learn_response(statement, previous_response) exists = self.chatbot.storage.find(statement.text) self.assertIsNotNone(exists) def test_update_does_not_add_new_statement(self): """ Test that a new statement is not learned if `read_only` is set to True. """ self.chatbot.read_only = True self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response('Hi!') statement_found = self.chatbot.storage.find('Hi!') self.assertEqual(response, self.test_statement.text) self.assertIsNone(statement_found)
def get(self, text, statement_list=None): statement = Statement("Hello") statement.add_extra_data("pos_tags", "NN") return 1, statement
class ChatterBotResponseTests(ChatBotTestCase): def setUp(self): super(ChatterBotResponseTests, self).setUp() response_list = [Response("Hi")] self.test_statement = Statement("Hello", in_response_to=response_list) def test_empty_database(self): """ If there is no statements in the database, then the user's input is the only thing that can be returned. """ response = self.chatbot.get_response("How are you?") self.assertEqual("How are you?", response) def test_statement_saved_empty_database(self): """ Test that when database is empty, the first statement is saved and returned as a response. """ statement_text = "Wow!" response = self.chatbot.get_response(statement_text) saved_statement = self.chatbot.storage.find(statement_text) self.assertIsNotNone(saved_statement) self.assertEqual(response, statement_text) def test_statement_added_to_recent_response_list(self): """ An input statement should be added to the recent response list. """ statement_text = "Wow!" response = self.chatbot.get_response(statement_text) self.assertIn(statement_text, self.chatbot.recent_statements[0]) self.assertEqual(response, statement_text) def test_response_known(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response("Hi") self.assertEqual(response, self.test_statement.text) def test_response_format(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response("Hi") statement_object = self.chatbot.storage.find(response.text) self.assertEqual(response, self.test_statement.text) self.assertEqual(len(statement_object.in_response_to), 1) self.assertIn("Hi", statement_object.in_response_to) def test_second_response_format(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response("Hi") # response = "Hello" second_response = self.chatbot.get_response("How are you?") statement = self.chatbot.storage.find(second_response.text) # Make sure that the second response was saved to the database self.assertIsNotNone(self.chatbot.storage.find("How are you?")) self.assertEqual(second_response, self.test_statement.text) self.assertEqual(len(statement.in_response_to), 1) self.assertIn("Hi", statement.in_response_to) def test_get_response_unicode(self): """ Test the case that a unicode string is passed in. """ response = self.chatbot.get_response(u'سلام') self.assertGreater(len(response.text), 0) def test_response_extra_data(self): """ If an input statement has data contained in the `extra_data` attribute of a statement object, that data should saved with the input statement. """ self.test_statement.add_extra_data("test", 1) self.chatbot.get_response(self.test_statement) saved_statement = self.chatbot.storage.find(self.test_statement.text) self.assertIn("test", saved_statement.extra_data) self.assertEqual(1, saved_statement.extra_data["test"]) def test_generate_response(self): statement = Statement( 'Many insects adopt a tripedal gait for rapid yet stable walking.') input_statement, response, confidence = self.chatbot.generate_response( statement) self.assertEqual(input_statement, statement) self.assertEqual(response, statement) self.assertEqual(confidence, 1) def test_learn_response(self): statement = Statement( 'Hemoglobin is an oxygen-transport metalloprotein.') self.chatbot.learn_response(statement) exists = self.chatbot.storage.find(statement.text) self.assertIsNotNone(exists)
class StatementIntegrationTestCase(TestCase): """ Test case to make sure that the Django Statement model and ChatterBot Statement object have a common interface. """ def setUp(self): super(StatementIntegrationTestCase, self).setUp() self.object = StatementObject(text='_') self.model = StatementModel(text='_') def test_text(self): self.assertTrue(hasattr(self.object, 'text')) self.assertTrue(hasattr(self.model, 'text')) def test_in_response_to(self): self.assertTrue(hasattr(self.object, 'in_response_to')) self.assertTrue(hasattr(self.model, 'in_response_to')) def test_extra_data(self): self.assertTrue(hasattr(self.object, 'extra_data')) self.assertTrue(hasattr(self.model, 'extra_data')) def test__str__(self): self.assertTrue(hasattr(self.object, '__str__')) self.assertTrue(hasattr(self.model, '__str__')) self.assertEqual(str(self.object), str(self.model)) def test_add_extra_data(self): self.object.add_extra_data('key', 'value') self.model.add_extra_data('key', 'value') def test_add_response(self): self.assertTrue(hasattr(self.object, 'add_response')) self.assertTrue(hasattr(self.model, 'add_response')) def test_remove_response(self): self.object.add_response(ResponseObject('Hello')) model_response_statement = StatementModel.objects.create(text='Hello') self.model.save() self.model.in_response.create(statement=self.model, response=model_response_statement) object_removed = self.object.remove_response('Hello') model_removed = self.model.remove_response('Hello') self.assertTrue(object_removed) self.assertTrue(model_removed) def test_get_response_count(self): self.object.add_response(ResponseObject('Hello', occurrence=2)) model_response_statement = StatementModel.objects.create(text='Hello') self.model.save() ResponseModel.objects.create( statement=self.model, response=model_response_statement ) ResponseModel.objects.create( statement=self.model, response=model_response_statement ) object_count = self.object.get_response_count(StatementObject(text='Hello')) model_count = self.model.get_response_count(StatementModel(text='Hello')) self.assertEqual(object_count, 2) self.assertEqual(model_count, 2) def test_serialize(self): object_data = self.object.serialize() model_data = self.model.serialize() self.assertEqual(object_data, model_data) def test_response_statement_cache(self): self.assertTrue(hasattr(self.object, 'response_statement_cache')) self.assertTrue(hasattr(self.model, 'response_statement_cache'))
class ChatterBotResponseTests(ChatBotTestCase): def setUp(self): super(ChatterBotResponseTests, self).setUp() response_list = [ Response("Hi") ] self.test_statement = Statement("Hello", in_response_to=response_list) def test_empty_database(self): """ If there is no statements in the database, then the user's input is the only thing that can be returned. """ response = self.chatbot.get_response("How are you?") self.assertEqual("How are you?", response) def test_statement_saved_empty_database(self): """ Test that when database is empty, the first statement is saved and returned as a response. """ statement_text = "Wow!" response = self.chatbot.get_response(statement_text) saved_statement = self.chatbot.storage.find(statement_text) self.assertIsNotNone(saved_statement) self.assertEqual(response, statement_text) def test_statement_added_to_recent_response_list(self): """ An input statement should be added to the recent response list. """ statement_text = "Wow!" response = self.chatbot.get_response(statement_text) self.assertIn(statement_text, self.chatbot.recent_statements[0]) self.assertEqual(response, statement_text) def test_response_known(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response("Hi") self.assertEqual(response, self.test_statement.text) def test_response_format(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response("Hi") statement_object = self.chatbot.storage.find(response.text) self.assertEqual(response, self.test_statement.text) self.assertEqual(len(statement_object.in_response_to), 1) self.assertIn("Hi", statement_object.in_response_to) def test_second_response_format(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response("Hi") # response = "Hello" second_response = self.chatbot.get_response("How are you?") statement = self.chatbot.storage.find(second_response.text) # Make sure that the second response was saved to the database self.assertIsNotNone(self.chatbot.storage.find("How are you?")) self.assertEqual(second_response, self.test_statement.text) self.assertEqual(len(statement.in_response_to), 1) self.assertIn("Hi", statement.in_response_to) def test_response_extra_data(self): """ If an input statement has data contained in the `extra_data` attribute of a statement object, that data should saved with the input statement. """ self.test_statement.add_extra_data("test", 1) self.chatbot.get_response( self.test_statement ) saved_statement = self.chatbot.storage.find( self.test_statement.text ) self.assertIn("test", saved_statement.extra_data) self.assertEqual(1, saved_statement.extra_data["test"]) def test_generate_response(self): statement = Statement('Many insects adopt a tripedal gait for rapid yet stable walking.') input_statement, response, confidence = self.chatbot.generate_response(statement) self.assertEqual(input_statement, statement) self.assertEqual(response, statement) self.assertEqual(confidence, 1) def test_learn_response(self): statement = Statement('Hemoglobin is an oxygen-transport metalloprotein.') self.chatbot.learn_response(statement) exists = self.chatbot.storage.find(statement.text) self.assertIsNotNone(exists)
def get(self, text, statement_list, current_conversation): statement = Statement("Hello") statement.add_extra_data("pos_tags", "NN") return statement
class ChatterBotResponseTestCase(ChatBotTestCase): def setUp(self): super(ChatterBotResponseTestCase, self).setUp() response_list = [ Response('Hi') ] self.test_statement = Statement('Hello', in_response_to=response_list) def test_empty_database(self): """ If there is no statements in the database, then the user's input is the only thing that can be returned. """ response = self.chatbot.get_response('How are you?') self.assertEqual('How are you?', response) def test_statement_saved_empty_database(self): """ Test that when database is empty, the first statement is saved and returned as a response. """ statement_text = 'Wow!' response = self.chatbot.get_response(statement_text) saved_statement = self.chatbot.storage.find(statement_text) self.assertIsNotNone(saved_statement) self.assertEqual(response, statement_text) def test_statement_added_to_recent_response_list(self): """ An input statement should be added to the recent response list. """ statement_text = 'Wow!' response = self.chatbot.get_response(statement_text) session = self.chatbot.conversation_sessions.get( self.chatbot.default_session.id_string ) self.assertIn(statement_text, session.conversation[0]) self.assertEqual(response, statement_text) def test_response_known(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response('Hi') self.assertEqual(response, self.test_statement.text) def test_response_format(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response('Hi') statement_object = self.chatbot.storage.find(response.text) self.assertEqual(response, self.test_statement.text) self.assertIsLength(statement_object.in_response_to, 1) self.assertIn('Hi', statement_object.in_response_to) def test_second_response_format(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response('Hi') # response = 'Hello' second_response = self.chatbot.get_response('How are you?') statement = self.chatbot.storage.find(second_response.text) # Make sure that the second response was saved to the database self.assertIsNotNone(self.chatbot.storage.find('How are you?')) self.assertEqual(second_response, self.test_statement.text) self.assertIsLength(statement.in_response_to, 1) self.assertIn('Hi', statement.in_response_to) def test_get_response_unicode(self): """ Test the case that a unicode string is passed in. """ response = self.chatbot.get_response(u'سلام') self.assertGreater(len(response.text), 0) def test_response_extra_data(self): """ If an input statement has data contained in the `extra_data` attribute of a statement object, that data should saved with the input statement. """ self.test_statement.add_extra_data('test', 1) self.chatbot.get_response( self.test_statement ) saved_statement = self.chatbot.storage.find( self.test_statement.text ) self.assertIn('test', saved_statement.extra_data) self.assertEqual(1, saved_statement.extra_data['test']) def test_generate_response(self): statement = Statement('Many insects adopt a tripedal gait for rapid yet stable walking.') input_statement, response, confidence = self.chatbot.generate_response(statement) self.assertEqual(input_statement, statement) self.assertEqual(response, statement) self.assertEqual(confidence, 1) def test_learn_response(self): previous_response = Statement('Define Hemoglobin.') statement = Statement('Hemoglobin is an oxygen-transport metalloprotein.') self.chatbot.learn_response(statement, previous_response) exists = self.chatbot.storage.find(statement.text) self.assertIsNotNone(exists) def test_update_does_not_add_new_statement(self): """ Test that a new statement is not learned if `read_only` is set to True. """ self.chatbot.read_only = True self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response('Hi!') statement_found = self.chatbot.storage.find('Hi!') self.assertEqual(response, self.test_statement.text) self.assertIsNone(statement_found)
class ChatterBotResponseTestCase(ChatBotTestCase): def setUp(self): super(ChatterBotResponseTestCase, self).setUp() response_list = [ Response('Hi') ] self.test_statement = Statement('Hello', in_response_to=response_list) def test_empty_database(self): """ If there is no statements in the database, then the user's input is the only thing that can be returned. """ response = self.chatbot.get_response('How are you?') self.assertEqual('How are you?', response) def test_statement_saved_empty_database(self): """ Test that when database is empty, the first statement is saved and returned as a response. """ statement_text = 'Wow!' response = self.chatbot.get_response(statement_text) saved_statement = self.chatbot.storage.find(statement_text) self.assertIsNotNone(saved_statement) self.assertEqual(response, statement_text) def test_statement_added_to_recent_response_list(self): """ An input statement should be added to the recent response list. """ statement_text = 'Wow!' response = self.chatbot.get_response(statement_text) response_statement = self.chatbot.storage.get_latest_response( self.chatbot.default_conversation_id ) self.assertEqual(statement_text, response_statement.text) self.assertEqual(response, statement_text) def test_response_known(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response('Hi') self.assertEqual(response, self.test_statement.text) def test_response_format(self): self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response('Hi') statement_object = self.chatbot.storage.find(response.text) self.assertEqual(response, self.test_statement.text) self.assertIsLength(statement_object.in_response_to, 1) self.assertIn('Hi', statement_object.in_response_to) def test_second_response_format(self): self.chatbot.storage.update(self.test_statement) self.chatbot.get_response('Hi') # >>> 'Hello' second_response = self.chatbot.get_response('How are you?') statement = self.chatbot.storage.find(second_response.text) # Make sure that the second response was saved to the database self.assertIsNotNone(self.chatbot.storage.find('How are you?')) self.assertEqual(second_response, self.test_statement.text) self.assertIsLength(statement.in_response_to, 1) self.assertIn('Hi', statement.in_response_to) def test_get_response_unicode(self): """ Test the case that a unicode string is passed in. """ response = self.chatbot.get_response(u'سلام') self.assertGreater(len(response.text), 0) def test_get_response_emoji(self): """ Test the case that the input string contains an emoji. """ response = self.chatbot.get_response(u'💩 ') self.assertGreater(len(response.text), 0) def test_get_response_non_whitespace(self): """ Test the case that a non-whitespace C1 control string is passed in. """ response = self.chatbot.get_response(u'') self.assertGreater(len(response.text), 0) def test_get_response_two_byte_characters(self): """ Test the case that a string containing two-byte characters is passed in. """ response = self.chatbot.get_response(u'田中さんにあげて下さい') self.assertGreater(len(response.text), 0) def test_get_response_corrupted_text(self): """ Test the case that a string contains "corrupted" text. """ response = self.chatbot.get_response(u'Ṱ̺̺̕h̼͓̲̦̳̘̲e͇̣̰̦̬͎ ̢̼̻̱̘h͚͎͙̜̣̲ͅi̦̲̣̰̤v̻͍e̺̭̳̪̰-m̢iͅn̖̺̞̲̯̰d̵̼̟͙̩̼̘̳.̨̹͈̣') self.assertGreater(len(response.text), 0) def test_response_extra_data(self): """ If an input statement has data contained in the `extra_data` attribute of a statement object, that data should saved with the input statement. """ self.test_statement.add_extra_data('test', 1) self.chatbot.get_response( self.test_statement ) saved_statement = self.chatbot.storage.find( self.test_statement.text ) self.assertIn('test', saved_statement.extra_data) self.assertEqual(1, saved_statement.extra_data['test']) def test_generate_response(self): statement = Statement('Many insects adopt a tripedal gait for rapid yet stable walking.') input_statement, response = self.chatbot.generate_response( statement, self.chatbot.default_conversation_id ) self.assertEqual(input_statement, statement) self.assertEqual(response, statement) self.assertEqual(response.confidence, 1) def test_learn_response(self): previous_response = Statement('Define Hemoglobin.') statement = Statement('Hemoglobin is an oxygen-transport metalloprotein.') self.chatbot.learn_response(statement, previous_response) exists = self.chatbot.storage.find(statement.text) self.assertIsNotNone(exists) def test_update_does_not_add_new_statement(self): """ Test that a new statement is not learned if `read_only` is set to True. """ self.chatbot.read_only = True self.chatbot.storage.update(self.test_statement) response = self.chatbot.get_response('Hi!') statement_found = self.chatbot.storage.find('Hi!') self.assertEqual(response, self.test_statement.text) self.assertIsNone(statement_found)