From 0468c8cdf46b159326f0d543820e95310437d13d Mon Sep 17 00:00:00 2001 From: Sirin Puenggun Date: Sun, 7 May 2023 22:41:34 +0700 Subject: [PATCH] fix,use prepare_db as function foodSearch path --- Essential/FoodSearch.py | 12 ++++++++++-- Essential/prepare_db.py | 35 ++++++++++++++++++++--------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/Essential/FoodSearch.py b/Essential/FoodSearch.py index 25ad312..a9f817d 100644 --- a/Essential/FoodSearch.py +++ b/Essential/FoodSearch.py @@ -1,8 +1,16 @@ import sqlite3 +import os.path + +current_dir = os.path.dirname(os.path.abspath(__file__)) +db_path = (current_dir + r"\data\food_data.db") class FoodSearch: - def __init__(self, db_path): - self.conn = sqlite3.connect(db_path) + def __init__(self): + self.status = os.path.exists(db_path) + if self.status: + self.conn = sqlite3.connect(db_path) + else: + raise FileNotFoundError self.cursor = self.conn.cursor() def search(self, user_input) -> list: diff --git a/Essential/prepare_db.py b/Essential/prepare_db.py index a92987c..0b60527 100644 --- a/Essential/prepare_db.py +++ b/Essential/prepare_db.py @@ -1,24 +1,29 @@ +import os import sqlite3 import pandas as pd import re -thai_df = pd.read_csv('thai_data.csv') -us_df = pd.read_csv('us_data.csv') -japan_df = pd.read_csv('japan_data.csv') +def prepare_db(): -conn = sqlite3.connect('food_data.db') + dfs = [] + for i in range(1, 5): + filename = f'Essential/data/us_data_{i}.csv' + df = pd.read_csv(filename) + dfs.append(df) -thai_df = thai_df[~thai_df.product_name.str.contains('to be deleted', na=False, flags=re.IGNORECASE)] -japan_df = japan_df[~japan_df.product_name.str.contains('to be deleted', na=False, flags=re.IGNORECASE)] -us_df = us_df[~us_df.product_name.str.contains('to be deleted', na=False, flags=re.IGNORECASE)] + thai_df = pd.read_csv('Essential/data/thai_data.csv') + us_df = pd.concat(dfs, ignore_index=True) + japan_df = pd.read_csv('Essential/data/japan_data.csv') -thai_df = thai_df.dropna(subset=['product_name']) -japan_df = japan_df.dropna(subset=['product_name']) -us_df = us_df.dropna(subset=['product_name']) + current_dir = os.path.dirname(os.path.abspath(__file__)) + conn = sqlite3.connect(current_dir + r"\data\food_data.db") -thai_df.to_sql('thai_food', conn, if_exists='replace', index=False) -us_df.to_sql('us_food', conn, if_exists='replace', index=False) -japan_df.to_sql('japan_food', conn, if_exists='replace', index=False) + # Clean + combined_df = pd.concat([thai_df, us_df, japan_df], ignore_index=True) + combined_df = combined_df[~combined_df.product_name.str.contains('to be deleted', na=False, flags=re.IGNORECASE)] + combined_df = combined_df.dropna(subset=['product_name']) -conn.commit() -conn.close() \ No newline at end of file + combined_df.to_sql('food_data', conn, if_exists='replace') + + conn.commit() + conn.close() \ No newline at end of file