-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexer.py
More file actions
75 lines (56 loc) · 1.67 KB
/
Copy pathindexer.py
File metadata and controls
75 lines (56 loc) · 1.67 KB
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
import os
import ujson
import json
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
with open('scraper_results.json', 'r') as f:
scraper_results = f.read()
data_dict = json.loads(scraper_results)
pub_name = []
pub_url = []
pub_author = []
for item in data_dict:
pub_name.append(item['name'])
pub_url.append(item['link'])
pub_author.append(item['author'])
#StopWord PreDefinition
STOPWORDS = stopwords.words('english')
stemmer = PorterStemmer()
pub_list = []
pub_list_special_rm = []
pub_list_stemmed =[]
#Remove all Special Chars
special_chars = '''!()--[]{};:'"\\, <>./?@#$%^&*_~0123456789+='''''
for file in pub_name:
word_sc_rm = ""
if len(file.split()) ==1 :
pub_list_special_rm.append(file)
else:
for a in file:
if a in special_chars:
word_sc_rm += ' '
else:
word_sc_rm += a
pub_list_special_rm.append(word_sc_rm)
for name in pub_list_special_rm:
words = word_tokenize(name)
stem_word = ""
for a in words:
if a.lower() not in STOPWORDS:
stem_word += stemmer.stem(a) + ' '
pub_list_stemmed.append(stem_word.lower())
data_dict = {}
for a in range(len(pub_list_stemmed)):
for b in pub_list_stemmed[a].split():
if b not in data_dict:
data_dict[b] = [a]
else:
data_dict[b].append(a)
with open('publication_list_stemmed.json', 'w') as f:
json_str = ujson.dumps(pub_list_stemmed)
f.write(json_str)
with open('publication_indexed_dictionary.json', 'w') as f:
print(data_dict)
json_str = ujson.dumps(data_dict)
f.write(json_str)