-
Notifications
You must be signed in to change notification settings - Fork 1
/
babi_data.py
130 lines (115 loc) · 4.86 KB
/
babi_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
from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader
from torch.utils.data.dataloader import default_collate
from glob import glob
import numpy as np
def pad_collate(batch):
max_context_sen_len = float('-inf')
max_context_len = float('-inf')
max_question_len = float('-inf')
for elem in batch:
context, question, _ = elem
max_context_len = max_context_len if max_context_len > len(context) else len(context)
max_question_len = max_question_len if max_question_len > len(question) else len(question)
for sen in context:
max_context_sen_len = max_context_sen_len if max_context_sen_len > len(sen) else len(sen)
max_context_len = min(max_context_len, 70)
for i, elem in enumerate(batch):
_context, question, answer = elem
_context = _context[-max_context_len:]
context = np.zeros((max_context_len, max_context_sen_len))
for j, sen in enumerate(_context):
context[j] = np.pad(sen, (0, max_context_sen_len - len(sen)), 'constant', constant_values=0)
question = np.pad(question, (0, max_question_len - len(question)), 'constant', constant_values=0)
batch[i] = (context, question, answer)
return default_collate(batch)
def get_babi_task(dpath, task_id):
fpaths = glob(f'{dpath}/qa{task_id}_*')
if not fpaths: print('No files')
for fpath in fpaths:
if 'train' in fpath:
with open(fpath, 'r') as fp:
train = fp.read()
elif 'test' in fpath:
with open (fpath, 'r') as fp:
test = fp.read()
return train, test
def get_unindexed_qa(raw):
tasks, task = [], None
lines = raw.strip().split('\n')
for i, line in enumerate(lines):
idx = int(line[0:line.find(' ')])
if idx == 1:
# context, question, answer, supporting facts
task = {'C': '','Q': '', 'A': '', 'S': ''}
count = 0
id_map = {}
line = line.strip().replace('.', ' . ')[line.find(' ')+1:]
# not a question
if line.find('?') == -1:
task['C'] += line+'<line>'
id_map[idx] = count
count += 1
else:
qidx = line.find('?')
tmp = line[qidx+1:].split('\t')
task['Q'] = line[:qidx]
task['A'] = tmp[1].strip()
task['S'] = [id_map[int(o.strip())] for o in tmp[2].split()]
tc = task.copy()
tc['C'] = tc['C'].split('<line>')[:-1]
tasks.append(tc)
return tasks
def format_sentence(sent):
return sent.lower().split()+['<EOS>']
# adapted from https://github.com/dandelin/Dynamic-memory-networks-plus-Pytorch
class BabiDataset(Dataset):
def __init__(self, dpath='/home/mark/data/datasets/nlp/babi/tasks_1-20_v1-2/en-10k', task_id=1, mode='train'):
self.vocab_path = f'dpath/babi{task_id}_vocab.pkl'
self.mode = mode
self.vocab = {'<PAD>': 0, '<EOS>': 1}
train_raw, test_raw = get_babi_task(dpath, task_id)
train = self.index_task(train_raw)
self.train = [train[i][:int(9*len(train[i])/10)] for i in range(3)]
self.valid = [train[i][int(-len(train[i])/10):] for i in range(3)]
self.test = self.index_task(test_raw)
def __len__(self):
if self.mode == 'train':
return len(self.train[0])
elif self.mode == 'valid':
return len(self.valid[0])
elif self.mode == 'test':
return len(self.test[0])
def __getitem__(self, index):
if self.mode == 'train':
contexts, questions, answers = self.train
elif self.mode == 'valid':
contexts, questions, answers = self.valid
elif self.mode == 'test':
contexts, questions, answers = self.test
return contexts[index], questions[index], answers[index]
def set_mode(self, mode):
self.mode = mode
def build_vocab(self, token):
if token not in self.vocab:
self.vocab[token] = len(self.vocab)
def index_task(self, raw):
unindexed = get_unindexed_qa(raw)
contexts, questions, answers = [], [], []
for qa in unindexed:
# context
context = [format_sentence(c) for c in qa['C']]
for c in context:
for token in c: self.build_vocab(token)
context = [[self.vocab[token] for token in sent] for sent in context]
#question
question = format_sentence(qa['Q'])
for token in question: self.build_vocab(token)
question = [self.vocab[token] for token in question]
# anwer
self.build_vocab(qa['A'].lower())
anwer = self.vocab[qa['A'].lower()]
contexts.append(context)
questions.append(question)
answers.append(anwer)
return (contexts, questions, answers)