@@ -65,7 +65,7 @@ def pattern_compare(self, input_string):
65
65
highest_similarity = similarity
66
66
most_similar_pattern = intent_class
67
67
68
- print (f"Similarity: { similarity_percentage :.2%} " )
68
+ # print(f"Similarity: {similarity_percentage:.2%}")
69
69
70
70
if most_similar_pattern :
71
71
highest_similarity = highest_similarity / 100
@@ -77,12 +77,15 @@ 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 []
83
84
84
85
for response in responses :
85
86
similarity = 0
87
+ Count = 0
88
+ InputCount = 0
86
89
response_lower = response .lower ()
87
90
word_list = self .tokenize (response_lower )
88
91
new_list = []
@@ -103,24 +106,36 @@ def response_compare(self, input_string, intent_class):
103
106
104
107
for word in word_list_2 :
105
108
if word in word_list :
106
- similarity += 1
109
+ # Check if the word begins with a capital letter
110
+ if word .istitle ():
111
+ similarity += 2 # Add 2 to the similarity for words with capital letters
112
+ else :
113
+ similarity += 1
114
+
115
+ response_words = self .tokenize (response )
116
+ input_words = self .tokenize (input_string )
117
+
118
+ for word in response_words :
119
+ Count += 0.01
120
+
121
+ for word in input_words :
122
+ InputCount += 0.01
123
+
124
+ distance = Count + InputCount / 2
125
+
126
+ # print(distance)
127
+
128
+ similarity = similarity - distance
129
+
130
+ # Calculate the similarity percentage and the distance
131
+ similarity_percentage = similarity / len (overall_word_list ) # Calculate average similarity
107
132
108
133
if similarity > highest_similarity :
109
- similarity_percentage = similarity / (len (overall_word_list ) + len (word_list_2 ))
110
134
highest_similarity = similarity
111
135
most_similar_response = response
112
136
113
- print (f"Similarity: { similarity_percentage :.2%} " )
114
-
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 )
120
-
121
- for low_response_word in low_response_list :
122
- if low_response_word == most_similar_response :
123
- most_similar_response = response
137
+ # print(f"Similarity: {similarity_percentage:.2%}")
138
+ # print(f"Distance: {distance}")
124
139
125
140
return most_similar_response
126
141
@@ -135,9 +150,10 @@ def stem(self, input_word):
135
150
def stem_sentence (self , input_string ):
136
151
word_list = input_string .split (" " )
137
152
stemmed_words = []
153
+
138
154
for input_word in word_list :
139
155
word = self .stem (input_word )
140
- stemmed_words .append (word )
156
+ stemmed_word .append (word )
141
157
142
158
return stemmed_words
143
159
0 commit comments