def decide_process(text):
    print("\nDEBUG> ------------ DECIDE PROCESS --------------")
    # PROSES
    stopwords = get_stopword('file/konjungsi_info.csv')
    contents = tokenizing(text)
    filters = filtering(contents, stopwords)
    stems = stemming(filters)
    sinonim = get_sinonim(stems)

    stopword_info_list = ["apa", "kenapa", "mengapa", "bagaimana", "obat", "sebab", "solusi", "gejala", "komplikasi", "cegah"]
    stop_list = [word for word in stopword_info_list if word in sinonim]

    for stop in stop_list:
        sinonim.remove(stop)
    if "sakit" in sinonim:
        sinonim.remove("sakit")
    if "demam" in sinonim:
        sinonim.remove("demam")

    print("DEBUG> sinonim baru = ", sinonim)
    print("DEBUG> stop_list = ", stop_list)

    daftar_gejala, id_gejala, nama_gejala = get_symptoms(conn, sinonim)
    print("DEBUG> daftar gejala", daftar_gejala)
    print("DEBUG> panjang gejala : ", len(daftar_gejala))

    daftar_penyakit = []
    for i in sinonim:
        cursor.execute("SELECT id_penyakit, nama_penyakit FROM penyakit WHERE nama_penyakit LIKE '%" + i + "%'")
        daftar_penyakit.append(cursor.fetchall())

    daftar_penyakit = [e for e in daftar_penyakit if e]  # list of tuple to list and not empty
    print("DEBUG> daftar penyakit", daftar_penyakit)

    if len(stop_list) != 0 :

        print("DEBUG> ------------ END DECIDE PROCESS --------------\n")

        if len(stop_list) >= 1:
            if len(daftar_penyakit) == 0 and len(daftar_gejala) < 2:
                return "informasi"
            elif len(daftar_penyakit) > 0 and len(daftar_gejala) < 2:
                return "informasi"
            else:
                return "konsultasi"
        else:
            return "informasi"

    else:
        if len(sinonim) == 1 and "tidak" in sinonim:
            return "konsultasi"
        elif len(daftar_penyakit) == 0 and len(daftar_gejala) < 2:
            return "konsultasi"
        elif len(daftar_penyakit) > 0 and len(daftar_gejala) < 2:
            return "informasi"
        else:
            return "konsultasi"
Beispiel #2
0
def db_stemming(gejala_arr):
    """digunakan untuk melakukan stemming hasil gejala yang didapat dari database"""

    stopwords = get_stopword('file/konjungsi.csv')
    for gj in gejala_arr:
        gejala_split = gj[2].split(" ")
        gejala_filter = filtering(gejala_split, stopwords)
        gejala_stemm = stemming(gejala_filter)
        gejala_join = ' '.join(gejala_stemm)
        gj[2] = gejala_join

    return gejala_arr
def get_info(text):
    stopwords = get_stopword('file/konjungsi_info.csv')
    contents = tokenizing(text)
    filters = filtering(contents, stopwords)
    stems = stemming(filters)
    sinonim = get_sinonim(stems)
    conn = create_connection()
    cursor = conn.cursor()


    penyakit = []

    sinonim_untuk_gejala = copy.deepcopy(sinonim)

    # remove
    stopword_info_list = ["apa", "mengapa", "bagaimana", "obat", "sebab", "solusi", "gejala", "komplikasi", "cegah"]
    stop_list = [word for word in stopword_info_list if word in sinonim_untuk_gejala]

    for stop in stop_list:
        sinonim_untuk_gejala.remove(stop)
    if "sakit" in sinonim_untuk_gejala:
        sinonim_untuk_gejala.remove("sakit")

    

    for i in sinonim_untuk_gejala:
        cursor.execute("SELECT id_penyakit, nama_penyakit FROM penyakit WHERE nama_penyakit LIKE '%" + i + "%'")
        penyakit.append(cursor.fetchall())


    arr_penyakit = [e for e in penyakit if e]  # list of tuple to list and not empty

    # if len(arr_penyakit) == 0:
    #     messages = check_greeting(sinonim)

    #     return sinonim, arr_penyakit, messages

    print("DEBUG> arr_penyakit = ", arr_penyakit)
    if len(arr_penyakit) != 0:
        penyakit_max = penyakit_count(arr_penyakit, sinonim)
        result = get_keywoard(sinonim, penyakit_max, conn)
    else:
        result = [[("Nama penyakit tidak dicantumkan. Silahkan menyertakan nama penyakit dan informasi yang ingin diketahui",)]]

    return sinonim, arr_penyakit, result
Beispiel #4
0
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
    # dapatkan argumen cmd, contoh: python app_local_bot.py "saya merasa mual muntah"
    args = sys.argv
    if len(args) == 1:
        # text = "saya mual, muntah, bintik merah pada kulit, nyeri untuk melirik"
        # text = "demam tinggi,mata tidak merah, batuk darah, mata berair, tidak bisa tidur, kepala tidak sakit, sensitif terhadap cahaya"
        # text  = "Saya merasa mual dan kepala serasa berputar, saya sakit apa?"
        text = "mag"
    else:
        text = args[1]

    conn = create_connection()
    cursor = conn.cursor()

    stopwords = get_stopword('file/konjungsi_info.csv')
    contents = tokenizing(text)
    filters = filtering(contents, stopwords)
    stems = stemming(filters)
    sinonim = get_sinonim(stems)

    # dapatkan gejala
    # gejalas = []
    # for sin in sinonim:
    #     cursor.execute("SELECT * FROM gejala WHERE nama_gejala LIKE '%" + sin + "%'")
    #     gejalas = [[r[0], r[1]]  for r in cursor.fetchall()]

    # print(gejalas)

    # daftar_penyakits = []
    # for gejala in gejalas:
    #     print(gejala)
Beispiel #6
0
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
Beispiel #7
0
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
Beispiel #8
0
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