def on_close(self): FileLogger().Warn("Connection closed")
def CalculateRequirement(self, sentenceList, parameterList, delete=True): query = """SELECT Conversation_Sentence_Requirement.Comparison, Conversation_Sentence_Requirement.Value, Conversation_Requirement.Name FROM Conversation_Sentence_Requirement, Conversation_Requirement WHERE Conversation_Sentence_Requirement.RequirementID = Conversation_Requirement.ID AND Conversation_Sentence_Requirement.SentenceID='{0}' GROUP BY Conversation_Sentence_Requirement.SentenceID, Conversation_Sentence_Requirement.RequirementID, Conversation_Sentence_Requirement.Comparison """ deleteList = [] for sentenceID in sentenceList.iterkeys(): sqlResult = db().Fetchall(query.format(sentenceID)) for r in sqlResult: requirementName = r[2].title() if requirementName not in parameterList and r[1].lower( ) != self.__NoneTag.lower(): FileLogger().Error( "SentenceResolver Line 171: Requirement missing in parameter list: {0}" .format(requirementName)) deleteList.append(sentenceID) continue if r[0] is None: if type(parameterList[requirementName]) == list and r[ 1].lower() not in parameterList[requirementName]: deleteList.append(sentenceID) elif type( parameterList[requirementName] ) == str and parameterList[requirementName].lower( ) != r[1].lower(): deleteList.append(sentenceID) else: sentenceList[sentenceID].AddPriority( self.__RequirementBonus) continue else: if r[1].lower() == self.__NoneTag.lower(): if (r[0] == "eq" and (requirementName in parameterList and parameterList[requirementName] is not None)): deleteList.append(sentenceID) continue if (r[0] == "ne" and not (requirementName in parameterList and parameterList[requirementName] is not None)): deleteList.append(sentenceID) continue sentenceList[sentenceID].AddPriority( self.__RequirementBonus) continue if r[0] == "lt" and not parameterList[requirementName] < r[ 1]: deleteList.append(sentenceID) continue if r[0] == "le" and not parameterList[ requirementName] <= r[1]: deleteList.append(sentenceID) continue if r[0] == "eq" and not parameterList[ requirementName] == r[1]: deleteList.append(sentenceID) continue if r[0] == "ne" and not parameterList[ requirementName] != r[1]: deleteList.append(sentenceID) continue if r[0] == "ge" and not parameterList[ requirementName] >= r[1]: deleteList.append(sentenceID) continue if r[0] == "gt" and not parameterList[requirementName] > r[ 1]: deleteList.append(sentenceID) continue sentenceList[sentenceID].AddPriority( self.__RequirementBonus) if delete: for d in list(set(deleteList)): del sentenceList[d] return {'sentenceList': sentenceList, 'deleteList': deleteList}
def on_inactivity_timeout(self, error): FileLogger().Error('Inactivity timeout: {}'.format(error))
def on_listening(self): FileLogger().Info('Service is listening')
def on_connected(self): FileLogger().Info('Connection was successful')
def on_error(self, error): FileLogger().Error('Error received: {}'.format(error))
def Process(self, PipelineArgs): sentence = PipelineArgs.GetRandomSentenceWithHighestValue() FileLogger().Info("ProcessResponse, Process(), Sentence: {0}".format(sentence)) responseFound = True if sentence is None or sentence.Rating < self.__sentenceRatingThreshold: responseFound = False if responseFound: user = User().LoadObject() PipelineArgs.ResponseRaw = sentence.GetSentenceString(user.Formal) PipelineArgs.Response = PipelineArgs.ResponseRaw PipelineArgs.ResponseID = sentence.ID PipelineArgs.Animation = sentence.GetAnimation() PipelineArgs.ResponseFound = True PipelineArgs.BasewordTrimmedInput = NLP.TrimBasewords(PipelineArgs) PipelineArgs.FullyTrimmedInput = NLP.TrimStopwords(PipelineArgs.BasewordTrimmedInput, PipelineArgs.Language) contextParameter = ContextParameter().LoadObject(240) if sentence.HasInteraction(): contextParameter.InteractionName = sentence.InteractionName sentenceAction = sentence.GetAction() if sentenceAction != None and len(sentenceAction["Module"]) > 0: FileLogger().Info("ProcessResponse, Process(), Call Action: {0}, {1}, {2}".format(sentenceAction["Module"], sentenceAction["Class"], sentenceAction["Function"])) actionResult = Action.CallFunction(sentenceAction["Module"], sentenceAction["Class"], sentenceAction["Function"], PipelineArgs) if actionResult["ResultType"].title() is "Error": PipelineArgs.Response = sentence.GetActionErrorResponse(PipelineArgs.Language, user.Formal) PipelineArgs.ResponseRaw = None PipelineArgs.Error.append("ProcessResponse - Action Error") else: contextParameter.SetInput(actionResult["Input"]) contextParameter.SetResult(actionResult["Result"]) contextParameter.SaveObject() contextParameter.ResetInteraction() contextParameterDict = contextParameter.GetParameterDictionary() keywords = re.findall(r"\{(.*?)\}", PipelineArgs.Response) for keyword in keywords: if keyword.title() in contextParameterDict: replaceword = contextParameterDict[keyword.title()] if replaceword is None or replaceword == "Unknown": replaceword = "" else: replaceword = "'{0}'".format(replaceword) PipelineArgs.Response = PipelineArgs.Response.replace("{{{0}}}".format(keyword.lower()), str(replaceword)) else: PipelineArgs.Response = PipelineArgs.Response.replace("{{{0}}}".format(keyword.lower()), "") FileLogger().Error("ProcessResponse Line 63: Parameter missing: '{0}'".format(keyword)) contextParameter.UnsetInputAndResult() contextParameter.SaveObject() elif not responseFound and self.__aliceAsFallback: PipelineArgs.Response = self.__alice.GetResponse(PipelineArgs.Input) PipelineArgs.ResponseFound = True PipelineArgs.TrainConversation = False FileLogger().Info("ProcessResponse, Process(), Response: {0}".format(PipelineArgs.Response)) return PipelineArgs
def Rebuild(self, datasetName, imageSize=None): FileLogger().Info("ModelMonitor: Rebuild {0} Model".format(datasetName)) ComputerVision().TrainModel(datasetName, imageSize) path = os.path.join(self.__DatasetBasePath, datasetName) folderHash = Hashing.GetDirHash(path) np.save(os.path.join(self.__DatasetBasePath, self.__HashFileName.format(datasetName)), folderHash)
def ProcessCategory(self, category, user=None): FileLogger().Info( "ProcessTrigger, ProcessCategory(), Category: {0}".format( category)) if (user is None): user = User().LoadObject() language = self.__DefaultLanguage if (user.Language is not None and user.Language != "None"): language = user.Language sentenceList = SentenceResolver().GetSentenceByCategory( category, language, (user.Admin or user.Trainer)) contextParameter = ContextParameter().LoadObject(240) contextParameterDict = contextParameter.GetParameterDictionary() calculationResult = SentenceResolver().CalculateRequirement( sentenceList, contextParameterDict) sentenceList = calculationResult["sentenceList"] if (len(sentenceList) == 0): FileLogger().Warn( "ProcessTrigger, ProcessCategory(), No Sentences found!") return "" responseID = random.choice(sentenceList.keys()) sentence = sentenceList[responseID] FileLogger().Info( "ProcessTrigger, ProcessCategory(), Sentence: {0}".format( sentence)) responseString = sentence.GetSentenceString(user.Formal) sentenceAction = sentence.GetAction() if sentenceAction != None and len(sentenceAction["Module"]) > 0: FileLogger().Info( "ProcessTrigger, ProcessCategory(), Call Action: {0}, {1}, {2}" .format(sentenceAction["Module"], sentenceAction["Class"], sentenceAction["Function"])) actionResult = Action.CallFunction(sentenceAction["Module"], sentenceAction["Class"], sentenceAction["Function"]) if actionResult["ResultType"].title() is "Error": responseString = sentence.GetActionErrorResponse( language, user.Formal) else: contextParameter.SetInput(actionResult["Input"]) contextParameter.SetResult(actionResult["Result"]) contextParameter.SaveObject() contextParameterDict = contextParameter.GetParameterDictionary() keywords = re.findall(r"\{(.*?)\}", responseString) for keyword in keywords: if keyword.title() in contextParameterDict: replaceword = contextParameterDict[keyword.title()] if replaceword is None or replaceword == "Unknown": replaceword = "" responseString = responseString.replace( "{{{0}}}".format(keyword.lower()), str(replaceword)) else: responseString = responseString.replace( "{{{0}}}".format(keyword.lower()), "") return responseString
def Process(self, PipelineArgs): FileLogger().Info("AnalyzeScope, Process()") sentenceList = {} wordParameterList = [] user = User().LoadObject() for word in PipelineArgs.WordList: if (len(word.SynonymList) > 0): wordList = "'{0}'".format("', '".join(word.SynonymList)) sentenceList = SentenceResolver().GetSentencesByKeyword( sentenceList, wordList, word.NormalizedWord, word.Language, True, (user.Admin or user.Trainer)) sentenceList = SentenceResolver().GetSentencesByKeyword( sentenceList, "'{0}'".format(word.NormalizedWord), word.NormalizedWord, word.Language, False, (user.Admin or user.Trainer)) wordParameterList += list( set(word.ParameterList) - set(wordParameterList)) sentenceList = SentenceResolver().GetSentencesByParameter( sentenceList, wordParameterList, PipelineArgs.Language, (user.Admin or user.Trainer)) contextParameter = ContextParameter().LoadObject(240) contextParameter.UpdateParameter("Wordtype", wordParameterList) contextParameter.SaveObject() if self.__RemoveStopwordOnlySentences: sentenceList = SentenceResolver().RemoveStopwordOnlySentences( sentenceList) if contextParameter.InteractionName is not None: sentenceList = SentenceResolver().GetSentenceByInteraction( sentenceList, contextParameter.InteractionName, PipelineArgs.Language, (user.Admin or user.Trainer)) else: SentenceResolver().AddInteractionBonus(sentenceList) sentenceParameterList = None for sentenceID in sentenceList.iterkeys(): if sentenceList[sentenceID].HasInteraction(): for word in PipelineArgs.WordList: for parameter in word.ParameterList: interactionName = "{0}{1}".format( sentenceList[sentenceID].InteractionName, parameter) contextParameter.InteractionData[ interactionName.title()] = word.Word if sentenceParameterList is None: sentenceParameterList = PipelineArgs.GetInputSentenceParameter( ) for sentenceParameter in sentenceParameterList: interactionName = "{0}{1}".format( sentenceList[sentenceID].InteractionName, sentenceParameter) dateObject = PipelineArgs.GetParsedStentenceDate() formatter = "%d.%m.%Y H:%M:%S" if sentenceParameter is "date": formatter = "%d.%m.%Y" if sentenceParameter is "time": formatter = "%H:%M" contextParameter.InteractionData[interactionName.title( )] = dateObject.strftime(formatter) contextParameter.SaveObject() if self.__RemoveBeforeRequirementCalculation: sentenceList = SentenceResolver().RemoveLowPrioritySentences( sentenceList) contextParameterDict = contextParameter.GetParameterDictionary() calculationResult = SentenceResolver().CalculateRequirement( sentenceList, contextParameterDict) sentenceList = calculationResult["sentenceList"] if self.__RemoveAfterRequirementCalculation: sentenceList = SentenceResolver().RemoveLowPrioritySentences( sentenceList, True) sentenceList = SentenceResolver().AddActionBonus(sentenceList) sentenceList = SentenceResolver().AddSentencePriority(sentenceList) sentenceList = SentenceResolver().CalculateCategory( sentenceList, contextParameterDict["Category"]) PipelineArgs.SentenceList = sentenceList FileLogger().Info("AnalyzeScope, Process(), SentenceList: {0}".format( PipelineArgs.SentenceList)) return PipelineArgs
def TrainFullSentence(self, Sentence, Language, KeywordList, RequirementObjectList, HasCategoryList, SetCategoryList, ActionName, Animation, InteractionID=None): # Train Keywords of sentence self.SaveKeywordsFromSentence(Sentence, Language) # save sentence sentenceID = self.SaveSentence(Sentence, Language, "Trainer", Animation, "1") # link keywords to sentence self.LinkKeywordAndSentence(KeywordList, Language, sentenceID) for requirement in RequirementObjectList: # create requirement if it does not exist - or get ID requirementID = self.SaveRequirement(requirement.Name) # Link requirement - sentence if (requirement.Comparison is None): query = "INSERT or IGNORE INTO Conversation_Sentence_Requirement ('SentenceID', 'RequirementID', 'Value') Values ('{0}', '{1}', '{2}')".format( sentenceID, requirementID, requirement.Value) else: query = "INSERT or IGNORE INTO Conversation_Sentence_Requirement ('SentenceID', 'RequirementID', 'Comparison', 'Value') Values ('{0}', '{1}', '{2}', '{3}')".format( sentenceID, requirementID, requirement.Comparison, requirement.Value) db().Execute(query) for category in HasCategoryList: if (len(category) > 1): # create category if it does not exist - or get ID categoryID = self.SaveCategory(category) # Link category - sentence query = "INSERT or IGNORE INTO Conversation_Sentence_Category_Has ('SentenceID', 'CategoryID') Values ('{0}', '{1}')".format( sentenceID, categoryID) db().Execute(query) for category in SetCategoryList: if (len(category) > 1): # create set category if it does not exist - or get ID categoryID = self.SaveCategory(category) # Link set category - sentence query = "INSERT or IGNORE INTO Conversation_Sentence_Category_Set ('SentenceID', 'CategoryID') Values ('{0}', '{1}')".format( sentenceID, categoryID) db().Execute(query) if (ActionName != None and len(ActionName) > 1): # Link follow up action - sentence query = "SELECT ID FROM Conversation_Action WHERE Name = '{0}'".format( ActionName) actionIDRow = db().Fetchall(query) if len(actionIDRow) > 0: query = "INSERT or IGNORE INTO Conversation_Sentence_Action ('SentenceID', 'ActionID') Values ('{0}', '{1}')".format( sentenceID, actionIDRow[0][0]) db().Execute(query) if (InteractionID is not None): query = "INSERT or IGNORE INTO Conversation_Interaction_Sentence ('InteractionID', 'SentenceID') Values ('{0}', '{1}')".format( InteractionID, sentenceID) db().Execute(query) FileLogger().Info( "DialogTrainer: Full sentence trained: {0}".format(Sentence)) return True