shivanis14
commited on
Commit
•
50b8927
1
Parent(s):
24757a7
Create nutrient_analyzer.py
Browse files- nutrient_analyzer.py +71 -0
nutrient_analyzer.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Nutrient thresholds for solids and liquids
|
2 |
+
thresholds = {
|
3 |
+
'solid': {
|
4 |
+
'calories': 250,
|
5 |
+
'sugar': 3,
|
6 |
+
'fat': 4.2,
|
7 |
+
'salt': 625
|
8 |
+
},
|
9 |
+
'liquid': {
|
10 |
+
'calories': 70,
|
11 |
+
'sugar': 2,
|
12 |
+
'fat': None, # Not applicable
|
13 |
+
'salt': 175
|
14 |
+
}
|
15 |
+
}
|
16 |
+
|
17 |
+
# Function to calculate percentage difference from threshold
|
18 |
+
def calculate_percentage_difference(value, threshold):
|
19 |
+
if threshold is None:
|
20 |
+
return None # For nutrients without a threshold
|
21 |
+
return ((value - threshold) / threshold) * 100
|
22 |
+
|
23 |
+
# Function to analyze nutrients and calculate differences
|
24 |
+
def analyze_nutrients(product_type, calories, sugar, salt, serving_size, added_fat=None):
|
25 |
+
threshold_data = thresholds.get(product_type)
|
26 |
+
if not threshold_data:
|
27 |
+
raise ValueError(f"Invalid product type: {product_type}")
|
28 |
+
|
29 |
+
scaled_calories = (calories / serving_size) * 100
|
30 |
+
scaled_sugar = (sugar / serving_size) * 100
|
31 |
+
scaled_salt = (salt / serving_size) * 100
|
32 |
+
scaled_fat = (added_fat / serving_size) * 100 if added_fat is not None else None
|
33 |
+
|
34 |
+
return {
|
35 |
+
'calories': {
|
36 |
+
'value': scaled_calories,
|
37 |
+
'threshold': threshold_data['calories'],
|
38 |
+
'difference': scaled_calories - threshold_data['calories'],
|
39 |
+
'percentageDiff': calculate_percentage_difference(scaled_calories, threshold_data['calories'])
|
40 |
+
},
|
41 |
+
'sugar': {
|
42 |
+
'value': scaled_sugar,
|
43 |
+
'threshold': threshold_data['sugar'],
|
44 |
+
'difference': scaled_sugar - threshold_data['sugar'],
|
45 |
+
'percentageDiff': calculate_percentage_difference(scaled_sugar, threshold_data['sugar'])
|
46 |
+
},
|
47 |
+
'salt': {
|
48 |
+
'value': scaled_salt,
|
49 |
+
'threshold': threshold_data['salt'],
|
50 |
+
'difference': scaled_salt - threshold_data['salt'],
|
51 |
+
'percentageDiff': calculate_percentage_difference(scaled_salt, threshold_data['salt'])
|
52 |
+
},
|
53 |
+
'fat': {
|
54 |
+
'value': scaled_fat,
|
55 |
+
'threshold': threshold_data['fat'],
|
56 |
+
'difference': scaled_fat - threshold_data['fat'] if scaled_fat is not None else None,
|
57 |
+
'percentageDiff': calculate_percentage_difference(scaled_fat, threshold_data['fat']) if scaled_fat is not None else None
|
58 |
+
}
|
59 |
+
}
|
60 |
+
|
61 |
+
# Example of how these functions can be called in the main code
|
62 |
+
#if __name__ == "__main__":
|
63 |
+
# product_type = 'solid' # 'solid' or 'liquid'
|
64 |
+
# calories = 300
|
65 |
+
# sugar = 4
|
66 |
+
# salt = 700
|
67 |
+
# serving_size = 100
|
68 |
+
# added_fat = 5 # Optional
|
69 |
+
|
70 |
+
# result = analyze_nutrients(product_type, calories, sugar, salt, serving_size, added_fat)
|
71 |
+
# print(result)
|