def get_gejalapenyakit(penyakit): id_gejala = [] for y in penyakit: cursor.execute( "SELECT gejala.nama_gejala FROM gejala_penyakit JOIN gejala ON gejala_penyakit.id_gejala = gejala.id_gejala WHERE gejala_penyakit.id_penyakit = " + str(y[0])) id_gejala.append(cursor.fetchall()) print("id gejala = ", id_gejala) for i in id_gejala: name_gejala = [item[0].split(" ") for item in i] # print("old = ", name_gejala) gj = [element for sub in name_gejala for element in sub] # print("gj = ", gj) # print("name gejala = ", i) list_gj = ",".join(map("-".join, i)) list = [list_gj] # print("list_gj = ", list) stopwords = get_stopword('file/konjungsi.csv') filters = filtering(gj, stopwords) stems = stemming(filters) sinonim = get_sinonim(stems) result, cf = get_cf(conn, sinonim) # print("result = ", cf) data = [result[0][0][1]] # print("data = ", data) nilai_cf = [cf] zips = zip(list, data, nilai_cf) newfilepath = 'testing all new.csv' with open(newfilepath, 'a', encoding="ISO-8859-1", newline='') as f: writer = csv.writer(f, delimiter=";") for row in zips: tmp = row writer.writerow(tmp) return zips
elif kondisi_gejala == "kurang": input_db = save_input(user_id, name_user, sinonim, conn) cursor.execute("SELECT COUNT (*) FROM gejala_input WHERE user_id = '" + user_id + "'") count_input = cursor.fetchall() if count_input[0][0] <= 3: print("Gejala yang anda masukkan kurang, silahkan tambahkan lagi") else: cursor.execute( "SELECT nama_gejala FROM gejala_input WHERE user_id = '" + user_id + "'") gejala_db = cursor.fetchall() gejala_new = [i[0] for i in gejala_db] get_cf(conn, gejala_new) elif kondisi_gejala == "ada": cursor.execute( "SELECT nama_gejala FROM gejala_input WHERE user_id = '" + user_id + "'") gejala_db = cursor.fetchall() if gejala_db is None: get_cf(conn, sinonim) else: gejala_new = [i[0] for i in gejala_db] sinonim.append(gejala_new) gejala_new2 = flat(sinonim) get_cf(conn, gejala_new2)
def message_bot(user_id, name_user, salam, text, time, conn): msg_penyakit = "Kemungkinan Anda terkena penyakit " msg_pengobatan = "\n\n#Pengobatan \nPertolongan pertama yang bisa dilakukan adalah " msg_pencegahan = "\n#Pencegahan \nPencegahan yang bisa dilakukan adalah " msg_peringatan = "Silahkan menghubungi dokter untuk mendapatkan informasi dan penanganan yang lebih baik" message = "" timestamp = tm.time() penyakit_result = "" definisi_result = "" disease = "" cursor = conn.cursor() if text.lower() == 'tidak': kondisi_gejala = 'ada' sinonim = [] else: stopwords = get_stopword('file/konjungsi.csv') contents = tokenizing(text) filters = filtering(contents, stopwords) stems = stemming(filters) sinonim = get_sinonim(stems) hapus_kata_sakit(sinonim) if len(sinonim) <= 2: gabung_sinonim = ' '.join(sinonim) if gabung_sinonim == 'selamat pagi' or gabung_sinonim == 'selamat malam' or gabung_sinonim == 'pagi' or gabung_sinonim == 'malam': greeting = check_greeting(sinonim) disease_id = 0 save_history(user_id, name_user, text, greeting, "", disease_id, time, conn) return greeting symp_db, symptoms, input = get_symptoms(conn, sinonim) kondisi_gejala = cek_total_gejala(symp_db) jml_penyakit, penyakit = cek_total_penyakit(conn, sinonim) cursor.execute( "SELECT DISTINCT input_user, time FROM gejala_input WHERE user_id = '" + user_id + "'") get_time = cursor.fetchall() if get_time: timestamp_now = tm.time() - float(get_time[0][1]) if timestamp_now >= 3600: cursor.execute("DELETE FROM gejala_input WHERE user_id = '" + user_id + "'") conn.commit() print("hapus gejala expired") # jika gejala kosong maka tampilkan pesan if kondisi_gejala == "kosong": # print("INFO> gejala kosong") disease_id = 0 # print("DEBUG> jumlah penyakit = ", jml_penyakit) if jml_penyakit == 0: disease = check_greeting(sinonim) elif jml_penyakit > 0: for pnykt in penyakit: disease = disease + pnykt[0][2] + "\n\n" message = message + str(disease) save_history(user_id, name_user, text, message, "", disease_id, time, conn) # jika gejalanya kurang elif kondisi_gejala == "kurang": # print("INFO> gejala kurang") input_to_sinonim = ",".join(input) # print("DEBUG> Sinonim disimpan ke tabel (gejala input) = ", input_to_sinonim) save_input(user_id, name_user, symp_db, input_to_sinonim, timestamp, conn) cursor.execute("SELECT COUNT (*) FROM gejala_input WHERE user_id = '" + user_id + "'") count_input = cursor.fetchall() if count_input[0][0] <= 3: message = message + "Apakah ada gejala lain ?\n\nGejala yang anda masukkan kurang. Masukkan minimal 4 gejala agar mendapatkan hasil yang akurat. \n\n Atau jawab TIDAK jika tidak ada gejala yang ingin ditambahkan." disease_id = 0 save_history(user_id, name_user, input_to_sinonim, message, "", disease_id, time, conn) else: cursor.execute( "SELECT nama_gejala FROM gejala_input WHERE user_id = '" + user_id + "'") gejala_db = cursor.fetchall() # print("DEBUG> Kurang | Gejala di DB = ", gejala_db) gejala = [i[0].split(',') for i in gejala_db] gejala_flat = flat(gejala) # print("DEBUG> Kurang | Gejala yang digabung = ", gejala_flat) result, cf = get_cf(conn, gejala_flat) # jika yang terdeteksi hanya 1 penyakit if len(result) == 1: for output in result: message = message + salam + name_user + "\n" \ + msg_penyakit + output[0][1] + "\n" + output[0][2] \ + msg_pengobatan + output[0][4] + "\n" \ + msg_pencegahan + output[0][5] + "\n\n" \ + (str(output[0][6]) + "\n\n" if output[0][6] is not None else '') \ + msg_peringatan output_sistem = msg_penyakit + result[0][0][1] disease_id = result[0][0][0] save_history(user_id, name_user, text, output_sistem, "", disease_id, time, conn) # jika yang terdeteksi lebih dari 1 penyakit else: for idx in result: penyakit_result = penyakit_result + " , " + idx[0][1] definisi_result = definisi_result + "\n\n" + idx[0][2] message = message + salam + name_user + "\n" + msg_penyakit + penyakit_result + "\n" + definisi_result + "\n\n" + msg_peringatan output_sistem = msg_penyakit + penyakit_result for dis in result: disease_id = dis[0][0] save_history(user_id, name_user, text, output_sistem, "", disease_id, time, conn) cursor.execute("DELETE FROM gejala_input WHERE user_id = '" + user_id + "'") conn.commit() # setelah sukses hapus yang ada di db elif kondisi_gejala == "ada": # print("INFO> gejala cukup") cursor.execute( "SELECT DISTINCT input_user, time FROM gejala_input WHERE user_id = '" + user_id + "'") gejala_db = cursor.fetchall() # print("DEBUG> Cukup | Gejala di DB = ", gejala_db) if not gejala_db: if len(sinonim) == 0: disease = check_greeting(sinonim) message = message + str(disease) return message else: result, cf = get_cf(conn, sinonim) # untuk mendapatkan daftar string gejala print( "\n----------proses dibawah ini untuk daftar gejala yang disimpan ke histroy------------" ) symp_db, symptoms, input = get_symptoms(conn, sinonim) string_gejala = ', '.join([symp[0][1] for symp in symp_db]) print( "-------------------------selesai-------------------------\n" ) else: gejala = [i[0].split(',') for i in gejala_db] gejala_flat = flat(gejala) # print("DEBUG> Cukup | Gejala yang digabung = ", gejala_flat) gejala_new2 = sinonim + gejala_flat # print("DEBUG> Cukup | Gejala yang digabung + kalimat sebelum = ", gejala_new2) # untuk mendapatkan daftar string gejala # print("\n----------proses dibawah ini untuk daftar gejala yang disimpan ke histroy------------") symp_db, symptoms, input = get_symptoms(conn, gejala_new2) string_gejala = ', '.join([symp[0][1] for symp in symp_db]) # print("-------------------------selesai-------------------------\n") if len(symp_db) <= 1: disease_id = 0 output_sistem = "Maaf data kurang akurat. Sistem tidak bisa memberikan diagnosa.\nSilahkan masukkan keluhan Anda kembali." save_history(user_id, name_user, text, output_sistem, string_gejala, disease_id, time, conn) cursor.execute("DELETE FROM gejala_input WHERE user_id = '" + user_id + "'") conn.commit() return salam + name_user + "\n" + output_sistem else: result, cf = get_cf(conn, gejala_new2) cursor.execute("DELETE FROM gejala_input WHERE user_id = '" + user_id + "'") conn.commit() # jika yang terdeteksi hanya 1 penyakit if len(result) == 1: for output in result: message = message + salam + name_user + "\n" \ + msg_penyakit + output[0][1] + "\n" + output[0][2] \ + msg_pengobatan + output[0][4] + "\n" \ + msg_pencegahan + output[0][5] + "\n\n" \ + (str(output[0][6]) + "\n\n" if output[0][6] is not None else '') \ + msg_peringatan output_sistem = msg_penyakit + result[0][0][1] disease_id = result[0][0][0] save_history(user_id, name_user, text, output_sistem, string_gejala, disease_id, time, conn) # jika yang terdeteksi lebih dari 1 penyakit else: for idx in result: # print("DEBUG> Penyakit lebih > 1 | Penyakit = ", idx) penyakit_result = penyakit_result + " , " + idx[0][1] definisi_result = definisi_result + "\n\n" + idx[0][2] message = message + salam + name_user + "\n" + msg_penyakit + penyakit_result + "\n" + definisi_result + "\n\n" + msg_peringatan output_sistem = msg_penyakit + penyakit_result for dis in result: disease_id = dis[0][0] save_history(user_id, name_user, text, output_sistem, string_gejala, disease_id, time, conn) return message
def combination_samegejala(penyakit): arr_penyakit = [] comb_penyakit = itertools.combinations(penyakit, 2) for cb in comb_penyakit: tuple_comb = [element for tupl in cb for element in tupl] # convert tuple of tuple to list arr_penyakit.append(tuple_comb) print("kumpulan penyakit = ", arr_penyakit) for i in range(len(arr_penyakit)): list_gj = [] arr_gejala = [] print("penyakit = ", arr_penyakit[i]) for j in arr_penyakit[i]: cursor.execute( "SELECT gejala.nama_gejala FROM gejala_penyakit JOIN gejala ON gejala_penyakit.id_gejala = gejala.id_gejala WHERE gejala_penyakit.id_penyakit = " + str(j)) id_gejala = cursor.fetchall() list_gj.append([i[0] for i in id_gejala ]) # convert list of tuple to list print("gejala = ", list_gj) for x in range(len(list_gj[0])): for y in range(len(list_gj[1])): if list_gj[0][x] == list_gj[1][y]: arr_gejala.append(list_gj[0][x]) if len(arr_gejala) >= 2: # baru tambahkan id penyakit new_list_penyakit = [] list_penyakit = [] id_penyakit = [] for k in arr_penyakit[i]: cursor.execute( "SELECT penyakit.nama_penyakit FROM penyakit WHERE id_penyakit = " + str(k)) id_penyakit.append(cursor.fetchall()) # list_penyakit.append([i[0] for i in id_penyakit]) # print("id penyakit = ", [i[0] for i in id_penyakit]) # print("penyakit list = ", flat(list_penyakit)) for y in id_penyakit: for z in y: list_penyakit.append(z[0]) new_list_penyakit = ",".join(map("".join, list_penyakit)) # print("z = ", new_list_penyakit) # new_list_penyakit = ",".join(map("".join, flat(list_penyakit))) penyakit_join_list = [new_list_penyakit] # print("new list penyakit = ", penyakit_join_list) # print(gj) name_gejala = [item.split(" ") for item in arr_gejala] # print("old = ", arr_gejala) gj = [element for sub in name_gejala for element in sub] # print("gj = ", gj) stopwords = get_stopword('file/konjungsi.csv') filters = filtering(gj, stopwords) stems = stemming(filters) sinonim = get_sinonim(stems) result, cf = get_cf(conn, sinonim) list_gj = ",".join(map("".join, arr_gejala)) list = [list_gj] # print("result = ", list) data = [result[0][0][1]] # print("data = ", data) nilai_cf = [cf] # print("nilai cf = ", nilai_cf) # print("array penyakit ke-i = ", arr_penyakit[i]) zips = zip(list, penyakit_join_list, data, nilai_cf) newfilepath = 'testing_kombinasi_same.csv' with open(newfilepath, 'a', encoding="ISO-8859-1", newline='') as f: writer = csv.writer(f, delimiter=";") for row in zips: tmp = row writer.writerow(tmp) return zips
def combination_other(penyakit): arr_penyakit = [] comb_penyakit = itertools.combinations(penyakit, 2) for cb in comb_penyakit: tuple_comb = [element for tupl in cb for element in tupl] # convert tuple of tuple to list arr_penyakit.append(tuple_comb) for i in range(len(arr_penyakit)): list_gj = [] arr_gejala = [] for j in arr_penyakit[i]: cursor.execute( "SELECT gejala.nama_gejala FROM gejala_penyakit JOIN gejala ON gejala_penyakit.id_gejala = gejala.id_gejala WHERE gejala_penyakit.id_penyakit = " + str(j)) id_gejala = cursor.fetchall() list_gj.append([i[0] for i in id_gejala ]) # convert list of tuple to list # print("penyakit = ", arr_penyakit[i]) # compare list 1 dan list 2 for x in range(len(list_gj[0])): for y in range(len(list_gj[1])): if list_gj[0][x] == list_gj[1][y]: arr_gejala.append(list_gj[0][x]) if len(arr_gejala) >= 2: # print("arr_gejala = ", arr_gejala) list_othergj = [] # baru tambahkan id penyakit new_list_penyakit = [] list_penyakit = [] id_penyakit = [] for k in arr_penyakit[i]: cursor.execute( "SELECT penyakit.nama_penyakit FROM penyakit WHERE id_penyakit = " + str(k)) id_penyakit.append(cursor.fetchall()) # list_penyakit.append([i[0] for i in id_penyakit]) # print("id penyakit = ", [i[0] for i in id_penyakit]) # print("penyakit list = ", flat(list_penyakit)) for l in id_penyakit: for j in l: list_penyakit.append(j[0]) new_list_penyakit = ",".join(map("".join, list_penyakit)) # print("z = ", new_list_penyakit) # new_list_penyakit = ",".join(map("".join, flat(list_penyakit))) penyakit_join_list = [new_list_penyakit] # print("new list penyakit = ", penyakit_join_list) gejala_same_join = ",".join(map("".join, arr_gejala)) gejala_same_list = [gejala_same_join] # print("gejala same list = ", gejala_same_list) for y in arr_penyakit[i]: cursor.execute( "SELECT gejala.nama_gejala FROM gejala_penyakit JOIN gejala ON gejala_penyakit.id_gejala = gejala.id_gejala WHERE gejala_penyakit.id_penyakit = " + str(y)) other_gejala = cursor.fetchall() list_othergj.append([i[0] for i in other_gejala]) # print("list_other gejala = ", list_othergj) for gj in list_othergj: arr_gejalanew = [] arr_othergejala = [ text for text in gj if text not in arr_gejala ] comb_othergejala = itertools.combinations(arr_othergejala, 2) for cg in comb_othergejala: # arr_gejala list_comb = list(cg) for ag in arr_gejala: list_comb.append(ag) split_gj = [item.split(" ") for item in list_comb] arr_gj = [element for sub in split_gj for element in sub] # print("gejala gabungan = ", list_comb) stopwords = get_stopword('file/konjungsi.csv') filters = filtering(arr_gj, stopwords) stems = stemming(filters) sinonim = get_sinonim(stems) result, cf = get_cf(conn, sinonim) join_gj = ",".join(map("".join, list_comb)) list_gj = [join_gj] # print("result = ", list_gj) data = [result[0][0][1]] # print("data = ", data) nilai_cf = [cf] zips = zip(gejala_same_list, list_gj, penyakit_join_list, data, nilai_cf) newfilepath = 'testing_kombinasiother2.csv' with open(newfilepath, 'a', encoding="ISO-8859-1", newline='') as f: writer = csv.writer(f, delimiter=";") for row in zips: tmp = row writer.writerow(tmp) return arr_gejala