-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
executable file
·65 lines (52 loc) · 2.03 KB
/
preprocess.py
File metadata and controls
executable file
·65 lines (52 loc) · 2.03 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
#!/usr/local/bin/python3
import json
import string
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
def flatten(list_of_lists):
return [val for sublist in list_of_lists for val in sublist]
def stemming(data):
ps = PorterStemmer()
return [ps.stem(d) for d in data]
def tokeniz(data):
return word_tokenize(data)
def removeStopwords(data):
stop_words = set(stopwords.words('english'))
return [d for d in data if not d in stop_words]
def removeNumbers(data):
return [d.lower() for d in data if len(d)>1 and d.isalpha()]
def removePunctuation(data):
translator = str.maketrans(string.punctuation, ' '*len(string.punctuation))
return data.translate(translator)
def dataPreProcess(data):
data = removePunctuation(data)
data = tokeniz(data)
data = removeNumbers(data) #also tolower and single words remove
data = removeStopwords(data)
data = stemming(data)
return data
def JsondataPreProcess(JsonData):
if(JsonData['abstract']): JsonData['abstract'] = dataPreProcess(JsonData['abstract'])
else: JsonData['abstract'] = ""
if(JsonData['keywords']): JsonData['keywords'] = flatten(list(map(dataPreProcess, JsonData['keywords'])))
else: JsonData['keywords'] = []
if(JsonData['title']): JsonData['title'] = dataPreProcess(JsonData['title'])
else: JsonData['title'] = JsonData['titleCopy'] = ""
return JsonData
if __name__ == "__main__":
fileName = "file.txt"
data = []
for line in open(fileName,'r', encoding='utf8'):
data.append(json.loads(line))
data[-1]['titleCopy'] = data[-1]['title']
if(data[-1]['keywords']): data[-1]['keywords'] = [i for i in data[-1]['keywords'] if i]
data = list(map(JsondataPreProcess, data))
with open("out_"+fileName, 'w') as outfile1:
for jsonitem in data:
dictionary = {}
dictionary['abstract'] = " ".join(jsonitem['abstract'])
dictionary['keywords'] = " ".join(jsonitem['keywords'])
dictionary['title'] = " ".join(jsonitem['title'])
dictionary['titleCopy'] = jsonitem['titleCopy']
print(json.dumps(dictionary), file=outfile1)