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