Exemplo n.º 1
0
    def test_filter_selection(self):
        """
        Test that repetitive responses are filtered out of the results.
        """
        from app.chatterbot_api.chatterbot.conversation import Statement
        from app.chatterbot_api.chatterbot.trainers import ListTrainer

        self.chatbot.filters = (filters.get_recent_repeated_responses, )

        self.trainer = ListTrainer(
            self.chatbot,
            show_training_progress=False
        )

        self.trainer.train([
            'Hi',
            'Hello',
            'Hi',
            'Hello',
            'Hi',
            'Hello',
            'How are you?',
            'I am good',
            'Glad to hear',
            'How are you?'
        ])

        statement = Statement(text='Hello', conversation='training')
        first_response = self.chatbot.get_response(statement)
        second_response = self.chatbot.get_response(statement)

        self.assertEqual('How are you?', first_response.text)
        self.assertEqual('Hi', second_response.text)
Exemplo n.º 2
0
class DataCachingTests(ChatBotTestCase):

    def setUp(self):
        super().setUp()

        self.chatbot.logic_adapters = [
            DummyMutatorLogicAdapter(self.chatbot)
        ]

        self.trainer = ListTrainer(
            self.chatbot,
            show_training_progress=False
        )

        self.trainer.train([
            'Hello',
            'How are you?'
        ])

    def test_additional_attributes_saved(self):
        """
        Test that an additional data attribute can be added to the statement
        and that this attribute is saved.
        """
        self.chatbot.get_response('Hello', conversation='test')
        results = list(self.chatbot.storage.filter(
            text='Hello',
            in_response_to=None,
            conversation='test'
        ))

        self.assertEqual(len(results), 1)
        self.assertIn('pos_tags:NN', results[0].get_tags())
Exemplo n.º 3
0
class ChatterBotResponseTests(ChatBotTestCase):
    def setUp(self):
        super().setUp()
        """
        Set up a database for testing.
        """
        self.trainer = ListTrainer(self.chatbot, show_training_progress=False)

        data1 = [
            "african or european?", "Huh? I... I don't know that.",
            "How do you know so much about swallows?"
        ]

        data2 = ["Siri is adorable", "Who is Seri?", "Siri is my cat"]

        data3 = [
            "What... is your quest?", "To seek the Holy Grail.",
            "What... is your favourite colour?", "Blue."
        ]

        self.trainer.train(data1)
        self.trainer.train(data2)
        self.trainer.train(data3)

    def test_answer_to_known_input(self):
        """
        Test that a matching response is returned
        when an exact match exists.
        """
        input_text = "What... is your favourite colour?"
        response = self.chatbot.get_response(input_text)

        self.assertIn("Blue", response.text)

    def test_answer_close_to_known_input(self):

        input_text = "What is your favourite colour?"
        response = self.chatbot.get_response(input_text)

        self.assertIn("Blue", response.text)

    def test_match_has_no_response(self):
        """
        Make sure that the if the last line in a file
        matches the input text then a index error does
        not occur.
        """
        input_text = "Siri is my cat"
        response = self.chatbot.get_response(input_text)

        self.assertGreater(len(response.text), 0)

    def test_empty_input(self):
        """
        If empty input is provided, anything may be returned.
        """
        response = self.chatbot.get_response("")

        self.assertTrue(len(response.text) >= 0)
Exemplo n.º 4
0
    def setUp(self):
        super().setUp()

        self.chatbot.logic_adapters = [
            DummyMutatorLogicAdapter(self.chatbot)
        ]

        self.trainer = ListTrainer(
            self.chatbot,
            show_training_progress=False
        )

        self.trainer.train([
            'Hello',
            'How are you?'
        ])
Exemplo n.º 5
0
    def setUp(self):
        super().setUp()
        """
        Set up a database for testing.
        """
        self.trainer = ListTrainer(self.chatbot, show_training_progress=False)

        data1 = [
            "african or european?", "Huh? I... I don't know that.",
            "How do you know so much about swallows?"
        ]

        data2 = ["Siri is adorable", "Who is Seri?", "Siri is my cat"]

        data3 = [
            "What... is your quest?", "To seek the Holy Grail.",
            "What... is your favourite colour?", "Blue."
        ]

        self.trainer.train(data1)
        self.trainer.train(data2)
        self.trainer.train(data3)
Exemplo n.º 6
0
def get_list_trainer(chatbot):
    return ListTrainer(
        chatbot,
        show_training_progress=False
    )
Exemplo n.º 7
0
 def setUp(self):
     super().setUp()
     self.trainer = ListTrainer(self.chatbot, show_training_progress=False)
