From 1c115144391e6e4b415b64d75b2f6fd23f3d4a6c Mon Sep 17 00:00:00 2001 From: Sirin Puenggun Date: Fri, 12 May 2023 10:40:17 +0700 Subject: [PATCH] Add nutrients value filter option --- Essential/FoodSearch.py | 57 +++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/Essential/FoodSearch.py b/Essential/FoodSearch.py index eea5e80..7dd0faf 100644 --- a/Essential/FoodSearch.py +++ b/Essential/FoodSearch.py @@ -22,10 +22,42 @@ class FoodSearch: raise FileNotFoundError("Database file not found.") self.db_path = db_path - - def search(self, user_input, countries=None, limit=100, categories=None, categories_both=None) -> list: + self.col_index = {'index': 0, 'product_name': 1, 'brands': 2, 'brands_tags': 3, 'categories': 4, 'categories_tags': 5, + 'categories_en': 6, 'origins': 7, 'origins_tags': 8, 'origins_en': 9, 'countries': 10, 'countries_tags': 11, + 'countries_en': 12, 'image_url': 13, 'image_ingredients_url': 14, 'image_nutrition_url': 15, + 'energy-kcal_100g': 16, 'fat_100g': 17, 'saturated-fat_100g': 18, 'unsaturated-fat_100g': 19, + 'omega-3-fat_100g': 20, 'omega-6-fat_100g': 21, 'omega-9-fat_100g': 22, 'trans-fat_100g': 23, + 'cholesterol_100g': 24, 'carbohydrates_100g': 25, 'sugars_100g': 26, 'sucrose_100g': 27, 'glucose_100g': 28, + 'fructose_100g': 29, 'lactose_100g': 30, 'maltose_100g': 31, 'fiber_100g': 32, 'soluble-fiber_100g': 33, + 'insoluble-fiber_100g': 34, 'proteins_100g': 35, 'salt_100g': 36, 'added-salt_100g': 37, 'sodium_100g': 38, + 'alcohol_100g': 39, 'vitamin-a_100g': 40, 'beta-carotene_100g': 41, 'vitamin-d_100g': 42, 'vitamin-e_100g': 43, + 'vitamin-k_100g': 44, 'vitamin-c_100g': 45, 'vitamin-b1_100g': 46, 'vitamin-b2_100g': 47, 'vitamin-pp_100g': 48, + 'vitamin-b6_100g': 49, 'vitamin-b9_100g': 50, 'vitamin-b12_100g': 51, 'bicarbonate_100g': 52, + 'potassium_100g': 53, 'chloride_100g': 54, 'calcium_100g': 55, 'phosphorus_100g': 56, 'iron_100g': 57, + 'magnesium_100g': 58, 'zinc_100g': 59, 'copper_100g': 60, 'manganese_100g': 61, 'fluoride_100g': 62, + 'selenium_100g': 63, 'chromium_100g': 64, 'molybdenum_100g': 65, 'iodine_100g': 66, 'caffeine_100g': 67, + + 'cocoa_100g': 68, 'carbon-footprint_100g': 69, 'carbon-footprint-from-meat-or-fish_100g': 70} + self.index_col = {0: 'index', 1: 'product_name', 2: 'brands', 3: 'brands_tags', 4: 'categories', 5: 'categories_tags', + 6: 'categories_en', 7: 'origins', 8: 'origins_tags', 9: 'origins_en', 10: 'countries', 11: 'countries_tags', + 12: 'countries_en', 13: 'image_url', 14: 'image_ingredients_url', 15: 'image_nutrition_url', + 16: 'energy-kcal_100g', 17: 'fat_100g', 18: 'saturated-fat_100g', 19: 'unsaturated-fat_100g', + 20: 'omega-3-fat_100g', 21: 'omega-6-fat_100g', 22: 'omega-9-fat_100g', 23: 'trans-fat_100g', + 24: 'cholesterol_100g', 25: 'carbohydrates_100g', 26: 'sugars_100g', 27: 'sucrose_100g', 28: 'glucose_100g', + 29: 'fructose_100g', 30: 'lactose_100g', 31: 'maltose_100g', 32: 'fiber_100g', 33: 'soluble-fiber_100g', + 34: 'insoluble-fiber_100g', 35: 'proteins_100g', 36: 'salt_100g', 37: 'added-salt_100g', 38: 'sodium_100g', + 39: 'alcohol_100g', 40: 'vitamin-a_100g', 41: 'beta-carotene_100g', 42: 'vitamin-d_100g', + 43: 'vitamin-e_100g', 44: 'vitamin-k_100g', 45: 'vitamin-c_100g', 46: 'vitamin-b1_100g', + 47: 'vitamin-b2_100g', 48: 'vitamin-pp_100g', 49: 'vitamin-b6_100g', 50: 'vitamin-b9_100g', + 51: 'vitamin-b12_100g', 52: 'bicarbonate_100g', 53: 'potassium_100g', 54: 'chloride_100g', + 55: 'calcium_100g', 56: 'phosphorus_100g', 57: 'iron_100g', 58: 'magnesium_100g', 59: 'zinc_100g', + 60: 'copper_100g', 61: 'manganese_100g', 62: 'fluoride_100g', 63: 'selenium_100g', 64: 'chromium_100g', + 65: 'molybdenum_100g', 66: 'iodine_100g', 67: 'caffeine_100g', 68: 'cocoa_100g', 69: 'carbon-footprint_100g', + 70: 'carbon-footprint-from-meat-or-fish_100g'} + + def search(self, user_input, countries=None, limit=100, categories=None, categories_both=None, column_filters=None) -> list: """ - Search for food data based on the user's input, country filter, and category filter. + Search for food data based on the user's input, country filter, category filter, and column filters. Parameters: user_input (str): The input provided by the user to search for food data. @@ -35,8 +67,13 @@ class FoodSearch: Defaults to 100. categories (list, optional): A list of category words to filter the results. If None, no filtering based on categories will be applied. Defaults to None. - categories_both (list, optional): Same as categories - Anyway, the result from this one will contain all word in list. + categories_both (list, optional): A list of category words to filter the results. + The result must contain all words in this list. + If None, no filtering based on categories will be applied. Defaults to None. + column_filters (dict, optional): A dictionary of column index and condition pairs for filtering. + The keys represent the column index (int), and the values represent the condition (str). + The condition format follows the pattern: {column_index}:{condition}. + If None, no filtering based on column values will be applied. Defaults to None. Returns: list: A list of tuples representing the search results from the database. @@ -74,7 +111,12 @@ class FoodSearch: category_filter2 = " AND ".join(category_filters2) query += f" AND ({category_filter2})" - + + if column_filters is not None: + for column_index, condition in column_filters.items(): + col_name = self.index_col[column_index] + query += f" AND [{col_name}] {condition}" + query += f" LIMIT {limit}" results = conn.execute(query, params).fetchall() @@ -125,6 +167,5 @@ class FoodSearch: # food_search = FoodSearch() -# results = food_search.search("apple", countries=["thai"], categories_both=["fruit", "snack"], limit=1) - +# results = food_search.search("apple", countries=["thai"], categories=["fruit", "snack"], column_filters={16:">=1"}, limit=10) # print(results) \ No newline at end of file