vikaso001 commited on
Commit
96c6c9e
·
verified ·
1 Parent(s): fabd2dd

Update search/extractor.py

Browse files
Files changed (1) hide show
  1. search/extractor.py +96 -96
search/extractor.py CHANGED
@@ -1,96 +1,96 @@
1
- # import pandas as pd
2
- # from rapidfuzz import process
3
-
4
- # # Load medicine data from the MID.xlsx file
5
- # df_meds = pd.read_excel("search/MID.xlsx")
6
-
7
- # def correct_medications_with_fuzzy(medications_input, valid_names, threshold=80):
8
- # corrected = []
9
- # for med in medications_input:
10
- # match, score, _ = process.extractOne(med, valid_names)
11
- # if score >= threshold:
12
- # corrected.append(match)
13
- # else:
14
- # corrected.append(med)
15
- # return corrected
16
-
17
- # def get_medication_details(medications_input):
18
- # valid_names = df_meds["NAME"].dropna().astype(str).tolist()
19
- # corrected_names = correct_medications_with_fuzzy(medications_input, valid_names)
20
-
21
- # results = []
22
- # for med in corrected_names:
23
- # row = df_meds[df_meds["NAME"].str.lower() == med.lower()]
24
- # if not row.empty:
25
- # row = row.iloc[0]
26
- # advice_points = [
27
- # f"*Liver:* {row.get('SAFETY_ADVICE_TO_LIVER', 'N/A')}",
28
- # f"*Kidney:* {row.get('SAFETY_ADVICE_TO_KIDNEY', 'N/A')}",
29
- # f"*Driving:* {row.get('SAFETY_ADVICE_TO_DRIVING', 'N/A')}",
30
- # f"*Pregnancy:* {row.get('SAFETY_ADVICE_TO_PREGNANCY', 'N/A')}",
31
- # f"*Alcoholics:* {row.get('SAFETY_ADVICE_TO_ALCOHOL', 'N/A')}",
32
- # ]
33
- # advice_combined = "\n".join(advice_points)
34
- # results.append({
35
- # "Name": row["NAME"],
36
- # "Contains": row.get("CONTAINS", "N/A"),
37
- # "How to Use": row.get("HOW_TO_USE", "N/A"),
38
- # "Advice": advice_combined,
39
- # "Link": f"<a href='{row.get('LINK', '#')}' target='_blank'>More Info</a>"
40
- # })
41
- # return pd.DataFrame(results)
42
-
43
- # extractor.py
44
-
45
- import pandas as pd
46
- from rapidfuzz import process
47
- import streamlit as st
48
-
49
-
50
- # # Load data from Excel
51
- @st.cache_data
52
- def load_med_data():
53
- return pd.read_excel("search/MID.xlsx")
54
-
55
- df_meds = load_med_data()
56
-
57
- def correct_medications_with_fuzzy(medications_input, valid_names, threshold=80):
58
- corrected = []
59
- for med in medications_input:
60
- match, score, _ = process.extractOne(med, valid_names)
61
- if score >= threshold:
62
- corrected.append(match)
63
- else:
64
- corrected.append(med)
65
- return corrected
66
-
67
- def get_medication_details(medications_input):
68
- valid_names = df_meds["NAME"].dropna().astype(str).tolist()
69
- corrected_names = correct_medications_with_fuzzy(medications_input, valid_names)
70
-
71
- results = []
72
- for med in corrected_names:
73
- row = df_meds[df_meds["NAME"].str.lower() == med.lower()]
74
- if not row.empty:
75
- row = row.iloc[0]
76
- advice_points = [
77
- f"**Liver:** {row.get('SAFETY_ADVICE_TO_LIVER', 'N/A')}",
78
- f"**Kidney:** {row.get('SAFETY_ADVICE_TO_KIDNEY', 'N/A')}",
79
- f"**Driving:** {row.get('SAFETY_ADVICE_TO_DRIVING', 'N/A')}",
80
- f"**Pregnancy:** {row.get('SAFETY_ADVICE_TO_PREGNANCY', 'N/A')}",
81
- f"**Alcoholics:** {row.get('SAFETY_ADVICE_TO_ALCOHOL', 'N/A')}",
82
- ]
83
- results.append({
84
- "Name": row["NAME"],
85
- "Contains": row.get("CONTAINS", "N/A"),
86
- "How to Use": row.get("HOW_TO_USE", "N/A"),
87
- "Advice": "<br>".join(advice_points),
88
- "Link": f"[More Info]({row.get('LINK', '#')})"
89
- })
90
-
91
- return pd.DataFrame(results)
92
-
93
- # medications_input="paracetamol"
94
- # Example usage
95
- # print(get_medication_details(medications_input))
96
-
 
