-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_utils.py
More file actions
276 lines (196 loc) · 8.1 KB
/
data_utils.py
File metadata and controls
276 lines (196 loc) · 8.1 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import numpy as np
from tqdm import *
import random
import re
from preprocessing import pickle_call, pickle_dump
_PAD = b"_PAD"
_GO = b"_GO"
_EOS = b"_EOS"
_UNK = b"_UNK"
_START_VOCAB = [_PAD, _GO, _EOS, _UNK]
PAD_ID = 2
GO_ID = 1
EOS_ID = 0
UNK_ID = 3
def tokenize(text, lower= False, numbers=False):
punctuation = "!\"#$%&\\'()*+,\-./:;<=>?@[\]\^\`\{\|\}~\"'\xe2\x80\x99"
mentions = list(re.findall("\s*([" + punctuation + "]|[^" + punctuation + "\s]*)\s*", text))
if lower or numbers:
new_mentions = []
for mention in mentions:
if lower:
mention = mention.lower()
if numbers:
if mention.isdigit():
mention = '0'
new_mentions.append(mention)
mentions = new_mentions
return mentions
def has_numbers(inputString):
return bool(re.search(r'\d', inputString))
def has_letters(inputString):
return bool(re.search(r'[a-zA-Z]', inputString))
def has_letters_and_numbers(inputString):
return has_numbers(inputString) and has_letters(inputString)
def sentences_train_test_validation_splitting_and_paragraph_generation(embeddings_mentions,file_name, splitting=(70,20,10)):
# embeddings_mentions = model.embeddings_mentions
paragraphs = pickle_call(file_name + '.pickle')
if paragraphs is None:
splitting = np.array(splitting)
splitting[1] += splitting[0]
splitting[2] += splitting[1]
splitting_counter = splitting[2]
train_sentences = []
test_sentences = []
validation_sentences = []
with open(file_name, 'r') as f:
for line in f:
rand_int = random.randint(1, splitting_counter)
line_tokens = line.split()
token = []
for word in line_tokens:
try:
token.append(embeddings_mentions[word])
except:
pass
if rand_int <= splitting[0]:
train_sentences.append(token)
elif rand_int <= splitting[1]:
test_sentences.append(token)
else:
validation_sentences.append(token)
paragraphs = [[],[],[]]
for i, sentences in enumerate([train_sentences, test_sentences, validation_sentences]):
sentences_length = len(sentences)
for j in tqdm(xrange(len(sentences) * 10 )):
nr_sentences = random.randint(1, 10)
batch_sentence = []
for k in xrange(nr_sentences):
sentence = random.randint(0, sentences_length - 1)
batch_sentence += sentences[sentence]
paragraphs[i].append(batch_sentence)
pickle_dump(file_name + '.pickle', paragraphs)
return paragraphs
def sentence_to_token_ids(text, embeddings_mentions):
tokens = []
tok = tokenize(text, True, True)
for ment in tok:
try:
tokens.append(embeddings_mentions[ment])
except:
pass
return tokens
class LogFileWriter:
def __init__(self, filename):
self.filename = filename
self.open = False
self.file = open(filename, 'a')
def __open__(self):
self.file = open(self.filename, 'a')
def append_text(self, text):
if not self.open:
self.__open__()
self.file.write(text + "\n")
self.close()
def close(self):
self.file.close()
def toy_text_generator(batch_size, buckets, data_set):
data_sizes = []
bucket_sizes = []
bucket_position = []
max_len = buckets[-1][0]
batch = []
data_buckets = [[] for _ in buckets]
nr_docs = 0
for data in data_set:
for i, size in enumerate(buckets):
if len(data) < buckets[i][0]:
data_buckets[i].append(data)
break
for data_ in data_buckets:
length = len(data_)
nr_docs += length
data_sizes.append(nr_docs)
bucket_sizes.append(length)
random.shuffle(data_)
bucket_position.append(0)
while True:
if bucket_sizes == bucket_position:
yield None, None
break
if batch == []:
while True:
rand_int = random.randint(1, nr_docs)
for bucket_nr, size in enumerate(data_sizes):
if rand_int < size:
break
if bucket_sizes[bucket_nr] != bucket_position[bucket_nr]:
break
doc_position = bucket_position[bucket_nr]
token = data_buckets[bucket_nr][doc_position]
bucket_position[bucket_nr] += 1
length = len(token)
if length > max_len:
length = max_len
token = token[:length]
batch.append(token + [EOS_ID])
if (len(batch) == batch_size) or (bucket_sizes[bucket_nr] == bucket_position[bucket_nr]):
yield batch, bucket_nr
batch = []
def get_embedding_data(location, vocab_size, all_entities=False):
pickle_file = 'data/embedding_data/embedding_data4.pickle'
embedding_data = pickle_call(pickle_file)
if embedding_data is None:
embedding_data = {}
# location = config.location
embeddings_mentions = {}
embeddings_mentions_list = []
embeddings = []
print("retrieving embeddings from file")
i = 0
with open(location, 'r') as f:
for line in tqdm((f)):
add = False
if i != 0:
chunk = line.split(' ')
mention = chunk[0]
embedding = np.append(np.array([0.0, 0.0], dtype=np.float64),
np.array([float(j) for j in chunk[1:]], dtype=np.float64))
if i == 1:
eol = np.zeros(len(embedding), dtype=np.float64)
eol[0] = 1.0
start = np.zeros(len(embedding), dtype=np.float64)
start[1] = 1.0
pad = np.zeros(len(embedding), dtype=np.float64)
embeddings.append(eol)
embeddings.append(start)
embeddings.append(pad)
embeddings_mentions['!EOL!'] = 0
embeddings_mentions['!START!'] = 1
embeddings_mentions['!PAD!'] = 2
embeddings_mentions_list.append('!EOL!')
embeddings_mentions_list.append('!START!')
embeddings_mentions_list.append('!PAD!')
if i <= vocab_size:
embeddings_mentions_list.append(mention)
embeddings_mentions[mention] = i + 2
embeddings.append(embedding)
add = True
if i == 0: add = True
if add:
i += 1
if (i == vocab_size and not all_entities):
break
vocab_size = len(embeddings_mentions)
embedding_dim = len(embeddings[0])
embeddings_mentions_list = embeddings_mentions_list
embeddings_mentions = embeddings_mentions
embeddings_np = np.array(embeddings)
print(str(vocab_size) + ' embeddings retrieved')
embedding_data['vocab_size'] = vocab_size
embedding_data['embedding_dim'] = embedding_dim
embedding_data['embeddings_mentions_list'] = embeddings_mentions_list
embedding_data['embeddings_mentions'] = embeddings_mentions
embedding_data['embeddings_np'] = embeddings_np
pickle_dump(pickle_file, embedding_data)
return embedding_data['vocab_size'], embedding_data['embedding_dim'], embedding_data['embeddings_mentions_list'], embedding_data['embeddings_mentions'], embedding_data['embeddings_np']