-
Notifications
You must be signed in to change notification settings - Fork 0
/
peters_project.py
346 lines (293 loc) · 10.4 KB
/
peters_project.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
import nltk
import re
from nltk.stem.snowball import SnowballStemmer
from nltk.corpus import stopwords
import numpy as np
import pandas as pd
import nltk
import re
import parsing
import os
import codecs
import scipy
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from sklearn.cluster import KMeans
from databaseConnection import Database
from operator import *
#from gensim import corpora, models, similarities
import unicodedata
import string
#from sklearn import feature_extraction
#import mpld3
def remove_accents(data):
return ''.join(x for x in unicodedata.normalize('NFKD', data) if x in string.ascii_letters).lower()
def tokenize_and_stem(text):
stemmer = SnowballStemmer('english')
tokens= [word for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)]
filtered_tokens = []
for token in tokens:
token = remove_accents(token)
if re.search('[a-zA-Z]', token):
if(len(token)>1 and len(token)<100):
filtered_tokens.append(token)
stems=[]
stems = [stemmer.stem(t) for t in filtered_tokens]
return stems
def tokenize_only(text):
tokens = [word.lower() for sent in nltk.sent_tokenize(text) for word in nltk.word_tokenize(sent)]
filtered_tokens = []
for token in tokens:
if re.search('[a-zA-Z]', token):
filtered_tokens.append(token)
return filtered_tokens
def count_main_list(dataset):
token_list = []
count_list = []
for set in dataset:
for temp_tuple in set:
token = temp_tuple[0]
tf = temp_tuple[1]
try:
tokenIndex = token_list.index(token)
count_list[tokenIndex][1]+= 1
count_list[tokenIndex][2] += tf
except ValueError:
token_list.append(token)
count_list.append([token, 1, tf])
return count_list
def count_words(string_list):
import string
#string_list = s.split(" ")
#string_list = sorted(s.split(" "), key=str)
#print(string_list)
#exit(0)
wordList = []
countList = []
for word in string_list:
#print(word + "| ")
word = word.strip()
try:
wordIndex = wordList.index(word)
countList[wordIndex][1]+= 1
except ValueError:
wordList.append(word)
countList.append([word, 1])
return countList
def normalizeString(content):
content = content.replace("'", '')
content = content.replace('"', '')
content = content.replace('/', ' ')
content = content.replace('\\', ' ')
content = content.replace(',', ' ')
content = content.replace('μ', 'µ')
content = content.replace('ß', 'β')
content = content.replace('(', ' ')
content = content.replace(')', ' ')
content = content.replace('%', ' ')
content = content.replace(':', ' ')
content = content.replace('.', ' ')
content = content.replace('∙', ' ')
content = content.replace('˚', '°')
content = content.replace('~', ' ')
content = content.replace('=', ' ')
content = content.replace('≤', ' ')
content = content.replace('−', '-')
content = content.replace('α', 'a')
content = content.replace('δ', 'b')
return content
#get row data from database
data = Database()
stopwords = nltk.corpus.stopwords.words('english')
synopses = data.getAbstract()
#initial abstract list
abstract_list=[]
TotalCountList = []
for i in synopses:
abstract_id = i[0]
abstract_list.append(str(abstract_id))
content = normalizeString(i[1])
allwords_stemmed = tokenize_and_stem(content)
allwords_stemmed = [word for word in allwords_stemmed if word not in stopwords]
count_list = count_words(allwords_stemmed)
#print(tuple_count)
TotalCountList.append(count_list)
#calculate result for every 100 abstracts and insert result to database.
if (len(abstract_list) == 100):
result = count_main_list(TotalCountList)
TotalCountList = []
#join abstract_ids to a comma breaken string
abstract_str = ",".join(abstract_list)
abstract_list = []
token_list = []
df_value_list = []
tf_value_list = []
for temp_tuple in result:
token_list.append(temp_tuple[0].strip())
df_value_list.append(str(temp_tuple[1]))
tf_value_list.append(str(temp_tuple[2]))
#join tf and df lists to comma breaked strings
token_str = ",".join(token_list)
df_str = ",".join(df_value_list)
tf_str = ",".join(tf_value_list)
print(abstract_str)
print(token_str)
print(df_str)
print(tf_str)
data.insertToken(abstract_str, token_str, df_str, tf_str)
print("inserted %s tokens into database" % str(len(token_list)))
if(len(abstract_list) > 0 ):
result = count_main_list(TotalCountList)
TotalCountList = []
# join abstract_ids to a comma breaken string
abstract_str = ",".join(abstract_list)
abstract_list = []
token_list = []
df_value_list = []
tf_value_list = []
for temp_tuple in result:
token_list.append(temp_tuple[0].strip())
df_value_list.append(str(temp_tuple[1]))
tf_value_list.append(str(temp_tuple[2]))
# join tf and df lists to comma breaked strings
token_str = ",".join(token_list)
df_str = ",".join(df_value_list)
tf_str = ",".join(tf_value_list)
print(abstract_str)
print(token_str)
print(df_str)
print(tf_str)
data.insertToken(abstract_str, token_str, df_str, tf_str)
print("inserted %s tokens into database" % str(len(token_list)))
exit(0)
for i in range(1,len(synopses)):
print('loading database')
#synopses = data.table()
print('finished Load database')
TotalCountList = []
abstract_list = []
tuple_count = 0;
for i in synopses:
abstract_id = i[0]
abstract_list.append(str(abstract_id))
content = normalizeString(i[1])
allwords_stemmed = tokenize_and_stem(content)
allwords_stemmed = [word for word in allwords_stemmed if word not in stopwords]
count_list = count_words(allwords_stemmed)
tuple_count += len(count_list)
##print(tuple_count)
TotalCountList.append(count_list)
result = count_main_list(TotalCountList)
token_list=[]
df_value_list=[]
tf_value_list=[]
for temp_tuple in result:
token_list.append(temp_tuple[0].strip())
df_value_list.append(str(temp_tuple[1]))
tf_value_list.append(str(temp_tuple[2]))
#print(len(token_list))
#token_list = np.unique(token_list)
#for token in token_list:
#print(token)
#print(len(token_list))
#exit(0);
if False:
token_list = np.unique(token_list)
print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
print(token_list)
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print(df_value_list)
print('_____________________')
print(tf_value_list)
# print(len(word_count))
exit(0)
# allwords_stemmed = [word for word in allwords_stemmed if word not in totalvocab_stemmed]
token_str = ", ".join(token_list)
df_str = ", ".join(df_value_list)
tf_str = ", ".join(tf_value_list)
abstract_str = ", ".join(abstract_list)
if False:
print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
print(token_str)
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print(df_str)
token_list = np.unique(token_list)
print('_____________________')
print(tf_str)
# print(len(word_count))
#exit(0)
data.insertToken(abstract_str, token_str, df_str, tf_str)
print("inserted %s tokens into database" % str(len(token_list)))
exit(0)
#tokenize
#tokenized_text = [tokenize_and_stem(text) for text in synopses]
#remove stop words
#texts = [[word for word in text if word not in stopwords] for text in tokenized_text]
count=0
for i in synopses:
abstract_id = i[0]
count += 1
content = normalizeString(i[1])
allwords_stemmed = tokenize_and_stem(content)
allwords_stemmed = [word for word in allwords_stemmed if word not in stopwords]
#allwords_stemmed = np.unique(allwords_stemmed)
count_list = count_words(allwords_stemmed)
token_list =[]
token_count = []
for temp_tuple in count_list:
token_list.append(temp_tuple[0].strip())
token_count.append(str(temp_tuple[1]))
if False:
print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^')
print(count_list)
print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
print(token_list)
token_list = np.unique(token_list)
print('_____________________')
print(token_list)
#print(len(word_count))
exit(0)
#allwords_stemmed = [word for word in allwords_stemmed if word not in totalvocab_stemmed]
token_str = ", ".join(token_list)
count_str = ", ".join(token_count)
#print(count_list)
#print('------------------')
#print(allwords_stemmed)
#print(word_count)
#print(allwords_stemmed_str)
#print(word_count_str)
#exit(0)
data.insertToken(abstract_id, token_str, count_str)
#totalvocab_stemmed.extend(allwords_stemmed_str) # extend the 'totalvocab_stemmed' list
#totalvocab_stemmed.append(allwords_stemmed_str)
#allwords_tokenized = tokenize_only(i[1])
#allwords_tokenized = [word for word in allwords_tokenized if word not in stopwords]
#totalvocab_tokenized.extend(allwords_tokenized)
if count%100 == 0:
print(str(count))
exit(0)
#vocab_frame = pd.DataFrame({'words': totalvocab_tokenized}, index = totalvocab_stemmed)
#print ('there are ' + str(vocab_frame.shape[0]) + ' items in vocab_frame')
tfidf_vectorizer = TfidfVectorizer(max_df=0.8, max_features=200000,
min_df=0.2, stop_words='english',
use_idf=True, tokenizer=tokenize_and_stem, ngram_range=(1, 3))
tfidf_matrix = tfidf_vectorizer.fit_transform(synopses)
tfidf_array=tfidf_matrix.toarray();
#print(tfidf_matrix.shape)
print(tfidf_matrix.shape)
terms = tfidf_vectorizer.get_feature_names()
#print(len(terms))
dist = 1 - cosine_similarity(tfidf_matrix)
num_clusters = 5
km = KMeans(n_clusters=num_clusters)
km.fit(tfidf_matrix)
clusters = km.labels_.tolist()
#
#std_dist_list = []
#for tempList in dist:
# std_dist_list.append(np.std(tempList))
#print(dist)
#tokenize_list = tokenize_and_stem(sentence)
#print(tokenize_list)
#tokens =nltk.word_tokenize(sentence)
#print(stopwords)