Exemplo n.º 8
0
class ListTrainingTests(ChatBotTestCase):
    def setUp(self):
        super().setUp()
        self.trainer = ListTrainer(self.chatbot, show_training_progress=False)

    def test_training_cleans_whitespace(self):
        """
        Test that the ``clean_whitespace`` preprocessor is used during
        the training process.
        """
        self.chatbot.preprocessors = [preprocessors.clean_whitespace]

        self.trainer.train([
            'Can I help you with anything?', 'No, I     think I am all set.',
            'Okay, have a nice day.', 'Thank you, you too.'
        ])

        response = self.chatbot.get_response('Can I help you with anything?')

        self.assertEqual(response.text, 'No, I think I am all set.')

    def test_training_adds_statements(self):
        """
        Test that the training method adds statements
        to the database.
        """
        conversation = [
            "Hello", "Hi there!", "How are you doing?", "I'm great.",
            "That is good to hear", "Thank you.", "You are welcome.",
            "Sure, any time.", "Yeah", "Can I help you with anything?"
        ]

        self.trainer.train(conversation)

        response = self.chatbot.get_response("Thank you.")

        self.assertEqual(response.text, "You are welcome.")

    def test_training_sets_in_response_to(self):

        conversation = ["Do you like my hat?", "I do not like your hat."]

        self.trainer.train(conversation)

        statements = list(
            self.chatbot.storage.filter(in_response_to="Do you like my hat?"))

        self.assertIsLength(statements, 1)
        self.assertEqual(statements[0].in_response_to, "Do you like my hat?")

    def test_training_sets_search_text(self):

        conversation = ["Do you like my hat?", "I do not like your hat."]

        self.trainer.train(conversation)

        statements = list(
            self.chatbot.storage.filter(in_response_to="Do you like my hat?"))

        self.assertIsLength(statements, 1)
        self.assertEqual(statements[0].search_text, 'VERB:hat')

    def test_training_sets_search_in_response_to(self):

        conversation = ["Do you like my hat?", "I do not like your hat."]

        self.trainer.train(conversation)

        statements = list(
            self.chatbot.storage.filter(in_response_to="Do you like my hat?"))

        self.assertIsLength(statements, 1)
        self.assertEqual(statements[0].search_in_response_to, 'VERB:hat')

    def test_database_has_correct_format(self):
        """
        Test that the database maintains a valid format
        when data is added and updated. This means that
        after the training process, the database should
        contain nine objects and eight of these objects
        should list the previous member of the list as
        a response.
        """
        conversation = [
            "Hello sir!", "Hi, can I help you?",
            "Yes, I am looking for italian parsely.",
            "Italian parsely is right over here in out produce department",
            "Great, thank you for your help.",
            "No problem, did you need help finding anything else?",
            "Nope, that was it.", "Alright, have a great day.",
            "Thanks, you too."
        ]

        self.trainer.train(conversation)

        # There should be a total of 9 statements in the database after training
        self.assertEqual(self.chatbot.storage.count(), 9)

        # The first statement should be in response to another statement
        first_statement = list(
            self.chatbot.storage.filter(text=conversation[0]))
        self.assertIsNone(first_statement[0].in_response_to)

        # The second statement should be in response to the first statement
        second_statement = list(
            self.chatbot.storage.filter(text=conversation[1]))
        self.assertEqual(second_statement[0].in_response_to, conversation[0])

    def test_training_with_unicode_characters(self):
        """
        Ensure that the training method adds unicode statements
        to the database.
        """
        conversation = [
            u'¶ ∑ ∞ ∫ π ∈ ℝ² ∖ ⩆ ⩇ ⩈ ⩉ ⩊ ⩋ ⪽ ⪾ ⪿ ⫀ ⫁ ⫂ ⋒ ⋓',
            u'⊂ ⊃ ⊆ ⊇ ⊈ ⊉ ⊊ ⊋ ⊄ ⊅ ⫅ ⫆ ⫋ ⫌ ⫃ ⫄ ⫇ ⫈ ⫉ ⫊ ⟃ ⟄',
            u'∠ ∡ ⦛ ⦞ ⦟ ⦢ ⦣ ⦤ ⦥ ⦦ ⦧ ⦨ ⦩ ⦪ ⦫ ⦬ ⦭ ⦮ ⦯ ⦓ ⦔ ⦕ ⦖ ⟀',
            u'∫ ∬ ∭ ∮ ∯ ∰ ∱ ∲ ∳ ⨋ ⨌ ⨍ ⨎ ⨏ ⨐ ⨑ ⨒ ⨓ ⨔ ⨕ ⨖ ⨗ ⨘ ⨙ ⨚ ⨛ ⨜',
            u'≁ ≂ ≃ ≄ ⋍ ≅ ≆ ≇ ≈ ≉ ≊ ≋ ≌ ⩯ ⩰ ⫏ ⫐ ⫑ ⫒ ⫓ ⫔ ⫕ ⫖',
            u'¬ ⫬ ⫭ ⊨ ⊭ ∀ ∁ ∃ ∄ ∴ ∵ ⊦ ⊬ ⊧ ⊩ ⊮ ⊫ ⊯ ⊪ ⊰ ⊱ ⫗ ⫘',
            u'∧ ∨ ⊻ ⊼ ⊽ ⋎ ⋏ ⟑ ⟇ ⩑ ⩒ ⩓ ⩔ ⩕ ⩖ ⩗ ⩘ ⩙ ⩚ ⩛ ⩜ ⩝ ⩞ ⩟ ⩠ ⩢',
        ]

        self.trainer.train(conversation)

        response = self.chatbot.get_response(conversation[1])

        self.assertEqual(response.text, conversation[2])

    def test_training_with_emoji_characters(self):
        """
        Ensure that the training method adds statements containing emojis.
        """
        conversation = [
            u'Hi, how are you? 😃', u'I am just dandy 👍', u'Superb! 🎆'
        ]

        self.trainer.train(conversation)

        response = self.chatbot.get_response(conversation[1])

        self.assertEqual(response.text, conversation[2])

    def test_training_with_unicode_bytestring(self):
        """
        Test training with an 8-bit bytestring.
        """
        conversation = [
            'Hi, how are you?', '\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x90\x97',
            'Superb!'
        ]

        self.trainer.train(conversation)

        response = self.chatbot.get_response(conversation[1])

        self.assertEqual(response.text, conversation[2])

    def test_similar_sentence_gets_same_response_multiple_times(self):
        """
        Tests if the bot returns the same response for the same
        question (which is similar to the one present in the training set)
        when asked repeatedly.
        """
        training_data = [
            'how do you login to gmail?',
            'Goto gmail.com, enter your login information and hit enter!'
        ]

        similar_question = 'how do I login to gmail?'

        self.trainer.train(training_data)

        response_to_trained_set = self.chatbot.get_response(
            text='how do you login to gmail?', conversation='a')
        response1 = self.chatbot.get_response(text=similar_question,
                                              conversation='b')
        response2 = self.chatbot.get_response(text=similar_question,
                                              conversation='c')

        self.assertEqual(response_to_trained_set.text, training_data[1])
        self.assertEqual(response1.text, training_data[1])
        self.assertEqual(response2.text, training_data[1])

    def test_consecutive_trainings_same_responses_different_inputs(self):
        """
        Test consecutive trainings with the same responses to different inputs.
        """
        self.trainer.train(["A", "B", "C"])
        self.trainer.train(["B", "C", "D"])

        response1 = self.chatbot.get_response("B")
        response2 = self.chatbot.get_response("C")

        self.assertEqual(response1.text, "C")
        self.assertEqual(response2.text, "D")
