You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.4 KiB
Python

# -*- coding: iso-8859-1 -*-
import random
import json
import pickle
import numpy as np
import nltk
from nltk.stem import WordNetLemmatizer
from tensorflow.keras.models import load_model
# Vorlage Code von NeuralNines YT Channel (https://www.youtube.com/watch?v=1lwddP0KUEg&t=1022s)
class Osiris_Sprache:
"""Osiris Sprach Tools
Enth<74>lt alle Funktionen zur Handhabung und Verarbeitung von Input Messages
Returns:
None
"""
lematizer: WordNetLemmatizer
intents: json
words: None
classes: None
model: None
def __init__(self):
self.lematizer = WordNetLemmatizer()
self.intents = json.loads(open('intents.json').read())
self.words = pickle.load(open('words.pkl', 'rb'))
self.classes = pickle.load(open('classes.pkl', 'rb'))
self.model = load_model('osiris_sprachmodel.h5')
def clean_up_sentence(self, sentence):
sentence_words = nltk.word_tokenize(sentence)
sentence_words = [self.lematizer.lemmatize(
word) for word in sentence_words]
return sentence_words
def bag_of_words(self, sentence):
sentence_words = self.clean_up_sentence(sentence)
bag = [0] * len(self.words)
for w in sentence_words:
for i, word in enumerate(self.words):
if word == w:
bag[i] = 1
return np.array(bag)
def predict_class(self, sentence):
bagOfWords = self.bag_of_words(sentence)
res = self.model.predict(np.array([bagOfWords]))[0]
ERROR_THRESHOLD = 0.25
results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append(
{'intent': self.classes[r[0]], 'probability': str(r[1])})
return return_list
def get_response(self, intent_list):
tag = intent_list[0]['intent']
list_of_intents = self.intents['intents']
for intent in list_of_intents:
if intent['tag'] == tag:
result = random.choice(i['response'])
break
return result
#print('Du kannst jetzt mit mir Reden!')
# while True:
# message = input("")
# ints = predict_class(message)
# res = get_response(ints, intents)
# print(res)