@@ -35,7 +35,23 @@ def patterncompare(input_string, intents_file_path):
35
35
Similarity = 0
36
36
pattern = pattern .lower ()
37
37
WordList = Tokenize (pattern )
38
- Similarity = len (set (BagOfWords ) & set (WordList )) / len (set (BagOfWords + WordList ))
38
+ NewList = []
39
+ NewBag = []
40
+
41
+ for word in WordList :
42
+ word = stem (word )
43
+ NewList .append (word )
44
+
45
+ for word in BagOfWords :
46
+ word = stem (word )
47
+ NewBag .append (word )
48
+
49
+ WordList = NewList
50
+ BagOfWords = NewBag
51
+
52
+ for word in BagOfWords :
53
+ if word in WordList :
54
+ Similarity = (Similarity + 1 / len (WordList + BagOfWords ))
39
55
40
56
if Similarity > MaxSimilarity :
41
57
SimilarityPercentage = Similarity * 100
@@ -69,30 +85,61 @@ def responsecompare(input_string, intents_file_path, intent_class):
69
85
raise NoMatchingIntentError ("No matching intent class found." )
70
86
71
87
for response in responses :
88
+
72
89
Similarity = 0
73
- response = response .lower ()
90
+ pattern = response .lower ()
74
91
WordList = Tokenize (response )
92
+ NewList = []
93
+ NewBag = []
75
94
76
- for InputWord in BagOfWords :
77
- for OutputWord in WordList :
78
- if InputWord == OutputWord :
79
- Similarity += 1
80
- # print("Match found!")
95
+ for word in WordList :
96
+ word = stem (word )
97
+ NewList .append (word )
81
98
82
- OutofHundred = len (BagOfWords ) # Total number of words in the input
83
- Hundred = len (BagOfWords + WordList ) # Total number of words in both input and pattern
99
+ for word in BagOfWords :
100
+ word = stem (word )
101
+ NewBag .append (word )
102
+
103
+ WordList = NewList
104
+ BagOfWords = NewBag
105
+
106
+ for word in BagOfWords :
107
+ if word in WordList :
108
+ Similarity = (Similarity + 1 / len (WordList + BagOfWords ))
84
109
85
110
if Similarity > MaxSimilarity :
86
- SimilarityPercentage = ( Similarity / Hundred ) * 100
111
+ SimilarityPercentage = Similarity * 100
87
112
MaxSimilarity = Similarity
88
113
MostSimilarResponse = response
89
114
90
115
print (f"Similarity: { SimilarityPercentage :.2f} %" )
91
116
92
117
# Convert MSR back into original string
93
118
for response in responses :
119
+ lowresponselist = []
94
120
lowresponse = response .lower ()
95
- if lowresponse == MostSimilarResponse :
96
- MostSimilarResponse = response
121
+ lowresponselist = stem_sentence (lowresponse )
122
+
123
+ for lowresponse in lowresponselist :
124
+ if lowresponse == MostSimilarResponse :
125
+ MostSImilarResponse = response
97
126
98
127
return MostSimilarResponse
128
+
129
+ def stem (input_word ):
130
+ suffixes = ['ing' , 'ly' , 'ed' , 'es' , 's' , 'er' , 'est' , 'y' ]
131
+ for suffix in suffixes :
132
+ if input_word .endswith (suffix ):
133
+ input_word = input_word [:- len (suffix )]
134
+ break
135
+ return input_word
136
+
137
+ def stem_sentence (input_string ):
138
+ wordlist = []
139
+ stemmedwords = []
140
+ wordlist = input_string .split ()
141
+ for input_word in wordlist :
142
+ word = stem (input_word )
143
+ stemmedwords .append (word )
144
+
145
+ return stemmedwords
0 commit comments