forked from yexinyinancy/SI650_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.py
423 lines (375 loc) · 15.5 KB
/
extract.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
423
import json
import spacy
import math
import pandas as pd
import tqdm
from keybert import KeyBERT
import warnings
import nltk
from nltk import word_tokenize
import string
from nltk.stem import WordNetLemmatizer
import numpy as np
warnings.filterwarnings("ignore")
def extract_tfidf(tag_num, json_file):
file = open(json_file, 'r')
json_list = json.load(file)
corpus = {}
res_title = []
res_tag = []
res_link = []
res_title_tag = []
res_category = []
res_author = []
res_image = []
for json_raw in tqdm.tqdm(json_list):
# for every product
review_list = []
for rev in json_raw["reviews"]:
if 'body' in rev.keys():
review_list.append(rev['body'])
else:
continue
if len(review_list) == 0:
continue
corpus[json_raw["title"]] = review_list
D = len(review_list)
nlp = spacy.load("en_core_web_sm")
dict_list = []
tokens_pos = {}
for review in review_list:
dict_all = {}
word_list = nlp(review)
for token in word_list:
if token.is_stop:
continue
if token.is_punct:
continue
tokens_pos[str(token.lemma_)] = token.pos_
if str(token.lemma_) in dict_all.keys():
dict_all[str(token.lemma_)] += 1
else:
dict_all[str(token.lemma_)] = 1
if str(token.lemma_) not in tokens_pos.keys():
tokens_pos[str(token.lemma_)] = tokens_pos
dict_list.append(dict_all)
tf_idf = {}
for doc in dict_list:
for key,value in doc.items():
tf = math.log(value + 1, 2)
k = 0
for i in dict_list:
if key in i.keys():
k += 1
idf = 1 + math.log(D / k)
if key not in tf_idf:
tf_idf[key] = tf * idf
else:
tf_idf[key] = max(tf * idf, tf_idf[key])
tf_idf_sorted = sorted(tf_idf.items(), key=lambda x: x[1], reverse=True)
i = 0
j = 0
title_str = json_raw["title"] + ' '
tag_str = ''
while i < len(tf_idf_sorted) and j < tag_num:
if tf_idf_sorted[i][0] in tokens_pos and tokens_pos[tf_idf_sorted[i][0]] == 'ADJ':
tag_str += tf_idf_sorted[i][0] + ', '
j += 1
i += 1
# for output
if 'title' in json_raw.keys():
res_title.append(json_raw["title"])
else:
res_title.append('')
res_title_tag.append((title_str+tag_str).strip())
res_tag.append(tag_str.strip())
if 'link' in json_raw.keys():
res_link.append(json_raw["link"])
else:
res_link.append('')
res_category_str = ''
if 'categories' in json_raw.keys():
if len(json_raw["categories"]) != 0:
if 'name' in json_raw["categories"][0].keys():
res_category_str = json_raw["categories"][0]['name']
res_category.append(res_category_str)
res_author_tmp = []
if 'authors' in json_raw.keys():
if len(json_raw["authors"]) != 0:
for a in json_raw["authors"]:
if 'name' in a.keys():
res_author_tmp.append(a['name'])
res_author_tmp = set(res_author_tmp)
res_author_str = ''
for a in res_author_tmp:
res_author_str += a
res_author_str += ', '
res_author.append(res_author_str)
if 'image' in json_raw.keys():
res_image.append(json_raw["image"])
else:
res_image.append('')
return res_title, res_tag, res_title_tag, res_link, res_category, res_author, res_image
def extract_bert(tag_num, json_file):
file = open(json_file, 'r')
json_list = json.load(file)
corpus = {}
res_title = []
res_tag = []
res_link = []
res_title_tag = []
res_category = []
res_author = []
res_image = []
for json_raw in tqdm.tqdm(json_list):
# for every product
review_list = []
for rev in json_raw["reviews"]:
if 'body' in rev.keys():
review_list.append(rev['body'])
else:
continue
if len(review_list) == 0:
continue
corpus[json_raw["title"]] = review_list
review_all = ''
for review in review_list:
review_all += review + ' '
kw_model = KeyBERT()
keywords = kw_model.extract_keywords(review_all, keyphrase_ngram_range=(1, 1), stop_words='english', use_mmr=True, diversity=0.7, top_n=int(tag_num*1.5))
tag_str = ''
ind = 0
for k in keywords:
if not k[0].isdigit():
ind += 1
tag_str += k[0] + ' '
if ind == tag_num:
break
title_str = json_raw["title"] + ', '
# for output
if 'title' in json_raw.keys():
res_title.append(json_raw["title"])
else:
res_title.append('')
res_title_tag.append((title_str+tag_str).strip())
res_tag.append(tag_str.strip())
if 'link' in json_raw.keys():
res_link.append(json_raw["link"])
else:
res_link.append('')
res_category_str = ''
if 'categories' in json_raw.keys():
if len(json_raw["categories"]) != 0:
if 'name' in json_raw["categories"][0].keys():
res_category_str = json_raw["categories"][0]['name']
res_category.append(res_category_str)
res_author_tmp = []
if 'authors' in json_raw.keys():
if len(json_raw["authors"]) != 0:
for a in json_raw["authors"]:
if 'name' in a.keys():
res_author_tmp.append(a['name'])
res_author_tmp = set(res_author_tmp)
res_author_str = ''
for a in res_author_tmp:
res_author_str += a
res_author_str += ', '
res_author.append(res_author_str)
if 'image' in json_raw.keys():
res_image.append(json_raw["image"])
else:
res_image.append('')
return res_title, res_tag, res_title_tag, res_link, res_category, res_author, res_image
def clean(text):
text = text.lower()
printable = set(string.printable)
text = filter(lambda x: x in printable, text)
text = "".join(list(text))
return text
def extract_textrank(tag_num, json_file):
file = open(json_file, 'r')
json_list = json.load(file)
corpus = {}
res_title = []
res_tag = []
res_link = []
res_title_tag = []
res_category = []
res_author = []
res_image = []
for json_raw in tqdm.tqdm(json_list):
# for every product
review_list = []
for rev in json_raw["reviews"]:
if 'body' in rev.keys():
review_list.append(rev['body'])
else:
continue
if len(review_list) == 0:
continue
corpus[json_raw["title"]] = review_list
review_all = ''
for review in review_list:
review_all += review + ' '
Cleaned_text = clean(review_all)
text = word_tokenize(Cleaned_text)
# nltk.download('averaged_perceptron_tagger')
POS_tag = nltk.pos_tag(text)
# nltk.download('wordnet')
wordnet_lemmatizer = WordNetLemmatizer()
adjective_tags = ['JJ','JJR','JJS']
lemmatized_text = []
for word in POS_tag:
if word[1] in adjective_tags:
lemmatized_text.append(str(wordnet_lemmatizer.lemmatize(word[0],pos="a")))
else:
lemmatized_text.append(str(wordnet_lemmatizer.lemmatize(word[0]))) #default POS = noun
POS_tag = nltk.pos_tag(lemmatized_text)
stopwords = []
wanted_POS = ['NN','NNS','NNP','NNPS','JJ','JJR','JJS','VBG','FW']
for word in POS_tag:
if word[1] not in wanted_POS:
stopwords.append(word[0])
punctuations = list(str(string.punctuation))
stopwords = stopwords + punctuations
stopword_file = open("long_stopwords.txt", "r")
#Source = https://www.ranks.nl/stopwords
lots_of_stopwords = []
for line in stopword_file.readlines():
lots_of_stopwords.append(str(line.strip()))
stopwords_plus = []
stopwords_plus = stopwords + lots_of_stopwords
stopwords_plus = set(stopwords_plus)
processed_text = []
for word in lemmatized_text:
if word not in stopwords_plus:
processed_text.append(word)
vocabulary = list(set(processed_text))
vocab_len = len(vocabulary)
weighted_edge = np.zeros((vocab_len,vocab_len),dtype=np.float32)
score = np.zeros((vocab_len),dtype=np.float32)
window_size = 3
covered_coocurrences = []
for i in range(0,vocab_len):
score[i]=1
for j in range(0,vocab_len):
if j==i:
weighted_edge[i][j]=0
else:
for window_start in range(0,(len(processed_text)-window_size)):
window_end = window_start+window_size
window = processed_text[window_start:window_end]
if (vocabulary[i] in window) and (vocabulary[j] in window):
index_of_i = window_start + window.index(vocabulary[i])
index_of_j = window_start + window.index(vocabulary[j])
# index_of_x is the absolute position of the xth term in the window
# (counting from 0)
# in the processed_text
if [index_of_i,index_of_j] not in covered_coocurrences:
weighted_edge[i][j]+=1/math.fabs(index_of_i-index_of_j)
covered_coocurrences.append([index_of_i,index_of_j])
inout = np.zeros((vocab_len),dtype=np.float32)
for i in range(0,vocab_len):
for j in range(0,vocab_len):
inout[i]+=weighted_edge[i][j]
MAX_ITERATIONS = 50
d = 0.85
threshold = 0.0001 #convergence threshold
for iter in range(0,MAX_ITERATIONS):
prev_score = np.copy(score)
for i in range(0,vocab_len):
summation = 0
for j in range(0,vocab_len):
if weighted_edge[i][j] != 0:
summation += (weighted_edge[i][j]/inout[j])*score[j]
score[i] = (1-d) + d*(summation)
if np.sum(np.fabs(prev_score-score)) <= threshold: #convergence condition
print("Converging at iteration "+str(iter)+"....")
break
phrases = []
phrase = " "
for word in lemmatized_text:
if word in stopwords_plus:
if phrase!= " ":
phrases.append(str(phrase).strip().split())
phrase = " "
elif word not in stopwords_plus:
phrase+=str(word)
phrase+=" "
unique_phrases = []
for phrase in phrases:
if phrase not in unique_phrases:
unique_phrases.append(phrase)
for word in vocabulary:
#print word
for phrase in unique_phrases:
if (word in phrase) and ([word] in unique_phrases) and (len(phrase)>1):
#if len(phrase)>1 then the current phrase is multi-worded.
#if the word in vocabulary is present in unique_phrases as a single-word-phrase
# and at the same time present as a word within a multi-worded phrase,
# then I will remove the single-word-phrase from the list.
unique_phrases.remove([word])
phrase_scores = []
keywords = []
for phrase in unique_phrases:
phrase_score=0
keyword = ''
for word in phrase:
keyword += str(word)
keyword += " "
phrase_score+=score[vocabulary.index(word)]
phrase_scores.append(phrase_score)
keywords.append(keyword.strip())
sorted_index = np.flip(np.argsort(phrase_scores),0)
tag_str = ''
for i in range(0, min(tag_num, len(sorted_index))):
# print(str(keywords[sorted_index[i]])+", ", end=' ')
tag_str += str(keywords[sorted_index[i]]) + ', '
title_str = json_raw["title"] + ' '
# for output
if 'title' in json_raw.keys():
res_title.append(json_raw["title"])
else:
res_title.append('')
res_title_tag.append((title_str+tag_str).strip())
res_tag.append(tag_str.strip())
if 'link' in json_raw.keys():
res_link.append(json_raw["link"])
else:
res_link.append('')
res_category_str = ''
if 'categories' in json_raw.keys():
if len(json_raw["categories"]) != 0:
if 'name' in json_raw["categories"][0].keys():
res_category_str = json_raw["categories"][0]['name']
res_category.append(res_category_str)
res_author_tmp = []
if 'authors' in json_raw.keys():
if len(json_raw["authors"]) != 0:
for a in json_raw["authors"]:
if 'name' in a.keys():
res_author_tmp.append(a['name'])
res_author_tmp = set(res_author_tmp)
res_author_str = ''
for a in res_author_tmp:
res_author_str += a
res_author_str += ', '
res_author.append(res_author_str)
if 'image' in json_raw.keys():
res_image.append(json_raw["image"])
else:
res_image.append('')
return res_title, res_tag, res_title_tag, res_link, res_category, res_author, res_image
if __name__ == '__main__':
# tf-idf
# res_title, res_tag, res_title_tag, res_link, res_category, res_author, res_image = extract_tfidf(10, 'books_result_page_2000.json')
# df = pd.DataFrame({'Title': res_title, 'Title_and_tag': res_title_tag, 'Tag': res_tag, 'Link': res_link, 'Category': res_category, 'Author': res_author, 'Image': res_image})
# df.to_csv('tag_10_tfidf_2000.csv', encoding='utf-8', index=False)
# bert
# res_title, res_tag, res_title_tag, res_link, res_category, res_author, res_image = extract_bert(10, 'books_result_page_2000.json')
# df = pd.DataFrame({'Title': res_title, 'Title_and_tag': res_title_tag, 'Tag': res_tag, 'Link': res_link, 'Category': res_category, 'Author': res_author, 'Image': res_image})
# df.to_csv('tag_10_bert_2000.csv', encoding='utf-8', index=False)
# textrank
res_title, res_tag, res_title_tag, res_link, res_category, res_author, res_image = extract_textrank(10, 'books_result_page_2000.json')
df = pd.DataFrame({'Title': res_title, 'Title_and_tag': res_title_tag, 'Tag': res_tag, 'Link': res_link, 'Category': res_category, 'Author': res_author, 'Image': res_image})
df.to_csv('tag_10_textrank_2000.csv', encoding='utf-8', index=False)