@@ -26,37 +26,40 @@ def patterncompare(input_string, intents_file_path):
2626 with open (intents_file_path , 'r' ) as json_data :
2727 intents = json .load (json_data )
2828
29- BagOfWords = Tokenize (input_string )
29+ WordList2 = Tokenize (input_string )
3030
3131 for intent_class in intents ['intents' ]:
32+ OverallWordList = []
33+ Similarity = 0
3234
3335 patterns = intent_class .get ('patterns' )
3436 for pattern in patterns :
35- Similarity = 0
37+ WordList = []
3638 pattern = pattern .lower ()
3739 WordList = Tokenize (pattern )
40+ OverallWordList .append (WordList )
3841 NewList = []
3942 NewBag = []
4043
4144 for word in WordList :
4245 word = stem (word )
4346 NewList .append (word )
4447
45- for word in BagOfWords :
48+ for word in WordList2 :
4649 word = stem (word )
4750 NewBag .append (word )
4851
4952 WordList = NewList
50- BagOfWords = NewBag
53+ WordList2 = NewBag
5154
52- for word in BagOfWords :
55+ for word in WordList2 :
5356 if word in WordList :
54- Similarity = ( Similarity + 1 / len ( WordList + BagOfWords ))
57+ Similarity = Similarity + 1
5558
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
6063
6164 print (f"Similarity: { SimilarityPercentage :.2f} %" )
6265
@@ -77,7 +80,7 @@ def responsecompare(input_string, intents_file_path, intent_class):
7780 with open (intents_file_path , 'r' ) as json_data :
7881 intents = json .load (json_data )
7982
80- BagOfWords = Tokenize (input_string )
83+ WordList2 = Tokenize (input_string )
8184
8285 if intent_class is not None :
8386 responses = intent_class .get ('responses' )
@@ -96,16 +99,16 @@ def responsecompare(input_string, intents_file_path, intent_class):
9699 word = stem (word )
97100 NewList .append (word )
98101
99- for word in BagOfWords :
102+ for word in WordList2 :
100103 word = stem (word )
101104 NewBag .append (word )
102105
103106 WordList = NewList
104- BagOfWords = NewBag
107+ WordList2 = NewBag
105108
106- for word in BagOfWords :
109+ for word in WordList2 :
107110 if word in WordList :
108- Similarity = (Similarity + 1 / len (WordList + BagOfWords ))
111+ Similarity = (Similarity + 1 / len (WordList + WordList2 ))
109112
110113 if Similarity > MaxSimilarity :
111114 SimilarityPercentage = Similarity * 100
0 commit comments