예제 #1
0
    def get_statement(self):
        from chatterbot.conversation import Statement as StatementObject

        statement = StatementObject(self.text, extra_data=self.extra_data)
        for response in self.in_response_to:
            statement.add_response(response.get_response())
        return statement
    def test_getting_and_updating_statement(self):
        statement = Statement("Hi")
        self.adapter.update(statement)

        statement.add_response(Response("Hello"))
        statement.add_response(Response("Hello"))
        self.adapter.update(statement)

        response = self.adapter.find(statement.text)

        self.assertEqual(len(response.in_response_to), 1)
        self.assertEqual(response.in_response_to[0].occurrence, 2)
예제 #3
0
    def test_update_does_not_modify_existing_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        self.adapter.read_only = True

        statement.add_response(Response("New response"))
        self.adapter.update(statement)

        statement_found = self.adapter.find("New statement")
        self.assertEqual(statement_found.text, statement.text)
        self.assertEqual(len(statement_found.in_response_to), 0)
    def model_to_object(self, statement_model):
        """
        Convert a Django model object into a ChatterBot Statement object.
        """
        statement = Statement(statement_model.text)

        for response_object in statement_model.in_response_to.all():
            statement.add_response(Response(
                response_object.response.text,
                occurrence=response_object.occurrence
            ))

        return statement
예제 #5
0
    def deserialize_responses(self, response_list):
        """
        Takes the list of response items and returns
        the list converted to Response objects.
        """
        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
예제 #6
0
    def model_to_object(self, statement_model):
        """
        Convert a Django model object into a ChatterBot Statement object.
        """
        statement = Statement(statement_model.text,
                              extra_data=json.loads(statement_model.extra_data,
                                                    encoding='utf8'))

        for response_object in statement_model.in_response_to.all():
            statement.add_response(
                Response(response_object.response.text,
                         occurrence=response_object.occurrence))

        return statement
예제 #7
0
파일: models.py 프로젝트: gabev/ChatterBot
    def get_statement(self):
        from chatterbot.conversation import Statement as StatementObject
        from 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 test_update_modifies_existing_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        # Check the initial values
        found_statement = self.adapter.find(statement.text)
        self.assertEqual(len(statement.in_response_to), 0)

        # Update the statement value
        statement.add_response(Statement("A response"))
        self.adapter.update(statement)

        # Check that the values have changed
        found_statement = self.adapter.find(statement.text)
        self.assertEqual(len(found_statement.in_response_to), 1)
예제 #9
0
    def deserialize_responses(self, response_list):
        """
        Takes the list of response items and returns
        the list converted to Response objects.
        """
        proxy_statement = Statement('')

        for response in response_list:
            data = response.copy()
            text = data['text']
            del (data['text'])

            proxy_statement.add_response(Response(text, **data))

        return proxy_statement.in_response_to
예제 #10
0
    def test_update_modifies_existing_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        # Check the initial values
        found_statement = self.adapter.find(statement.text)
        self.assertEqual(len(found_statement.in_response_to), 0)

        # Update the statement value
        statement.add_response(Response("New response"))
        self.adapter.update(statement)

        # Check that the values have changed
        found_statement = self.adapter.find(statement.text)
        self.assertEqual(len(found_statement.in_response_to), 1)
예제 #11
0
    def deserialize_responses(self, response_list):
        """
        Takes the list of response items and returns
        the list converted to Response objects.
        """
        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
예제 #12
0
    def model_to_object(self, statement_model):
        """
        Convert a Django model object into a ChatterBot Statement object.
        """
        statement = Statement(
            statement_model.text,
            extra_data=json.loads(statement_model.extra_data, encoding='utf8')
        )

        for response_object in statement_model.in_response_to.all():
            statement.add_response(Response(
                response_object.response.text,
                occurrence=response_object.occurrence
            ))

        return statement
예제 #13
0
    def test_update_does_not_modify_existing_statement(self):
        statement = Statement("New statement")
        self.adapter.update(statement)

        self.adapter.read_only = True

        statement.add_response(
            Response("New response")
        )

        self.adapter.update(statement)

        statement_found = self.adapter.find("New statement")
        self.assertEqual(statement_found.text, statement.text)
        self.assertEqual(
            len(statement_found.in_response_to), 0
        )
예제 #14
0
    def deserialize_responses(self, response_list):
        """
        Takes the list of response items and returns
        the list converted to Response objects.
        """
        proxy_statement = Statement('')

        for response in response_list:
            data = response.copy()
            text = data['text']
            del(data['text'])

            proxy_statement.add_response(
                Response(text, **data)
            )

        return proxy_statement.in_response_to
예제 #15
0
    def train(self, conversation):
        statement_history = []

        for text in conversation:
            statement = self.storage.find(text)

            # Create the statement if a match was not found
            if not statement:
                statement = Statement(text)

            previous_statement = None
            if statement_history:
                previous_statement = statement_history[-1]

            if previous_statement:
                statement.add_response(previous_statement)

            statement_history.append(statement)
            self.storage.update(statement)
