|
| 1 | +import json |
| 2 | +from Janex.vectortoolkit import * |
| 3 | +from Janex.word_manipulation import * |
| 4 | + |
| 5 | +class IntentClassifier: |
| 6 | + def __init__(self): |
| 7 | + self.intents_file_path = None |
| 8 | + self.thesaurus_file_path = None |
| 9 | + self.vectors_file_path = None |
| 10 | + |
| 11 | + def set_vectorsfp(self, vectors_file_path): |
| 12 | + self.vectors_file_path = vectors_file_path |
| 13 | + print(f"Janex: Pre-trained vector file path set to {vectors_file_path}") |
| 14 | + |
| 15 | + def set_intentsfp(self, intents_file_path): |
| 16 | + self.intents_file_path = intents_file_path |
| 17 | + |
| 18 | + def set_dimensions(self, dimensions): |
| 19 | + self.dimensions = dimensions |
| 20 | + |
| 21 | + def load_intents(self): |
| 22 | + with open(self.intents_file_path, "r") as json_file: |
| 23 | + data = json.load(json_file) |
| 24 | + return data |
| 25 | + |
| 26 | + def load_vectors(self): |
| 27 | + with open(self.vectors_file_path, "r") as json_file: |
| 28 | + data = json.load(json_file) |
| 29 | + return data |
| 30 | + |
| 31 | + def train_vectors(self): |
| 32 | + vectors = {} |
| 33 | + |
| 34 | + intents = self.load_intents() |
| 35 | + |
| 36 | + for intent_class in intents["intents"]: |
| 37 | + for pattern in intent_class["patterns"]: |
| 38 | + if pattern is not None: |
| 39 | + pattern_tokens = tokenize(pattern) |
| 40 | + for token in pattern_tokens: |
| 41 | + token_vectors = string_vectorize(token) |
| 42 | + token_vectors = reshape_array_dimensions(token_vectors, self.dimensions) |
| 43 | + token_vectors = token_vectors.tolist() |
| 44 | + vectors[token] = token_vectors |
| 45 | + for response in intent_class["responses"]: |
| 46 | + if response is not None: |
| 47 | + response_tokens = tokenize(response) |
| 48 | + for token in response_tokens: |
| 49 | + token_vectors = string_vectorize(token) |
| 50 | + token_vectors = reshape_array_dimensions(token_vectors, self.dimensions) |
| 51 | + vectors[token] = token_vectors.tolist() |
| 52 | + |
| 53 | + with open(self.vectors_file_path, "w") as vectors_file: |
| 54 | + json.dump(vectors, vectors_file) |
| 55 | + |
| 56 | + def classify(self, input_string): |
| 57 | + predefined_vectors = self.load_vectors() |
| 58 | + intents = self.load_intents() |
| 59 | + |
| 60 | + input_tokens = tokenize(input_string) |
| 61 | + vectors_to_classify = [] |
| 62 | + highest_similarity = 0 |
| 63 | + |
| 64 | + for token in input_tokens: |
| 65 | + if token in predefined_vectors: |
| 66 | + token_vectors = predefined_vectors[token] |
| 67 | + else: |
| 68 | + token_vectors = string_vectorize(token) |
| 69 | + |
| 70 | + token_vectors = reshape_array_dimensions(token_vectors, self.dimensions) |
| 71 | + vectors_to_classify.append(token_vectors) |
| 72 | + |
| 73 | + for intent_class in intents["intents"]: |
| 74 | + intent_vectors_to_classify = [] |
| 75 | + for pattern in intent_class["patterns"]: |
| 76 | + pattern_tokens = tokenize(pattern) |
| 77 | + for token in pattern_tokens: |
| 78 | + if token in predefined_vectors: |
| 79 | + pattern_token_vectors = predefined_vectors[token] |
| 80 | + intent_vectors_to_classify.append(pattern_token_vectors) |
| 81 | + |
| 82 | + vectors_to_classify = reshape_array_dimensions(vectors_to_classify, self.dimensions) |
| 83 | + intent_vectors_to_classify = reshape_array_dimensions(intent_vectors_to_classify, self.dimensions) |
| 84 | + |
| 85 | + similarity = calculate_cosine_similarity(vectors_to_classify, intent_vectors_to_classify) |
| 86 | + |
| 87 | + if similarity > highest_similarity: |
| 88 | + highest_similarity = similarity |
| 89 | + most_similar_class = intent_class |
| 90 | + |
| 91 | + if most_similar_class: |
| 92 | + return most_similar_class |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + Classifer = IntentClassifier() |
| 96 | + |
| 97 | + Classifier.set_intentsfp("intents.json") |
| 98 | + Classifier.set_vectorsfp("vectors.json") |
| 99 | + |
| 100 | + Classifier.train_vectors() |
| 101 | + |
| 102 | + Input = input("You: ") |
| 103 | + |
| 104 | + classification = Classifier.classify(Input) |
| 105 | + |
| 106 | + Response = random.choice(classification["responses"]) |
| 107 | + |
| 108 | + print(Response) |
0 commit comments