当前位置: 首页>>代码示例>>Python>>正文


Python Statement.add_extra_data方法代码示例

本文整理汇总了Python中chatterbot.conversation.Statement.add_extra_data方法的典型用法代码示例。如果您正苦于以下问题:Python Statement.add_extra_data方法的具体用法?Python Statement.add_extra_data怎么用?Python Statement.add_extra_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chatterbot.conversation.Statement的用法示例。


在下文中一共展示了Statement.add_extra_data方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: process_input

# 需要导入模块: from chatterbot.conversation import Statement [as 别名]
# 或者: from chatterbot.conversation.Statement import add_extra_data [as 别名]
    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
开发者ID:AugustoQueiroz,项目名称:ChatterBot,代码行数:35,代码来源:hipchat.py

示例2: process_input

# 需要导入模块: from chatterbot.conversation import Statement [as 别名]
# 或者: from chatterbot.conversation.Statement import add_extra_data [as 别名]
    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
开发者ID:Gustavo6046,项目名称:ChatterBot,代码行数:43,代码来源:hipchat.py

示例3: StatementIntegrationTestCase

# 需要导入模块: from chatterbot.conversation import Statement [as 别名]
# 或者: from chatterbot.conversation.Statement import add_extra_data [as 别名]
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'))
开发者ID:runciters,项目名称:ChatterBot,代码行数:77,代码来源:test_statement_integration.py

示例4: ChatterBotResponseTests

# 需要导入模块: from chatterbot.conversation import Statement [as 别名]
# 或者: from chatterbot.conversation.Statement import add_extra_data [as 别名]
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)
#.........这里部分代码省略.........
开发者ID:totalgood,项目名称:ChatterBot,代码行数:103,代码来源:test_chatbot.py

示例5: get

# 需要导入模块: from chatterbot.conversation import Statement [as 别名]
# 或者: from chatterbot.conversation.Statement import add_extra_data [as 别名]
    def get(self, text, statement_list, current_conversation):
        statement = Statement("Hello")
        statement.add_extra_data("pos_tags", "NN")

        return statement
开发者ID:copyfun,项目名称:ChatterBot,代码行数:7,代码来源:test_data_cache.py

示例6: ChatterBotResponseTestCase

# 需要导入模块: from chatterbot.conversation import Statement [as 别名]
# 或者: from chatterbot.conversation.Statement import add_extra_data [as 别名]

#.........这里部分代码省略.........

    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)
开发者ID:runciters,项目名称:ChatterBot,代码行数:104,代码来源:test_chatbot.py

示例7: ChatterBotResponseTestCase

# 需要导入模块: from chatterbot.conversation import Statement [as 别名]
# 或者: from chatterbot.conversation.Statement import add_extra_data [as 别名]
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
        )

#.........这里部分代码省略.........
开发者ID:HarshGrandeur,项目名称:ChatterBot,代码行数:103,代码来源:test_chatbot.py

示例8: get

# 需要导入模块: from chatterbot.conversation import Statement [as 别名]
# 或者: from chatterbot.conversation.Statement import add_extra_data [as 别名]
    def get(self, text, statement_list=None):
        statement = Statement("Hello")
        statement.add_extra_data("pos_tags", "NN")

        return 1, statement
开发者ID:joab40,项目名称:ChatterBot,代码行数:7,代码来源:test_data_cache.py


注:本文中的chatterbot.conversation.Statement.add_extra_data方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。