-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelevantsearch.py
More file actions
242 lines (168 loc) · 8.96 KB
/
relevantsearch.py
File metadata and controls
242 lines (168 loc) · 8.96 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
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
241
242
import streamlit as st
from PIL import Image
import pandas as pd
import pickle
from sklearn.feature_extraction.text import TfidfVectorizer
#from sklearn.cluster import KMeans
###Loading KMeans Clustering Model######
model1=pickle.load(open('Kmeans_cluster.pkl','rb'))
def local_css(file_name):
with open(file_name) as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
def remote_css(url):
st.markdown(f'<link href="{url}" rel="stylesheet">', unsafe_allow_html=True)
def icon(icon_name):
st.markdown(f'<i class="material-icons">{icon_name}</i>', unsafe_allow_html=True)
local_css("style.css")
remote_css('https://fonts.googleapis.com/icon?family=Material+Icons')
side_image=Image.open('Home_depot_icon.JPG')
st.sidebar.image(side_image,use_column_width=True)
image=Image.open('home_depot.JPG')
st.image(image,use_column_width=True)
icon("search")
st.sidebar.info("Example search like :bulb, cutting tool ,heater, sink , water, power etc.")
st.sidebar.info("This Application is developed by Siddhesh D. Munagekar for recommended product search in order to maximize sales in Home Depot.")
placeholder = st.empty()
placeholder.text("What can we help you find?")
searched_object = st.text_input("")
#Loading Dataset
url = 'https://drive.google.com/file/d/1ayy2Qr-DzdHZrmKKm4PC-iQ0CcmLaQ54/view?usp=sharing'
path = 'https://drive.google.com/uc?export=download&id='+url.split('/')[-2]
joined_df = pd.read_csv(path,encoding= 'unicode_escape')
joined_df=joined_df.dropna()
#
def run():
##Loading cleaned corpus file
corpus = []
# open file and read the content in a list
with open('corpus.txt', 'r') as filehandle:
for line in filehandle:
# remove linebreak which is the last character of the string
currentPlace = line[:-1]
# add item to the list
corpus.append(currentPlace)
####Feature extraction from Product Description usinng tfidf######
vectorizer=TfidfVectorizer(stop_words='english',analyzer='word',max_features=500)
vectorizer.fit_transform(corpus)
###Creating a function to print clusters
def print_cluster(i):
cluster_list=[]
#print("Cluster %d:"% i)
for ind in ordered_centroids[i,:10]:
#print(' %s' % terms[ind])
cluster_list.append(terms[ind])
print('Cluster List',cluster_list)
return cluster_list
k_value=10
print("Top 10 search per clusters :")
ordered_centroids=model1.cluster_centers_.argsort()[:,::-1]
terms=vectorizer.get_feature_names()
for i in range(k_value):
print_cluster(i)
########Selecting the the cluster from the group based on user search#######
def selected_cluster(i):
cluster = []
cluster.clear()
#print("Cluster %d:" % i)
for ind in ordered_centroids[i, :10]:
#print(' %s' % terms[ind])
cluster.append(terms[ind])
return cluster
#@st.cache(allow_output_mutation=True)
def show_recommendations(product):
Y = vectorizer.transform([product])
prediction = model1.predict(Y)
cluster = selected_cluster(prediction[0])
return cluster
domain = show_recommendations(searched_object)
choice = st.radio("Select your preference", domain)
if domain.index(choice) == 0:
new_df = joined_df[joined_df['product_title'].str.contains(choice, regex=False, case=False, na=False)]
products = new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
if domain.index(choice) == 1:
new_df = joined_df[joined_df['product_title'].str.contains(choice, regex=False, case=False, na=False)]
products = new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
if domain.index(choice) == 2:
if choice =='lithiumion':
new_df = joined_df[joined_df['product_title'].str.contains('lithium-ion', regex=False, case=False, na=False)]
products=new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
else:
new_df = joined_df[joined_df['product_title'].str.contains(choice, regex=False, case=False, na=False)]
products = new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
if domain.index(choice) == 3:
new_df = joined_df[joined_df['product_title'].str.contains(choice, regex=False, case=False, na=False)]
products = new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
if domain.index(choice) == 4:
new_df = joined_df[joined_df['product_title'].str.contains(choice, regex=False, case=False, na=False)]
products = new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
if domain.index(choice) == 5:
new_df = joined_df[joined_df['product_title'].str.contains(choice, regex=False, case=False, na=False)]
products = new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
if domain.index(choice) == 6:
new_df = joined_df[joined_df['product_title'].str.contains(choice, regex=False, case=False, na=False)]
products = new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
if domain.index(choice) == 7:
new_df = joined_df[joined_df['product_title'].str.contains(choice, regex=False, case=False, na=False)]
products = new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
if domain.index(choice) == 8:
new_df = joined_df[joined_df['product_title'].str.contains(choice, regex=False, case=False, na=False)]
products = new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
if domain.index(choice) == 9:
new_df = joined_df[joined_df['product_title'].str.contains(choice, regex=False, case=False, na=False)]
products = new_df['product_title'].unique()
df = pd.DataFrame(products[:10])
df.rename({0: "Featuring top 10 "+choice+" related trending Products"}, axis=1, inplace=True)
st.table(df)
placeholder.empty()
#
if __name__=='__main__':
###This is the list of cluster items (TFIDF features) which we receive from print_cluster items
cluster_names=['use', 'ft', 'easy', 'steel', 'used', 'design', 'feature', 'designed', 'product', 'outdoor',
'fan', 'air', 'control', 'cooking', 'filter', 'heat', 'oven', 'help', 'ft', 'room',
'paint', 'wood', 'color', 'rug', 'vary', 'surface', '65', 'seenbspproposition', 'resident', 'stain',
'battery', 'power', 'lithiumion', 'charger', 'tool', 'compact', 'protection', 'plan', 'impact', 'depot',
'tile', 'piece', 'floor', 'wall', 'flooring', 'indoor', 'commercial', 'installation', 'residential', 'case',
'door', 'glass', 'hinge', 'security', 'steel', 'opening', 'wood', 'panel', 'easy', 'lock',
'light', 'bulb', 'led', 'lighting', 'fixture', 'energy', 'incandescent', 'lamp', 'glass', 'shade',
'blade', 'cutting', 'saw', 'cut', 'steel', 'handle', 'tool', 'speed', 'motor', 'feature',
'water', 'valve', 'pipe', 'heater', 'faucet', 'toilet', 'tank', 'hot', 'pressure', 'fitting',
'shelf', 'cabinet', 'storage', 'drawer', 'vanity', 'finish', 'sink', 'hardware','design','faucet','cutting tool']
if not searched_object:
st.write('Please type home improvement materials in the above text box')
elif searched_object not in cluster_names:
st.info("Hmm...we couldn't find "+searched_object)
st.info('Please search items related to home improvement or refer example search in the side bar')
#exit(0)
else:
run()
#searched_object=""