def setOutputHistory(self, outputHistory, collection=AimlConfig.historyColl): self._History["outputHistory"] = outputHistory # make a new history data datetime = time.getCurrentTime() newInput = self._History["inputHistory"][-1] newOutput = self._History["outputHistory"][-1] if (newInput == "LOAD ALICE"): print("load alice 不是对话内容") return datas = { "ownerID": self._CurOwnerID, "userID": self._CurUserID, "botID": self._CurBotID, "inputHistory": newInput, "outputHistory": newOutput, "idx": self.nextidx, "datetime": datetime } result = _mongoDB.insert(collection, datas) if self._verbose: if result: print("Insert History Result:\n\t {0}".format(result)) else: print("Fail in inserting aiml history into mongoDB") if result: self.nextidx += 1
def setPredicate(self, name, value, filename, ownerID, userID, botID, vaildTime=AimlConfig.valitime, collection=AimlConfig.varColl): if self._verbose: try: name = name.encode("utf-8") if name == "topic": state = AimlConfig.dialog_state value = value.encode("utf-8") ownerID = ownerID.encode("utf-8") userID = userID.encode("utf-8") botID = botID.encode("utf-8") except: pass try: print("Set {0}: {1} in session ({2} :({3} : {4}))".format( name, value, ownerID, userID, botID)) except: pass # make a new var # if value == "None" or value == "": # return datetime = time.getCurrentTime() if name == "topic": data = { "ownerID": ownerID, "userID": userID, "botID": botID, "name": name, "value": value, "filename": filename, "state": AimlConfig.dialog_state, "2idx": self.nextidx, "datetime": datetime, "vaildTime": vaildTime } else: data = { "ownerID": ownerID, "userID": userID, "botID": botID, "name": name, "value": value, "filename": filename, "2idx": self.nextidx, "datetime": datetime, "vaildTime": vaildTime } result = _mongoDB.insert(collection, data) if self._verbose: print(result)
def setRuleBatch(self, collection, ownerID, userID, botID, ruleList): """ :param list: a list of (match, response) :return: size of rule List, insert number, Success number , Has number, Error number """ numS = numH = numE = numA = 0 try: self._filters["ownerID"] = ownerID self._filters["userID"] = userID self._filters["botID"] = botID nextidx = _mongoDB.count(collection, self._filters) + 1 check = { "ownerID": ownerID, "userID": userID, "botID": botID } datetime = time.getCurrentTime() data = { "ownerID": ownerID, "userID": userID, "botID": botID, "datetime": datetime } # for (matched, response) in ruleList: for ele in ruleList: matched = ele[0] response = ele[1] numA += 1 check["matched"] = matched check["response"] = response isExist = _mongoDB.count(collection, check) if isExist: numH += 1 continue data["matched"] = matched data["response"] = response data["ruleID"] = nextidx result = _mongoDB.insert(collection, data) if result: numS += 1 try: data.pop("_id") except: pass else: numE += 1 if self._verbose: print ("Fail in inserting the ruleList {0}".format(numA)) nextidx += 1 except: pass finally: return len(ruleList), numA, numS, numH, numE
def setSingleRule(self, collection, ownerID, userID, botID, matched, response): """ 插入单条规则,成功则返回 1, 存在则返回 0 失败则返回 -1 :return: """ try: # is exist? data = { "ownerID": ownerID, "userID": userID, "botID": botID, "matched": matched, "response": response } is_exist = _mongoDB.count(collection, data) if is_exist: return 0 self._filters["ownerID"] = ownerID self._filters["userID"] = userID self._filters["botID"] = botID nextidx = _mongoDB.count(collection, self._filters) + 1 datetime = time.getCurrentTime() data["datetime"] = datetime data["ruleID"] = nextidx if self._verbose: A = DebugInfo() A.context += ("Insert", data) print ("Insert", data) result = _mongoDB.insert(collection, data) if result: return 1 else: return -1 except PyMongoTimeOutError as e: DebugInfo.context += "Custom Design:\n\t MongoDB Error: {0}".format(e) print("Custom Design:\n\t MongoDB Error: {0}".format(e)) return -1 except Exception as e: DebugInfo.context +="Custom Design:\n\t{0}".format(e) print ("Custom Design:\n\t{0}".format(e)) return -1
def insert(self, collection, ownerId, userId, botId, dialog_context, dialog_answer, type): try: if ownerId == None or ownerId == "": ownerId = cf.DEFAULT_ID if userId == None or userId == "": userId = cf.DEFAULT_ID if botId == None or botId == "": botId = cf.DEFAULT_ID if type == None or type == "": type = cf.AIMLTYPE data = { "ownerId": ownerId, "userId": userId, "botId": botId, "dialog_context": dialog_context, "dialog_answer": dialog_answer, "type": type } datetime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) data["create_time"] = datetime if self._verbose: A = DebugInfo() A.context += ("Insert", data) print("Insert", data) result = _mongoDB.insert(collection, data) if result: return 1 else: return -1 except PyMongoTimeOutError as e: DebugInfo.context += " dialog:\n\t MongoDB Error: {0}".format(e) print("dialog:\n\t MongoDB Error: {0}".format(e)) return -1 except Exception as e: DebugInfo.context += "dialog:\n\t{0}".format(e) print("dialog:\n\t{0}".format(e)) return -1
def setDialogState(self, ownerID, userID, botID, query, response, filename, state, collection=AimlConfig.stateColl): if query == "LOAD ALICE": return if self._verbose: try: query = query.encode("utf-8") response = response.encode("utf-8") ownerID = ownerID.encode("utf-8") userID = userID.encode("utf-8") botID = botID.encode("utf-8") except: pass try: print("Set {0}: {1} in session ({2} :({3} : {4}))".format( query, response, ownerID, userID, botID)) except: pass datetime = time.getCurrentTime() data = { "ownerID": ownerID, "userID": userID, "botID": botID, "query": query, "response": response, "state": state, "filename": filename, "datetime": datetime } result = _mongoDB.insert(collection, data) if self._verbose: print(result)
def setRuleFromFile(self, collection, ownerID, userID, botID, file_name): assert (isinstance(file_name, str)) assert (os.path.isfile(file_name)) numS = numH = numE = numA = numL = 0 try: self._filters["ownerID"] = ownerID self._filters["userID"] = userID self._filters["botID"] = botID nextidx = _mongoDB.count(collection, self._filters) + 1 datetime = time.getCurrentTime() check = { "ownerID": ownerID, "userID": userID, "botID": botID } data = { "ownerID": ownerID, "userID": userID, "botID": botID, "datetime": datetime } comment = re.compile('^##') with open(file_name, "r") as f: for line in f: numL += 1 if isinstance(line, str): line = line.decode("GBK") if comment.match(line): continue if "#@#@#@" in line: line = line.split("#@#@#@") else: continue numA += 1 matched, response = line matched = re.match(r'^\s*(.*?)\s*$', matched, flags=re.I).group(1) response = re.match(r'^\s*(.*?)\s*$', response).group(1) # print (u"'{0}'".format(matched)) # print (u"'{0}'".format(response)) check["matched"] = matched check["response"] = response isExit = _mongoDB.count(collection, check) if isExit: numH += 1 continue data["matched"] = matched data["response"] = response data["ruleID"] = nextidx result = _mongoDB.insert(collection, data) if result: numS += 1 try: data.pop("_id") except: pass else: numE += 1 if self._verbose: print ("Fail in inserting line: {0}".format(numL)) nextidx += 1 except IOError as e: print("Error in set rules from file :\n\t", e) finally: return numA, numS, numH, numE