-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_utils.py
211 lines (163 loc) · 6.32 KB
/
data_utils.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
209
210
211
from __future__ import absolute_import
import logging
import os
import re
import numpy as np
def load_task(data_dir, task_id, only_supporting=False, load_test=False):
'''Load the nth task.
Returns a tuple containing the training and testing data for the task.
'''
files = os.listdir(data_dir)
files = [os.path.join(data_dir, f) for f in files]
if load_test:
test_files = [f for f in files if 'test' in f] # load all test files
logging.info("Loading test from %s..." % str(test_files))
test_data = {f: get_stories(f, only_supporting) for f in test_files}
return test_data
else:
s = 'qa{}_'.format(task_id)
train_file = [f for f in files if s in f and 'train' in f][0]
logging.info("Loading train from %s..." % train_file)
train_data = get_stories(train_file, only_supporting)
return train_data
def tokenize(sent):
'''Return the tokens of a sentence including punctuation.
>>> tokenize('Bob dropped the apple. Where is the apple?')
['Bob', 'dropped', 'the', 'apple', '.', 'Where', 'is', 'the', 'apple', '?']
'''
return [x.strip() for x in re.split('(\W+)?', sent) if x.strip()]
def parse_stories(lines, only_supporting=False):
'''Parse stories provided in the bAbI tasks format
If only_supporting is true, only the sentences that support the answer are kept.
'''
data = []
story = []
for line in lines:
line = str.lower(line)
nid, line = line.split(' ', 1)
nid = int(nid)
if nid == 1:
story = []
substory_counter = 0
if '?' in line: # question
if not story:
raise Exception
q, a, supporting = line.split('\t')
supporting = map(int, supporting.split())
# Remove question marks
q = q.replace("?", "")
q = tokenize(q)
# Answer is one vocab word even if it's actually multiple words
a = [a]
substory = None
if only_supporting:
# Only select the related substory
substory = [story[i - 1] for i in supporting]
else:
# Provide all the substories
substory = [x for x in story if x]
# Extract the observer information
substory, observers = zip(*substory)
supporting = [x - substory_counter for x in supporting]
data.append((substory, observers, q, a, supporting))
substory_counter += 1
story.append('')
else: # story line
# Check for observer labels
try:
line, observers = line.split('\t')
observers = map(int, observers.split())
except ValueError:
observers = None
# remove periods
sent = tokenize(line)
if sent[-1] == ".":
sent = sent[:-1]
story.append((sent, observers))
return data
def get_stories(f, only_supporting=False):
'''Given a file name, read the file, retrieve the stories, and then convert the sentences into a single story.
If max_length is supplied, any stories longer than max_length tokens will be discarded.
'''
with open(f) as f:
return parse_stories(f.readlines(), only_supporting=only_supporting)
def vectorize_data(data, word_idx, sentence_size, memory_size, num_caches):
"""
Vectorize stories and queries.
If a sentence length < sentence_size, the sentence will be padded with 0's.
If a story length < memory_size, the story will be padded with empty memories.
Empty memories are 1-D arrays of length sentence_size filled with 0's.
The answer array is returned as a one-hot encoding.
"""
S = []
O = []
Q = []
A = []
L = []
for story, observers, query, answer, support in data:
# STORY LINES
ss = []
for i, sentence in enumerate(story, 1):
ls = max(0, sentence_size - len(sentence))
ss.append([word_idx[w] for w in sentence] + [0] * ls)
# take only the most recent sentences that fit in memory
ss = ss[::-1][:memory_size][::-1]
# pad to memory_size
lm = max(0, memory_size - len(ss))
for _ in range(lm):
ss.append([0] * sentence_size)
# OBSERVER FLAGS
observer_flag_unused = False
observer_flag_present = False
observers = observers[::-1][:memory_size][::-1]
o = np.zeros((memory_size, num_caches))
for i, x in enumerate(observers):
o[i, 0] = 1 # the oracle observer
if x is not None:
for j in x:
assert j > 0
try:
assert j < num_caches
o[i, j] = 1
observer_flag_present = True
except AssertionError:
observer_flag_unused = True
if num_caches > 1 and not observer_flag_present:
logging.debug('Observer flags not present but number of caches > 1.')
o = np.ones((memory_size, num_caches))
observer_flag_unused = False
if observer_flag_unused:
logging.debug('Observer flags present but unused.')
o = np.ones((memory_size, num_caches))
# QUERIES
lq = max(0, sentence_size - len(query))
q = [word_idx[w] for w in query] + [0] * lq
# ANSWERS
y = np.zeros(len(word_idx) + 1) # 0 is reserved for nil word
for a in answer:
y[word_idx[a]] = 1
# SUPPORTING SENTENCES
l = np.zeros(memory_size)
for supp in support:
supp = memory_size+1 # TODO: remove hack
if supp <= memory_size:
try:
l[supp - 1] = 1
except Error:
pass
S.append(ss)
O.append(o)
Q.append(q)
A.append(y)
L.append(l)
return np.array(S), np.array(O), np.array(Q), np.array(A), np.array(L)
def list_to_path(lst):
"""
Converts a list into a decent looking
string for a filename. Removes pesky /
and \ charachters.
"""
lst = [str(x) for x in lst]
s = "_".join(lst)
s = s.replace("/", "").replace("\\", "").replace("[","").replace("]", "")
return s