Esempio n. 1
0
    def test_exact_match_different_capitalization(self):
        """
        Test that text capitalization is ignored.
        """
        statement = Statement('Hi HoW ArE yOu?')
        other_statement = Statement('hI hOw are YoU?')

        value = comparisons.levenshtein_distance(statement, other_statement)

        self.assertEqual(value, 1)
Esempio n. 2
0
    def test_levenshtein_distance_other_statement_false(self):
        """
        Falsy values should match by zero.
        """
        statement = Statement('Hello')
        other_statement = Statement('')

        value = comparisons.levenshtein_distance(statement, other_statement)

        self.assertEqual(value, 0)
    def test_exact_match_different_capitalization(self):
        """
        Test that text capitalization is ignored.
        """
        statement = Statement(text='Hi HoW ArE yOu?')
        other_statement = Statement(text='hI hOw are YoU?')

        value = comparisons.levenshtein_distance(statement, other_statement)

        self.assertEqual(value, 1)
    def test_levenshtein_distance_other_statement_false(self):
        """
        Falsy values should match by zero.
        """
        statement = Statement(text='Hello')
        other_statement = Statement(text='')

        value = comparisons.levenshtein_distance(statement, other_statement)

        self.assertEqual(value, 0)
Esempio n. 5
0
    def test_levenshtein_distance_statement_integer(self):
        """
        Test that an exception is not raised if a statement is initialized
        with an integer value as its text attribute.
        """
        statement = Statement(2)
        other_statement = Statement('Hello')

        value = comparisons.levenshtein_distance(statement, other_statement)

        self.assertEqual(value, 0)
    def test_levenshtein_distance_statement_integer(self):
        """
        Test that an exception is not raised if a statement is initialized
        with an integer value as its text attribute.
        """
        statement = Statement(text=2)
        other_statement = Statement(text='Hello')

        value = comparisons.levenshtein_distance(statement, other_statement)

        self.assertEqual(value, 0)
Esempio n. 7
0
def similar_statement(input_statement):
	closest_match = input_statement
	closest_match.confidence = 0
	statement_list = database.keys()
	# Find the closest matching known statement
	for statement in statement_list:
		confidence = (comparisons.levenshtein_distance(input_statement, statement) + comparisons.synset_distance(input_statement,statement)+comparisons.jaccard_similarity(input_statement,statement)+ comparisons.sentiment_comparison(input_statement,statement))*1.0/4	 #comparisons.sentiment_comparison(input_statement,statement)+

		if confidence > closest_match.confidence:
		    statement.confidence = confidence
		    closest_match = statement
	print closest_match.confidence
	return (closest_match.confidence,closest_match)   
    def get(self, input_statement):
        statement_list = self.chatbot.storage.get_response_statements()

        if not statement_list:
            if self.chatbot.storage.count():
                # Use a randomly picked statement
                self.logger.info('No statements have known responses. ' +
                                 'Choosing a random response to return.')
                random_response = self.chatbot.storage.get_random()
                random_response.confidence = 0
                return random_response
            else:
                raise self.EmptyDatasetException()

        closest_match = input_statement
        closest_match.confidence = 0
        closest_match.lev = 0
        closest_match.cos = 0
        # Find the closest chatbot known statement
        questionVector = self.getVector(input_statement.tokens, True)
        if not all(x == 0 for x in questionVector):
            # If there is a vector for the statement compare it with all other
            # statements in the database
            for statement in statement_list:
                vec = self.getStatementVec(statement)
                lev_similarity = comparisons.levenshtein_distance(
                    Statement(input_statement.text), Statement(statement.text))
                cosine_similarity = self.cosine_similarity(questionVector, vec)
                # normalize
                cosine_similarity = (cosine_similarity + 1) / 2
                if all(x == 0 for x in vec):
                    # There is no vector for the statement so the comparison is meaningless
                    lev_similarity = 0
                    cosine_similarity = 0

                # print(statement.text,cosine_similarity,lev_similarity)
                if cosine_similarity > closest_match.cos:
                    closest_match = statement
                    closest_match.lev = lev_similarity
                    closest_match.cos = cosine_similarity
                elif abs(cosine_similarity - closest_match.cos
                         ) < 0.01 and lev_similarity > closest_match.lev:
                    closest_match = statement
                    closest_match.lev = lev_similarity
                    closest_match.cos = cosine_similarity

        closest_match.confidence = closest_match.cos
        print("Closest Match:", closest_match,\
              "Lev:",closest_match.lev, \
              "Cos:",closest_match.cos, "Confidence:",closest_match.confidence)
        return closest_match