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

Commit de19d13

Browse files
author
Cipher
committed
Modified structure to a package
1 parent 5b7fbbf commit de19d13

23 files changed

+441
-8
lines changed

Janex.egg-info/PKG-INFO

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Metadata-Version: 2.1
22
Name: Janex
3-
Version: 0.0.2b2
3+
Version: 0.0.4b0
44
Home-page: https://github.com/Cipher58
5+
Download-URL: https://github.com/Cipher58/Janex/archive/refs/tags/v0.0.2-beta.tar.gz
56
Author: Cipher58
67
Author-email: cipher58public@gmail.com
78
License: Lily 1.0
@@ -46,7 +47,7 @@ from Janex import *
4647
Before anything else, you need to create an instance of the IntentMatcher class.
4748

4849
```
49-
intents_file_path = "intents.json"
50+
intents_file_path = "./intents.json"
5051

5152
matcher = IntentMatcher(intents_file_path)
5253
```
@@ -70,7 +71,7 @@ To compare the input with the patterns from your intents.json storage file, you
7071
```
7172
intents_file_path = "intents.json"
7273

73-
intent_class = matcher.patterncompare(input_string, intents_file_path)
74+
intent_class = matcher.pattern_compare(input_string, intents_file_path)
7475

7576
print(intent_class)
7677
```
@@ -80,7 +81,7 @@ print(intent_class)
8081
Sometimes a list of responses in a class can become varied in terms of context, and so in order to get the best possible response, we can use the 'responsecompare' function to compare the input string with your list of responses.
8182

8283
```
83-
BestResponse = matcher.responsecompare(input_string, intents_file_path, intent_class)
84+
BestResponse = matcher.response_compare(input_string, intents_file_path, intent_class)
8485

8586
print(BestResponse)
8687
```
@@ -96,7 +97,7 @@ print(output)
9697

9798
<h3> Functionality </h3>
9899

99-
<h4>Version 0.0.2-beta</h4>
100+
<h4>Version 0.0.2-Gamma</h4>
100101

101102
- Word tokenizer ✓
102103
- Intent classifier ✓

Janex.egg-info/SOURCES.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
LICENSE
22
README.md
3+
Setup.py
34
setup.py
5+
Janex/Janex.py
6+
Janex/__init__.py
7+
Janex/chat.py
48
Janex.egg-info/PKG-INFO
59
Janex.egg-info/SOURCES.txt
610
Janex.egg-info/dependency_links.txt
711
Janex.egg-info/requires.txt
8-
Janex.egg-info/top_level.txt
12+
Janex.egg-info/top_level.txt
13+
Janex/JanexSub/JanexSub.py
14+
Janex/JanexSub/__init__.py

Janex.egg-info/top_level.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
Janex
Binary file not shown.

Janex/Janex.js

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
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+
module.exports = IntentMatcher;

0 commit comments

Comments
 (0)