-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_data.py
148 lines (99 loc) · 3.28 KB
/
load_data.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import jsonpickle as jp
import _pickle as cPickle
import json
import os
from nltk.tokenize import word_tokenize
import numpy as np
data_dir = 'data/op_spam_v1.4'
#negative reviews that are true/false
negative_dir_true = data_dir+'/negative_polarity/truthful_from_Web'
negative_dir_false = data_dir+'/negative_polarity/deceptive_from_MTurk'
#positive reviews that are true/false
positive_dir_true = data_dir+'/positive_polarity/truthful_from_TripAdvisor'
positive_dir_false = data_dir+'/positive_polarity/deceptive_from_MTurk'
trueX = []
falseX = []
for i in range(1,6):
curdir = negative_dir_true + '/fold{}'.format(i)
trueX.append([])
falseX.append([])
for file in os.listdir(curdir):
with open(curdir + '/' + file) as f:
trueX[i-1].append(word_tokenize(f.read()))
curdir = negative_dir_false + '/fold{}'.format(i)
for file in os.listdir(curdir):
with open(curdir + '/' + file) as f:
falseX[i-1].append(word_tokenize(f.read()))
curdir = positive_dir_true + '/fold{}'.format(i)
for file in os.listdir(curdir):
with open(curdir + '/' + file) as f:
trueX[i-1].append(word_tokenize(f.read()))
curdir = positive_dir_false + '/fold{}'.format(i)
for file in os.listdir(curdir):
with open(curdir + '/' + file) as f:
falseX[i-1].append(word_tokenize(f.read()))
#trueX, falseX dims: (5*160*n_words)
# trueX = np.array(trueX)
# falseX = np.array(falseX)
all_X = []
all_Y = []
for i in range(5):
all_X.append([])
all_Y.append([])
for x in trueX[i]:
all_X[i].append(x)
all_Y[i].append(True)
if(falseX[i]):
all_X[i].append(falseX[i].pop())
all_Y[i].append(False)
if(falseX[i]):
all_X[i].append(falseX[i])
all_Y[i].append([False]*len(falseX[i]))
with open('data/5_fold_input.pkl', 'w+b') as output_file:
cPickle.dump((all_X, all_Y), output_file)
"""
positive_true = []
positive_false = []
negative_true = []
negative_false = []
for i in range(1,6):
curdir = negative_dir_true + '/fold{}'.format(i)
for file in os.listdir(curdir):
with open(curdir + '/' + file) as f:
negative_true.append(word_tokenize(f.read()))
curdir = negative_dir_false + '/fold{}'.format(i)
for file in os.listdir(curdir):
with open(curdir + '/' + file) as f:
negative_false.append(word_tokenize(f.read()))
curdir = positive_dir_true + '/fold{}'.format(i)
for file in os.listdir(curdir):
with open(curdir + '/' + file) as f:
positive_true.append(word_tokenize(f.read()))
curdir = positive_dir_false + '/fold{}'.format(i)
for file in os.listdir(curdir):
with open(curdir + '/' + file) as f:
positive_false.append(word_tokenize(f.read()))
positive_X = []
positive_Y = []
negative_X = []
negative_Y = []
for x in positive_true:
positive_X.append(x)
positive_Y.append(True)
if(positive_false):
positive_X.append(positive_false.pop())
positive_Y.append(False)
if(positive_false):
positive_X.append(positive_false)
positive_Y.append([False]*len(positive_false))
for x in negative_true:
negative_X.append(x)
negative_Y.append(True)
if(negative_false):
negative_X.append(negative_false.pop())
negative_Y.append(False)
if(negative_false):
negative_X.append(negative_false)
negative_Y.append([False]*len(negative_false))
with open('data/polar_data.pkl', 'w+b') as output_file:
cPickle.dump((positive_X, positive_Y, negative_X, negative_Y), output_file)"""