forked from Harsh24893/EmotionRecognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_helper.py
184 lines (155 loc) · 6.19 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
import itertools
import re
from collections import Counter
import numpy as np
import pandas as pd
def clean_str(s):
"""Clean sentence"""
s = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", s)
s = re.sub(r"\'s", " \'s", s)
s = re.sub(r"\'ve", " \'ve", s)
s = re.sub(r"n\'t", " n\'t", s)
s = re.sub(r"\'re", " \'re", s)
s = re.sub(r"\'d", " \'d", s)
s = re.sub(r"\'ll", " \'ll", s)
s = re.sub(r",", " , ", s)
s = re.sub(r"!", " ! ", s)
s = re.sub(r"\(", " \( ", s)
s = re.sub(r"\)", " \) ", s)
s = re.sub(r"\?", " \? ", s)
s = re.sub(r"\s{2,}", " ", s)
s = re.sub(r'\S*(x{2,}|X{2,})\S*', "xxx", s)
s = re.sub(r'[^\x00-\x7F]+', "", s)
return s.strip().lower()
def load_data_and_labels(filename):
"""Load sentences and labels"""
df = pd.read_csv(filename)
selected = ['label', 'text']
non_selected = list(set(df.columns) - set(selected))
df = df.drop(non_selected, axis=1) # Drop non selected columns
df = df.dropna(axis=0, how='any', subset=selected) # Drop null rows
df = df.reindex(np.random.permutation(df.index)) # Shuffle the dataframe
df = df[0:100000]
print len(df)
# Map the actual labels to one hot labels
labels = sorted(list(set(df[selected[0]].tolist())))
one_hot = np.zeros((len(labels), len(labels)), int)
np.fill_diagonal(one_hot, 1)
label_dict = dict(zip(labels, one_hot))
x_raw = df[selected[1]].apply(lambda x: clean_str(x)).tolist()
y_raw = df[selected[0]].apply(lambda y: label_dict[y]).tolist()
vocabulary, vocabulary_inv = build_vocab(x_raw)
word2vec = vocab_to_word2vec("GoogleNews-vectors-negative300.bin", vocabulary)
embedding_mat = build_word_embedding_mat(word2vec, vocabulary_inv)
return x_raw, y_raw, df, labels, embedding_mat
def build_word_embedding_mat(word_vecs, vocabulary_inv, k=300):
"""
Get the word embedding matrix, of size(vocabulary_size, word_vector_size)
ith row is the embedding of ith word in vocabulary
"""
vocab_size = len(vocabulary_inv)
embedding_mat = np.zeros(shape=(9000, k), dtype='float32')
for idx in range(len(vocabulary_inv)):
embedding_mat[idx + 1] = word_vecs[vocabulary_inv[idx]]
print "Embedding matrix of size " + str(np.shape(embedding_mat))
# initialize the first row,
embedding_mat[0] = np.random.uniform(-0.25, 0.25, k)
return embedding_mat
def build_input_data(sentences, labels, vocabulary):
"""
Maps sentencs and labels to vectors based on a vocabulary.
"""
x = [[vocabulary[word] for word in sentence] for sentence in sentences]
y = np.array(labels)
return [x, y]
def build_vocab(sentences):
"""
Builds a vocabulary mapping from word to index based on the sentences.
Returns vocabulary mapping and inverse vocabulary mapping.
"""
# Build vocabulary
word_counts = Counter(itertools.chain(*sentences))
# Mapping from index to word
vocabulary_inv = [x[0] for x in word_counts.most_common()]
# Mapping from word to index
vocabulary = {x: i + 1 for i, x in enumerate(vocabulary_inv)}
return [vocabulary, vocabulary_inv]
def vocab_to_word2vec(fname, vocab, k=300):
"""
Load word2vec from Mikolov
"""
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 xrange(vocab_size):
word = []
while True:
ch = f.read(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)
print str(len(word_vecs)) + " words found in word2vec."
# add unknown words by generating random word vectors
count_missing = 0
for word in vocab:
if word not in word_vecs:
word_vecs[word] = np.random.uniform(-0.25, 0.25, k)
count_missing += 1
print str(count_missing) + " words not found, generated by random."
return word_vecs
def batch_iter(data, batch_size, num_epochs, shuffle=True):
"""Iterate the data batch by batch"""
data = np.array(data)
data_size = len(data)
num_batches_per_epoch = int(data_size / batch_size) + 1
for epoch in range(num_epochs):
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_data = data[shuffle_indices]
else:
shuffled_data = data
for batch_num in range(num_batches_per_epoch):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
yield shuffled_data[start_index:end_index]
def load_embedding_vectors(vocabulary):
# load embedding_vectors from the word2vec
filename = 'GoogleNews-vectors-negative300.bin'
encoding = 'utf-8'
with open(filename, "rb") as f:
header = f.readline()
vocab_size, vector_size = map(int, header.split())
# initial matrix with random uniform
embedding_vectors = np.random.uniform(-0.25, 0.25, (len(vocabulary), vector_size))
if True:
binary_len = np.dtype('float32').itemsize * vector_size
for line_no in range(vocab_size):
word = []
while True:
ch = f.read(1)
if ch == b' ':
break
if ch == b'':
raise EOFError("unexpected end of input; is count incorrect or file otherwise damaged?")
if ch != b'\n':
word.append(ch)
word = str(b''.join(word))
idx = vocabulary.get(word)
if idx != 0:
embedding_vectors[idx] = np.fromstring(f.read(binary_len), dtype='float32')
else:
f.seek(binary_len, 1)
f.close()
return embedding_vectors
if __name__ == '__main__':
# TODO
input_file = 'iseardataset.csv'
load_data_and_labels(input_file)