-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathner_clustering.py
503 lines (304 loc) · 9.21 KB
/
ner_clustering.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/usr/bin/env python
# coding: utf-8
# ### Import libraries
# In[1]:
import spacy
import pandas as pd
import numpy as np
nlp = spacy.load("en_core_web_sm")
# ### Import Data
# import pickle
# p0 = pickle.load( open( "data_part_0.pickle", "rb" ) )
# p0 = p0.iloc[:, 3:].copy()
#
# p0
# In[2]:
'''
Import CSV file
'''
p0 = pd.read_csv('chunk0.csv')
print(p0.shape)
print()
p0 = p0.iloc[:, 2:].copy()
p0.head()
# ### Preprocess data:
#
# * Filter out unwanted columns
# * Number of article and length of each article
# In[3]:
# Unnecessary for the csv file
cols = p0.columns.values.tolist()
# key = cols[:6] + cols[7] + cols[9]
# key
# key = cols[:6]
# key.append(cols[7])
# key.append(cols[9])
key = cols[:5] + cols[-2:]
print(key)
df = p0[key].copy()
# In[4]:
p0
# In[5]:
import statistics as stat
articles = p0.loc[:, 'article'].values.tolist()
articlesLen = [len(str(article)) for article in articles]
len(articlesLen), round(stat.mean(articlesLen), 3)
# ### NER and get Document Embedding
#
# **Steps**
# 1. Run NER on each article
# 1. For each recognized entities in NER: get embedding for the query and entity. Do elementwise multiplication to get combined embedding of NER results.
# 1. Get embedding of the document using Spacy.
# 1. Concat the embeddings of the document together to get final embedding of the documents
# In[6]:
## For each article
def entityPerArticle(article):
debug = False
doc = nlp(article)
d = {}
for ent in doc.ents:
d[ent.text] = ent.label_
if debug: print(d)
return d
# In[7]:
p0.loc[0, :]
## For test purpose
# In[ ]:
# entities = []
# i = 0
# for article in articles:
# ent = entityPerArticle(str(article))
# entities.append(ent)
# i+=1
# if i <=3:
# print(ent)
# print()
# # In[ ]:
# p0['entities'] = entities
# key.append('entities')
# df = p0[key]
# df
# # In[ ]:
# df.to_csv('chunk0_ner.csv', index=False)
# # In[ ]:
# for sent in doc.sents:
# for token in sent:
# print(token, token.pos_)
# # In[ ]:
# import re
# import shutil
# import string
# import tensorflow as tf
# from tensorflow.keras import Sequential
# from tensorflow.keras.layers import Dense, Embedding, GlobalAveragePooling1D
# from tensorflow.keras.layers import TextVectorization
# # In[9]:
a = articles[0]
# print(a)
# In[8]:
def getArticleNEREmbedding(article, article_meta):
'''
Get embedding of the article.
Use NER to get known entities, get embedding of queries and entiteis from NER.
Get embeddings of article metadata and article itself.
Return the final embedding.
Input:
article: String containing article
article_meta: List of String containing author, section, publication
Output:
Array of float containing final embedding of the documents
'''
debug = False
# Perform NER
entities = entityPerArticle(article)
# Get the embeddings of entities from NER
i, M, N = 0, len(entities.keys()), 96
if debug: print(M, N)
embd = np.zeros((M, N)) # Numpy array to store embeddings
for (item, key) in entities.items():
query = nlp(item)
key = nlp(key)
q2v = query.vector
k2v = key.vector
vec = q2v*k2v
embd[i] = vec
# if i == 0:
# print(vec.shape)
# print(vec)
i += 1
avg_embd = np.average(embd, axis=0).flatten()
if debug: print(avg_embd.shape)
# # Embedding of the article meta
# i, M = 0, 3
# embd = np.zeros((M, N))
# for meta in article_meta:
# if meta !=None or meta != '':
# doc = nlp(meta)
# vec = doc.vector
# print(meta)
# embd[i]= vec
# i += 1
# meta_embd = np.average(embd, axis=0)
# Embedding of the document
# doc_embd = np.zeros((1, 96))
# e = nlp(article).vector
# doc_embd[:e.shape[0]] = e
doc_embd = nlp(article).vector
if doc_embd.shape[0]<96:
print(article)
if debug: print(doc_embd.shape)
embd_final = np.concatenate([avg_embd, doc_embd])
embd_final = embd_final
return embd_final
# In[98]:
print(entityPerArticle(a))
# In[10]:
y = nlp(a).vector
x = np.zeros((1, 96))
x[:y.shape[0]] = y
# ### Run embedding process for each article and prepare data for clustering
# In[11]:
p0.fillna('', inplace=True)
p0 = p0[p0['article']!=''].reset_index(drop=True)
# In[37]:
m, n = p0.shape
embd_array = np.zeros((m, 96*2))
p0.fillna('', inplace=True)
k=0
for (i, row) in p0.iterrows():
if k%100==0: print(f'k = {k}...')
# print(i)
article = row['article']
author, sec, pub = row['author'], row['section'], row['publication']
# print(i, author, sec, pub)
embd = getArticleNEREmbedding(article, [author, sec, pub])
if embd.shape[0]<192: print(article)
embd_array[k]=embd
k+=1
# if k == 500: break
# In[38]:
import pickle
pickle.dump(embd_array, open("chunk0_embd.pkl", "wb"))
print(embd_array)
# In[36]:
# embd_array_1 = pickle.load(open("chunk0_embd.pkl", 'rb'))
# ### Clusterng
# #### Agglomerative Clustering
# In[18]:
import numpy as np
import pandas as pd
from sklearn.cluster import AgglomerativeClustering
from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import dendrogram
# In[50]:
def plot_dendrogram(model, **kwargs):
# Create linkage matrix and then plot the dendrogram
# create the counts of samples under each node
counts = np.zeros(model.children_.shape[0])
n_samples = len(model.labels_)
for i, merge in enumerate(model.children_):
current_count = 0
for child_idx in merge:
if child_idx < n_samples:
current_count += 1 # leaf node
else:
current_count += counts[child_idx - n_samples]
counts[i] = current_count
linkage_matrix = np.column_stack(
[model.children_, model.distances_, counts]
).astype(float)
# Plot the corresponding dendrogram
dendrogram(linkage_matrix, **kwargs)
# In[40]:
embd_array_1 = np.nan_to_num(embd_array, 0)
embd_array_1[np.isinf(embd_array_1)]=0
embd_array = embd_array_1
# **Ward Linkage**
# In[91]:
model = AgglomerativeClustering(distance_threshold=0.01, n_clusters=None)
model = model.fit(embd_array)
# In[92]:
plt.figure(figsize=(20, 10))
plt.title("Hierarchical Clustering Dendrogram (Ward)")
# plot the top three levels of the dendrogram
plot_dendrogram(model, truncate_mode="level", p=3)
plt.xlabel("Number of article in cluster (or index of point if no parenthesis).")
plt.show()
# In[93]:
y = model.labels_
# print(np.unique(y, return_counts=True))
y.shape
# **Complete Linkage**
# In[55]:
embd_array.shape
# In[96]:
model = AgglomerativeClustering(distance_threshold=0.01, linkage='complete', n_clusters=None)
model = model.fit(embd_array)
# In[97]:
plt.figure(figsize=(20, 10))
plt.title("Hierarchical Clustering Dendrogram (Complete)")
# plot the top three levels of the dendrogram
plot_dendrogram(model, truncate_mode="level", p=3)
plt.xlabel("Number of article in cluster (or index of point if no parenthesis).")
plt.show()
# In[70]:
y = model.labels_
# print(np.unique(y, return_counts=True))
y.shape
# #### DBScan
# In[71]:
from sklearn.cluster import DBSCAN
from sklearn import metrics
from sklearn.datasets import make_blobs
from sklearn.preprocessing import StandardScaler
# In[78]:
# Compute DBSCAN
db = DBSCAN(eps=0.5, min_samples=3).fit(embd_array)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_
# In[79]:
# Number of clusters in labels, ignoring noise if present.
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
n_noise_ = list(labels).count(-1)
print("Estimated number of clusters: %d" % n_clusters_)
print("Estimated number of noise points: %d" % n_noise_)
# print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels))
# print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels))
# print("V-measure: %0.3f" % metrics.v_measure_score(labels_true, labels))
# print("Adjusted Rand Index: %0.3f" % metrics.adjusted_rand_score(labels_true, labels))
# print(
# "Adjusted Mutual Information: %0.3f"
# % metrics.adjusted_mutual_info_score(labels_true, labels)
# )
# print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(X, labels))
# In[80]:
# Black removed and is used for noise instead.
unique_labels = set(labels)
colors = [plt.cm.Spectral(each) for each in np.linspace(0, 1, len(unique_labels))]
for k, col in zip(unique_labels, colors):
if k == -1:
# Black used for noise.
col = [0, 0, 0, 1]
class_member_mask = labels == k
xy = embd_array[class_member_mask & core_samples_mask]
plt.plot(
xy[:, 0],
xy[:, 1],
"o",
markerfacecolor=tuple(col),
markeredgecolor="k",
markersize=14,
)
xy = embd_array[class_member_mask & ~core_samples_mask]
plt.plot(
xy[:, 0],
xy[:, 1],
"o",
markerfacecolor=tuple(col),
markeredgecolor="k",
markersize=6,
)
plt.title("Estimated number of clusters: %d" % n_clusters_)
plt.show()
# In[ ]: