trttung1610 commited on
Commit
c2261c9
·
1 Parent(s): 0c4451d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -12,43 +12,57 @@ def calculate_total_calories(user_input):
12
  results = []
13
 
14
  for item in menu_items:
15
- # Split the menu item into quantity and item name
16
  parts = item.strip().split(' ', 1)
17
-
18
  if len(parts) == 2:
19
- quantity = int(parts[0])
20
- item_name = parts[1]
 
 
 
 
 
 
 
 
 
21
  else:
22
- quantity = 1 # Assume a default quantity of 1 if not specified
23
  item_name = item.strip()
24
-
25
  # Calculate the similarity scores between the item name and menu item names
26
  similarity_scores = df_menu['food'].apply(lambda x: fuzz.token_set_ratio(x.lower(), item_name.lower()))
27
-
28
  # Find the closest match with the highest similarity score
29
  closest_match_index = similarity_scores.idxmax()
30
  closest_match_score = similarity_scores[closest_match_index]
31
-
32
  # Check if the similarity score is above a certain threshold
33
  threshold = 60
34
  if closest_match_score < threshold:
35
  results.append("Không tìm thấy thông tin thức ăn: " + item_name)
36
  continue
37
-
38
  # Get the closest match menu item details
39
  closest_match = df_menu.loc[closest_match_index]
40
  menu_name = closest_match['food']
41
- unit = closest_match['detail']
42
  calories = closest_match['calo']
43
-
 
 
 
 
 
 
 
44
  # Calculate the total calories for the current menu item
45
- item_calories = calories * quantity
46
  total_calories += item_calories
47
  results.append("Tên món ăn: " + menu_name)
48
- results.append("Số lượng: " + str(quantity))
49
- results.append("Đơn vị: " + unit)
50
- results.append("Lượng calories trong mỗi đơn vị: " + str(calories)+ " Kcals")
51
- results.append("Tổng lượng calories của " + menu_name + ": " + str(item_calories)+ " Kcals")
52
  results.append("") # Add an empty entry for spacing
53
 
54
  results.append(str(total_calories) + " Kcals")
 
12
  results = []
13
 
14
  for item in menu_items:
15
+ # Split the menu item into quantity and item name or unit and item name
16
  parts = item.strip().split(' ', 1)
17
+
18
  if len(parts) == 2:
19
+ first_part = parts[0]
20
+ second_part = parts[1]
21
+
22
+ # Check if the first part is a valid quantity (e.g., "200g")
23
+ try:
24
+ quantity = float(first_part)
25
+ item_name = second_part
26
+ except ValueError:
27
+ quantity = 1.0 # Assume a default quantity of 1 if not specified
28
+ unit = first_part
29
+ item_name = second_part
30
  else:
31
+ quantity = 1.0 # Assume a default quantity of 1 if not specified
32
  item_name = item.strip()
33
+
34
  # Calculate the similarity scores between the item name and menu item names
35
  similarity_scores = df_menu['food'].apply(lambda x: fuzz.token_set_ratio(x.lower(), item_name.lower()))
36
+
37
  # Find the closest match with the highest similarity score
38
  closest_match_index = similarity_scores.idxmax()
39
  closest_match_score = similarity_scores[closest_match_index]
40
+
41
  # Check if the similarity score is above a certain threshold
42
  threshold = 60
43
  if closest_match_score < threshold:
44
  results.append("Không tìm thấy thông tin thức ăn: " + item_name)
45
  continue
46
+
47
  # Get the closest match menu item details
48
  closest_match = df_menu.loc[closest_match_index]
49
  menu_name = closest_match['food']
50
+ item_unit = closest_match['unit']
51
  calories = closest_match['calo']
52
+
53
+ if quantity != 1.0:
54
+ # Check if the quantity is in grams or milliliters
55
+ if item_unit.lower() == 'g' or item_unit.lower() == 'gram':
56
+ quantity = float(quantity)
57
+ elif item_unit.lower() == 'l' or item_unit.lower() == 'lit':
58
+ quantity = float(quantity) * 1000
59
+
60
  # Calculate the total calories for the current menu item
61
+ item_calories = (calories / item_unit) * quantity
62
  total_calories += item_calories
63
  results.append("Tên món ăn: " + menu_name)
64
+ results.append("Lượng: " + str(quantity) + " " + item_unit)
65
+ results.append("Lượng calories: " + str(item_calories) + " Kcals")
 
 
66
  results.append("") # Add an empty entry for spacing
67
 
68
  results.append(str(total_calories) + " Kcals")