Exemplo n.º 9
0
from app.chatterbot_api.chatterbot import ChatBot
from app.chatterbot_api.chatterbot.trainers import ListTrainer


'''
This is an example showing how to train a chat bot using the
ChatterBot ListTrainer.
'''

chatbot = ChatBot('Example Bot')

# Start by training our bot with the ChatterBot corpus data
trainer = ListTrainer(chatbot)

trainer.train([
    'Hello, how are you?',
    'I am doing well.',
    'That is good to hear.',
    'Thank you'
])

# You can train with a second list of data to add response variations

trainer.train([
    'Hello, how are you?',
    'I am great.',
    'That is awesome.',
    'Thanks'
])

# Now let's get a response to a greeting
Exemplo n.º 10
0
from app.chatterbot_api.chatterbot import ChatBot
from app.chatterbot_api.chatterbot.trainers import ListTrainer

# Create a new instance of a ChatBot
bot = ChatBot('Example Bot',
              storage_adapter='chatterbot.storage.SQLStorageAdapter',
              logic_adapters=[{
                  'import_path': 'chatterbot.logic.BestMatch',
                  'default_response': 'I am sorry, but I do not understand.',
                  'maximum_similarity_threshold': 0.90
              }])

trainer = ListTrainer(bot)

# Train the chat bot with a few responses
trainer.train([
    'How can I help you?', 'I want to create a chat bot',
    'Have you read the documentation?', 'No, I have not',
    'This should help get you started: http://chatterbot.rtfd.org/en/latest/quickstart.html'
])

# Get a response for some unexpected input
response = bot.get_response('How do I make an omelette?')
print(response)
Exemplo n.º 11
0
from app.chatterbot_api.chatterbot import ChatBot
from app.chatterbot_api.chatterbot.trainers import ListTrainer

# Create a new chat bot named Charlie
chatbot = ChatBot('Charlie')

trainer = ListTrainer(chatbot)

trainer.train([
    "Hi, can I help you?",
    "Sure, I'd like to book a flight to Iceland.",
    "Your flight has been booked."
])

# Get a response to the input text 'I would like to book a flight.'
response = chatbot.get_response('I would like to book a flight.')

print(response)