def main(): sys.stdout.flush() DM = initialize() disease = [] week = [] division = [] doctor = [] with open('../data_resource/disease_dict.txt', 'r', encoding='utf-8') as r_disease: for line in r_disease: disease.append(line.replace('\n','')) with open('../data_resource/week_dict.txt', 'r', encoding='utf-8') as r_week: for line in r_week: week.append(line.replace('\n','')) with open('../data_resource/division_dict.txt', 'r', encoding='utf-8') as r_division: for line in r_division: division.append(line.replace('\n','')) # with open('../data_resource/doctor_dict.txt','r') as r_doctor: # for line in r_doctor: # doctor.append(line) lu_model = get_lu_pred.LuModel() fb = conn.cursor() fb.execute('select MAX(ID) from fb_doctor_chatbot_fb_db') vid = fb.fetchone()[0] #fb id number ex:235 print("initial vid= ") print(vid) print("waiting for the fb input...") def multiuser(buffer2,after): buffer2=[] fb.execute('select content from fb_doctor_chatbot_fb_db') buffer2.extend(after) buffer2=list(set(buffer2)) return buffer2 fb.execute('select * from fb_doctor_chatbot_fb_db') init = fb.fetchall() # while True: fb = conn.cursor() fb.execute('select MAX(ID) from fb_doctor_chatbot_fb_db') new_id = fb.fetchone()[0] multi_id=[] #ids' list if(new_id !=vid or len(multi_id) != 0): #有新輸入的時候或還有使用者輸入沒有完成的時候 print("==============") fb.execute('select * from fb_doctor_chatbot_fb_db ') after = fb.fetchall() after = list(set(after)-set(init)) #只要這次執行DST.py之後的FB輸入 print("after") print(after) fb.execute('select max(id) from fb_doctor_chatbot_fb_db ') vid = fb.fetchone()[0] print("vid") print(vid) after_id = [] for i in range(0,len(after)): after_id.append(list(after[i])[1]) print("after_id") print(after_id) #if after: # multi_id = multiuser(multi_id,after_id) #列出所有的輸入sender_id multi_id = list(set(after_id)) print("multi_id") print(multi_id) vvid = multi_id.pop(0) #取出第一個sender_id #vid = vvid[0] print("vvid") print(vvid) fb.execute("select * from fb_doctor_chatbot_fb_db where content='"+str(vvid)+"'") #取出第一個sender_id的內容 message = fb.fetchall() #此輸入者所有的輸入 TODO 可能要看先前的句子是否存在 message = list(set(message)-set(init)) #只要這次執行DST.py之後的FB輸入 print("this sender's message") print(message) mes_fir = list(message.pop(0)) #最先的輸入句子 print("first message") print(mes_fir) mes_fir_id = mes_fir[0] #最先的輸入句子的id mes_fir_sen = mes_fir[3] #最先的輸入句子的content #if(True): sentence = mes_fir_sen name="" for i in range(8,len(vvid)-2): #save json with sender's id name+=vvid[i] print("name") print(name) if os.path.exists("DM_"+name+".json"): #如果此sender id之前有輸入的話就讀取裡面內容 with open("DM_"+name+".json", 'r') as f: DM = json.load(f) slot_dictionary = {'disease': '', 'division': '', 'doctor': '', 'time': ''} #sentence = input('U: ') pattern = re.compile("[0-9]+\.[0-9]+\.[0-9]+") match = pattern.match(sentence) if match: DM["State"]["time"] = sentence elif sentence in week: DM["State"]["time"] = sentence # elif sentence in doctor: # DM["State"]["doctor"] = sentence elif sentence in division: DM["State"]["division"] = sentence elif sentence in disease: DM["State"]["disease"] = sentence else: semantic_frame = lu_model.semantic_frame(sentence) slot_dictionary = semantic_frame['slot'] print("[ LU ]") for slot, value in semantic_frame['slot'].items(): print(slot, ": ", value) for slot in slot_dictionary: if slot_dictionary[slot] != '' and (DM["State"][slot] == None or (type(DM["State"][slot]) == list and len(DM["State"][slot]) > 1)): DM["State"][slot] = slot_dictionary[slot] if type(DM["State"]["time"]) == str and DM["State"]["time"] not in week and not match: DM["State"]["time"] = None if DM["Intent"] == None: DM["Intent"] = int(semantic_frame['intent']) print("Intent : ", DM["Intent"]) DM = DM_request(DM) DM['Sentence'] = get_sentence(DM) print ("[ DM ]") for i in DM: print (i, DM[i]) DM_path = "DM_" + str(vid)+".json" #print (os.path) #name="" #for i in range(8,len(vid[0])-2): #save json with sender's id # name+=vid[0][i] print(name) with open("DM_"+name+".json", 'w') as fp: json.dump(DM, fp) print("save succeed.") if DM["Request"] == "end": sys.exit() #vid += 1 #print("update vid = " +str(vid)) print("vid") print(vid) print("new_id") print(new_id) print("multi_id") print(multi_id) #break time.sleep(0.5) time.sleep(0.5) #wait 0.5 secone to listen to if a fb new data stored.
def dst(self): sys.stdout.flush() #DM = initialize() disease = [] week = [] division = [] doctor = [] with open('../brain/brain_libs/data_resource/disease_dict.txt', 'r', encoding='utf-8') as r_disease: for line in r_disease: disease.append(line.replace('\n', '')) with open('../brain/brain_libs/data_resource/week_dict.txt', 'r', encoding='utf-8') as r_week: for line in r_week: week.append(line.replace('\n', '')) with open('../brain/brain_libs/data_resource/division_dict.txt', 'r', encoding='utf-8') as r_division: for line in r_division: division.append(line.replace('\n', '')) # with open('../data_resource/doctor_dict.txt','r') as r_doctor: # for line in r_doctor: # doctor.append(line) lu_model = get_lu_pred.LuModel() fb.execute('select MAX(ID) from fb_doctor_chatbot_fb_db') vid = fb.fetchone()[0] #fb id number ex:235 print("waiting for the fb input") print(os.getcwd()) #while True: fb.execute( 'select * from fb_doctor_chatbot_fb_db where ID=(select MAX(ID) from fb_doctor_chatbot_fb_db) ' ) message = fb.fetchone() #if(message[0] != vid): sentence = message[3] #if os.path.exists("DM.json"): with open("DM.json", 'r') as f: DM = json.load(f) slot_dictionary = { 'disease': '', 'division': '', 'doctor': '', 'time': '' } #sentence = input('U: ') pattern = re.compile("[0-9]+\.[0-9]+\.[0-9]+") match = pattern.match(sentence) if match: DM["State"]["time"] = sentence elif sentence in week: DM["State"]["time"] = sentence # elif sentence in doctor: # DM["State"]["doctor"] = sentence elif sentence in division: DM["State"]["division"] = sentence elif sentence in disease: DM["State"]["disease"] = sentence else: semantic_frame = lu_model.semantic_frame(sentence) slot_dictionary = semantic_frame['slot'] print("[ LU ]") for slot, value in semantic_frame['slot'].items(): print(slot, ": ", value) for slot in slot_dictionary: if slot_dictionary[slot] != '' and ( DM["State"][slot] == None or (type(DM["State"][slot]) == list and len(DM["State"][slot]) > 1)): DM["State"][slot] = slot_dictionary[slot] if type(DM["State"]["time"] ) == str and DM["State"]["time"] not in week and not match: DM["State"]["time"] = None if DM["Intent"] == None: DM["Intent"] = int(semantic_frame['intent']) print("Intent : ", DM["Intent"]) DM = DM_request(DM) print("[ DM ]") for i in DM: print(i, DM[i]) DM_path = "DM_" + str(vid) + ".json" print(os.path) with open("DM.json", 'w') as fp: json.dump(DM, fp) #if DM["Request"] == "end": sys.exit()
def main(): sys.stdout.flush() DM = initialize() disease = [] week = [] division = [] #doctor = [] with open('../data_resource/disease_dict.txt', 'r', encoding='utf-8') as r_disease: for line in r_disease: disease.append(line.replace('\n', '')) with open('../data_resource/week_dict.txt', 'r', encoding='utf-8') as r_week: for line in r_week: week.append(line.replace('\n', '')) with open('../data_resource/division_dict.txt', 'r', encoding='utf-8') as r_division: for line in r_division: division.append(line.replace('\n', '')) # with open('../data_resource/doctor_dict.txt','r') as r_doctor: # for line in r_doctor: # doctor.append(line) lu_model = get_lu_pred.LuModel() while True: if os.path.exists("DM.json"): with open("DM.json", 'r') as f: DM = json.load(f) slot_dictionary = { 'disease': '', 'division': '', 'doctor': '', 'time': '' } sentence = input('U: ') pattern = re.compile("[0-9]+\.[0-9]+\.[0-9]+") match = pattern.match(sentence) if match: DM["State"]["time"] = sentence elif sentence in week: DM["State"]["time"] = sentence # elif sentence in doctor: # DM["State"]["doctor"] = sentence elif sentence in division: DM["State"]["division"] = sentence elif sentence in disease: DM["State"]["disease"] = sentence else: semantic_frame = lu_model.semantic_frame(sentence) slot_dictionary = semantic_frame['slot'] print("[ LU ]") for slot, value in semantic_frame['slot'].items(): print(slot, ": ", value) for slot in slot_dictionary: if slot_dictionary[slot] != '' and ( DM["State"][slot] == None or (isinstance(DM["State"][slot], list) and len(DM["State"][slot]) > 1)): DM["State"][slot] = slot_dictionary[slot] if isinstance(DM["State"]["time"], str) and DM["State"]["time"] not in week and not match: DM["State"]["time"] = None if DM["Intent_pre"] == None: DM["Intent_pre"] = int(semantic_frame['intent']) DM["Intent"] = int(semantic_frame['intent']) reset = 0 print("Intent : ", DM["Intent"]) else: DM["Intent"] = int(semantic_frame['intent']) if DM["Intent_pre"] != DM["Intent"]: DM["Intent"] = int(semantic_frame['intent']) reset = 1 print("Intent_pre : ", DM["Intent_pre"]) print("Intent : ", DM["Intent"]) DM = DM_request(DM) print("[ DM ]") for i in DM: print(i, DM[i]) with open("DM.json", 'w') as fp: json.dump(DM, fp) if DM["Request"] == "end": sys.exit() if reset == 1: sentence = input('Change intent? y/N: ') if sentence == "y": DM = initialize() print(DM)