-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
352 lines (244 loc) · 9.48 KB
/
main.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
from __future__ import division
import numpy as np
from sklearn.metrics import jaccard_similarity_score
from math import log10, sqrt
from string import punctuation
import os
import nltk
# Variables
MODEL = 'trigram'
MEASURE = 'jaccard'
NUM_DOCS = 0
MASTER_DOC = 'combined_docs'
STOPWORDS = 'nltk_en_stopwords'
DATASET = 'docs'
# Return unigram unique words from a text
def extract_unique_words(text):
#return text.translate(None, punctuation).split()
return set(text.translate(None, punctuation).lower().split())
# Return bigram unique words from a text
def extract_bigram_unique_words(text):
# Remove punctuations
text_no_punctuations = text.translate(None, punctuation).lower().split()
# Create bigrams from the input text
bigrms = list(nltk.bigrams(text_no_punctuations))
# Create unique bigrams
unique_bigrms = set(bigrms)
# Convert the set of unique bigrams back into list
list_of_unique_bigrms = list(unique_bigrms)
# Create list of unique bigrams represented as string
list_of_unique_bigrms_str = [ "%s %s" % x for x in list_of_unique_bigrms ]
return list_of_unique_bigrms_str
# Return trigram unique words from a text
def extract_trigram_unique_words(text):
# Remove punctuations
text_no_punctuations = text.translate(None, punctuation).lower().split()
# Create trigrams from the input text
trigrms = list(nltk.trigrams(text_no_punctuations))
# Create unique trigrams
unique_trigrms = set(trigrms)
# Convert the set of unique trigrams back into list
list_of_unique_trigrms = list(unique_trigrms)
# Create list of unique trigrams represented as string
list_of_unique_trigrms_str = [ "%s %s %s" % x for x in list_of_unique_trigrms ]
return list_of_unique_trigrms_str
# Return the document frequency for each term in the input list
def computeDFs(unique_words, list_of_assignment_files):
# DF for each term t (dfT) was calculated by counting the number of
# documents which had the term t
list_of_df = []
for unique_word in unique_words:
counter = 0
for assignment_file in list_of_assignment_files:
with open(assignment_file, 'r') as f:
all_text = f.read().replace('\n', ' ')
# Convert the whole text into lower case
all_text = all_text.lower()
# Replace single quote (" ' ") into single white space
all_text_no_quote = all_text.replace("'", " ")
if unique_word in all_text_no_quote:
counter = counter + 1
list_of_df.append(counter)
return list_of_df
# Return the inverse document frequency for each term in the input list
def computeIDFs(NUM_DOCS, DFs):
# Formula: idf(t) = 1 + log N / df(t)
# df(t) = document frequency for term t
# idf(t) = inverse document frequency for term t
# N = total number of documents
list_of_idf = []
for df in DFs:
idf = 0
if df == 0:
idf = 1
else:
idf = 1 + (log10(NUM_DOCS / df))
list_of_idf.append(idf)
return list_of_idf
# Return the term frequency of a term in a document
def computeTF(assignment_file, unique_word):
with open(assignment_file, 'r') as f:
all_text = f.read().replace('\n', ' ')
# Convert the whole text into lower case
all_text = all_text.lower()
# Replace single quote (" ' ") into single white space
all_text_no_quote = all_text.replace("'", " ")
return all_text_no_quote.count(unique_word) / computeNumOfWordsInText(all_text_no_quote)
# Return the TF-IDF weight vector for a document
def computeTFIDFweightvector(assignment_file, unique_words, IDFs):
# Wtd = TFtd x IDFt
# Wtd = TF-IDF weight vector
# TFtd = frequency of a term in a document
# IDFt = inverse document frequency for term t
list_of_TFIDFweightvector = []
for idx in range(0, len(unique_words)):
TF = computeTF(assignment_file, unique_words[idx])
print 'TF'
print TF
print '\n'
weightVector = TF * IDFs[idx]
print 'Weight Vector'
print weightVector
print '\n'
list_of_TFIDFweightvector.append(weightVector)
return list_of_TFIDFweightvector
# Return the value of cosine between two document vectors
def compareDocument(TFIDF_weightvector_1, TFIDF_weightvector_2):
# Compute the dot products
dotProducts = 0
for idx in range(0, len(TFIDF_weightvector_1)):
dotProducts = dotProducts + (TFIDF_weightvector_1[idx] * TFIDF_weightvector_2[idx])
# Compute the magnitude of the 1st TFIDF weight vector
magnitude_1 = 0
for idx in range(0, len(TFIDF_weightvector_1)):
magnitude_1 = magnitude_1 + (TFIDF_weightvector_1[idx] * TFIDF_weightvector_1[idx])
# Compute the magnitude of the 2nd TFIDF weight vector
magnitude_2 = 0
for idx in range(0, len(TFIDF_weightvector_2)):
magnitude_2 = magnitude_2 + (TFIDF_weightvector_2[idx] * TFIDF_weightvector_2[idx])
# Compute the cosine
if magnitude_1 == 0:
magnitude_1 = 0.000001
if magnitude_2 == 0:
magnitude_2 = 0.000001
cosine = dotProducts / (sqrt(magnitude_1) * sqrt(magnitude_2))
return cosine
# Return the value of jaccard similarity between two document vectors
def compareDocumentJaccard(TFIDF_weightvector_1, TFIDF_weightvector_2):
# How to read the Jaccard coeficient:
# The coeficient is multiplied by 100
# Two sets that share all members would be 100% similar
# The closer to 100%, the more similarity (e.g. 90% is more similar than 89%)
# If they share no members, they are 0% similar
# The midway point (50%) means that the two sets share half of the members
# Find the intersection between two document vectors
TFIDF_weightvector_intersection = []
for tfidfweightvector_1 in TFIDF_weightvector_1:
if tfidfweightvector_1 in TFIDF_weightvector_2:
TFIDF_weightvector_intersection.append(tfidfweightvector_1)
break
# Find the union of all elements (unique values) from both document vectors
TFIDF_weightvector_union = []
for tfidfweightvector_1 in TFIDF_weightvector_1:
TFIDF_weightvector_union.append(tfidfweightvector_1)
for tfidfweightvector_2 in TFIDF_weightvector_2:
TFIDF_weightvector_union.append(tfidfweightvector_2)
TFIDF_weightvector_union = list(set(TFIDF_weightvector_union))
# Compute the Jaccard coeficient
jaccardCoef = len(TFIDF_weightvector_intersection) / len(TFIDF_weightvector_union)
return jaccardCoef
# Return the list of unique words without stopwords
def eliminateStopwords(unique_words):
stopwords = nltk.corpus.stopwords.words('english')
no_stopwords_list = []
for unique_word in unique_words:
words = unique_word.split()
no_stopwords_list.append(unique_word)
for word in words:
if word in stopwords:
no_stopwords_list.pop()
break
return no_stopwords_list
# Return the number of unigram words in a string
def computeNumOfWordsInText(text):
numOfWords = len(text.split())
if MODEL == 'bigram':
if numOfWords == 1:
numOfWords = 0
else:
numOfWords = numOfWords - 1
elif MODEL == 'trigram':
if numOfWords == 1 or numOfWords == 2:
numOfWords = 0
else:
numOfWords = numOfWords - 2
return numOfWords
# Combine all documents into one file called MASTER DOCUMENT
assignment_files = []
for filename in os.listdir(DATASET):
assignment_files.append(DATASET + '/' + filename)
with open(MASTER_DOC, 'w') as outfile:
for fname in assignment_files:
with open(fname) as infile:
for line in infile:
outfile.write(line)
# Extract unique words (unigram, bigram, trigram) from the MASTER DOCUMENT
with open(MASTER_DOC, 'r') as f:
all_text = f.read().replace('\n', ' ')
# Convert the whole text into lower case
all_text = all_text.lower()
# Replace single quote (" ' ") into single white space
all_text_no_quote = all_text.replace("'", " ")
# Create unique words (vocabulary) based on the applied model
# Default is unigram
unique_words = extract_unique_words(all_text_no_quote)
if MODEL == 'bigram':
# Unique words for bigram vector
unique_words = extract_bigram_unique_words(all_text_no_quote)
elif MODEL == 'trigram':
# Unique words for trigram vector
unique_words = extract_trigram_unique_words(all_text_no_quote)
# DATASET PREPROCESSING
# Eliminate stopwords
'''
with open(STOPWORDS, 'r') as f:
stopwords = f.readlines()
stopwords = [x.strip() for x in stopwords]
unique_words_no_stopwords = [x for x in unique_words if x not in stopwords]
'''
unique_words_no_stopwords = eliminateStopwords(unique_words)
print 'Unique words without stopwords'
print unique_words_no_stopwords
# VECTOR SPACE MODEL WITH COSINE SIMILARITY MEASURE
NUM_DOCS = len(assignment_files)
# Computer Document Frequency (DF) for each term t
DFs = computeDFs(unique_words_no_stopwords, assignment_files)
print 'DFs'
print DFs
print '\n'
# Compute Inverse Document Frequency (IDF) for each term t
IDFs = computeIDFs(NUM_DOCS, DFs)
print 'IDFs'
print IDFs
print '\n'
# Compute TF-IDF weight vector for each document
TFIDF_weightvectors = []
for assignment_file in assignment_files:
TFIDF_weightvectors.append(computeTFIDFweightvector(assignment_file, unique_words_no_stopwords, IDFs))
print 'TFIDF weight vectors'
print TFIDF_weightvectors
print '\n'
if MEASURE == 'cosine':
# Compare each pair of assignment using Cosine Similarity
for idx_1 in range(0, NUM_DOCS):
for idx_2 in range(0, NUM_DOCS):
if idx_1 != idx_2:
cosineSim = compareDocument(TFIDF_weightvectors[idx_1], TFIDF_weightvectors[idx_2])
print 'Cosine similarity measure between document {0} and {1} gives {2} as the result'.format(idx_1, idx_2, cosineSim)
else:
# Compare each pair of assignment using Jaccard Similarity
for idx_1 in range(0, NUM_DOCS):
for idx_2 in range(0, NUM_DOCS):
if idx_1 != idx_2:
jaccardSim = compareDocumentJaccard(TFIDF_weightvectors[idx_1], TFIDF_weightvectors[idx_2])
print 'Jaccard similarity measure between document {0} and {1} gives {2} as the result'.format(idx_1, idx_2, jaccardSim)