-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfile_reader.py
More file actions
71 lines (55 loc) · 2.23 KB
/
Copy pathfile_reader.py
File metadata and controls
71 lines (55 loc) · 2.23 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
from os import chdir
class FileReader:
def __init__(self):
chdir("..")
self.x_data, self.y_data = [], []
self.x_train, self.y_train = [], []
self.x_valid, self.y_valid = [], []
self.x_test, self.y_test = [], []
def read_from_file(self):
print("Reading data from files...")
all_data_file = open('./data/train.txt', 'r')
train_file = open('./data/train.txt', 'r')
valid_file = open('./data/valid.txt', 'r')
test_file = open('./data/test.txt', 'r')
for line in all_data_file.readlines():
words = line.strip().split('\t')
if len(words) == 2:
self.x_data.append(words[0])
self.y_data.append(words[1])
for line in train_file.readlines():
words = line.strip().split('\t')
if len(words) == 2:
self.x_train.append(words[0])
self.y_train.append(words[1])
for line in valid_file.readlines():
words = line.strip().split('\t')
if len(words) == 2:
self.x_valid.append(words[0])
self.y_valid.append(words[1])
for line in test_file.readlines():
words = line.strip().split('\t')
if len(words) == 2:
self.x_test.append(words[0])
self.y_test.append(words[1])
all_data_file.close()
train_file.close()
valid_file.close()
test_file.close()
def return_all_data(self):
return self.x_data, self.y_data
def return_training_sets(self):
return self.x_train, self.y_train
def return_valid_sets(self):
return self.x_valid, self.y_valid
def return_test_sets(self):
return self.x_test, self.y_test
if __name__ == '__main__':
file_reader = FileReader()
file_reader.read_from_file()
print("The training set file: \nInputs: {}\nOutputs: {}\n".format(
file_reader.x_train[:5], file_reader.y_train[:5]))
print("The validation set file: \nInputs: {}\nOutputs: {}\n".format(
file_reader.x_valid[:5], file_reader.y_valid[:5]))
print("The test set file: \nInputs: {}\nOutputs: {}\n".format(
file_reader.x_test[:5], file_reader.y_test[:5]))