-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_reader.py
More file actions
54 lines (35 loc) · 1.19 KB
/
Copy pathdata_reader.py
File metadata and controls
54 lines (35 loc) · 1.19 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
import pickle
import matplotlib.pyplot as plt
import pandas as pd
import os
# Folder Path
path = "./enron_with_categories"
listOfFiles = list()
for (dirpath, dirnames, filenames) in os.walk(path):
listOfFiles += [os.path.join(dirpath, file) for file in filenames]
#print(len(listOfFiles))
text_file_names = list(filter(lambda x: x[-4:] == '.txt', listOfFiles))
#print(txt_files)
#print(len(text_file_names))
#print(text_file_names[0].split('/'))
#exit()
#we can get labels from here
#Now get text data from files in txt files:
def read_text_file(file_path):
with open(file_path, 'r') as f:
text = f.read()
return text
text_file_path = []
text_file_data = []
text_file_labels = []
for file_name in text_file_names:
text = read_text_file(file_name)
if(int(file_name.split('/')[2]) != 7 and int(file_name.split('/')[2]) != 8):
text_file_path.append(file_name)
text_file_data.append(text)
text_file_labels.append( int(file_name.split('/')[2]) )
print(len(text_file_data))
print(text_file_path[0], text_file_data[0])
print( text_file_labels[0])
with open('text_data_labels.pkl', 'wb') as f:
pickle.dump((text_file_data, text_file_labels), f)