-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreprocess3.py
More file actions
36 lines (27 loc) · 1.06 KB
/
Preprocess3.py
File metadata and controls
36 lines (27 loc) · 1.06 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
import glob
import pickle
import os
def read_preprocess_files():
dir = os.path.dirname(__file__)
path=os.path.join(dir, 'Health-Tweets', '**' )
tweets = []
lens = []
if 'lens.pickle' not in os.listdir(dir):
for file in glob.glob(dir+'Health-Tweets/**'):
with open(file, 'r', encoding='utf-8', errors='ignore') as f:
print(file)
lines = f.readlines()
for line in lines:
tweet = ''.join(line.split('|')[2:]).strip().lower().split(' ')
tweets.append(tweet)
lens.append(len(tweet))
with open(dir+'/tweets.pickle', 'wb') as handle:
pickle.dump(tweets, handle)
with open(dir+'/lens.pickle', 'wb') as handle:
pickle.dump(lens, handle)
else:
with open(dir+'/tweets.pickle', 'rb') as handle:
tweets = pickle.load(handle)
with open(dir+'/lens.pickle', 'rb') as handle:
lens = pickle.load(handle)
return tweets, lens