예제 #16
0
    def train(self, conversation):
        statement_history = []

        for text in conversation:
            statement = self.storage.find(text)

            # Create the statement if a match was not found
            if not statement:
                statement = Statement(text)

            previous_statement = None
            if statement_history:
                previous_statement = statement_history[-1]

            if previous_statement:
                statement.add_response(previous_statement)

            statement_history.append(statement)
            self.storage.update(statement)
예제 #17
0
class StatementTests(TestCase):

    def setUp(self):
        self.statement = Statement("A test statement.")

    def test_equality(self):
        """
        It should be possible to check if a statement
        exists in the list of statements that another
        statement has been issued in response to.
        """
        self.statement.add_response(Statement("Yo"))
        self.assertEqual(len(self.statement.in_response_to), 1)
        self.assertIn(
            Statement("Yo"),
            self.statement.in_response_to
        )

    def test_occurrence_count(self):
        self.statement.update_occurrence_count()
        self.assertTrue(
            self.statement.get_occurrence_count(),
            2
        )

    def test_update_response_list_new(self):
        new_statement = Statement("Hello")
        self.statement.add_response(new_statement)
        self.assertTrue(
            len(self.statement.in_response_to),
            1
        )

    def test_update_response_list_existing(self):
        previous_statement = Statement("Hello")
        self.statement.add_response(previous_statement)
        self.statement.add_response(previous_statement)
        self.assertTrue(
            len(self.statement.in_response_to),
            1
        )

    def test_add_signature(self):
        signature = Signature("Gunther Cox")
        self.statement.add_signature(signature)
        self.assertIn(signature, self.statement.signatures)

    def test_serializer(self):
        data = self.statement.serialize()
        self.assertEqual(self.statement.text, data["text"])
예제 #18
0
class StatementTests(TestCase):
    def setUp(self):
        self.statement = Statement("A test statement.")

    def test_equality(self):
        """
        It should be possible to check if a statement
        exists in the list of statements that another
        statement has been issued in response to.
        """
        self.statement.add_response(Statement("Yo"))
        self.assertEqual(len(self.statement.in_response_to), 1)
        self.assertIn(Statement("Yo"), self.statement.in_response_to)

    def test_occurrence_count(self):
        self.statement.update_occurrence_count()
        self.assertTrue(self.statement.get_occurrence_count(), 2)

    def test_update_response_list_new(self):
        new_statement = Statement("Hello")
        self.statement.add_response(new_statement)
        self.assertTrue(len(self.statement.in_response_to), 1)

    def test_update_response_list_existing(self):
        previous_statement = Statement("Hello")
        self.statement.add_response(previous_statement)
        self.statement.add_response(previous_statement)
        self.assertTrue(len(self.statement.in_response_to), 1)

    def test_add_signature(self):
        signature = Signature("Gunther Cox")
        self.statement.add_signature(signature)
        self.assertIn(signature, self.statement.signatures)

    def test_serializer(self):
        data = self.statement.serialize()
        self.assertEqual(self.statement.text, data["text"])
예제 #19
0
 def get_statement(self):
     statement = Statement(self.text, extra_data=self.extra_data)
     for response in self.in_response_to:
         statement.add_response(response.get_response())
     return statement
