Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit 7aaaa6c

Browse files
author
Cipher
committed
Added a JavaScript version of Janex.py
1 parent a9718f3 commit 7aaaa6c

File tree

3 files changed

+289
-35
lines changed

3 files changed

+289
-35
lines changed

Experimental/.DS_Store

0 Bytes
Binary file not shown.

Janex.js

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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+
});

Janex.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -87,41 +87,6 @@ def patterncompare(self, input_string):
8787
else:
8888
raise self.NoMatchingIntentError("No matching intent class found.")
8989

90-
def Transform(self, input_string):
91-
# Example usage
92-
max_seq_len = 10
93-
d_model = 64
94-
num_heads = 4
95-
d_ff = 128
96-
num_layers = 2
97-
98-
pos_enc = PositionalEncoding(d_model, max_seq_len)
99-
100-
# Text preprocessing
101-
tokens = self.Tokenize(input_string)
102-
max_seq_len = max_seq_len # Specify the desired maximum sequence length
103-
104-
print(tokens)
105-
106-
# Convert tokens to numerical representation (e.g., using word embeddings)
107-
word_embeddings = {} # Replace with your word embeddings dictionary
108-
X = np.array([word_embeddings.get(token, np.zeros(d_model)) for token in tokens])
109-
110-
# Pad or truncate X to match the desired sequence length
111-
X = X[:max_seq_len]
112-
padding = np.zeros((max_seq_len - X.shape[0], d_model))
113-
X = np.vstack((X, padding))
114-
115-
positions = np.arange(max_seq_len)
116-
pos_encoding = pos_enc.get_positional_encoding(positions)
117-
118-
X_with_pos_enc = X + pos_encoding
119-
120-
transformer = Transformer(d_model, num_heads, d_ff, num_layers)
121-
output = transformer.forward(X_with_pos_enc)
122-
123-
return output
124-
12590
def responsecompare(self, input_string, intent_class):
12691
input_string = input_string.lower()
12792
HighestSimilarity = 0
@@ -254,6 +219,41 @@ def outputcompare(self, output):
254219
else:
255220
raise self.NoMatchingIntentError("No matching intent class found.")
256221

222+
def Transform(self, input_string):
223+
# Example usage
224+
max_seq_len = 10
225+
d_model = 64
226+
num_heads = 4
227+
d_ff = 128
228+
num_layers = 2
229+
230+
pos_enc = PositionalEncoding(d_model, max_seq_len)
231+
232+
# Text preprocessing
233+
tokens = self.Tokenize(input_string)
234+
max_seq_len = max_seq_len # Specify the desired maximum sequence length
235+
236+
print(tokens)
237+
238+
# Convert tokens to numerical representation (e.g., using word embeddings)
239+
word_embeddings = {} # Replace with your word embeddings dictionary
240+
X = np.array([word_embeddings.get(token, np.zeros(d_model)) for token in tokens])
241+
242+
# Pad or truncate X to match the desired sequence length
243+
X = X[:max_seq_len]
244+
padding = np.zeros((max_seq_len - X.shape[0], d_model))
245+
X = np.vstack((X, padding))
246+
247+
positions = np.arange(max_seq_len)
248+
pos_encoding = pos_enc.get_positional_encoding(positions)
249+
250+
X_with_pos_enc = X + pos_encoding
251+
252+
transformer = Transformer(d_model, num_heads, d_ff, num_layers)
253+
output = transformer.forward(X_with_pos_enc)
254+
255+
return output
256+
257257
def softmax(x, axis=-1):
258258
# Apply softmax operation to the input array along the specified axis
259259
e_x = np.exp(x - np.max(x, axis=axis, keepdims=True))

0 commit comments

Comments
 (0)