-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclustering.py
381 lines (291 loc) · 12.3 KB
/
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
# -*- coding: utf-8 -*-
"""
This script contains classes definition for managing clusters: creation, identification, evaluation ...
"""
from __future__ import unicode_literals
import operator
from itertools import groupby
from operator import itemgetter
import nltk
from nltk.tag import StanfordPOSTagger
from sklearn import cluster
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from utils import *
if DB_SERVER == 'couchdb':
from db_couchdb import *
elif DB_SERVER == 'mongodb':
from db_mongodb import *
class ArticleManager():
"""
Parent class used of both Clusterizer and Merger.
Defines method for them
"""
@staticmethod
def get_articles_text(articles):
"""
Gets a list of all articles.
For best result, title is added three time
"""
articles_text = []
for article in articles:
content = article.title + " " + article.title + " " + article.title + " " + article.text
content = content.replace("\r", "").replace("\n", "")
articles_text.append(content)
return articles_text
@staticmethod
def get_articles_by_cluster(articles):
"""
Gets articles per cluster.
"""
table = []
for article in articles:
table.append((article, article.num_cluster))
sorted_table = sorted(table, key=itemgetter(1))
table = groupby(sorted_table, key=itemgetter(1))
article_table = [{'cluster': k,
'articles': [elms1 for (elms1, elms2) in v]} for k, v in table]
return article_table
class CustomClusterizer(ArticleManager):
"""
This clas aims to execute the clustering.
"""
def __init__(self):
ar = Article()
self.articles = ar.get_all_articles()
self.classifier = cluster.KMeans()
self.calculate_intial_clusters()
self.vectorizer = TfidfVectorizer(
max_features = 200000,
tokenizer = self.custom_tokenizer
)
# Customizes tokenization.
@staticmethod
def custom_tokenizer(text):
sw = Stopword()
if not sw.sw_exist():
sw.set_stopwords()
stopwords = []
for stopword in sw.get_all_stopwords():
stopwords.append(stopword.word)
tokens = [word for word in nltk.word_tokenize(text) if word.isalpha() and len(word) > 2]
filtered_tokens = []
for token in tokens:
if token not in stopwords:
filtered_tokens.append(token)
return filtered_tokens
# Calculate probable number of clusters
def calculate_intial_clusters(self,articles=None):
if articles is None:
articles_count = len(self.articles)
else:
articles_count = len(articles)
self.update_nb_cluster(int((articles_count * 5) / 100) + 1)
# updates cluster number
def update_nb_cluster(self, nb_clusters):
self.nb_clusters = nb_clusters
self.classifier.n_clusters = self.nb_clusters
# calls the K-means method
def compute_kmeans(self, matrix):
print("\t\tNb Clusters: "+str(self.nb_clusters))
return self.classifier.fit_predict(matrix)
# vectorizes texts
def vectorize(self, texts):
return self.vectorizer.fit_transform(texts)
# execute clustering once
def clusterize(self, articles=None, nb_clusters=None):
# can be used on standalone
if articles is not None:
self.articles = articles
self.calculate_intial_clusters()
if nb_clusters is not None:
self.update_nb_cluster(nb_clusters)
# get all texts
articles_text = self.get_articles_text(self.articles)
# vectorization with tf-idf
print("\t\tVectorizing texts")
text_matrix = self.vectorize(articles_text)
# K-means
print("\t\tClustering")
cluster_labels = self.compute_kmeans(text_matrix)
# Retrieves articles text for each cluster
print("\t\tMapping to articles")
iter = 0
clusters = {}
for label in cluster_labels:
if label not in clusters:
clusters[label] = []
clusters[label].append(self.articles[iter])
iter += 1
return clusters
# Iterates clustering, first with all articles, next in each cluster, and so on, until iteration number is reached
def multi_clusterize(self, articles=None, iterations=1, human_readable_return=False):#nb_clusters=None,
if articles is not None:
self.articles = articles
self.calculate_intial_clusters()
original_articles = self.articles
# Each cluster is a batch. First, we have unique batch
articles_batchs = []
articles_batchs.append(self.articles)
print("Starting clustering with " + str(iterations) + " iterations and " + str(self.nb_clusters) + " clusters")
# looping according to iteration number
for iteration in range(0, iterations):
print("\r\nRunning iteration number " + str(iteration + 1))
batch_iteration = 0
clusters = {}
for articles_batch in articles_batchs:
self.calculate_intial_clusters(articles_batch)
#if len(articles_batch) > self.nb_clusters:
print("\tBatch number " + str(batch_iteration + 1) + "/" + str(len(articles_batchs)))
clusters[batch_iteration] = self.clusterize(articles_batch, self.nb_clusters)
batch_iteration += 1
clusters = self.consolidate_clusters(clusters)
# in order to proceed to next iteration, drain previous batch
articles_batchs = []
for key, value in clusters.iteritems():
articles_batchs.append(value)
# For debug, this simplifies the reading of results
if human_readable_return:
clusters = self.humanize_clusters(clusters)
# Set the cluster number in which each article belongs
self.update_articles(clusters)
# Necessary only in case two iterations are execute in the same script
self.articles = original_articles
return clusters
# Re-organizes datas in cluster as dictionnary for further usage
def consolidate_clusters(self, clusters):
consolidated_clusters = {}
iterator = 0
for cluster_key, cluster in clusters.iteritems():
for key, article_batch in cluster.iteritems():
consolidated_clusters[str(iterator)] = article_batch
iterator += 1
return consolidated_clusters
def humanize_clusters(self, clusters):
readable_clusters = {}
iterator = 0
for cluster_key, cluster in clusters.iteritems():
if cluster_key not in readable_clusters:
readable_clusters[str(cluster_key)] = []
for article in cluster:
readable_clusters[str(cluster_key)].append(article.title)
iterator += 1
return readable_clusters
def update_articles(self, clusters):
for cluster_key, cluster in clusters.iteritems():
for article in cluster:
article.update_article(cluster_key)
#article.update(set__num_cluster=cluster_key)
return None
def __str__(self):
return "<CustomClusterizer>"
class CustomMerger(ArticleManager):
"""
Gets keywords per cluster and aims to gather those which are similar
"""
def __init__(self):
self.vectorizer = CountVectorizer(
max_features = 200000,
tokenizer = CustomClusterizer.custom_tokenizer
)
def invert_dict(self, original_dict):
return {v: k for k, v in original_dict.iteritems()}
# determines most valued words
def extract_values(self, tokens, count):
real_count = len(tokens.values())
if count > real_count:
count = real_count
return dict(sorted(tokens.iteritems(), key=operator.itemgetter(1), reverse=True)[:count])
return articles_text
# occurrence of a token in a matrix
def normalize_tokens(self, labels, matrix):
lines = matrix.shape[0]
cols = matrix.shape[1]
labels = self.invert_dict(labels)
token_list = {}
for line in range(0, lines):
for col in range(0, cols):
if matrix[line, col] != 0:
label = labels[col]
if label not in token_list:
token_list[label] = matrix[line, col]
else:
token_list[label] += matrix[line, col]
return token_list
def token_frequency(self, tokens, precision=4):
token_sum = float(sum(tokens.values()))
token_frequencies = {}
for key, value in tokens.iteritems():
frequency = round(value/token_sum, precision)
token_frequencies[key] = frequency
return token_frequencies
# tokens frequency in corpus
def tokenize(self, text):
result = self.vectorizer.fit_transform(text)
return self.normalize_tokens(self.vectorizer.vocabulary_, result)
# mean of token frequency
def tokenize_frequency(self, text):
result = self.vectorizer.fit_transform(text)
tokens = self.normalize_tokens(self.vectorizer.vocabulary_, result)
return self.token_frequency(tokens)
# Classifies texts using the frequency or occurrence of tokens
def tokenize_clusters(self, clusters, comparison_sample=10, use_frequency=True):
working_clusters = {}
for cluster_label, cluster_list in clusters.iteritems():
cluster_label = str(cluster_label)
print("Working on cluster " + cluster_label)
if cluster_label not in working_clusters:
working_clusters[cluster_label] = {}
texts = self.get_articles_text(cluster_list)
if use_frequency:
tokens = self.tokenize_frequency(texts)
else:
tokens = self.tokenize(texts)
working_clusters[cluster_label] = self.extract_values(tokens, comparison_sample)
self._remove_clusters()
self.save_clusters(working_clusters)
return working_clusters
@staticmethod
def _remove_clusters():
cl = ClusteringResume()
cl.remove_cluster_content()
del cl
# Stores clusters with their keywords in DB
def save_clusters(self, working_clusters):
st = StanfordPOSTagger('french.tagger')
for cluster_label, token_values in working_clusters.iteritems():
new_token_values = {}
cluster_tag = st.tag(token_values)
for (word,tag) in cluster_tag:
if tag in ('NPP','NC', 'N'):
word_value = [b for a,b in token_values.iteritems() if a == word]
new_token_values[word]=word_value[0]
else:
pass
cluster_title = max(new_token_values)
cluster_resume = ClusteringResume()
cluster_resume.set_dataclusters(cluster_label,working_clusters[cluster_label],cluster_title)
return None
# Manually names a selection of keywords
def process_manual(self, tokenized_clusters, classified_clusters = {}, known_tags = []):
unclassified_clusters = {}
for key, value in tokenized_clusters.iteritems():
print("Cluster: " + str(value))
print("Known tags: " + str(known_tags))
print("Saisir un tag (ou 'ignore' pour ignorer le cluster. Il ne sera pas traité.) :")
tag = raw_input("--> ")
tag = tag.lower()
if tag == '' or tag == 'ignore':
unclassified_clusters[key] = value
else:
if tag not in classified_clusters:
classified_clusters[tag] = {}
known_tags.append(tag)
for item in value.iteritems():
word = item[0]
if word not in classified_clusters[tag]:
classified_clusters[tag][word] = 0
classified_clusters[tag][word] += 1
print(" ")
return classified_clusters, unclassified_clusters, known_tags
def __str__(self):
return "<CustomMerger>"