forked from clairett/pytorch-sentiment-classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_helper.py
208 lines (171 loc) · 6.11 KB
/
data_helper.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import csv
import re
import numpy as np
import torch
from torch.autograd import Variable
import torchtext.datasets as datasets
import os
max_sequence_len = 500
SEED = 1
def prepare_sequence(seq, word_to_ix, cuda=False):
var = Variable(torch.LongTensor([word_to_ix[w] for w in seq]))
return var
def prepare_label(label, cuda=False):
var = Variable(torch.LongTensor([label]))
return var
def load_bin_vec(fname, vocab):
"""
Loads 300x1 word vecs from Google (Mikolov) word2vec
"""
word_vecs = {}
with open(fname, "rb") as f:
header = f.readline()
vocab_size, layer1_size = map(int, header.split())
binary_len = np.dtype('float32').itemsize * layer1_size
for line in range(vocab_size):
word = []
while True:
ch = f.read(1).decode('latin-1')
if ch == ' ':
word = ''.join(word)
break
if ch != '\n':
word.append(ch)
if word in vocab:
word_vecs[word] = np.fromstring(f.read(binary_len), dtype='float32')
else:
f.read(binary_len)
return word_vecs
def line_to_words(line):
clean_line = clean_str_sst(line.strip())
words = clean_line.split(' ')
return words
def get_vocab(file_list):
max_sent_len = 0
word_to_idx = {}
idx = 0
for filename in file_list:
f = open(filename, "r")
for line in f:
words = line_to_words(line)
max_sent_len = max(max_sent_len, len(words))
for word in words:
if not word in word_to_idx:
word_to_idx[word] = idx
idx += 1
f.close()
return max_sent_len, word_to_idx
def convert_to_tsv(train_name, dev_name, test_name):
f_names = [train_name]
if not test_name == '': f_names.append(test_name)
if not dev_name == '': f_names.append(dev_name)
train, dev, test, data = [], [], [], []
train_out, dev_out, test_out = open('./data/SST2_TSV/'+os.path.basename(train_name).split('.')[-1]+'.tsv', 'w'), \
open('./data/SST2_TSV/' + os.path.basename(dev_name).split('.')[-1] + '.tsv', 'w'),\
open('./data/SST2_TSV/' + os.path.basename(test_name).split('.')[-1] + '.tsv', 'w')
files = []
f_train = open(train_name, 'r')
files.append(f_train)
data.append(train)
if not test == '':
f_test = open(test_name, 'r')
files.append(f_test)
data.append(test)
if not dev == '':
f_dev = open(dev_name, 'r')
files.append(f_dev)
data.append(dev)
for d, f in zip(data, files):
for line in f:
words = line_to_words(line)
sent = " ".join(words[1:]) + '\t' + words[0]
d.append(sent)
f_train.close()
if not test_name == '':
f_test.close()
if not dev_name == '':
f_dev.close()
train_out.write('\n'.join(train))
dev_out.write('\n'.join(dev))
test_out.write('\n'.join(test))
return train, dev, test
def load_data(train_name, dev_name, test_name):
print('loading SST2 data for training and evaluation')
f_names = [train_name]
if not test_name == '': f_names.append(test_name)
if not dev_name == '': f_names.append(dev_name)
max_sent_len, word_to_idx = get_vocab(f_names)
train, dev, test, data = [], [], [], []
files = []
f_train = open(train_name, 'r')
files.append(f_train)
data.append(train)
if not test == '':
f_test = open(test_name, 'r')
files.append(f_test)
data.append(test)
if not dev == '':
f_dev = open(dev_name, 'r')
files.append(f_dev)
data.append(dev)
for d, f in zip(data, files):
for line in f:
words = line_to_words(line)
if len(words) > max_sent_len:
sent = sent[:max_sent_len]
d.append(words)
print('train:', len(train), 'dev:', len(dev), 'test:', len(test))
f_train.close()
if not test_name == '':
f_test.close()
if not dev_name == '':
f_dev.close()
return train, dev, test, word_to_idx
def clean_str_sst(string):
"""
Tokenization/string cleaning for the SST dataset
"""
string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower()
def load_qualtrics_gold(infile):
outfile, content = 'qualtrics_gold_standard.test', ''
writer = open(outfile, 'w')
num_of_sentence, pos_sents, neg_sents = 0, 0, 0
with open(infile, newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
# check if the row is empty
if row[0] != '' and row[0] != 'Verbatim':
num_of_sentence += 1
if row[1] == 'pos':
content += '1 ' + clean_str(row[0]) + '\n'
pos_sents += 1
elif row[1] == 'neg':
content += '0 ' + clean_str(row[0]) + '\n'
neg_sents += 1
print(num_of_sentence)
print("Pos sents: %d" % pos_sents)
print("Neg sents: %d" % neg_sents)
writer.write(content.strip())
def clean_str(string):
"""
Tokenization/string cleaning for all datasets except for SST.
"""
string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
string = re.sub(r"\'s", " \'s", string)
string = re.sub(r"\'ve", " \'ve", string)
string = re.sub(r"n\'t", " n\'t", string)
string = re.sub(r"\'re", " \'re", string)
string = re.sub(r"\'d", " \'d", string)
string = re.sub(r"\'ll", " \'ll", string)
string = re.sub(r",", " , ", string)
string = re.sub(r"!", " ! ", string)
string = re.sub(r"\(", " ( ", string)
string = re.sub(r"\)", " ) ", string)
string = re.sub(r"\?", " ? ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower()
if __name__ == '__main__':
train, dev, test = convert_to_tsv('data/SST2/stsa.binary.train', 'data/SST2/stsa.binary.dev', 'data/SST2/stsa.binary.test')
print(train)