@@ -77,6 +77,7 @@ def response_compare(self, input_string, intent_class):
77
77
input_string_lower = input_string .lower ()
78
78
highest_similarity = 0
79
79
similarity_percentage = 0
80
+ distance = 0
80
81
most_similar_response = None
81
82
82
83
responses = intent_class ["responses" ] if intent_class else []
@@ -103,24 +104,26 @@ def response_compare(self, input_string, intent_class):
103
104
104
105
for word in word_list_2 :
105
106
if word in word_list :
106
- similarity += 1
107
+ # Check if the word begins with a capital letter
108
+ if word .istitle ():
109
+ similarity += 2 # Add 2 to the similarity for words with capital letters
110
+ else :
111
+ similarity += 1
107
112
108
- if similarity > highest_similarity :
109
- similarity_percentage = similarity / (len (overall_word_list ) + len (word_list_2 ))
110
- highest_similarity = similarity
111
- most_similar_response = response
113
+ # Calculate the similarity percentage and the distance
114
+ similarity_percentage = similarity / (len (overall_word_list ) + len (word_list_2 ))
115
+ distance = abs (len (response ) - len (input_string ))
112
116
113
- print (f"Similarity: { similarity_percentage :.2%} " )
117
+ # Combine similarity and distance with appropriate weights
118
+ # You can adjust the weights based on your preference
119
+ combined_similarity = 0.2 * similarity_percentage + 0.8 * (1 - distance / max (len (response ), len (input_string )))
114
120
115
- # Convert most_similar_response back into the original string
116
- for response in responses :
117
- low_response_list = []
118
- low_response = response .lower ()
119
- low_response_list = self .stem_sentence (low_response )
121
+ if combined_similarity > highest_similarity :
122
+ highest_similarity = combined_similarity
123
+ most_similar_response = response
120
124
121
- for low_response_word in low_response_list :
122
- if low_response_word == most_similar_response :
123
- most_similar_response = response
125
+ print (f"Similarity: { highest_similarity :.2%} " )
126
+ print (f"Distance: { distance } " )
124
127
125
128
return most_similar_response
126
129
0 commit comments