@@ -18,6 +18,7 @@ def patterncompare(input_string, intents_file_path):
18
18
input_string = input_string .lower ()
19
19
MaxSimilarity = 0
20
20
MostSimilarPattern = None
21
+ SimilarityPercentage = 0
21
22
22
23
patterns = []
23
24
Similarity = 0
@@ -28,21 +29,19 @@ def patterncompare(input_string, intents_file_path):
28
29
BagOfWords = Tokenize (input_string )
29
30
30
31
for intent_class in intents ['intents' ]:
31
-
32
+
32
33
patterns = intent_class .get ('patterns' )
33
34
for pattern in patterns :
35
+ Similarity = 0
34
36
pattern = pattern .lower ()
35
37
WordList = Tokenize (pattern )
36
38
Similarity = len (set (BagOfWords ) & set (WordList )) / len (set (BagOfWords + WordList ))
37
- SimilarityPercentage = Similarity * 100
38
39
39
40
if Similarity > MaxSimilarity :
41
+ SimilarityPercentage = Similarity * 100
40
42
MaxSimilarity = Similarity
41
43
MostSimilarPattern = intent_class
42
-
43
- if SimilarityPercentage > 100 :
44
- SimilarityPercentage = SimilarityPercentage / 100
45
-
44
+
46
45
print (f"Similarity: { SimilarityPercentage :.2f} %" )
47
46
48
47
if MostSimilarPattern :
@@ -53,6 +52,7 @@ def patterncompare(input_string, intents_file_path):
53
52
def responsecompare (input_string , intents_file_path , intent_class ):
54
53
input_string = input_string .lower ()
55
54
MaxSimilarity = 0
55
+ SimilarityPercentage = 0
56
56
MostSimilarResponse = None
57
57
58
58
responses = []
@@ -69,6 +69,7 @@ def responsecompare(input_string, intents_file_path, intent_class):
69
69
raise NoMatchingIntentError ("No matching intent class found." )
70
70
71
71
for response in responses :
72
+ Similarity = 0
72
73
response = response .lower ()
73
74
WordList = Tokenize (response )
74
75
@@ -81,22 +82,17 @@ def responsecompare(input_string, intents_file_path, intent_class):
81
82
OutofHundred = len (BagOfWords ) # Total number of words in the input
82
83
Hundred = len (BagOfWords + WordList ) # Total number of words in both input and pattern
83
84
84
- SimilarityPercentage = (Similarity / Hundred ) * 100 # Corrected calculation
85
-
86
85
if Similarity > MaxSimilarity :
86
+ SimilarityPercentage = (Similarity / Hundred ) * 100
87
87
MaxSimilarity = Similarity
88
88
MostSimilarResponse = response
89
-
90
- if SimilarityPercentage > 100 :
91
- SimilarityPercentage = SimilarityPercentage / 100
92
-
89
+
93
90
print (f"Similarity: { SimilarityPercentage :.2f} %" )
94
-
91
+
95
92
# Convert MSR back into original string
96
93
for response in responses :
97
94
lowresponse = response .lower ()
98
95
if lowresponse == MostSimilarResponse :
99
96
MostSimilarResponse = response
100
-
101
- return MostSimilarResponse
102
97
98
+ return MostSimilarResponse
0 commit comments