updatepush
This commit is contained in:
@@ -11,6 +11,7 @@ def database():
|
|||||||
)
|
)
|
||||||
return db_connection
|
return db_connection
|
||||||
|
|
||||||
|
|
||||||
def get_all_products():
|
def get_all_products():
|
||||||
|
|
||||||
db_con = database()
|
db_con = database()
|
||||||
@@ -37,32 +38,58 @@ def get_all_products():
|
|||||||
results = cursor.fetchall()
|
results = cursor.fetchall()
|
||||||
|
|
||||||
print(results[1])
|
print(results[1])
|
||||||
|
final = []
|
||||||
for row in results:
|
for row in results:
|
||||||
|
final.append(row)
|
||||||
print(row)
|
print(row)
|
||||||
|
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
db_con.close()
|
||||||
|
return final
|
||||||
|
|
||||||
def get_user_history():
|
def get_user_history(user_id):
|
||||||
db_con = database()
|
db_con = database()
|
||||||
cursor = db_con.cursor()
|
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)
|
cursor.execute("SELECT CategoryID FROM Category")
|
||||||
prod_query = f"""select * from Product where ProductID IN {querydata} """
|
categories = cursor.fetchall()
|
||||||
cursor.execute(prod_query)
|
|
||||||
res = cursor.fetchall()
|
select_clause = "SELECT p.ProductID"
|
||||||
for i in res:
|
for category in categories:
|
||||||
print(i,"\n")
|
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
|
||||||
|
|
||||||
|
print("all products:")
|
||||||
|
get_all_products()
|
||||||
|
|
||||||
|
print("User History products:")
|
||||||
|
get_user_history(1)
|
||||||
|
|
||||||
|
|
||||||
|
def Calculate_cosin_similarity():
|
||||||
|
pass
|
||||||
|
|
||||||
get_user_history()
|
def Main:
|
||||||
|
pass
|
||||||
|
|
||||||
#get_all_products()
|
|
||||||
|
|||||||
5
recommondation-engine/example.sql
Normal file
5
recommondation-engine/example.sql
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
select *
|
||||||
|
from Product
|
||||||
|
Where ProductID in (select ProductID
|
||||||
|
from History
|
||||||
|
where UserID=1);
|
||||||
33
recommondation-engine/test.py
Normal file
33
recommondation-engine/test.py
Normal file
@@ -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()
|
||||||
Reference in New Issue
Block a user