@@ -26,37 +26,40 @@ def patterncompare(input_string, intents_file_path):
26
26
with open (intents_file_path , 'r' ) as json_data :
27
27
intents = json .load (json_data )
28
28
29
- BagOfWords = Tokenize (input_string )
29
+ WordList2 = Tokenize (input_string )
30
30
31
31
for intent_class in intents ['intents' ]:
32
+ OverallWordList = []
33
+ Similarity = 0
32
34
33
35
patterns = intent_class .get ('patterns' )
34
36
for pattern in patterns :
35
- Similarity = 0
37
+ WordList = []
36
38
pattern = pattern .lower ()
37
39
WordList = Tokenize (pattern )
40
+ OverallWordList .append (WordList )
38
41
NewList = []
39
42
NewBag = []
40
43
41
44
for word in WordList :
42
45
word = stem (word )
43
46
NewList .append (word )
44
47
45
- for word in BagOfWords :
48
+ for word in WordList2 :
46
49
word = stem (word )
47
50
NewBag .append (word )
48
51
49
52
WordList = NewList
50
- BagOfWords = NewBag
53
+ WordList2 = NewBag
51
54
52
- for word in BagOfWords :
55
+ for word in WordList2 :
53
56
if word in WordList :
54
- Similarity = ( Similarity + 1 / len ( WordList + BagOfWords ))
57
+ Similarity = Similarity + 1
55
58
56
- if Similarity > MaxSimilarity :
57
- SimilarityPercentage = Similarity * 100
58
- MaxSimilarity = Similarity
59
- MostSimilarPattern = intent_class
59
+ if Similarity > MaxSimilarity :
60
+ SimilarityPercentage = Similarity / len ( OverallWordList + WordList2 )
61
+ MaxSimilarity = Similarity
62
+ MostSimilarPattern = intent_class
60
63
61
64
print (f"Similarity: { SimilarityPercentage :.2f} %" )
62
65
@@ -77,7 +80,7 @@ def responsecompare(input_string, intents_file_path, intent_class):
77
80
with open (intents_file_path , 'r' ) as json_data :
78
81
intents = json .load (json_data )
79
82
80
- BagOfWords = Tokenize (input_string )
83
+ WordList2 = Tokenize (input_string )
81
84
82
85
if intent_class is not None :
83
86
responses = intent_class .get ('responses' )
@@ -96,16 +99,16 @@ def responsecompare(input_string, intents_file_path, intent_class):
96
99
word = stem (word )
97
100
NewList .append (word )
98
101
99
- for word in BagOfWords :
102
+ for word in WordList2 :
100
103
word = stem (word )
101
104
NewBag .append (word )
102
105
103
106
WordList = NewList
104
- BagOfWords = NewBag
107
+ WordList2 = NewBag
105
108
106
- for word in BagOfWords :
109
+ for word in WordList2 :
107
110
if word in WordList :
108
- Similarity = (Similarity + 1 / len (WordList + BagOfWords ))
111
+ Similarity = (Similarity + 1 / len (WordList + WordList2 ))
109
112
110
113
if Similarity > MaxSimilarity :
111
114
SimilarityPercentage = Similarity * 100
0 commit comments