Example #1
0
def get_closest(text, log_directory, similarity_threshold):
    from chatterbot.conversation import Statement, Conversation
    import os

    closest_response = []
    closest_ratio = 0

    for log in os.listdir(log_directory):
        path = log_directory + "/" + log

        if os.path.isfile(path):
            conversation = Conversation()
            conversation.read(path)
            response, ratio = conversation.find_closest_response(text)

            # A response may not be returned if a log has less than one line
            if response:
                if ratio > closest_ratio:
                    closest_response = []
                    closest_response.append(response)
                    closest_ratio = ratio
                elif ratio == closest_ratio and closest_ratio != 0:
                    closest_response.append(response)
                    closest_ratio = ratio

    return closest_response, closest_ratio
Example #2
0
    def test_conversation_has_len_of_statement(self):
        conversation = Conversation()

        s1 = Statement("Bilbo", "Good Morning!")
        s2 = Statement("Gandalf", "What do you mean?")

        conversation.statements = [s1, s2]

        self.assertEqual(len(conversation), 2)
Example #3
0
    def test_conversation_is_iterable(self):
        conversation = Conversation()

        count = 0

        s1 = Statement("Bilbo", "Good Morning!")
        s2 = Statement("Gandalf", "What do you mean?")

        conversation.statements = [s1, s2]

        for statement in conversation:
            count += 1

        self.assertEqual(count, 2)
Example #4
0
    def test_next_line(self):
        conversation = Conversation()

        s1 = Statement("Bilbo", "Good Morning!")
        s2 = Statement("Gandalf", "What do you mean?")
        s3 = Statement(
            "Bilbo", "I mean it's a good morning whether you want it or not.")

        conversation.statements = [s1, s2, s3]

        index = 0
        next_line, next_index = conversation.next_line(index)

        self.assertEqual(next_line.text, s2.text)
        self.assertEqual(next_index, index + 1)
Example #5
0
    def test_find_closest_response_loose_match(self):
        conversation = Conversation()

        s1 = Statement("Bilbo", "Good Morning!")
        s2 = Statement("Gandalf", "What do you mean?")
        s3 = Statement(
            "Bilbo", "I mean it's a good morning whether you want it or not.")
        s4 = Statement("Gandalf", "Good morning then.")

        conversation.statements = [s1, s2, s3, s4]

        response, ratio = conversation.find_closest_response("What?")

        self.assertEqual(len(response), 1)
        self.assertEqual(response[0].text, s3.text)
Example #6
0
    def test_find_closest_response_return_multiple_lines(self):
        conversation = Conversation()

        s1 = Statement("Gandalf", "What is your definition of relativity?")
        s2 = Statement(
            "Albert Einstein",
            "When you are courting a nice girl an hour seems like a second.")
        s3 = Statement(
            "Albert Einstein",
            "When you sit on a red-hot cinder a second seems like an hour.")
        s4 = Statement("Albert Einstein", "That's relativity.")

        conversation.statements = [s1, s2, s3, s4]

        response, ratio = conversation.find_closest_response(s1.text)

        self.assertEqual(len(response), 3)
        self.assertEqual(response[0].text, s2.text)
        self.assertEqual(response[1].text, s3.text)
        self.assertEqual(response[2].text, s4.text)