File size: 3,568 Bytes
96c6c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# import pandas as pd
# from rapidfuzz import process

# # Load medicine data from the MID.xlsx file
# df_meds = pd.read_excel("search/MID.xlsx")

# def correct_medications_with_fuzzy(medications_input, valid_names, threshold=80):
#     corrected = []
#     for med in medications_input:
#         match, score, _ = process.extractOne(med, valid_names)
#         if score >= threshold:
#             corrected.append(match)
#         else:
#             corrected.append(med)
#     return corrected

# def get_medication_details(medications_input):
#     valid_names = df_meds["NAME"].dropna().astype(str).tolist()
#     corrected_names = correct_medications_with_fuzzy(medications_input, valid_names)

#     results = []
#     for med in corrected_names:
#         row = df_meds[df_meds["NAME"].str.lower() == med.lower()]
#         if not row.empty:
#             row = row.iloc[0]
#             advice_points = [
#                 f"*Liver:* {row.get('SAFETY_ADVICE_TO_LIVER', 'N/A')}",
#                 f"*Kidney:* {row.get('SAFETY_ADVICE_TO_KIDNEY', 'N/A')}",
#                 f"*Driving:* {row.get('SAFETY_ADVICE_TO_DRIVING', 'N/A')}",
#                 f"*Pregnancy:* {row.get('SAFETY_ADVICE_TO_PREGNANCY', 'N/A')}",
#                 f"*Alcoholics:* {row.get('SAFETY_ADVICE_TO_ALCOHOL', 'N/A')}",
#             ]
#             advice_combined = "\n".join(advice_points)
#             results.append({
#                 "Name": row["NAME"],
#                 "Contains": row.get("CONTAINS", "N/A"),
#                 "How to Use": row.get("HOW_TO_USE", "N/A"),
#                 "Advice": advice_combined,
#                 "Link": f"<a href='{row.get('LINK', '#')}' target='_blank'>More Info</a>"
#             })
#     return pd.DataFrame(results)

# extractor.py

import pandas as pd
from rapidfuzz import process
import streamlit as st


# # Load data from Excel
@st.cache_data
def load_med_data():
    return pd.read_excel("search/MID.xlsx")

df_meds = load_med_data()

def correct_medications_with_fuzzy(medications_input, valid_names, threshold=80):
    corrected = []
    for med in medications_input:
        match, score, _ = process.extractOne(med, valid_names)
        if score >= threshold:
            corrected.append(match)
        else:
            corrected.append(med)
    return corrected

def get_medication_details(medications_input):
    valid_names = df_meds["NAME"].dropna().astype(str).tolist()
    corrected_names = correct_medications_with_fuzzy(medications_input, valid_names)

    results = []
    for med in corrected_names:
        row = df_meds[df_meds["NAME"].str.lower() == med.lower()]
        if not row.empty:
            row = row.iloc[0]
            advice_points = [
                f"-> Liver: {row.get('SAFETY_ADVICE_TO_LIVER', 'N/A')}",
                f"-> Kidney: {row.get('SAFETY_ADVICE_TO_KIDNEY', 'N/A')}",
                f"-> Driving: {row.get('SAFETY_ADVICE_TO_DRIVING', 'N/A')}",
                f"-> Pregnancy: {row.get('SAFETY_ADVICE_TO_PREGNANCY', 'N/A')}",
                f"-> Alcoholics: {row.get('SAFETY_ADVICE_TO_ALCOHOL', 'N/A')}",
            ]
            results.append({
                "Name": row["NAME"],
                "Contains": row.get("CONTAINS", "N/A"),
                "How to Use": row.get("HOW_TO_USE", "N/A"),
                "Advice": "<br>".join(advice_points),
                "Link": f"[More Info]({row.get('LINK', '#')})"
            })

    return pd.DataFrame(results)

# medications_input="paracetamol"
# Example usage
# print(get_medication_details(medications_input))