-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·283 lines (229 loc) · 8.31 KB
/
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
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
import json
import pickle
from typing import Optional
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.pipeline import Pipeline
from sklearn.linear_model import SGDClassifier
import torch
import torch.distributed as dist
from transformers import BertTokenizer, PreTrainedTokenizer
from unidecode import unidecode
class IntentClassifier:
def __init__(self, intent_labels=[]):
self.clf = self._create_model()
self.labels = intent_labels
self._savepath = "models/intentclf.pickle"
@staticmethod
def _create_model():
clf = Pipeline([('vect', CountVectorizer()),
('tfidf', TfidfTransformer()),
('clf', SGDClassifier())
])
return clf
def save(self):
outfile = open(self._savepath, 'wb')
pickle.dump([self.clf, self.labels], outfile)
outfile.close()
def load(self):
infile = open(self._savepath, 'rb')
clf, labels = pickle.load(infile)
self.clf = clf
self.labels = labels
infile.close()
def get_intent_labels(self):
return self.labels
def train(self, train_x, train_y):
self.clf = self.clf.fit(train_x, train_y)
def predict(self, test_x):
return self.clf.predict(test_x)
class OneHotEncoder:
def __init__(self, classes: list):
self.classes = classes
def encode(self, outputs):
encoded_outputs = []
for row in outputs:
encoded_row = [0] * len(self.classes)
for idx, label in enumerate(self.classes):
if label in row:
encoded_row[idx] = 1
encoded_outputs.append(encoded_row)
return encoded_outputs
class WordLabelMapper:
tokenizer: Optional[BertTokenizer]
spcl_char: str
unk_token: str
def __init__(self, tokenizer:PreTrainedTokenizer=None):
self.tokenizer = tokenizer
self.spcl_char = "#"
self.unk_token = "[UNK]"
@staticmethod
def verify_strings(str1, str2) -> bool:
if str1 == str2:
return True
else:
str2 = unidecode(str2)
if str1 == str2:
return True
return False
def tokenize_word_with_limits(self, sentence:str)-> (list, list):
tokenized = self.tokenizer.tokenize(sentence)
copied = sentence.lower()
words = list()
limits = list()
start: int = 0
for token in tokenized:
clean_token = token.replace(self.spcl_char, "")
c = len(clean_token)
# unknown token should be of 1 length for all cases
if clean_token == self.unk_token:
c = 1
idx = 0
for i in range(len(copied)):
if copied[i] == " ":
continue
else:
idx = i
break
token_start = idx
token_end = idx + c
# no need to verify in case of unknown token
if clean_token != self.unk_token:
verified = self.verify_strings(clean_token, copied[token_start:token_end])
else:
verified = True
if verified:
limits.append([start+token_start, start+token_end - 1])
words.append(token)
copied = copied[token_end:]
start += token_end
else:
print(sentence)
print(tokenized)
print(token)
print(copied)
raise ValueError('tokenization error.')
assert len(words) == len(limits)
return words, limits
@staticmethod
def get_word_with_limits(sentence: str) -> (list, list):
words = list()
limits = list()
start: int = 0
for c in range(0, len(sentence)):
if sentence[c] == " ":
word = sentence[start:c]
words.append(word)
limits.append([start, c - 1])
start = c + 1
elif c == len(sentence) - 1:
word = sentence[start:c+1]
words.append(word)
limits.append([start, c])
return words, limits
@staticmethod
def get_word_limits(sentence: str) -> list:
limits = list()
start: int = 0
for c in range(0, len(sentence)):
if sentence[c] == " ":
limits.append([start, c-1])
start = c + 1
elif c == len(sentence)-1:
limits.append([start, c])
return limits
@staticmethod
def check_word_limit_with_label(limit: list, labels: dict) -> str:
for key in labels.keys():
position = labels[key]
if len(position) > 1 and len(limit) > 1:
if limit[0] == position[0] and limit[1] == position[1]:
return 'B-' + key.upper()
elif limit[0] == position[0] and limit[1] < position[1]:
return 'B-' + key.upper()
elif limit[0] > position[0] and limit[1] <= position[1]:
return 'I-' + key.upper()
return 'O'
def compute_sentence_labels(self, sentence: str, labels: dict) -> (list, list):
"""
separate words using space and label each word from labels dictionary
where all slots with positions are in the dictionary
:param sentence: string with many words
:param labels: dictionary of labels with their respective positions
:return: list of words and respective tags on a different list
"""
if self.tokenizer:
words, limits = self.tokenize_word_with_limits(sentence)
else:
words, limits = self.get_word_with_limits(sentence)
new_words = list()
tags = list()
for i in range(len(limits)):
if words[i] != '':
label = self.check_word_limit_with_label(limits[i], labels)
new_words.append(words[i])
tags.append(label)
return new_words, tags
def import_data(file_path:str, limit=-1, tokenizer:PreTrainedTokenizer=None):
with open(file_path, "r") as read_file:
json_data = json.load(read_file)
mapper = WordLabelMapper(tokenizer=tokenizer)
sentences, labels = list(), list()
idx = 0
max_sentence_len = 0
for key in json_data.keys():
data = json_data[key]
words, tagged = mapper.compute_sentence_labels(data['text'], data['positions'])
intent = data['intent']
if len(words) > max_sentence_len:
max_sentence_len = len(words)
sentences.append(words)
labels.append((intent, tagged))
if limit != -1 and idx >= limit:
break
idx += 1
print('Imported data with a maximum sentence length of: {}'.format(max_sentence_len))
return sentences, labels
def import_intent_data(file_path: str, pre_intent_labels: list = None):
with open(file_path, "r") as read_file:
json_data = json.load(read_file)
sentences = list()
intent = list()
intent_labels = set()
for key in json_data.keys():
data = json_data[key]
sentences.append(data["text"])
label = data['intent']
intent.append(label)
intent_labels.add(label)
if pre_intent_labels is not None:
intent_labels = pre_intent_labels
else:
intent_labels = list(intent_labels)
intent_trans = [intent_labels.index(l) for l in intent]
return sentences, intent_trans, intent_labels
def import_dev_data(file_path: str):
with open(file_path, "r") as read_file:
json_data = json.load(read_file)
sentences = list()
for key in json_data.keys():
data = json_data[key]
sentence = data["text"]
# tokens = sentence.split()
sentences.append(sentence)
return sentences
def is_dist_avail_and_initialized():
if not dist.is_available():
return False
if not dist.is_initialized():
return False
return True
def get_rank():
if not is_dist_avail_and_initialized():
return 0
return dist.get_rank()
def is_main_process():
return get_rank() == 0
def save_on_master(*args, **kwargs):
if is_main_process():
torch.save(*args, **kwargs)