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

Commit bdd2df9

Browse files
author
Cipher
committed
Added percentage
1 parent 921ba81 commit bdd2df9

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

Janex.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import json
22
import string
33

4+
class NoMatchingIntentError(Exception):
5+
pass
6+
47
def Tokenize(input_string):
58
input_string = input_string.strip()
6-
79
input_string = input_string.translate(str.maketrans("", "", string.punctuation))
8-
910
words = input_string.split()
10-
1111
return words
1212

1313
def train(intents_file_path):
@@ -16,7 +16,7 @@ def train(intents_file_path):
1616

1717
def patterncompare(input_string, intents_file_path):
1818
MaxSimilarity = 0
19-
MostSimilarPattern = ""
19+
MostSimilarPattern = None
2020

2121
patterns = []
2222
Similarity = 0
@@ -25,25 +25,28 @@ def patterncompare(input_string, intents_file_path):
2525
intents = json.load(json_data)
2626

2727
BagOfWords = Tokenize(input_string)
28-
28+
2929
for intent_class in intents['intents']:
30+
3031
patterns = intent_class.get('patterns')
3132
for pattern in patterns:
3233
WordList = Tokenize(pattern)
33-
for word in WordList:
34-
if word in BagOfWords:
35-
Similarity = Similarity + 1
36-
print(Similarity)
37-
34+
Similarity = len(set(BagOfWords) & set(WordList)) / len(set(BagOfWords + WordList))
35+
SimilarityPercentage = Similarity * 100
36+
3837
if Similarity > MaxSimilarity:
38+
print(f"Similarity: {SimilarityPercentage:.2f}%")
3939
MaxSimilarity = Similarity
4040
MostSimilarPattern = intent_class
41-
42-
return MostSimilarPattern
41+
42+
if MostSimilarPattern:
43+
return MostSimilarPattern
44+
else:
45+
raise NoMatchingIntentError("No matching intent class found.")
4346

4447
def responsecompare(input_string, intents_file_path, intent_class):
4548
MaxSimilarity = 0
46-
MostSimilarResponse = ""
49+
MostSimilarResponse = None
4750

4851
responses = []
4952
Similarity = 0
@@ -52,21 +55,26 @@ def responsecompare(input_string, intents_file_path, intent_class):
5255
intents = json.load(json_data)
5356

5457
BagOfWords = Tokenize(input_string)
55-
56-
responses = intent_class.get('responses')
58+
59+
if intent_class is not None:
60+
responses = intent_class.get('responses')
61+
else:
62+
raise NoMatchingIntentError("No matching intent class found.")
5763

5864
for response in responses:
5965
WordList = Tokenize(response)
60-
for word in WordList:
61-
if word in BagOfWords:
62-
Similarity = Similarity + 1
63-
print(Similarity)
64-
66+
Similarity = len(set(BagOfWords) & set(WordList)) / len(set(BagOfWords + WordList))
67+
SimilarityPercentage = Similarity * 100
68+
6569
if Similarity > MaxSimilarity:
70+
print(f"Similarity: {SimilarityPercentage:.2f}%")
6671
MaxSimilarity = Similarity
6772
MostSimilarResponse = response
6873

69-
return MostSimilarResponse
70-
74+
if MostSimilarResponse:
7175

76+
return MostSimilarResponse
77+
78+
else:
7279

80+
raise NoMatchingIntentError("No matching response found.")

__pycache__/Janex.cpython-311.pyc

985 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)