-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathword2vec_rec.py
241 lines (202 loc) · 7.75 KB
/
word2vec_rec.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import os
import sys
import logging
import unidecode
import ast
import numpy as np
import pandas as pd
from gensim.models import Word2Vec
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from collections import defaultdict
import config
from ingredient_parser import ingredient_parser
def get_and_sort_corpus(data):
"""
Get corpus with the documents sorted in alphabetical order
"""
corpus_sorted = []
for doc in data.parsed.values:
doc.sort()
corpus_sorted.append(doc)
return corpus_sorted
def get_recommendations(N, scores):
"""
Top-N recomendations order by score
"""
# 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"] = f"{scores[i]}"
count += 1
return recommendation
def title_parser(title):
title = unidecode.unidecode(title)
return title
def ingredient_parser_final(ingredient):
"""
neaten the ingredients being outputted
"""
if isinstance(ingredient, list):
ingredients = ingredient
else:
ingredients = ast.literal_eval(ingredient)
ingredients = ",".join(ingredients)
ingredients = unidecode.unidecode(ingredients)
return ingredients
class MeanEmbeddingVectorizer(object):
def __init__(self, word_model):
self.word_model = word_model
self.vector_size = word_model.wv.vector_size
def fit(self): # comply with scikit-learn transformer requirement
return self
def transform(self, docs): # comply with scikit-learn transformer requirement
doc_word_vector = self.word_average_list(docs)
return doc_word_vector
def word_average(self, sent):
"""
Compute average word vector for a single doc/sentence.
:param sent: list of sentence tokens
:return:
mean: float of averaging word vectors
"""
mean = []
for word in sent:
if word in self.word_model.wv.index_to_key:
mean.append(self.word_model.wv.get_vector(word))
if not mean: # empty words
# If a text is empty, return a vector of zeros.
# logging.warning(
# "cannot compute average owing to no vector for {}".format(sent)
# )
return np.zeros(self.vector_size)
else:
mean = np.array(mean).mean(axis=0)
return mean
def word_average_list(self, docs):
"""
Compute average word vector for multiple docs, where docs had been tokenized.
:param docs: list of sentence in list of separated tokens
:return:
array of average word vector in shape (len(docs),)
"""
return np.vstack([self.word_average(sent) for sent in docs])
class TfidfEmbeddingVectorizer(object):
def __init__(self, word_model):
self.word_model = word_model
self.word_idf_weight = None
self.vector_size = word_model.wv.vector_size
def fit(self, docs): # comply with scikit-learn transformer requirement
"""
Fit in a list of docs, which had been preprocessed and tokenized,
such as word bi-grammed, stop-words removed, lemmatized, part of speech filtered.
Then build up a tfidf model to compute each word's idf as its weight.
Noted that tf weight is already involved when constructing average word vectors, and thus omitted.
:param
pre_processed_docs: list of docs, which are tokenized
:return:
self
"""
text_docs = []
for doc in docs:
text_docs.append(" ".join(doc))
tfidf = TfidfVectorizer()
tfidf.fit(text_docs) # must be list of text string
# if a word was never seen - it must be at least as infrequent
# as any of the known words - so the default idf is the max of
# known idf's
max_idf = max(tfidf.idf_) # used as default value for defaultdict
self.word_idf_weight = defaultdict(
lambda: max_idf,
[(word, tfidf.idf_[i]) for word, i in tfidf.vocabulary_.items()],
)
return self
def transform(self, docs): # comply with scikit-learn transformer requirement
doc_word_vector = self.word_average_list(docs)
return doc_word_vector
def word_average(self, sent):
"""
Compute average word vector for a single doc/sentence.
:param sent: list of sentence tokens
:return:
mean: float of averaging word vectors
"""
mean = []
for word in sent:
if word in self.word_model.wv.index_to_key:
mean.append(
self.word_model.wv.get_vector(word) * self.word_idf_weight[word]
) # idf weighted
if not mean: # empty words
# If a text is empty, return a vector of zeros.
# logging.warning(
# "cannot compute average owing to no vector for {}".format(sent)
# )
return np.zeros(self.vector_size)
else:
mean = np.array(mean).mean(axis=0)
return mean
def word_average_list(self, docs):
"""
Compute average word vector for multiple docs, where docs had been tokenized.
:param docs: list of sentence in list of separated tokens
:return:
array of average word vector in shape (len(docs),)
"""
return np.vstack([self.word_average(sent) for sent in docs])
def get_recs(ingredients, N=5, mean=False):
# load in word2vec model
model = Word2Vec.load("models/model_cbow.bin")
model.init_sims(replace=True)
if model:
print("Successfully loaded model")
# load in data
data = pd.read_csv("input/df_parsed.csv")
# parse ingredients
data["parsed"] = data.ingredients.apply(ingredient_parser)
# create corpus
corpus = get_and_sort_corpus(data)
if mean:
# get average embdeddings for each document
mean_vec_tr = MeanEmbeddingVectorizer(model)
doc_vec = mean_vec_tr.transform(corpus)
doc_vec = [doc.reshape(1, -1) for doc in doc_vec]
assert len(doc_vec) == len(corpus)
else:
# use TF-IDF as weights for each word embedding
tfidf_vec_tr = TfidfEmbeddingVectorizer(model)
tfidf_vec_tr.fit(corpus)
doc_vec = tfidf_vec_tr.transform(corpus)
doc_vec = [doc.reshape(1, -1) for doc in doc_vec]
assert len(doc_vec) == len(corpus)
# create embessing for input text
input = ingredients
# create tokens with elements
input = input.split(",")
# parse ingredient list
input = ingredient_parser(input)
# get embeddings for ingredient doc
if mean:
input_embedding = mean_vec_tr.transform([input])[0].reshape(1, -1)
else:
input_embedding = tfidf_vec_tr.transform([input])[0].reshape(1, -1)
# get cosine similarity between input embedding and all the document embeddings
cos_sim = map(lambda x: cosine_similarity(input_embedding, x)[0][0], doc_vec)
scores = list(cos_sim)
# Filter top N recommendations
recommendations = get_recommendations(N, scores)
return recommendations
if __name__ == "__main__":
input = "chicken thigh, risdlfgbviahsddsagv, onion, rice noodle, seaweed nori sheet, sesame, shallot, soy, spinach, star, tofu"
rec = get_recs(input)
print(rec)