def find_if(self, f): ret_arr = [] sql2 = ('SELECT {} FROM {} WHERE {} LIKE {} '.format( '*', 'rule_data', 'part_if', '\'%{}%\''.format(str(f)))) cr_0.execute(sql2) for if_ in cr_0.fetchall(): ret_arr.append("%s%s%s%s%s" % (if_[0], "*", if_[2], "*", if_[3])) return ret_arr
def get_factname(id): ret = "" sql = ('SELECT {} FROM {} WHERE {}'.format( 'fact_name', 'fact_data', 'fact_id=\'{}\''.format(id))) cr_0.execute(sql) for fact_name in cr_0.fetchall(): ret = fact_name[0] return ret
def recv_fact(fact): sql = ('SELECT {} FROM {} WHERE {}'.format( 'fact_name', 'fact_data', 'fact_id=\'{}\''.format(fact))) cr_0.execute(sql) for fact_name in cr_0.fetchall(): end = inf.inference(fact_name[0]).forward_() if end == "end": return "end" return
def get_fact_id(bb_in): ret = [] for i in bb_in: sql = ('SELECT {} FROM {} WHERE {}'.format( 'fact_id', 'fact_data', 'fact_name=\'{}\''.format(i))) cr_0.execute(sql) for fact_id in cr_0.fetchall(): ret.append(blackboard.bb_manage.ch_digit(fact_id[0])) return ret
def get_fact_bb(bb): ret = [] for i in bb: sql = ('SELECT {} FROM {} WHERE {}'.format( 'fact_name', 'fact_data', 'fact_id=\'{}\''.format(i))) cr_0.execute(sql) for fact_name in cr_0.fetchall(): ret.append(fact_name[0]) return ret
def find_fact(self): arr = [] sql = ('SELECT {} FROM {} WHERE {}'.format( 'fact_id', 'fact_data', 'fact_name=\'{}\''.format(self.fact))) cr_0.execute(sql) for fact_all in cr_0.fetchall(): arr.append(fact_all) if arr is None: return arr return arr
def input_bb(fact): for f in fact: # if (f in bb): # return # else: sql = ('SELECT {} FROM {} WHERE {}'.format( 'terminal_id', 'fact_data', 'fact_id=\'{}\''.format(f))) cr_0.execute(sql) result = cr_0.fetchall() if result[0][0] == 1: bb.append(f) # print("%s%s" % ("คุณเเป็น :",blackboard.bb_manage.get_factname(f))) return "end" else: bb.append(f) return
def inferance_engine(): # pull data fact = request.json.get('fact') data_blackboard_user = request.json.get('blackboard') data_not_exist_blackborad_user = request.json.get('not_exist_blackboard') print(fact) print(data_blackboard_user) print(data_not_exist_blackborad_user) data_blackboard = blackboard.bb_manage.get_fact_id(data_blackboard_user) data_not_exist_blackborad = blackboard.bb_manage.get_fact_id( data_not_exist_blackborad_user) print(data_blackboard) print(data_not_exist_blackborad) # define data blackboard.bb = data_blackboard blackboard.no_bb = data_not_exist_blackborad # processing intent intent = {"{}".format(fact)} res_fulfill, intent_name = mgDLF.detect_intent_texts(intent, 'th') # default intent (You can add if you want.) default_intent = [ "Default Welcome Intent", "Default Fallback Intent", "คุยเล่น", "คำหยาบ", "ตลก", "มีอาการ", "ไม่มีอาการ" ] excep_intent = ["มีอาการ", "ไม่มีอาการ"] if intent_name in default_intent: if fact == "อยากตรวจใหม่": # define response data TYPE "DEFAULT" data = { 'response': "ได้สิ แน่นอนเลย", 'blackboard': [], 'not_exist_blackboard': [], 'type_response': 'DEFAULT' } elif intent_name in excep_intent: # define response data TYPE "DEFAULT" data = { 'response': "ตอบให้มันดีๆ หน่อยสิ", 'blackboard': data_blackboard, 'not_exist_blackboard': data_not_exist_blackborad, 'type_response': 'DEFAULT' } else: # define response data TYPE "DEFAULT" data = { 'response': res_fulfill, 'blackboard': data_blackboard, 'not_exist_blackboard': data_not_exist_blackborad, 'type_response': 'DEFAULT' } else: inf.inference(res_fulfill).forward_() data_ask_user = blackboard.bb_manage.unique( blackboard.bb_manage.get_fact_bb(blackboard.ask_arr)) data_blackboard = blackboard.bb_manage.unique( blackboard.bb_manage.get_fact_bb(blackboard.bb)) data_not_exist_blackborad = blackboard.bb_manage.unique( blackboard.bb_manage.get_fact_bb(blackboard.no_bb)) if not data_ask_user: blackboard.ask_arr = [] ter = [] sql = 'SELECT fact_id FROM fact_data WHERE terminal_id="1"' cr_0.execute(sql) for terminal in cr_0.fetchall(): if terminal[0] < 10: ter_0 = ("%s%s" % ("00000", terminal[0])) elif terminal[0] >= 10 and terminal[0] <= 99: ter_0 = ("%s%s" % ("0000", terminal[0])) elif terminal[0] >= 100 and terminal[0] <= 999: ter_0 = ("%s%s" % ("000", terminal[0])) elif terminal[0] >= 1000 and terminal[0] <= 9999: ter_0 = ("%s%s" % ("00", terminal[0])) elif terminal[0] >= 10000 and terminal[0] <= 99999: ter_0 = ("%s%s" % ("0", terminal[0])) else: ter_0 = terminal[0] if (ter_0 in blackboard.bb): ter.append(blackboard.bb_manage.get_factname(ter_0)) # define response data TYPE "CONCUSION" if not ter: data = { 'response': 'งั้นหมอไม่แน่ใจ ลองบอกอาการอื่นมาได้ไหม', 'blackboard': data_blackboard, 'not_exist_blackboard': data_not_exist_blackborad, 'type_response': 'NO-CONCUSION' } else: data = { 'response': '{}'.format(ter), 'blackboard': data_blackboard, 'not_exist_blackboard': data_not_exist_blackborad, 'type_response': 'CONCUSION' } else: # define response data TYPE "ASK" blackboard.ask_arr = [] data = { 'response': data_ask_user[0], 'blackboard': data_blackboard, 'not_exist_blackboard': data_not_exist_blackborad, 'type_response': 'ASK' } return jsonify(data)
def check_start(fact): a = [] sql2 = ('SELECT {} FROM {} WHERE {} LIKE {} '.format( '*', 'rule_data', 'part_then', '\'%{}%\''.format(str(fact)))) cr_0.execute(sql2) for then_ in cr_0.fetchall(): a.append(then_[2]) if len(a) < 1: if (fact in bb): return "y" else: if (fact in no_bb): return "n" else: ask_arr.append(fact) return "end" # ask = input("\n%s%s" % (blackboard.bb_manage.get_factname(fact), " is true? by check start(y/n):")) # if ask == "y": # end = blackboard.bb_manage.recv_fact(fact) # if end == "end": # return "end" # return "y" # elif ask == "end": # return "end" # else: # no_bb.append(fact) # return "n" else: for fact_ in a: if ((fact_).find(',') > -1): fact_2 = fact_.split(',') c = 0 for i in fact_2: non = 0 for j in fact_2: if (j in no_bb): non += 1 if non is 0: ask = blackboard.bb_manage.check_start(i) if ask == "y": end = blackboard.bb_manage.recv_fact(i) if end == "end": return "end" else: c += 1 elif ask == "end": return "end" else: no_bb.append(i) else: return "n" if c == len(fact_2): return "y" else: return "n" else: ask = blackboard.bb_manage.check_start(fact_) if ask == "y": return ask elif ask == "end": return "end" else: no_bb.append(fact_) return return