1
+ # import pandas as pd
2
+ # from rapidfuzz import process
3
+
4
+ # # Load medicine data from the MID.xlsx file
5
+ # df_meds = pd.read_excel("search/MID.xlsx")
6
+
7
+ # def correct_medications_with_fuzzy(medications_input, valid_names, threshold=80):
8
+ # corrected = []
9
+ # for med in medications_input:
10
+ # match, score, _ = process.extractOne(med, valid_names)
11
+ # if score >= threshold:
12
+ # corrected.append(match)
13
+ # else:
14
+ # corrected.append(med)
15
+ # return corrected
16
+
17
+ # def get_medication_details(medications_input):
18
+ # valid_names = df_meds["NAME"].dropna().astype(str).tolist()
19
+ # corrected_names = correct_medications_with_fuzzy(medications_input, valid_names)
20
+
21
+ # results = []
22
+ # for med in corrected_names:
23
+ # row = df_meds[df_meds["NAME"].str.lower() == med.lower()]
24
+ # if not row.empty:
25
+ # row = row.iloc[0]
26
+ # advice_points = [
27
+ # f"*Liver:* {row.get('SAFETY_ADVICE_TO_LIVER', 'N/A')}",
28
+ # f"*Kidney:* {row.get('SAFETY_ADVICE_TO_KIDNEY', 'N/A')}",
29
+ # f"*Driving:* {row.get('SAFETY_ADVICE_TO_DRIVING', 'N/A')}",
30
+ # f"*Pregnancy:* {row.get('SAFETY_ADVICE_TO_PREGNANCY', 'N/A')}",
31
+ # f"*Alcoholics:* {row.get('SAFETY_ADVICE_TO_ALCOHOL', 'N/A')}",
32
+ # ]
33
+ # advice_combined = "\n".join(advice_points)
34
+ # results.append({
35
+ # "Name": row["NAME"],
36
+ # "Contains": row.get("CONTAINS", "N/A"),
37
+ # "How to Use": row.get("HOW_TO_USE", "N/A"),
38
+ # "Advice": advice_combined,
39
+ # "Link": f"<a href='{row.get('LINK', '#')}' target='_blank'>More Info</a>"
40
+ # })
41
+ # return pd.DataFrame(results)
42
+
43
+ # extractor.py
44
+
45
+ import pandas as pd
46
+ from rapidfuzz import process
47
+ import streamlit as st
48
+
49
+
50
+ # # Load data from Excel
51
+ @st.cache_data
52
+ def load_med_data():
53
+ return pd.read_excel("search/MID.xlsx")
54
+
55
+ df_meds = load_med_data()
56
+
57
+ def correct_medications_with_fuzzy(medications_input, valid_names, threshold=80):
58
+ corrected = []
59
+ for med in medications_input:
60
+ match, score, _ = process.extractOne(med, valid_names)
61
+ if score >= threshold:
62
+ corrected.append(match)
63
+ else:
64
+ corrected.append(med)
65
+ return corrected
66
+
67
+ def get_medication_details(medications_input):
68
+ valid_names = df_meds["NAME"].dropna().astype(str).tolist()
69
+ corrected_names = correct_medications_with_fuzzy(medications_input, valid_names)
70
+
71
+ results = []
72
+ for med in corrected_names:
73
+ row = df_meds[df_meds["NAME"].str.lower() == med.lower()]
74
+ if not row.empty:
75
+ row = row.iloc[0]
76
+ advice_points = [
77
+ f"-> Liver: {row.get('SAFETY_ADVICE_TO_LIVER', 'N/A')}",
78
+ f"-> Kidney: {row.get('SAFETY_ADVICE_TO_KIDNEY', 'N/A')}",
79
+ f"-> Driving: {row.get('SAFETY_ADVICE_TO_DRIVING', 'N/A')}",
80
+ f"-> Pregnancy: {row.get('SAFETY_ADVICE_TO_PREGNANCY', 'N/A')}",
81
+ f"-> Alcoholics: {row.get('SAFETY_ADVICE_TO_ALCOHOL', 'N/A')}",
82
+ ]
83
+ results.append({
84
+ "Name": row["NAME"],
85
+ "Contains": row.get("CONTAINS", "N/A"),
86
+ "How to Use": row.get("HOW_TO_USE", "N/A"),
87
+ "Advice": "<br>".join(advice_points),
88
+ "Link": f"[More Info]({row.get('LINK', '#')})"
89
+ })
90
+
91
+ return pd.DataFrame(results)
92
+
93
+ # medications_input="paracetamol"
94
+ # Example usage
95
+ # print(get_medication_details(medications_input))
96
+