-
Notifications
You must be signed in to change notification settings - Fork 1
/
qa_data.py
194 lines (164 loc) · 7.48 KB
/
qa_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
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import gzip
import os
import re
import tarfile
import argparse
# from pycorenlp import StanfordCoreNLP
from six.moves import urllib
from tensorflow.python.platform import gfile
from tqdm import *
import numpy as np
from os.path import join as pjoin
_PAD = b"<pad>"
_SOS = b"<sos>"
_UNK = b"<unk>"
_START_VOCAB = [_PAD, _SOS, _UNK]
PAD_ID = 0
SOS_ID = 1
UNK_ID = 2
def setup_args():
parser = argparse.ArgumentParser()
code_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)))
vocab_dir = os.path.join("data", "squad")
glove_dir = os.path.join("download", "dwr")
source_dir = os.path.join("data", "squad")
parser.add_argument("--source_dir", default=source_dir)
parser.add_argument("--glove_dir", default=glove_dir)
parser.add_argument("--vocab_dir", default=vocab_dir)
parser.add_argument("--glove_dim", default=300, type=int)
parser.add_argument("--glove_crawl_size", default="840B")
parser.add_argument("--random_init", default=True, type=bool)
parser.add_argument('--include_dev', dest='include_dev', action='store_true')
parser.set_defaults(include_dev=False)
return parser.parse_args()
def basic_tokenizer(sentence):
words = []
for space_separated_fragment in sentence.strip().split():
words.extend(re.split(" ", space_separated_fragment))
return [w for w in words if w]
def initialize_vocabulary(vocabulary_path):
# map vocab to word embeddings
if gfile.Exists(vocabulary_path):
rev_vocab = []
with gfile.GFile(vocabulary_path, mode="r") as f:
rev_vocab.extend(f.readlines())
rev_vocab = [line.strip('\n') for line in rev_vocab]
vocab = dict([(x, y) for (y, x) in enumerate(rev_vocab)])
return vocab, rev_vocab
else:
raise ValueError("Vocabulary file %s not found.", vocabulary_path)
def process_glove(args, vocab_list, save_path, size=4e5, random_init=True):
"""
:param vocab_list: [vocab]
:return:
"""
if not gfile.Exists(save_path + ".npz"):
glove_path = os.path.join(args.glove_dir, "glove.{}.{}d.txt".format(args.glove_crawl_size, args.glove_dim))
if random_init:
glove = np.random.randn(len(vocab_list), args.glove_dim)
else:
glove = np.zeros((len(vocab_list), args.glove_dim))
found = 0
size = {"6B": 400000, "840B": 2196017}[args.glove_crawl_size]
with open(glove_path, 'r') as fh:
for line in tqdm(fh, total=size):
array = line.lstrip().rstrip().split(" ")
word = array[0]
vector = list(map(float, array[1:]))
if word in vocab_list:
idx = vocab_list.index(word)
glove[idx, :] = vector
found += 1
if args.glove_crawl_size == "6B":
if word.capitalize() in vocab_list:
idx = vocab_list.index(word.capitalize())
glove[idx, :] = vector
found += 1
if word.upper() in vocab_list:
idx = vocab_list.index(word.upper())
glove[idx, :] = vector
found += 1
print("{}/{} of word vocab have corresponding vectors in {}".format(found, len(vocab_list), glove_path))
np.savez_compressed(save_path, glove=glove)
print("saved trimmed glove matrix at: {}".format(save_path))
def create_vocabulary(vocabulary_path, data_paths, tokenizer=None):
if not gfile.Exists(vocabulary_path):
print("Creating vocabulary %s from data %s" % (vocabulary_path, str(data_paths)))
vocab = {}
for path in data_paths:
with open(path, mode="rb") as f:
counter = 0
for line in f:
counter += 1
if counter % 100000 == 0:
print("processing line %d" % counter)
tokens = tokenizer(line) if tokenizer else basic_tokenizer(line)
for w in tokens:
if w in vocab:
vocab[w] += 1
else:
vocab[w] = 1
vocab_list = _START_VOCAB + sorted(vocab, key=vocab.get, reverse=True)
print("Vocabulary size: %d" % len(vocab_list))
with gfile.GFile(vocabulary_path, mode="wb") as vocab_file:
for w in vocab_list:
vocab_file.write(w + b"\n")
def sentence_to_token_ids(sentence, vocabulary, tokenizer=None):
if tokenizer:
words = tokenizer(sentence)
else:
words = basic_tokenizer(sentence)
return [vocabulary.get(w, UNK_ID) for w in words]
def data_to_token_ids(data_path, target_path, vocabulary_path,
tokenizer=None):
if not gfile.Exists(target_path):
print("Tokenizing data in %s" % data_path)
vocab, _ = initialize_vocabulary(vocabulary_path)
with gfile.GFile(data_path, mode="rb") as data_file:
with gfile.GFile(target_path, mode="w") as tokens_file:
counter = 0
for line in data_file:
counter += 1
if counter % 5000 == 0:
print("tokenizing line %d" % counter)
token_ids = sentence_to_token_ids(line, vocab, tokenizer)
tokens_file.write(" ".join([str(tok) for tok in token_ids]) + "\n")
if __name__ == '__main__':
args = setup_args()
vocab_path = pjoin(args.vocab_dir, "vocab.dat")
train_path = pjoin(args.source_dir, "train")
valid_path = pjoin(args.source_dir, "val")
dev_path = pjoin(args.source_dir, "dev")
data_paths = [pjoin(args.source_dir, "train.context"),
pjoin(args.source_dir, "train.question"),
pjoin(args.source_dir, "val.context"),
pjoin(args.source_dir, "val.question")]
if args.include_dev:
data_paths += [pjoin(args.source_dir, "dev.context"),
pjoin(args.source_dir, "dev.question")]
create_vocabulary(vocab_path, data_paths,)
vocab, rev_vocab = initialize_vocabulary(pjoin(args.vocab_dir, "vocab.dat"))
# ======== Trim Distributed Word Representation =======
# If you use other word representations, you should change the code below
process_glove(args, rev_vocab, args.source_dir + "/glove.trimmed.{}.{}".format(args.glove_crawl_size, args.glove_dim),
random_init=args.random_init)
# ======== Creating Dataset =========
# We created our data files seperately
# If your model loads data differently (like in bulk)
# You should change the below code
x_train_dis_path = train_path + ".ids.context"
y_train_ids_path = train_path + ".ids.question"
data_to_token_ids(train_path + ".context", x_train_dis_path, vocab_path)
data_to_token_ids(train_path + ".question", y_train_ids_path, vocab_path)
x_dis_path = valid_path + ".ids.context"
y_ids_path = valid_path + ".ids.question"
data_to_token_ids(valid_path + ".context", x_dis_path, vocab_path)
data_to_token_ids(valid_path + ".question", y_ids_path, vocab_path)
if args.include_dev:
x_dis_path = dev_path + ".ids.context"
y_ids_path = dev_path + ".ids.question"
data_to_token_ids(dev_path + ".context", x_dis_path, vocab_path)
data_to_token_ids(dev_path + ".question", y_ids_path, vocab_path)