-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhelper_functions.py
More file actions
321 lines (286 loc) · 10.1 KB
/
helper_functions.py
File metadata and controls
321 lines (286 loc) · 10.1 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import re
from typing import List, Dict, Optional, Union
# Pre-compiled regex patterns
NUMERIC_PATTERN = re.compile(r"[\d.]+")
UNIT_PATTERN = re.compile(r"[a-zA-Z]+")
# Comprehensive Indian health condition mappings (same as before)
HEALTH_CONDITIONS = {
# Diabetes variants
"Diabetes Type 1": {
"keywords": [
"sugar",
"glucose",
"fructose",
"syrup",
"honey",
"jaggery",
"corn syrup",
],
"message": "Contains sugars - monitor blood glucose carefully",
},
"Diabetes Type 2": {
"keywords": [
"sugar",
"glucose",
"fructose",
"refined flour",
"maida",
"corn starch",
],
"message": "High glycemic ingredients present - check with doctor",
},
"Pre-Diabetes": {
"keywords": ["sugar", "glucose", "refined", "processed", "high fructose"],
"message": "Contains processed sugars - may affect blood sugar",
},
# Cardiovascular conditions
"High Blood Pressure": {
"keywords": [
"sodium",
"salt",
"msg",
"monosodium glutamate",
"sodium chloride",
"baking soda",
],
"message": "High sodium content - may increase blood pressure",
},
"Heart Disease": {
"keywords": [
"trans fat",
"hydrogenated",
"saturated fat",
"palm oil",
"coconut oil",
],
"message": "Contains fats that may affect heart health",
},
"High Cholesterol": {
"keywords": [
"saturated fat",
"trans fat",
"cholesterol",
"butter",
"ghee",
"coconut oil",
],
"message": "May increase cholesterol levels",
},
# Organ-specific conditions
"Kidney Disease": {
"keywords": ["sodium", "potassium", "phosphorus", "protein", "msg"],
"message": "May strain kidney function",
},
"Liver Disease": {
"keywords": [
"artificial colors",
"preservatives",
"alcohol",
"high fat",
"processed",
],
"message": "Contains additives that may burden liver",
},
"Thyroid Issues": {
"keywords": ["iodine", "soy", "cruciferous", "goitrogenic", "cabbage"],
"message": "May interfere with thyroid function",
},
# Women's health
"PCOD/PCOS": {
"keywords": ["sugar", "refined flour", "trans fat", "artificial sweeteners"],
"message": "May worsen PCOD/PCOS symptoms",
},
"Pregnancy": {
"keywords": [
"artificial sweeteners",
"high mercury",
"raw",
"unpasteurized",
"alcohol",
],
"message": "Not recommended during pregnancy",
},
"Breastfeeding": {
"keywords": ["caffeine", "alcohol", "artificial colors", "msg"],
"message": "May affect milk quality - consult doctor",
},
# Digestive conditions
"Gastric Issues": {
"keywords": ["spicy", "acidic", "citric acid", "vinegar", "chili", "pepper"],
"message": "May trigger gastric problems",
},
"IBS": {
"keywords": ["lactose", "gluten", "artificial sweeteners", "high fat", "spicy"],
"message": "May trigger IBS symptoms",
},
# Allergies
"Nut Allergy": {
"keywords": [
"nut",
"almond",
"cashew",
"peanut",
"walnut",
"hazelnut",
"pistachio",
],
"message": "Contains nuts - severe allergy risk",
},
"Gluten Sensitivity": {
"keywords": ["wheat", "barley", "rye", "gluten", "flour", "malt", "semolina"],
"message": "Contains gluten - may cause sensitivity reaction",
},
"Lactose Intolerance": {
"keywords": ["milk", "lactose", "dairy", "whey", "casein", "butter", "cheese"],
"message": "Contains dairy - may cause digestive issues",
},
"Soy Allergy": {
"keywords": ["soy", "soybean", "lecithin", "tofu", "soya"],
"message": "Contains soy - allergy risk",
},
"Egg Allergy": {
"keywords": ["egg", "albumin", "lecithin", "mayonnaise"],
"message": "Contains egg - allergy risk",
},
# Lifestyle conditions
"Weight Management": {
"keywords": ["high calorie", "sugar", "saturated fat", "trans fat", "refined"],
"message": "High calorie - may affect weight management",
},
"Muscle Building": {
"keywords": ["low protein", "high sugar", "processed", "artificial"],
"message": "Not optimal for muscle building goals",
},
# Age-specific
"Child (2-12 years)": {
"keywords": [
"artificial colors",
"high sugar",
"caffeine",
"preservatives",
"msg",
],
"message": "May not be suitable for children",
},
"Elderly (60+)": {
"keywords": ["high sodium", "hard to digest", "artificial", "high sugar"],
"message": "May not be suitable for elderly",
},
# Dietary preferences
"Vegetarian": {
"keywords": ["gelatin", "animal fat", "lard", "chicken", "beef", "fish"],
"message": "Contains non-vegetarian ingredients",
},
"Vegan": {
"keywords": ["milk", "dairy", "honey", "gelatin", "whey", "casein", "egg"],
"message": "Contains animal-derived ingredients",
},
"Jain Food": {
"keywords": ["onion", "garlic", "potato", "ginger", "root vegetables"],
"message": "Contains ingredients not suitable for Jain diet",
},
}
def run_health_analysis(product: Dict, health_profile: List[str]) -> List[str]:
"""Enhanced health analysis for Indian health conditions"""
if not health_profile or not product:
return []
warnings = set()
# Combine all text for analysis
ingredient_names = " ".join(
ing.get("name", "").lower() for ing in product.get("ingredients", [])
)
allergen_info = " ".join(product.get("allergens", [])).lower()
combined_text = f"{ingredient_names} {allergen_info}"
# Check each health condition
for condition in health_profile:
if condition in HEALTH_CONDITIONS:
condition_data = HEALTH_CONDITIONS[condition]
# Check if any keywords match
if any(keyword in combined_text for keyword in condition_data["keywords"]):
warnings.add(f"🚨 {condition}: {condition_data['message']}")
return list(warnings)
def calculate_per_serve_nutrition(
nutrition_per_100g: List[Dict], net_weight: Union[int, float]
) -> Optional[List[str]]:
"""Calculate per-serving nutrition with WHO compliance check"""
if not net_weight or not isinstance(net_weight, (int, float)) or net_weight <= 0:
return None
if not nutrition_per_100g or not isinstance(nutrition_per_100g, list):
return None
per_serve_facts = []
# WHO limits per 100g (approximate)
who_limits = {
"saturated": 10, # <10% of energy (roughly 10g per 100g)
"trans": 1, # <1% of energy (roughly 1g per 100g)
"sodium": 500, # <5g per day (roughly 500mg per 100g)
"sugar": 12, # <10% of energy (roughly 12g per 100g)
}
for fact in nutrition_per_100g:
if not isinstance(fact, dict):
per_serve_facts.append("N/A")
continue
value_str = str(fact.get("Value", ""))
try:
numeric_match = NUMERIC_PATTERN.search(value_str)
if not numeric_match:
per_serve_facts.append("N/A")
continue
value_100g = float(numeric_match.group())
unit_matches = UNIT_PATTERN.findall(value_str)
unit = "".join(unit_matches) if unit_matches else ""
# Calculate per-serve value
per_serve_value = (value_100g / 100) * net_weight
# Format value
if per_serve_value < 1:
formatted_value = f"{per_serve_value:.2f}"
else:
formatted_value = f"{per_serve_value:.1f}"
# Add WHO warning if needed
nutrient_lower = fact.get("Nutrient", "").lower()
warning = ""
for limit_nutrient, limit_value in who_limits.items():
if limit_nutrient in nutrient_lower and value_100g > limit_value:
warning = " ⚠️"
break
per_serve_facts.append(f"{formatted_value} {unit}{warning}")
except (ValueError, AttributeError, TypeError):
per_serve_facts.append("N/A")
return per_serve_facts
def get_health_score_color(score: int) -> tuple:
"""Get color coding for health scores based on WHO compliance"""
if score >= 90:
return ("score-green", "🟢")
elif score >= 80:
return ("score-green", "🟢")
elif score >= 70:
return ("score-yellow", "🟡")
elif score >= 60:
return ("score-orange", "🟠")
else:
return ("score-red", "🔴")
def check_who_compliance(nutrition_facts: List[Dict]) -> List[str]:
"""Check WHO compliance for nutrition facts"""
compliance_issues = []
# WHO limits per 100g
limits = {
"saturated fat": 10,
"trans fat": 1,
"sodium": 500,
"total sugars": 12,
"added sugars": 6,
}
for fact in nutrition_facts:
nutrient = fact.get("Nutrient", "").lower()
value_str = fact.get("Value", "")
try:
value = float(NUMERIC_PATTERN.search(value_str).group())
for limit_nutrient, limit_value in limits.items():
if limit_nutrient in nutrient and value > limit_value:
compliance_issues.append(
f"⚠️ {fact.get('Nutrient')} ({value}) exceeds WHO recommendations ({limit_value})"
)
except (AttributeError, ValueError):
continue
if not compliance_issues:
compliance_issues.append("✅ Meets WHO nutritional guidelines")
return compliance_issues