-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cluestering_TFIDF_HC.py
230 lines (197 loc) · 6.76 KB
/
Cluestering_TFIDF_HC.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 27 17:34:06 2019
@author: Preeti
"""
# importing libraries
import nltk
import re
from urllib import request
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
stemmer = PorterStemmer()
stop_words = set(stopwords.words('english'))
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
from sklearn import metrics
import numpy as np
#Data preparation and preprocess
def custom_preprocessor(text):
text = re.sub(r'\W+|\d+|_', ' ', text) #removing numbers and punctuations
text = re.sub(r'\s+',' ',text) #remove multiple spaces into a single space
text = re.sub(r"\s+[a-zA-Z]\s+",' ',text) #remove a single character
text = text.lower()
text = nltk.word_tokenize(text) #tokenizing
text = [word for word in text if not word in stop_words] #English Stopwords
text = [lemmatizer.lemmatize(word) for word in text] #Lemmatising
return text
filepath_dict = {'Book1': 'https://www.gutenberg.org/files/58764/58764-0.txt',
'Book2': 'https://www.gutenberg.org/files/58751/58751-0.txt',
'Book3': 'http://www.gutenberg.org/cache/epub/345/pg345.txt'}
for key, value in filepath_dict.items():
if (key == "Book1"):
bookLoc = filepath_dict[key]
response = request.urlopen(bookLoc)
raw = response.read().decode('utf-8')
len(raw)
first_book = custom_preprocessor(raw)
elif (key == "Book2"):
bookLoc = filepath_dict[key]
response = request.urlopen(bookLoc)
raw = response.read().decode('utf-8')
len(raw)
second_book = custom_preprocessor(raw)
elif (key == "Book3"):
bookLoc = filepath_dict[key]
response = request.urlopen(bookLoc)
raw = response.read().decode('utf-8')
len(raw)
third_book = custom_preprocessor(raw)
else:
pass
#Building First Book
first_book_text = ' '.join(first_book)
fileLoc = '/Users/sfuhaid/Desktop/EBC7100Assign2-Group7/firstbook/a.txt'
with open(fileLoc, 'a') as fout:
fout.write(first_book_text)
fout.close()
#Building Second Book
second_book_text = ' '.join(second_book)
fileLoc = '/Users/sfuhaid/Desktop/EBC7100Assign2-Group7/secondbook/b.txt'
with open(fileLoc, 'a') as fout:
fout.write(second_book_text)
fout.close()
#Building Third Book
third_book_text = ' '.join(third_book)
fileLoc = '/Users/sfuhaid/Desktop/EBC7100Assign2-Group7/thirdbook/c.txt'
with open(fileLoc, 'a') as fout:
fout.write(third_book_text)
fout.close()
# labeling
# Cretaing tuple
# aBooklist = []
def readAtxtfile(bookText, docs, labels):
x = 0
i = 0
n = 150
while x < 200:
temp = ""
words = bookText.split(" ")[i:n]
for word in words:
temp = word + " " + temp
docs.append(temp)
labels.append(0)
i += 150
n += 150
x += 1
return docs, labels
# Cretaing tuple
# bBooklist = []
def readBtxtfile(bookText, docs, labels):
x = 0
i = 0
n = 150
while x < 184:
temp = ""
words = bookText.split(" ")[i:n]
for word in words:
temp = word + " " + temp
docs.append(temp)
labels.append(1)
i += 150
n += 150
x += 1
return docs, labels
# Cretaing tuple
# cBooklist = []
def readCtxtfile(bookText, docs, labels):
x = 0
i = 0
n = 150
while x < 200:
temp = ""
words = bookText.split(" ")[i:n]
for word in words:
temp = word + " " + temp
docs.append(temp)
labels.append(2)
i += 150
n += 150
x += 1
return docs, labels
docs = []
labels = []
docs, labels = readAtxtfile(first_book_text, docs, labels)
# print(aBooklist)
docs, labels = readBtxtfile(second_book_text, docs, labels)
# print(bBooklist)
docs, labels = readCtxtfile(third_book_text, docs, labels)
# print(cBooklist)
#print(len(docs))
#print(docs)
#print(labels)
#print(len(labels))
#***********************collocation********************************************
import nltk
from nltk.collocations import *
bigram_measures = nltk.collocations.BigramAssocMeasures()
trigram_measures = nltk.collocations.TrigramAssocMeasures()
# change this to read in your data
finder = BigramCollocationFinder.from_words(first_book+second_book+third_book)
# only bigrams that appear 3+ times
finder.apply_freq_filter(3)
# return the 10 n-grams with the highest PMI
book_collocation = finder.nbest(bigram_measures.pmi, 10)
#print('collocation : ',book_collocation)
#Tf-IDF Model Implementation
# Creating the Tf-Idf model
from sklearn.feature_extraction.text import TfidfVectorizer
vectorizer = TfidfVectorizer(max_features = 2000 , min_df = 3, max_df = 0.6)
X = vectorizer.fit_transform(docs)
X.toarray()
#**********************Hierarchical clustering*********************************
def tfidf_hc(X,k):
from sklearn.cluster import AgglomerativeClustering
HC_X = X.toarray()
cluster = AgglomerativeClustering(n_clusters=3, affinity='cosine', linkage='complete', compute_full_tree = 'bool')
labelsPred = cluster.fit_predict(HC_X)
return labelsPred
#*****************************calculation**************************************
cluster = tfidf_hc(X, 3)#cluster = 3
#print(cluster)
#kappa algo for evaluation
print (metrics.cohen_kappa_score(labels, cluster,weights = 'linear'))
from scipy.stats import spearmanr
from time import time
name = 'Hierarchical'
t0 = time()
print(82 * '_')
print('init\t\ttime\thomo\tcompl\tv-meas\tARI\tAMI\tkappa\tcorr\tsilh_Clus\tsilh_HMN')
print('%-9s\t%.2fs\t%i\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%-9s\t%.3f\t%.3f'
% (name, (time() - t0),
metrics.homogeneity_score(labels, cluster),
metrics.completeness_score(labels, cluster),
metrics.v_measure_score(labels, cluster),
metrics.adjusted_rand_score(labels, cluster),
metrics.adjusted_mutual_info_score(labels, cluster),
metrics.cohen_kappa_score(labels, cluster,weights = 'linear'),
str(spearmanr(labels,cluster)),
metrics.silhouette_score(X, cluster),
metrics.silhouette_score(X, labels),
))
#**************************error analysis**************************************
from sklearn.metrics.cluster import contingency_matrix
x = labels #actual labels
y = cluster #predicted labels
error_analysis = contingency_matrix(x, y)
#***************************plot************************************************
#visulization
from scipy.cluster import hierarchy
import matplotlib.pyplot as plt
X = np.array(X.toarray())
X = X[:20]
Z = hierarchy.linkage(X, 'complete')
plt.figure()
dn = hierarchy.dendrogram(Z)
plt.show()