-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedders.py
More file actions
224 lines (201 loc) · 8.26 KB
/
Copy pathembedders.py
File metadata and controls
224 lines (201 loc) · 8.26 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
from glove_embed import GloVe
from bert_embed import BERT
from use_embed import USE
import pandas as pd
import numpy as np
import os
from numpy import dot
from numpy.linalg import norm
from tqdm import tqdm
class WordLookUpSimilarty:
def __init__(self):
self.path_to_tags = './product_wise_tags.csv'
self.save_dir = 'combined_tags_glove'
os.system('mkdir '+self.save_dir)
self.confidence = 0.85
self.depth_modifier = 0.825
self.glove = GloVe()
self.similarity_importance = 0.5
self.depth_similarity = True
self.disable = False
self.average_tags_removed = []
def similarity(self, a, b):
if norm(a) == 0 or norm(b) == 0:
cos_sim = 0
else:
cos_sim = dot(a, b)/(norm(a)*norm(b))
return cos_sim
def all_tags_similarity(self, tag_embeds):
similarity = np.zeros((len(tag_embeds), len(tag_embeds)))
for i in range(len(tag_embeds)):
for j in range(len(tag_embeds)):
similarity_score = self.similarity(tag_embeds[i], tag_embeds[j])
similarity[i][j] = similarity_score
return similarity
def extract(self):
tags_ensemble = pd.read_csv(self.path_to_tags)
tags_ensemble = tags_ensemble.values
products = tags_ensemble.T[0]
tags_ensemble = tags_ensemble.T[1:].T
for product, tags in tqdm(zip(products, tags_ensemble), total=len(products), disable=self.disable, desc='Clustering Tags-per-Product using WordLookUpSimilarty Methodology'):
tag_embeds = np.zeros((len(tags), 100))
for i, tag in enumerate(tags):
embeds = np.mean(self.glove.glove_extract(tag), axis=0)
tag_embeds[i] = embeds
similarity = self.all_tags_similarity(tag_embeds)
new_similarity = np.zeros(similarity.shape)
for row in range(similarity.shape[0]):
for col in range(similarity.shape[1]):
confidence_item = similarity[row][col]
new_similarity[row] += (confidence_item * similarity[col])/12
if self.depth_similarity:
similarity = new_similarity
self.confidence = self.confidence * self.depth_modifier
positions = (12 - np.arange(12))/120
importance = self.similarity_importance*np.mean(similarity, axis=0)+(1-self.similarity_importance)*positions
importance_order = np.argsort(importance)[::-1]
words_already_clustered = []
clusters = []
for index in importance_order:
cluster = []
for row in importance_order:
if 1 >= similarity[row][index] > self.confidence and tags[row] not in words_already_clustered:
words_already_clustered.append(tags[row])
cluster.append(tags[row])
if len(cluster)>1:
clusters.append(cluster)
row = pd.DataFrame({0:['cluster-head'], 1:['similar-tags']})
row.to_csv('./'+self.save_dir+'./'+product+'.csv', mode='w', header=False, index=False)
for cluster in clusters:
row = pd.DataFrame({i:[c] for i,c in enumerate(cluster)})
row.to_csv('./'+self.save_dir+'./'+product+'.csv', mode='a', header=False, index=False)
self.average_tags_removed.append(len(words_already_clustered))
class ContextualWordEmbeddingsSimilarity:
def __init__(self):
self.path_to_tags = './product_wise_tags.csv'
self.save_dir = 'combined_tags_bert'
os.system('mkdir '+self.save_dir)
self.confidence = 0.85
self.depth_modifier = 0.825
self.similarity_importance = 0.5
self.depth_similarity = False
self.disable = False
self.average_tags_removed = []
self.bert = BERT()
def similarity(self, a, b):
if norm(a) == 0 or norm(b) == 0:
cos_sim = 0
else:
cos_sim = dot(a, b)/(norm(a)*norm(b))
return cos_sim
def all_tags_similarity(self, tag_embeds):
similarity = np.zeros((len(tag_embeds), len(tag_embeds)))
for i in range(len(tag_embeds)):
for j in range(len(tag_embeds)):
similarity_score = self.similarity(tag_embeds[i], tag_embeds[j])
similarity[i][j] = similarity_score
return similarity
def extract(self):
tags_ensemble = pd.read_csv(self.path_to_tags)
tags_ensemble = tags_ensemble.values
products = tags_ensemble.T[0]
tags_ensemble = tags_ensemble.T[1:].T
for product, tags in tqdm(zip(products, tags_ensemble), total=len(products), disable=self.disable, desc='Clustering Tags-per-Product using ContextualWordEmbeddingsSimilarity Methodology'):
tag_embeds = self.bert.bert_extract(tags)
tag_embeds = np.array([np.mean(i, axis=0) for i in tag_embeds])
similarity = self.all_tags_similarity(tag_embeds)
new_similarity = np.zeros(similarity.shape)
for row in range(similarity.shape[0]):
for col in range(similarity.shape[1]):
confidence_item = similarity[row][col]
new_similarity[row] += (confidence_item * similarity[col])/12
if self.depth_similarity:
similarity = new_similarity
self.confidence = self.confidence * self.depth_modifier
positions = (12 - np.arange(12))/120
importance = self.similarity_importance*np.mean(similarity, axis=0)+(1-self.similarity_importance)*positions
importance_order = np.argsort(importance)[::-1]
words_already_clustered = []
clusters = []
for index in importance_order:
cluster = []
for row in importance_order:
if 1 >= similarity[row][index] > self.confidence and tags[row] not in words_already_clustered:
words_already_clustered.append(tags[row])
cluster.append(tags[row])
if len(cluster)>1:
clusters.append(cluster)
row = pd.DataFrame({0:['cluster-head'], 1:['similar-tags']})
row.to_csv('./'+self.save_dir+'./'+product+'.csv', mode='w', header=False, index=False)
for cluster in clusters:
row = pd.DataFrame({i:[c] for i,c in enumerate(cluster)})
row.to_csv('./'+self.save_dir+'./'+product+'.csv', mode='a', header=False, index=False)
self.average_tags_removed.append(len(words_already_clustered))
class SentenceEmbeddingsSimilarity:
def __init__(self):
self.path_to_tags = './product_wise_tags.csv'
self.save_dir = 'combined_tags_use'
os.system('mkdir '+self.save_dir)
self.confidence = 0.75
self.depth_modifier = 0.825
self.similarity_importance = 0.5
self.depth_similarity = False
self.disable = False
self.average_tags_removed = []
self.use = USE()
def similarity(self, a, b):
if norm(a) == 0 or norm(b) == 0:
cos_sim = 0
else:
cos_sim = dot(a, b)/(norm(a)*norm(b))
return cos_sim
def all_tags_similarity(self, tag_embeds):
similarity = np.zeros((len(tag_embeds), len(tag_embeds)))
for i in range(len(tag_embeds)):
for j in range(len(tag_embeds)):
similarity_score = self.similarity(tag_embeds[i], tag_embeds[j])
similarity[i][j] = similarity_score
return similarity
def extract(self):
tags_ensemble = pd.read_csv(self.path_to_tags)
tags_ensemble = tags_ensemble.values
products = tags_ensemble.T[0]
tags_ensemble = tags_ensemble.T[1:].T
for product, tags in tqdm(zip(products, tags_ensemble), total=len(products), disable=self.disable, desc='Clustering Tags-per-Product using SentenceEmbeddingsSimilarity Methodology'):
tag_embeds = self.use.use_extract(tags)
similarity = self.all_tags_similarity(tag_embeds)
new_similarity = np.zeros(similarity.shape)
for row in range(similarity.shape[0]):
for col in range(similarity.shape[1]):
confidence_item = similarity[row][col]
new_similarity[row] += (confidence_item * similarity[col])/12
if self.depth_similarity:
similarity = new_similarity
self.confidence = self.confidence * self.depth_modifier
positions = (12 - np.arange(12))/120
importance = self.similarity_importance*np.mean(similarity, axis=0)+(1-self.similarity_importance)*positions
importance_order = np.argsort(importance)[::-1]
words_already_clustered = []
clusters = []
for index in importance_order:
cluster = []
for row in importance_order:
if 1 >= similarity[row][index] > self.confidence and tags[row] not in words_already_clustered:
words_already_clustered.append(tags[row])
cluster.append(tags[row])
if len(cluster)>1:
clusters.append(cluster)
row = pd.DataFrame({0:['cluster-head'], 1:['similar-tags']})
row.to_csv('./'+self.save_dir+'./'+product+'.csv', mode='w', header=False, index=False)
for cluster in clusters:
row = pd.DataFrame({i:[c] for i,c in enumerate(cluster)})
row.to_csv('./'+self.save_dir+'./'+product+'.csv', mode='a', header=False, index=False)
self.average_tags_removed.append(len(words_already_clustered))
'''
wlus = WordLookUpSimilarty()
wlus.extract()
cwes = ContextualWordEmbeddingsSimilarity()
cwes.extract()
'''
ses = SentenceEmbeddingsSimilarity()
ses.extract()