-
Notifications
You must be signed in to change notification settings - Fork 1
/
Analyse-Content.py
444 lines (316 loc) · 11.8 KB
/
Analyse-Content.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
import nltk
import os
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
import pandas as pd
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
import matplotlib as mpl
from sklearn.manifold import MDS
import random
from scipy.cluster.hierarchy import ward, dendrogram
import gensim
from gensim.models.doc2vec import TaggedDocument
from gensim import corpora, models
from collections import OrderedDict
lemmatizer = nltk.wordnet.WordNetLemmatizer()
def loadBook(folder,filename):
filepath=folder + "/" + filename
f=open(filepath)
raw=f.read()
return(raw)
def preProcess(book):
book=book.strip()
words=nltk.tokenize.RegexpTokenizer(r'\w+').tokenize(book)
words=[w.lower() for w in words if w not in stopwords.words('english')]
words=[lemmatizer.lemmatize(w) for w in words if len(w)>1]
return(words)
def createChunks(words,filename,text_list,id_list):
i=0
count=0
while(i<len(words)):
if((i+1000)<len(words)):
chunk=words[i:i+1000]
chunk=" ".join(chunk)
else:
chunk=words[i:len(words)]
chunk=" ".join(chunk)
i=i+1000
count=count+1
text_list.append(chunk)
id_list.append(filename + "-" + str(count))
return(text_list,id_list)
author1_books=[]
author1_id=[]
folder="author1/en/"
print("Loading English books for author 1")
i=1
dictionaryOfAuthor1Books=OrderedDict()
for file in os.listdir(folder):
#print(file)
book=loadBook(folder,file)
words=preProcess(book)
filename="A1-"+"B"+str(i)
createChunks(words,filename,author1_books,author1_id)
dictionaryOfAuthor1Books["B"+str(i)]=file
i=i+1
for i in dictionaryOfAuthor1Books.keys():
print(i,"----",dictionaryOfAuthor1Books[i])
print("\n\n\nLoading English books for author 2")
author2_books=[]
author2_id=[]
folder="author2/en/"
i=1
dictionaryOfAuthor2Books=OrderedDict()
for file in os.listdir(folder):
#print(file)
book=loadBook(folder,file)
words=preProcess(book)
filename="A2-"+"B"+str(i)
createChunks(words,filename,author2_books,author2_id)
dictionaryOfAuthor2Books["B"+str(i)]=file
i=i+1
for i in dictionaryOfAuthor2Books.keys():
print(i,"----",dictionaryOfAuthor2Books[i])
books=author1_books+author2_books
ids=author1_id+author2_id
###### Merge the smaller chunks ###############
i=0
while(i<len(books)):
b=nltk.word_tokenize(books[i])
if(len(b)<700):
prev_l=len(nltk.word_tokenize(books[i-1]))
books[i-1]=" ".join(books[i-1:i+1])
new_l=len(nltk.word_tokenize(books[i-1]))
#print(ids[i],len(b),prev_l,new_l)
del books[i]
del ids[i]
i=i+1
print("\n\n\n")
#print("Number of chunks of text written by author 1: ",len(author1_id))
#print("Number of chunks of text written by author 2: ",len(author2_id))
######################################################## TF-IDF based Clustering #############################################################
print("TfIDF based clustering........")
#TF IDF Matrix
tfidfVectorizer=TfidfVectorizer(max_df=0.8)
tfidfMatrix=tfidfVectorizer.fit_transform(books)
#Distance matrix
dist = 1 - cosine_similarity(tfidfMatrix)
#K-Means clustering
num_of_clusters=25
kmeans=KMeans(n_clusters=num_of_clusters)
kmeans.fit(tfidfMatrix)
clusters=kmeans.labels_.tolist()
author=[" "]*len(books)
num_author1=0
num_author2=0
for i in range(len(ids)):
if (ids[i][0:2]=="A1"):
author[i]="Author1"
num_author1=num_author1+1
else:
author[i]="Author2"
num_author2=num_author2+1
documents = { 'title': ids, 'text': books, 'cluster': clusters, 'author':author }
documentFrame = pd.DataFrame(documents, index = [clusters])
#Convert to 2D for plotting
mds = MDS(n_components=2, dissimilarity="precomputed", random_state=7)
transformed = mds.fit_transform(dist)
x=transformed[:, 0]
y=transformed[:, 1]
x_author1=transformed[:num_author1,0]
y_author1=transformed[:num_author1,1]
x_author2=transformed[num_author1:,0]
y_author2=transformed[num_author1:,1]
#Generate colours for plotting
num_colours=num_of_clusters
col=mpl.colors.cnames.items()
colours={}
random.seed(5)
i=0
while (i<num_of_clusters):
c=random.choice(list(col))
if(c[1] not in colours.keys()):
colours[i]=c[1]
i=i+1
cluster_names={}
for i in range(num_of_clusters):
cluster_names[i]="Cluster-"+str(i)
#Preparing for plotting
df = pd.DataFrame(dict(x=x, y=y, label=clusters, title=ids,author=author))
groups = df.groupby('label')
df_author1=pd.DataFrame(dict(x=x_author1, y=y_author1, label=clusters[:num_author1], title=ids[:num_author1],author=author[:num_author1]))
groups_author1 = df_author1.groupby('label')
df_author2=pd.DataFrame(dict(x=x_author2, y=y_author2, label=clusters[:num_author2], title=ids[:num_author2],author=author[:num_author2]))
groups_author2 = df_author2.groupby('label')
print("Author 1 is represented by Diamonds")
print("Author 2 is represented by circles")
#Plot using Matplotlib
fig, ax = plt.subplots(figsize=(20, 10)) # set size
#Author 1 books are marked by circles
for name, group in groups_author1:
ax.plot(group.x, group.y, marker='D', linestyle='', ms=12,
label=cluster_names[name], color=colours[name],
mec='none')
#Author 2 books are marked by diamonds
for name, group in groups_author2:
ax.plot(group.x, group.y, marker='o', linestyle='', ms=12,
label=cluster_names[name], color=colours[name],
mec='none')
ax.legend(numpoints=1) #show legend
for i in range(len(df)):
ax.text(df.ix[i]['x'], df.ix[i]['y'], df.ix[i]['title'], size=3)
plt.show()
print("TFIdf Based clustering is now complete.")
########################################################### Doc2Vec based clustering ############################################################
print("\n\n\n\n")
print("Generating new features using Gensim's Doc2Vec")
#Converting documents to tagged document format
taggeddoc=[]
text=[]
for i in range(len(books)):
t=nltk.word_tokenize(books[i])
text.append(t)
td = TaggedDocument(gensim.utils.to_unicode(str.encode(' '.join(t))).split(),tags=[u'Doc_{:d}'.format(i)])
taggeddoc.append(td)
#Training the Gensim model
numOfFeatures=200 #change if required
model = gensim.models.Doc2Vec(taggeddoc,alpha=0.025, size= numOfFeatures, min_alpha=0.025, min_count=0)
for epoch in range(20):
if(epoch%5==0):
print('Now training epoch %s'%epoch)
model.train(taggeddoc,total_examples=model.corpus_count,epochs=model.iter)
model.alpha -= 0.002
model.min_alpha = model.alpha
#Generating features
features=np.zeros((len(books),numOfFeatures))
for i in range(len(books)):
features[i]=model.docvecs[u'Doc_{:d}'.format(i)]
dist = 1 - cosine_similarity(features)
#Clustering
num_of_clusters=25
kmeans=KMeans(n_clusters=num_of_clusters)
kmeans.fit(features)
clusters=kmeans.labels_.tolist()
documents = { 'title': ids, 'text': books, 'cluster': clusters, 'author':author }
documentFrame = pd.DataFrame(documents, index = [clusters])
#Converting to 2D
mds = MDS(n_components=2, dissimilarity="precomputed", random_state=7)
transformed = mds.fit_transform(dist)
x=transformed[:, 0]
y=transformed[:, 1]
x_author1=transformed[:num_author1,0]
y_author1=transformed[:num_author1,1]
x_author2=transformed[num_author1:,0]
y_author2=transformed[num_author1:,1]
#Generating colours
num_colours=num_of_clusters
col=mpl.colors.cnames.items()
colours={}
random.seed(7)
i=0
while (i<num_of_clusters):
c=random.choice(list(col))
if(c[1] not in colours.keys()):
colours[i]=c[1]
i=i+1
cluster_names={}
for i in range(num_of_clusters):
cluster_names[i]="Cluster-"+str(i)
#Prepration for plotting
df = pd.DataFrame(dict(x=x, y=y, label=clusters, title=ids,author=author))
groups = df.groupby('label')
df_author1=pd.DataFrame(dict(x=x_author1, y=y_author1, label=clusters[:num_author1], title=ids[:num_author1],author=author[:num_author1]))
groups_author1 = df_author1.groupby('label')
df_author2=pd.DataFrame(dict(x=x_author2, y=y_author2, label=clusters[:num_author2], title=ids[:num_author2],author=author[:num_author2]))
groups_author2 = df_author2.groupby('label')
#Plotting
fig, ax = plt.subplots(figsize=(20, 10)) # set size
for name, group in groups_author1:
ax.plot(group.x, group.y, marker='D', linestyle='', ms=12,
label=cluster_names[name], color=colours[name],
mec='none')
for name, group in groups_author2:
ax.plot(group.x, group.y, marker='o', linestyle='', ms=12,
label=cluster_names[name], color=colours[name],
mec='none')
ax.legend(numpoints=1) #show legend
for i in range(len(df)):
ax.text(df.ix[i]['x'], df.ix[i]['y'], df.ix[i]['title'], size=3)
plt.show()
print("Doc2Vec based clustering is now over.")
################################################## Using Topic Modelling to create Content based features####################################################
print("\n\n\n\n")
print("Extracting a document-topic matrix using LDA. Weights in this matrix will be used as features for further clustering.")
#Preparing a corpus
def prepareCorpus(docs):
text=list()
for i in range(len(docs)):
words=nltk.word_tokenize(docs[i])
text.append(words)
dictionary = corpora.Dictionary(text)
corpus = [dictionary.doc2bow(t) for t in text]
return(dictionary,corpus)
dic,corp=prepareCorpus(books)
print("Training LDA. Will take a while...")
numOfTopics=50
ldamodel = gensim.models.ldamodel.LdaModel(corpus=corp, num_topics=numOfTopics, id2word = dic, passes=20)
docsTopicMatrix=np.zeros((len(books),numOfTopics))
for i in range(len(books)):
topicsList=ldamodel.get_document_topics(corp[i],minimum_probability=0)
for j in range(len(topicsList)):
docsTopicMatrix[i,j]=topicsList[j][1]
dist = 1 - cosine_similarity(docsTopicMatrix)
#Clustering
num_of_clusters=25
kmeans=KMeans(n_clusters=num_of_clusters)
kmeans.fit(features)
clusters=kmeans.labels_.tolist()
documents = { 'title': ids, 'text': books, 'cluster': clusters, 'author':author }
documentFrame = pd.DataFrame(documents, index = [clusters])
#Converting to 2D
mds = MDS(n_components=2, dissimilarity="precomputed", random_state=7)
transformed = mds.fit_transform(dist)
x=transformed[:, 0]
y=transformed[:, 1]
x_author1=transformed[:num_author1,0]
y_author1=transformed[:num_author1,1]
x_author2=transformed[num_author1:,0]
y_author2=transformed[num_author1:,1]
#Generating colours
num_colours=num_of_clusters
col=mpl.colors.cnames.items()
colours={}
random.seed(7)
i=0
while (i<num_of_clusters):
c=random.choice(list(col))
if(c[1] not in colours.keys()):
colours[i]=c[1]
i=i+1
cluster_names={}
for i in range(num_of_clusters):
cluster_names[i]="Cluster-"+str(i)
#Prepration for plotting
df = pd.DataFrame(dict(x=x, y=y, label=clusters, title=ids,author=author))
groups = df.groupby('label')
df_author1=pd.DataFrame(dict(x=x_author1, y=y_author1, label=clusters[:num_author1], title=ids[:num_author1],author=author[:num_author1]))
groups_author1 = df_author1.groupby('label')
df_author2=pd.DataFrame(dict(x=x_author2, y=y_author2, label=clusters[:num_author2], title=ids[:num_author2],author=author[:num_author2]))
groups_author2 = df_author2.groupby('label')
#Plotting
fig, ax = plt.subplots(figsize=(20, 10)) # set size
for name, group in groups_author1:
ax.plot(group.x, group.y, marker='D', linestyle='', ms=12,
label=cluster_names[name], color=colours[name],
mec='none')
for name, group in groups_author2:
ax.plot(group.x, group.y, marker='o', linestyle='', ms=12,
label=cluster_names[name], color=colours[name],
mec='none')
ax.legend(numpoints=1) #show legend
for i in range(len(df)):
ax.text(df.ix[i]['x'], df.ix[i]['y'], df.ix[i]['title'], size=3)
plt.show()