This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJanex.js
235 lines (190 loc) · 5.95 KB
/
Janex.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const readline = require("readline");
class IntentMatcher {
constructor(intentsFilePath) {
this.intentsFilePath = intentsFilePath;
this.intents = this.train();
}
Tokenize(inputString) {
let processedString = inputString.toLowerCase().trim().replace(/[^\w\s]|_/g, "").replace(/\s+/g, " ");
let words = processedString.split(" ");
words = this.stemList(words);
return words;
}
TokenizeList(inputList) {
let tokenWords = [];
for (let word of inputList) {
let token = this.Tokenize(word);
tokenWords.push(token);
}
return tokenWords;
}
train() {
const fs = require("fs");
const intents = JSON.parse(fs.readFileSync(this.intentsFilePath, "utf8"));
return intents;
}
patternCompare(inputString) {
let inputStringLower = inputString.toLowerCase();
let highestSimilarity = 0;
let mostSimilarPattern = null;
let similarityPercentage = 0;
for (let intentClass of this.intents.intents) {
let overallWordList = [];
let similarity = 0;
for (let pattern of intentClass.patterns) {
let wordList = [];
let patternLower = pattern.toLowerCase();
wordList = this.Tokenize(patternLower);
overallWordList.push(wordList);
let newList = [];
let newBag = [];
for (let word of wordList) {
word = this.stem(word);
newList.push(word);
}
let wordList2 = this.Tokenize(inputStringLower);
for (let word of wordList2) {
word = this.stem(word);
newBag.push(word);
}
wordList = newList;
wordList2 = newBag;
for (let word of wordList2) {
if (wordList.includes(word)) {
similarity++;
}
}
if (similarity > highestSimilarity) {
similarityPercentage = similarity / (overallWordList.length + wordList2.length);
highestSimilarity = similarity;
mostSimilarPattern = intentClass;
}
}
}
console.log(`Similarity: ${similarityPercentage.toFixed(2)}%`);
if (mostSimilarPattern) {
return mostSimilarPattern;
} else {
throw new Error("No matching intent class found.");
}
}
responseCompare(inputString, intentClass) {
let inputStringLower = inputString.toLowerCase();
let highestSimilarity = 0;
let similarityPercentage = 0;
let mostSimilarResponse = null;
let responses = intentClass ? intentClass.responses : [];
for (let response of responses) {
let similarity = 0;
let responseLower = response.toLowerCase();
let wordList = this.Tokenize(responseLower);
let newList = [];
let newBag = [];
for (let word of wordList) {
word = this.stem(word);
newList.push(word);
}
let wordList2 = this.Tokenize(inputStringLower);
for (let word of wordList2) {
word = this.stem(word);
newBag.push(word);
}
wordList = newList;
wordList2 = newBag;
for (let word of wordList2) {
if (wordList.includes(word)) {
similarity += 1 / (wordList.length + wordList2.length);
}
}
if (similarity > highestSimilarity) {
similarityPercentage = similarity * 100;
highestSimilarity = similarity;
mostSimilarResponse = response;
}
}
console.log(`Similarity: ${similarityPercentage.toFixed(2)}%`);
// Convert mostSimilarResponse back into the original string
for (let response of responses) {
let lowResponseList = [];
let lowResponse = response.toLowerCase();
lowResponseList = this.stemSentence(lowResponse);
for (let lowResponseWord of lowResponseList) {
if (lowResponseWord === mostSimilarResponse) {
mostSimilarResponse = response;
}
}
}
return mostSimilarResponse;
}
stem(inputWord) {
let suffixes = ["ing", "ly", "ed", "es", "'s", "er", "est", "y", "ily", "able", "ful", "ness", "less", "ment", "ive", "ize", "ous"];
for (let suffix of suffixes) {
if (inputWord.endsWith(suffix)) {
inputWord = inputWord.slice(0, -suffix.length);
break;
}
}
return inputWord;
}
stemSentence(inputString) {
let wordList = [];
let stemmedWords = [];
wordList = inputString.split(" ");
for (let inputWord of wordList) {
let word = this.stem(inputWord);
stemmedWords.push(word);
}
return stemmedWords;
}
stemList(inputList) {
let stemmedWords = [];
for (let word of inputList) {
let stemmedWord = this.stem(word);
stemmedWords.push(stemmedWord);
}
return stemmedWords;
}
outputCompare(output) {
let highestSimilarity = 0;
let mostSimilarPattern = null;
let similarityPercentage = 0;
for (let intentClass of this.intents.intents) {
let overallWordList = [];
let similarity = 0;
for (let pattern of intentClass.patterns) {
let wordList = [];
let patternLower = pattern.toLowerCase();
wordList = this.Transform(patternLower);
overallWordList.push(wordList);
let newList = [];
let newBag = [];
for (let word of wordList) {
newList.push(word);
}
let wordList2 = output;
for (let word of wordList2) {
newBag.push(word);
}
wordList = newList;
wordList2 = newBag;
for (let word of wordList2) {
if (wordList.includes(word)) {
similarity++;
}
}
if (similarity > highestSimilarity) {
similarityPercentage = similarity / (overallWordList.length + wordList2.length);
highestSimilarity = similarity;
mostSimilarPattern = intentClass;
}
}
}
console.log(`Similarity: ${similarityPercentage.toFixed(2)}%`);
if (mostSimilarPattern) {
return mostSimilarPattern;
} else {
throw new Error("No matching intent class found.");
}
}
};
module.exports = IntentMatcher;