From a1ca7304eb32f5d7636ea0db8bfa6031bfcf773c Mon Sep 17 00:00:00 2001 From: Mann Patel <130435633+MannPatel0@users.noreply.github.com> Date: Wed, 2 Apr 2025 13:56:48 -0600 Subject: [PATCH] updatepush --- recommondation-engine/example.py | 69 +++++++++++++++++++++---------- recommondation-engine/example.sql | 5 +++ recommondation-engine/test.py | 33 +++++++++++++++ 3 files changed, 86 insertions(+), 21 deletions(-) create mode 100644 recommondation-engine/example.sql create mode 100644 recommondation-engine/test.py diff --git a/recommondation-engine/example.py b/recommondation-engine/example.py index c101c76..d55c0f7 100644 --- a/recommondation-engine/example.py +++ b/recommondation-engine/example.py @@ -11,6 +11,7 @@ def database(): ) return db_connection + def get_all_products(): db_con = database() @@ -37,32 +38,58 @@ def get_all_products(): results = cursor.fetchall() print(results[1]) + final = [] for row in results: + final.append(row) + print(row) + + cursor.close() + db_con.close() + return final + +def get_user_history(user_id): + db_con = database() + cursor = db_con.cursor() + + cursor.execute("SELECT CategoryID FROM Category") + categories = cursor.fetchall() + + select_clause = "SELECT p.ProductID" + for category in categories: + category_id = category[0] # get the uid of the catefory and then append that to the new column + select_clause += f", MAX(CASE WHEN pc.CategoryID = {category_id} THEN 1 ELSE 0 END) AS `Cat_{category_id}`" + + final_query = f""" + {select_clause} + FROM Product p + LEFT JOIN Product_Category pc ON p.ProductID = pc.ProductID + LEFT JOIN Category c ON pc.CategoryID = c.CategoryID + where p.ProductID in (select ProductID from History where UserID = {user_id}) + GROUP BY p.ProductID; + """ + + cursor.execute(final_query) + results = cursor.fetchall() + + print(results[1]) + final = [] + for row in results: + final.append(row) print(row) cursor.close() + db_con.close() + return final -def get_user_history(): - db_con = database() - cursor = db_con.cursor() - user_id = 1 - query= f"""select ProductID from History where UserID={user_id}""" - cursor.execute(query) - data = cursor.fetchall() - product_arr = [] - for item in data: - product_arr.append(item[0]) - - querydata= tuple(product_arr) - prod_query = f"""select * from Product where ProductID IN {querydata} """ - cursor.execute(prod_query) - res = cursor.fetchall() - for i in res: - print(i,"\n") +print("all products:") +get_all_products() + +print("User History products:") +get_user_history(1) +def Calculate_cosin_similarity(): + pass -get_user_history() - - -#get_all_products() +def Main: + pass diff --git a/recommondation-engine/example.sql b/recommondation-engine/example.sql new file mode 100644 index 0000000..f7fccb7 --- /dev/null +++ b/recommondation-engine/example.sql @@ -0,0 +1,5 @@ +select * +from Product +Where ProductID in (select ProductID + from History + where UserID=1); diff --git a/recommondation-engine/test.py b/recommondation-engine/test.py new file mode 100644 index 0000000..3107da7 --- /dev/null +++ b/recommondation-engine/test.py @@ -0,0 +1,33 @@ +import mysql.connector as db_con + +#TODO: Specify all the required queries +query_get_all_Prod= ("SELECT * FROM Product ") + + +#TODO: connect with the db +def database(): + db = db_con.connect( + host = "localhost", + port = 3306, + user = "root", + database = "Marketplace" + ) + + cursor = db.cursor() + cursor.execute(query_get_all_Prod) + + data = [None] + for item in cursor: + data.append(item) + # print(item) + + print(data[1]) + cursor.close() + db.close() + + + +#TODO: Get All products +# Make it into a dictionary with product id and the list of category it would have +# {Prod1:[1,0,0,0,1]} this could mean its a [elctronics, 0,0,0, kitchen] +database()