forked from jackmleitch/whatscooking-deployment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrec_sys.py
More file actions
86 lines (69 loc) · 2.86 KB
/
rec_sys.py
File metadata and controls
86 lines (69 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from ingredient_parser import ingredient_parser
import pickle
import config
import unidecode, ast
# Top-N recomendations order by score
def get_recommendations(N, scores):
# load in recipe dataset
df_recipes = pd.read_csv(config.PARSED_PATH)
# order the scores with and filter to get the highest N scores
top = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)[:N]
# create dataframe to load in recommendations
recommendation = pd.DataFrame(columns=["recipe", "ingredients", "score", "url"])
count = 0
for i in top:
recommendation.at[count, "recipe"] = title_parser(df_recipes["recipe_name"][i])
recommendation.at[count, "ingredients"] = ingredient_parser_final(
df_recipes["ingredients"][i]
)
recommendation.at[count, "url"] = df_recipes["recipe_urls"][i]
recommendation.at[count, "score"] = "{:.3f}".format(float(scores[i]))
count += 1
return recommendation
# neaten the ingredients being outputted
def ingredient_parser_final(ingredient):
if isinstance(ingredient, list):
ingredients = ingredient
else:
ingredients = ast.literal_eval(ingredient)
ingredients = ",".join(ingredients)
ingredients = unidecode.unidecode(ingredients)
return ingredients
def title_parser(title):
title = unidecode.unidecode(title)
return title
def RecSys(ingredients, N=5):
"""
The reccomendation system takes in a list of ingredients and returns a list of top 5
recipes based of of cosine similarity.
:param ingredients: a list of ingredients
:param N: the number of reccomendations returned
:return: top 5 reccomendations for cooking recipes
"""
# load in tdidf model and encodings
with open(config.TFIDF_ENCODING_PATH, "rb") as f:
tfidf_encodings = pickle.load(f)
with open(config.TFIDF_MODEL_PATH, "rb") as f:
tfidf = pickle.load(f)
# parse the ingredients using my ingredient_parser
try:
ingredients_parsed = ingredient_parser(ingredients)
except:
ingredients_parsed = ingredient_parser([ingredients])
# use our pretrained tfidf model to encode our input ingredients
ingredients_parsed = " ".join(ingredients_parsed)
ingredients_tfidf = tfidf.transform([ingredients_parsed])
# calculate cosine similarity between actual recipe ingreds and test ingreds
cos_sim = map(lambda x: cosine_similarity(ingredients_tfidf, x), tfidf_encodings)
scores = list(cos_sim)
# Filter top N recommendations
recommendations = get_recommendations(N, scores)
return recommendations
if __name__ == "__main__":
# test ingredients
test_ingredients = "pasta, tomato, onion"
recs = RecSys(test_ingredients)
print(recs.score)