def getResponse(self): # Get user message userMessage = self.userInput.get() userin = userMessage # language detection user_lang = languageDeteaction(userMessage) if (user_lang != 'en'): userMessage = translateThis(userMessage) # Ignore empty messages if userMessage == "": return # Clear the user input self.userInput.delete(0, "end") # Get our reply raw_reply = m.getFinalOutput(self.loaded_clf, userMessage) reply = "" if (user_lang != 'en'): translated = translateTo(raw_reply, user_lang) reply = "BakerAI: " + translated + "\n" else: reply = "BakerAI: " + raw_reply + "\n" # Send reply to client storedUserMessage = "You: " + userin + "\n" self.outputBox.configure(state="normal") self.outputBox.insert(tk.END, storedUserMessage) self.outputBox.insert(tk.END, reply) self.outputBox.configure(state="disabled")
def testDefault(self): # ensure we can get a default response when not discussing any subjects print("Default Response testing:") totalSuccess = 0 totalFail = 0 for test in DEFAULT_TESTS: modelResponse = m.getFinalOutput(self.loaded_clf, test) try: self.assertTrue(modelResponse in m.DEFAULT_RESPONSES) totalSuccess += 1 except AssertionError as e: print(f'AI response failed test for input: "{test}"') totalFail += 1 if (totalFail == 0): print("----No tests failed----") print(f'Passed {totalSuccess} tests, failed {totalFail} tests.\n')
def testIntent(self): print("Negative intentent testing:") totalSuccess = 0 totalFail = 0 # ensure an extremely negative response is negative for test in NEGATIVE_TESTS: modelResponse = m.getFinalOutput(self.loaded_clf, test) try: self.assertTrue(modelResponse in m.NEGATIVE_RESPONSES) totalSuccess += 1 except AssertionError as e: print(f'AI response failed test for input: "{sampleInput}"') totalFail += 1 if (totalFail == 0): print("----No tests failed----") print(f'Passed {totalSuccess} tests, failed {totalFail} tests.\n')
def getResponse(self): # Get user message userMessage = self.userInput.get() # Ignore empty messages if userMessage == "": return # Clear the user input self.userInput.delete(0, "end") # Get our reply reply = "BakerAI: " + m.getFinalOutput(self.loaded_clf, userMessage) + "\n" # Send reply to client storedUserMessage = "You: " + userMessage + "\n" self.outputBox.configure(state="normal") self.outputBox.insert(tk.END, storedUserMessage) self.outputBox.insert(tk.END, reply) self.outputBox.configure(state="disabled")
def openCloseConnection(self, connectionInfo): self.sock.bind(connectionInfo) # Bind to an address to recive client responses self.sock.listen(1) # Get one clinet conn, addr, = self.sock.accept() loaded_clf = m.load_sentiment_analysis()[0] while(True): # While we still get valid responses from our 1 client clientResponse=self.getResponse(conn) if not clientResponse: # If the data we got is empty or terminating condition break print("Client message: " + clientResponse) response = m.getFinalOutput(loaded_clf, clientResponse) if clientResponse == "quit": response = "Goodbye!" self.sendResponse(response, conn) conn.close() print("We've lost our client")
def getResponse(self): # Get user message userMessage = self.userInput.get() # Language is usable forms shortLan = self.languages[self.lan.get()] # Ignore empty messages if userMessage == "": return # Clear the user input self.userInput.delete(0, "end") # Check if the wiki command is in the chat if "wiki " == userMessage.lower()[0:5]: # Send user message without the wiki portion, and save the response wikiReply = self.wikiResponse(userMessage[5:], shortLan) self.addExchange(userMessage, wikiReply) return # Translate input to english to send to our bot userEnglish = m.translate(userMessage, shortLan, 'en') # Get our reply reply = m.getFinalOutput(self.loaded_clf, userEnglish) # Translate reply to proper language replyLan = m.translate(reply, 'en', shortLan) self.addExchange(userMessage, replyLan)
def getResponse(self): # Get user message userMessage = self.userInput.get() userMessageInEng = userMessage #translate the user message into english if its not in english detected_language = translate.detect_language(userMessage) if detected_language != 'en': userMessageInEng = translate.translate_to_English(userMessage) # Ignore empty messages if userMessageInEng == "": return # Clear the user input self.userInput.delete(0, "end") # Get our reply reply = "BakerAI: " + m.getFinalOutput( self.loaded_clf, userMessageInEng, detected_language['language']) + "\n" # Send reply to client storedUserMessage = "You: " + userMessage + "\n" self.outputBox.configure(state="normal") self.outputBox.insert(tk.END, storedUserMessage) self.outputBox.insert(tk.END, reply) self.outputBox.configure(state="disabled")
def testResponses(self): print("Accurate Response testing:") # ensure we can get desired responses for very typical questions totalSuccess = 0 totalFail = 0 for intent in self.data["intents"]: possibleOutputs = [] for responses in intent["responses"]: possibleOutputs.append(responses) for pattern in intent["patterns"]: #sampleInput = one of the input sentences for this tag sampleInput = pattern modelResponse = m.getFinalOutput(self.loaded_clf, sampleInput) #possibleOutputs = list of all outputs for that tag try: self.assertTrue(modelResponse in possibleOutputs) totalSuccess += 1 except AssertionError as e: print( f'AI response failed test for input: "{sampleInput}"') totalFail += 1 if (totalFail == 0): print("----No tests failed----") print(f'Passed {totalSuccess} tests, failed {totalFail} tests.\n')