-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
338 lines (271 loc) · 10.9 KB
/
dataset.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import torch
from torch.utils.data import Dataset
from collections import Counter
from tqdm import tqdm
class SvevaDataset(Dataset):
def __init__(self,
input_file:str,
char_len:int,
vocabulary:dict=None,
vocabulary_label:dict=None,
vocabulary_char:dict=None,
min_freq:int=1):
"""
Args:
- input_file (string): the path to the dataset to be loaded
- char_len (int): identifies the maximum size of each word
- vocabulary (dictionary): vocabulary of words
- vocabulary_label (dictionary): vocabulary of labels
- vocabulary_char (dictionary): vocabulary of chars
- min_freq (int): minimum frequency of words and char that you want to consider when the vocabulary is created
"""
self.input_file = input_file
self.char_len = char_len
self.tokens, self.labels = self.sentences_of_file(self.input_file)
self.max_len=self.find_max_len(self.tokens)
if vocabulary is None:
self.vocab = self.build_vocabulary(self.tokens,min_freq)
else:
self.vocab = vocabulary
if vocabulary_label is None:
self.vocab_label = self.build_vocabulary_label(self.labels)
else:
self.vocab_label = vocabulary_label
if vocabulary_char is None:
self.vocab_char = self.build_vocabulary_char(self.tokens,min_freq)
else:
self.vocab_char = vocabulary_char
self.char2idx = self.char2idx()
def __len__(self):
return len(self.char2idx)
def __getitem__(self, idx):
return self.char2idx[idx]
def find_max_len(self,sentences):
"""
Args:
- sentences (list of lists of words): where each list represent a sentence of the file
Return:
- max_len :the length of the longest sentence
"""
max_len = 0
for sentence in sentences:
if len(sentence)>max_len:
max_len=len(sentence)
return max_len
def char2idx(self):
"""
Return:
A list with elements inside that are dictionaries.
Each element of dictionary is composed of 3 subelements:
- inputs: encoding a sentence
- outputs: encoding of the labels corresponding to that sentence
- char: encoding of the characters of the words of that sentence
"""
vector = []
for t,l in zip(self.tokens,self.labels):
data_t =[]
data_l = []
window_t = self.create_fix_dim_sentence(t)
window_l = self.create_fix_dim_sentence(l)
chars = self.create_fix_dim_char(window_t)
encode_char = self.encode_char(chars)
encode_sentence= self.encode_input(window_t)
encode_label = self.encode_output(window_l)
item = {"inputs":torch.tensor(encode_sentence), "outputs": torch.tensor(encode_label), "char":torch.tensor(encode_char)}
vector.append(item)
return vector
def create_fix_dim_char(self,sentence):
"""
Args:
- sentence: list of words
Return:
- data: list of lists of characters.
Each list identifies the characters of that word in the sentence,
all lists have a fixed length.
If the word has a length smaller than the fixed length then I add None,
otherwise I cut it to the chosen size
"""
data =[]
for word in sentence:
build_char = []
if word is None:
for i in range(0,self.char_len):
build_char.append(None)
else:
if len(word)>self.char_len:
for i in range(self.char_len):
build_char.append(word[i])
else:
if len(word) < self.char_len:
for i in range(self.char_len):
if i>=len(word):
build_char.append(None)
else:
build_char.append(word[i])
else:
for i in range(self.char_len):
build_char.append(word[i])
assert len(build_char) == self.char_len
data.append(build_char)
return data
def create_fix_dim_sentence(self,sentence):
"""
Args:
- sentence: list of words
Return:
- data: list of words.
list identifies the words in the sentence,
but list have a fixed length.
If the sentence has a length smaller than the fixed length then I add None,
otherwise I cut it to the chosen size
"""
for i in range(0,len(sentence)):
w = sentence[:self.max_len]
if len(w) < self.max_len:
w = w + [None]*(self.max_len - len(w))
assert len(w) == self.max_len
return w
def encode_char(self,sentences):
"""
Args:
- sentences: list of lists of sentences
Return:
- all_elements: list of lists of the encoding of the chars of words in a sentence.
If the char is in the vocabulary of chars, I associate the corresponding value to it,
if the char is None I take the PADDING value,
otherwise I take the value of UNK
"""
all_elements = []
for sentence in sentences:
data = []
for c in sentence:
if c is None:
data.append(self.vocab_char['<pad>'])
elif c in self.vocab_char:
data.append(self.vocab_char[c])
else:
data.append(self.vocab_char['<unk>'])
all_elements.append(data)
return all_elements
def encode_input(self,sentence):
"""
Args:
- sentences: list of lists of sentences
Return:
- data: list of the encoding of the words in the sentence.
If the word is in the vocabulary, I associate the corresponding value to it,
if the word is None I take the PADDING value,
otherwise I take the value of UNK
"""
data =[]
for word in sentence:
if word is None:
data.append(self.vocab["<pad>"])
elif word in self.vocab:
data.append(self.vocab[word])
else:
data.append(self.vocab["<unk>"])
return data
def encode_output(self,sentence):
"""
Args:
- sentences: list of lists of sentences, each sentence contains labels
Return:
- data: list of the encoding of the labels in the sentence.
If the label is in the vocabulary of the labels, I take the corresponding value to it,
otherwise the label is None I take the PADDING value
"""
data =[]
for word in sentence:
if word in self.vocab_label:
data.append(self.vocab_label[word])
else:
data.append(self.vocab_label["<pad>"])
return data
def sentences_of_file(self,path):
"""
Args:
- path: path of file
Return:
- tokens: list of list of sentences of the file
- labels: list of list of labels of sentences of the file
"""
tokens = []
labels = []
all_tokens = []
all_labels = []
with open(path) as f:
for line in f:
line = line.strip()
if line.startswith('# '):
all_tokens = []
all_labels = []
elif line == '':
tokens.append(all_tokens)
labels.append(all_labels)
else:
_, token, label = line.split('\t')
all_tokens.append(token)
all_labels.append(label)
return tokens,labels
def build_vocabulary (self,tokens,min_freq=1):
"""
Args:
- tokens: list of lists of sentences
- min_freq: minimum frequency threshold to take a certain word
Return:
- dictionary_tok: dictionary containing all the words with minimum frequency specified before.
In which the key is the word and the value is the integer that represents it.
"""
dictionary_tok={}
counter = Counter()
for i in tqdm(range(len(tokens))):
for word in tokens[i]:
if word is not None:
counter[word]+=1
dictionary_tok.update({'<pad>': 0})
dictionary_tok.update({'<unk>': 1})
for index, (key,value) in enumerate(counter.most_common()):
if value >= min_freq:
dictionary_tok.update({key: index+2})
return dictionary_tok
def build_vocabulary_label(self,labels):
"""
Args:
- labels: list of lists of labels
Return:
- dictionary_lab: dictionary containing all the labels.
In which the key is the label and the value is the integer that represents it.
"""
dictionary_lab={}
counter_lab = Counter()
for i in tqdm(range(len(labels))):
for word in labels[i]:
if word is not None:
counter_lab[word]+=1
dictionary_lab['<pad>'] = 0
for index, (key,_) in enumerate(counter_lab.most_common()):
dictionary_lab[key] = index+1
return dictionary_lab
def build_vocabulary_char(self,tokens,min_freq=1):
"""
Args:
- tokens: list of lists of sentences
- min_freq: minimum frequency threshold to take a certain char
Return:
- dictionary_char: dictionary containing all the chars with minimum frequency specified before.
In which the key is the char and the value is the integer that represents it.
"""
dictionary_char={}
counter_char = Counter()
for i in tqdm(range(len(tokens))):
for word in tokens[i]:
if word is not None:
for c in word:
counter_char[c]+=1
dictionary_char.update({'<pad>': 0})
dictionary_char.update({'<unk>': 1})
for index, (key,value) in enumerate(counter_char.most_common()):
if value >= min_freq:
dictionary_char.update({key: index+2})
return dictionary_char