예제 #20
0
class StatementTests(TestCase):

    def setUp(self):
        self.statement = Statement("A test statement.")

    def test_equality(self):
        """
        It should be possible to check if a statement
        exists in the list of statements that another
        statement has been issued in response to.
        """
        self.statement.add_response(Response("Yo"))
        self.assertEqual(len(self.statement.in_response_to), 1)
        self.assertIn(Response("Yo"), self.statement.in_response_to)

    def test_update_response_list_new(self):
        self.statement.add_response(Response("Hello"))
        self.assertTrue(len(self.statement.in_response_to), 1)

    def test_update_response_list_existing(self):
        response = Response("Hello")
        self.statement.add_response(response)
        self.statement.add_response(response)
        self.assertTrue(len(self.statement.in_response_to), 1)

    def test_remove_response_exists(self):
        self.statement.add_response(Response("Testing"))
        removed = self.statement.remove_response("Testing")
        self.assertTrue(removed)

    def test_remove_response_does_not_exist(self):
        self.statement.add_response(Response("Testing"))
        removed = self.statement.remove_response("Test")
        self.assertFalse(removed)

    def test_serializer(self):
        data = self.statement.serialize()
        self.assertEqual(self.statement.text, data["text"])

    def test_occurrence_count_for_new_statement(self):
        """
        When the occurrence is updated for a statement that
        previously did not exist as a statement that the current
        statement was issued in response to, then the new statement
        should be added to the response list and the occurence count
        for that response should be set to 1.
        """
        response = Response("This is a test.")

        self.statement.add_response(response)
        self.assertTrue(self.statement.get_response_count(response), 1)

    def test_occurrence_count_for_existing_statement(self):
        self.statement.add_response(Response("ABC"))
        self.statement.add_response(Response("ABC"))
        self.assertTrue(
            self.statement.get_response_count(Response("ABC")),
            2
        )

    def test_occurrence_count_incremented(self):
        self.statement.add_response(Response("ABC"))
        self.statement.add_response(Response("ABC"))

        self.assertEqual(len(self.statement.in_response_to), 1)
        self.assertEqual(self.statement.in_response_to[0].occurrence, 2)
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'))
예제 #22
0
class StatementTests(TestCase):

    def setUp(self):
        self.statement = Statement("A test statement.")

    def test_equality(self):
        """
        It should be possible to check if a statement
        exists in the list of statements that another
        statement has been issued in response to.
        """
        self.statement.add_response(Statement("Yo"))
        self.assertEqual(len(self.statement.in_response_to), 1)
        self.assertIn(
            Statement("Yo"),
            self.statement.in_response_to
        )

    def test_update_response_list_new(self):
        new_statement = Statement("Hello")
        self.statement.add_response(new_statement)
        self.assertTrue(
            len(self.statement.in_response_to),
            1
        )

    def test_update_response_list_existing(self):
        previous_statement = Statement("Hello")
        self.statement.add_response(previous_statement)
        self.statement.add_response(previous_statement)
        self.assertTrue(
            len(self.statement.in_response_to),
            1
        )

    def test_serializer(self):
        data = self.statement.serialize()
        self.assertEqual(self.statement.text, data["text"])

    def test_occurrence_count_for_new_statement(self):
        """
        When the occurrence is updated for a statement that
        previously did not exist as a statement that the current
        statement was issued in response to, then the new statement
        should be added to the response list and the occurence count
        for that response should be set to 1.
        """
        statement = Statement("This is a test.")

        self.statement.add_response(statement)
        self.assertTrue(
            self.statement.get_response_count(statement),
            1
        )

    def test_occurrence_count_for_existing_statement(self):
        self.statement.add_response(self.statement)
        self.statement.add_response(self.statement)
        self.assertTrue(
            self.statement.get_response_count(self.statement),
            2
        )

    def test_occurrence_count_incremented(self):
        self.statement.add_response(self.statement)
        self.statement.add_response(self.statement)

        self.assertEqual(len(self.statement.in_response_to), 1)
        self.assertEqual(self.statement.in_response_to[0].occurrence, 2)
예제 #23
0
class StatementTests(TestCase):
    def setUp(self):
        self.statement = Statement("A test statement.")

    def test_list_equality(self):
        """
        It should be possible to check if a statement
        exists in the list of statements that another
        statement has been issued in response to.
        """
        self.statement.add_response(Response("Yo"))
        self.assertEqual(len(self.statement.in_response_to), 1)
        self.assertIn(Response("Yo"), self.statement.in_response_to)

    def test_list_equality_unicode(self):
        """
        Test that it is possible to check if a statement
        is in a list of other statements when the
        statements text is unicode.
        """
        statements = [Statement("Hello"), Statement("我很好太感谢")]
        statement = Statement("我很好太感谢")
        self.assertIn(statement, statements)

    def test_update_response_list_new(self):
        self.statement.add_response(Response("Hello"))
        self.assertTrue(len(self.statement.in_response_to), 1)

    def test_update_response_list_existing(self):
        response = Response("Hello")
        self.statement.add_response(response)
        self.statement.add_response(response)
        self.assertTrue(len(self.statement.in_response_to), 1)

    def test_remove_response_exists(self):
        self.statement.add_response(Response("Testing"))
        removed = self.statement.remove_response("Testing")
        self.assertTrue(removed)

    def test_remove_response_does_not_exist(self):
        self.statement.add_response(Response("Testing"))
        removed = self.statement.remove_response("Test")
        self.assertFalse(removed)

    def test_serializer(self):
        data = self.statement.serialize()
        self.assertEqual(self.statement.text, data["text"])

    def test_occurrence_count_for_new_statement(self):
        """
        When the occurrence is updated for a statement that
        previously did not exist as a statement that the current
        statement was issued in response to, then the new statement
        should be added to the response list and the occurence count
        for that response should be set to 1.
        """
        response = Response("This is a test.")

        self.statement.add_response(response)
        self.assertTrue(self.statement.get_response_count(response), 1)

    def test_occurrence_count_for_existing_statement(self):
        self.statement.add_response(Response("ABC"))
        self.statement.add_response(Response("ABC"))
        self.assertTrue(self.statement.get_response_count(Response("ABC")), 2)

    def test_occurrence_count_incremented(self):
        self.statement.add_response(Response("ABC"))
        self.statement.add_response(Response("ABC"))

        self.assertEqual(len(self.statement.in_response_to), 1)
        self.assertEqual(self.statement.in_response_to[0].occurrence, 2)

    def test_add_non_response(self):
        with self.assertRaises(Statement.InvalidTypeException):
            self.statement.add_response(Statement("Blah"))
예제 #24
0
 def get_statement(self):
     stmt = Statement(self.text, **self.extra_data)
     for resp in self.in_response_to:
         stmt.add_response(resp.get_response())
     return stmt
예제 #25
0
 def get_statement(self):
     statement = Statement(self.text, extra_data=self.extra_data)
     for response in self.in_response_to:
         statement.add_response(response.get_response())
     return statement