def test_ylogger(self): client_context = ClientContext(TestClient(), "testid") snapshot = YLoggerSnapshot() self.assertIsNotNone(snapshot) self.assertEquals( str(snapshot), "Critical(0) Fatal(0) Error(0) Exception(0) Warning(0) Info(0), Debug(0)" ) YLogger.reset_snapshot() YLogger.critical(client_context, "Test Message") snapshot = YLogger.snapshot() self.assertIsNotNone(snapshot) self.assertEquals( str(snapshot), "Critical(1) Fatal(0) Error(0) Exception(0) Warning(0) Info(0), Debug(0)" ) YLogger.fatal(client_context, "Test Message") snapshot = YLogger.snapshot() self.assertIsNotNone(snapshot) self.assertEquals( str(snapshot), "Critical(1) Fatal(1) Error(0) Exception(0) Warning(0) Info(0), Debug(0)" ) YLogger.error(client_context, "Test Message") snapshot = YLogger.snapshot() self.assertIsNotNone(snapshot) self.assertEquals( str(snapshot), "Critical(1) Fatal(1) Error(1) Exception(0) Warning(0) Info(0), Debug(0)" ) YLogger.exception(client_context, "Test Message", Exception("Test error")) snapshot = YLogger.snapshot() self.assertIsNotNone(snapshot) self.assertEquals( str(snapshot), "Critical(1) Fatal(1) Error(1) Exception(1) Warning(0) Info(0), Debug(0)" ) YLogger.warning(client_context, "Test Message") snapshot = YLogger.snapshot() self.assertIsNotNone(snapshot) self.assertEquals( str(snapshot), "Critical(1) Fatal(1) Error(1) Exception(1) Warning(1) Info(0), Debug(0)" ) YLogger.info(client_context, "Test Message") snapshot = YLogger.snapshot() self.assertIsNotNone(snapshot) self.assertEquals( str(snapshot), "Critical(1) Fatal(1) Error(1) Exception(1) Warning(1) Info(1), Debug(0)" ) YLogger.debug(client_context, "Test Message") snapshot = YLogger.snapshot() self.assertIsNotNone(snapshot) self.assertEquals( str(snapshot), "Critical(1) Fatal(1) Error(1) Exception(1) Warning(1) Info(1), Debug(1)" )
def resolve(self, client_context): try: return self.resolve_to_string(client_context) except Exception as excep: YLogger.exception(client_context, "Failed to resolve", excep) return ""
def execute(self, client_context, data): YLogger.debug(client_context, "Hot Reload Admin - [%s]", data) try: commands = HotReloadAdminExtension.split_into_commands(data) if commands[0] == 'RELOAD': entity = commands[1] if entity in [ 'AIML', 'DENORMAL', 'NORMAL', 'GENDER', 'PERSON', 'PERSON2', 'PROPERTIES', 'DEFAULTS', 'REGEX', 'PATTERNS', 'TEMPLATES', 'SET', 'MAP', 'RDF' ]: if entity == 'DENORMAL': return HotReloadAdminExtension.reload_denormal( client_context) elif entity == 'NORMAL': return HotReloadAdminExtension.reload_normal( client_context) elif entity == 'GENDER': return HotReloadAdminExtension.reload_gender( client_context) elif entity == 'PERSON': return HotReloadAdminExtension.reload_person( client_context) elif entity == 'PERSON2': return HotReloadAdminExtension.reload_person2( client_context) elif entity == 'PROPERTIES': return HotReloadAdminExtension.reload_properties( client_context) elif entity == 'DEFAULTS': return HotReloadAdminExtension.reload_defaults( client_context) elif entity == 'REGEX': return HotReloadAdminExtension.reload_regex( client_context) elif entity == 'PATTERNS': return HotReloadAdminExtension.reload_patterns( client_context) elif entity == 'TEMPLATES': return HotReloadAdminExtension.reload_templates( client_context) elif entity == 'AIML': if len(commands) == 3: aimlname = commands[2] return HotReloadAdminExtension.reload_aiml( client_context, aimlname) else: raise Exception("Missing AIML name") elif entity == 'SET': if len(commands) == 3: setname = commands[2] return HotReloadAdminExtension.reload_set( client_context, setname) else: raise Exception("Missing Set name") elif entity == 'MAP': if len(commands) == 3: mapname = commands[2] return HotReloadAdminExtension.reload_map( client_context, mapname) else: raise Exception("Missing Map name") elif entity == 'RDF': if len(commands) == 3: rdfname = commands[2] return HotReloadAdminExtension.reload_rdf( client_context, rdfname) else: raise Exception("Missing RDF name") elif entity == 'ALL': if len(commands) == 3: entities = commands[2] if entities == 'AIML': return HotReloadAdminExtension.reload_aimls( client_context) elif entities == 'MAPS': return HotReloadAdminExtension.reload_maps( client_context) elif entities == 'SETS': return HotReloadAdminExtension.reload_sets( client_context) elif entities == 'RDFS': return HotReloadAdminExtension.reload_rdfs( client_context) else: return HotReloadAdminExtension.reload_all( client_context) else: raise Exception("Unknonw reload entity [%s]" % entity) elif commands[0] == 'COMMANDS': return "RELOAD [DENORMAL|NORMAL|GENDER|PERSON|PERSON2|PROPERTIES|DEFAULTS|REGEX|PATTERNS|TEMPLATES] | [SET|MAP|RDF] NAME | ALL [AIML|MAPS|SETS|RDFS]" else: raise Exception("Unknonw reload command [%s]" % commands[0]) except Exception as e: YLogger.exception(client_context, "Failed to execute hot reload extension", e) return "Hot Reload Admin Error"
def receive_message(): try: return FACEBOOK_CLIENT.receive_message(request) except Exception as e: YLogger.exception(None, "Facebook Error", e)
def get_value(self, client_context, value=None): userid = client_context.userid variables = client_context.bot.conversations[userid].properties bot = client_context.bot nlp = client_context.brain.nlp text = variables["data"] #todo the problem with some forms of text like #if bot.configuration.emotive: #YLogger.info(self, "bot is in emotive mode") try: # print(f"in sentiment_analysis, text: {text}") # NOTE: On only 2 occasions text has been none when trying to analyze sentiment # When this occured the question was "why just say...", a misinterpretation from kaldi # FIXME: We need a more sophisticated error check, fix after demo. # if text is None: # return sentiment = "neutral" sentiment, sentiment_distribution = nlp.sentiment_analysis.get_sentence_sentiment(text) except Exception as exception: YLogger.exception(self, "sentiment analysis module broke", exception) raise exception sentiment_value = None final_sentiment_value = None if bot.facial_expression_recognition is not None: if len(bot.facial_expression_recognition.values): last_fer_value = 0#bot.facial_expression_recognition.last_fer_value #the logic of mixing fer and sentiment goes here alpha = nlp.sentiment_analysis.alpha positive_threshold = nlp.sentiment_analysis.positive_threshold negative_threshold = nlp.sentiment_analysis.negative_threshold sentiment_value = nlp.sentiment_analysis.expected_sentiment_value(sentiment_distribution) print("FER: ", last_fer_value) print("Sentiment:", sentiment_value) final_sentiment_value = alpha * last_fer_value + (1-alpha)*sentiment_value print("Final sentiment: ", final_sentiment_value) if final_sentiment_value > positive_threshold: sentiment = "positive" elif final_sentiment_value < positive_threshold and final_sentiment_value > negative_threshold: sentiment = "neutral" else: sentiment = "negative" else: if sentiment == "positive": sentiment_value = 1 final_sentiment_value = 1 elif sentiment == "neutral": sentiment_value = 0 final_sentiment_value = 0 elif sentiment == "negative": sentiment_value = -1 final_sentiment_value = -1 bot.sentiment.append_sentiment(sentiment_value) bot.sentiment.append_sentiment_distribution(sentiment_distribution) bot.sentiment.append_final_sentiment(final_sentiment_value) if bot.configuration.emotive: YLogger.info(self, "bot is in emotive mode") else: YLogger.info(self, "bot is in non emotive mode") sentiment = "neutral" print("In GetSentiment sentiment: {}".format(sentiment)) return sentiment
def save_conversation(self, client_context): userid = client_context.userid bot_properties = client_context.bot.conversations[userid].properties questions = client_context.bot.conversations[userid].questions answers = client_context.bot.conversations[userid].answers try: last_question = questions[-1].sentences except Exception as e: YLogger.exception(self, "question sentences length is zero", e) raise e try: last_sentiment_value = client_context.bot.sentiment.last_sentiment_value except Exception as e: # print("Exception caught for last_sentiment_value: {}".format(e)) last_sentiment_value = None try: last_fer_value = client_context.bot.facial_expression_recognition.last_fer_value except Exception as e: # print("Exception caught for last_fer_value: {}".format(e)) last_fer_value = None try: last_final_sentiment_value = client_context.bot.sentiment.last_final_sentiment_value except Exception as e: # print("Exception caught for last_final_sentiment_value: {}".format(e)) last_final_sentiment_value = None try: # todo this doesn't handle good when sentence is empty last_answer = answers[-1].sentences except Exception as e: YLogger.exception(self, "answer sentences length is zero", e) raise e try: image_filename = answers[-1].robot[0]['robot']['image']['filename'] except Exception as e: image_filename = None try: duration = answers[-1].robot[0]['robot']['image']['duration'] except Exception as e: duration = None try: video_filename = answers[-1].robot[0]['robot']['video']['filename'] except Exception as e: video_filename = None try: session_number = bot_properties["session_number"] except Exception as e: session_number = None try: username = bot_properties["username"] except Exception as e: username = None question_sentence_text, answer_sentence_text = self.create_conversation( last_question, last_answer) # print("last sentiment: ", last_sentiment_value) # print("final_sentiment_value: ", last_final_sentiment_value) document = { "conversation": { "question": question_sentence_text, "answer": answer_sentence_text, "timestamp": datetime.datetime.now(), "sentiment": last_sentiment_value, "fer": last_fer_value, "final_sentiment_value": last_final_sentiment_value }, "session_info": { "session_number": session_number, "username": username }, "image": { "filename": image_filename, "duration": duration, }, "video": { "filename": video_filename } } try: # writing to db self.db[self._config.collection_name].insert_one(document) except Exception as e: YLogger.error(self, e)
def worker_run_loop(self): majordomo_worker = MajorDomoWorker( self.configuration.client_configuration) response = None username = None client_context = None while True: print(response) request = majordomo_worker.receive(response) if request is None: YLogger.debug(self, "worker was interrupted") break if type(request) is ReadyRequest: YLogger.info(self, "ready request") response = [request.command] elif type(request) is UserRequest: YLogger.info(self, "user request") username = request.username client_context = self.client_context client_context.bot.initiate_conversation_storage() elif type(request) is SessionRequest: YLogger.info(self, "session request") if client_context is None: YLogger.debug( self, "Client context is not initiated. Messages are out of order" ) client_context = self.client_context client_context.bot.initiate_conversation_storage() if username is not None: question = self.initial_question(request, username) print("question", question) answer = self.process_question_with_options( client_context, question) print("answer", answer) response = self.render_response(client_context, answer) else: response = ["username is not specified"] elif type(request) is QuestionRequest: try: YLogger.info(self, "question request") if client_context is not None: print(request.question) #answer = self.process_question(client_context, request.question) answer = self.process_question_with_options( client_context, request.question) client_context.bot.save_conversation(client_context) response = self.render_response(client_context, answer) else: response = [ "client context is not initiated. Initial Session request" ] except Exception as e: YLogger.exception(self, "chatbot encountered an internal crash", e)
def worker_run_loop2(self): ''' This loop is for the case that we don't have session and user info :return: ''' majordomo_worker = MajorDomoWorker( self.configuration.client_configuration) response = None client_context = None session_number = None username = None print("before while") while True: print(str(response)) request = majordomo_worker.receive(response) if request is None: YLogger.debug(self, "worker was interrupted") break if type(request) is ReadyRequest: YLogger.info(self, "ready request") # if the robot sends the ready message again # it will NOT reload anything. # we can change this part to use it as a signal # to initialize client_context client_context = self.client_context #MAYBE initialize client context??! client_context.bot.initiate_conversation_storage() response = [request.command] elif type(request) is QuestionRequest: try: YLogger.info(self, "question request") if client_context is not None: print(request.question) # if self._first_time: # client_context = self.client_context # self._first_time = False userid = client_context.userid if session_number is None: session_number = 1 if username is None: username = "******" client_context.bot.conversations[userid].set_property( "session_number", session_number) client_context.bot.conversations[userid].set_property( "username", username) client_context.bot.facial_expression_recognition.append( request.emotion) answer = self.process_question_with_options( client_context, request.question) #client_context.bot.save_conversation(client_context) response = self.render_response(client_context, answer) else: response = [ "client context is not initiated. Initial Session request" ] except Exception as e: YLogger.exception(self, "chatbot encounter an internal crash", e)
def worker_run_loop1(self): majordomo_worker = MajorDomoWorker( self.configuration.client_configuration) response = None client_context = None print("before while") while True: print(str(response)) request = majordomo_worker.receive(response) if request is None: YLogger.debug(self, "worker was interrupted") break if type(request) is ReadyRequest: YLogger.info(self, "ready request") # if the robot sends the ready message again # it will NOT reload anything. # we can change this part to use it as a signal # to initialize client_context client_context = self.client_context #MAYBE initialize client context??! client_context.bot.initiate_conversation_storage() response = [request.command] # elif type(request) is ServiceRequest: # self.service = ServiceFactory.get_service(request.service_name) elif type(request) is SessionUserRequest: YLogger.info(self, "session user request") if client_context: session_number = request.session_number username = request.username if self.session_saving_mode: #response = ["welcome to session saving mode"]#todo should pull the latest question we asked last_question = client_context.bot.conversations[ self.configuration.client_configuration. id].questions[-1].sentences[-1].response response = self.render_response( client_context, last_question) else: question = self.initial_question( request, request.username) print("question", question) answer = self.process_question_with_options( client_context, question) print("answer", answer) response = self.render_response(client_context, answer) else: response = [ "client context is not initiated. Initial Session request" ] elif type(request) is QuestionRequest: try: YLogger.info(self, "question request") if client_context is not None: print(request.question) # if self._first_time: # client_context = self.client_context # self._first_time = False userid = client_context.userid client_context.bot.conversations[userid].set_property( "session_number", session_number) client_context.bot.conversations[userid].set_property( "username", username) client_context.bot.facial_expression_recognition.append( request.emotion) #answer = self.process_question_service(client_context, request.question) # todo this part should be cleaned up preprocessing question string # import string # exclude = set(string.punctuation) # question_no_punctuation = ''.join(ch for ch in request.question if ch not in exclude) answer = self.process_question_with_options( client_context, request.question) client_context.bot.save_conversation(client_context) response = self.render_response(client_context, answer) else: response = [ "client context is not initiated. Initial Session request" ] except Exception as e: YLogger.exception(self, "chatbot encounter an internal crash", e)