-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdata.py
313 lines (251 loc) · 9.17 KB
/
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
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
import pickle
import numpy as np
import gzip
MIN_FREQ = 3
def invert_dict(d):
"""
Exchanges keys and values in a dictionary
:param d: dictionary for invertion
:return: inverted dictionary
"""
res = {}
for k, v in d.items():
res[v] = k
return res
def add_to_dictionary(dictionary, key, value):
"""
Add values to a dictionary when values are lists
:param dictionary: the dictionary to be used
:param key: the key to be appended
:param value: the value to be added
:return: nothing
"""
if key not in dictionary:
dictionary[key] = [value]
else:
dictionary[key].append(value)
def increment_count(count_dict, key):
"""
Puts the key in the dictionary if does not exist or adds one if it does.
:param count_dict: dictionary for the count performed
:param key: key to add to dictionary
:return: dictionary after addition of the key
"""
if key in count_dict:
count_dict[key] += 1
else:
count_dict[key] = 1
def compute_vocab_count(sents):
"""
Takes a corpus and computes the frequency of each word's appearance
:param sents: sentences for counting frequency
:return: dictionary with count of words
"""
vocab = {}
for sent in sents:
for token in sent:
increment_count(vocab, token[0])
return vocab
def reorganize_data(texts):
"""
Reorganize data to contain tuples of a all signs combined and all trans combined
:param texts: sentences in format of tuples of (sign, tran)
:return: data reorganized
"""
data = []
for sentence in texts:
signs = []
trans = []
for sign, tran in sentence:
signs.append(sign)
trans.append(tran)
data.append((signs, trans))
return data
def from_key_to_line_number(key):
"""
Takes a key and returns the line number in it
:param key: The key to parse
:return: line number
"""
n = key.split(".", 2)[1]
# Sometimes line number contains a redundant "l" at the end ("Q005624.1l" for example), so we ignore it.
if n[-1] == "l":
n = n[:-1]
if not n.isdigit():
return -1
line_number = int(n)
return line_number
def from_key_to_text_and_line_numbers(key):
"""
Takes a key and divides it into the text, start line and end line
:param key: The key to parse
:return: text, start line and end line
"""
# Calculation of start_line and end_line for signs and transcriptions
text = key[0].split(".", 2)[0]
start_line = from_key_to_line_number(key[0])
if start_line == -1:
return text, -1, -1
# Sometimes the end line is not specified when it's one line ("n057" for example), so we use the start line.
if "." in key[1]:
end_line = from_key_to_line_number(key[1])
else:
end_line = start_line
return text, start_line, end_line
def give_idx(key, dict):
"""
Gives unique value for every new key
:param key: the key to be added to the dict
:param dict: the dictionary that will be changed
:return: nothing
"""
if key not in dict:
dict[key] = len(dict)
def rep_to_ix(data):
"""
Builds 2 dictionaries with unique values for each sign/transliteration
:param data: the data in a format of signs and transliterations
:return: the dictionaries - one for signs, one for transliterations
"""
sign_to_ix = {}
tran_to_ix = {}
for signs, trans in data:
for sign in signs:
give_idx(sign, sign_to_ix)
for tran in trans:
give_idx(tran, tran_to_ix)
return sign_to_ix, tran_to_ix
def build_tag_to_idx_dict(train_sentences):
"""
Builds dictionary with unique values for each transliteration
:param train_sentences: the data in a format of signs and transliterations
:return: the dictionary
"""
curr_tag_index = 0
tag_to_idx_dict = {}
for train_sent in train_sentences:
for token in train_sent:
tag = token[1]
if tag not in tag_to_idx_dict:
tag_to_idx_dict[tag] = curr_tag_index
curr_tag_index += 1
tag_to_idx_dict['*'] = curr_tag_index
return tag_to_idx_dict
def dump_object_to_file(object, object_name):
"""
Dumps object to a file called object_name (usually learned stuff)
:param object: learned data which will be saved
:param object_name: file name to save the object
:return: nothing
"""
with open(object_name, "wb") as output:
pickle.dump(object, output, pickle.HIGHEST_PROTOCOL)
def load_object_from_file(object_name):
"""
Loads object from a file called object_name (usually learned stuff)
:param object_name: file name to load the object from
:return: the object which was learned and saved
"""
with open(object_name, "rb") as input:
object = pickle.load(input)
return object
def logits_to_trans(tag_logits, model, id_to_tran):
"""
Builds lists of the predicted tags and their scores according to BiLSTM (3 most reasonable tags)
:param tag_logits: the tags' probabilities as predicted by BiLSTM
:param model: the model which was learned by BiLSTM
:param id_to_tran: dictionary that maps unique id to the corresponding transliteration
:return: 3 lists of the top 3 predicted tags and lists of their scores
"""
tag_ids = np.argmax(tag_logits, axis=-1)
scores = []
for i in range(len(tag_logits)):
scores.append(tag_logits[i][tag_ids[i]])
tag_logits[i][tag_ids[i]] = -1000000
tag2_ids = np.argmax(tag_logits, axis=-1)
scores2 = []
for i in range(len(tag_logits)):
scores2.append(tag_logits[i][tag2_ids[i]])
tag_logits[i][tag2_ids[i]] = -1000000
tag3_ids = np.argmax(tag_logits, axis=-1)
scores3 = []
for i in range(len(tag_logits)):
scores3.append(tag_logits[i][tag3_ids[i]])
prediction = []
for id in [model.vocab.get_token_from_index(i, 'labels') for i in tag_ids]:
tran = id_to_tran[int(id)]
prediction.append(tran)
prediction2 = []
for id in [model.vocab.get_token_from_index(i, 'labels') for i in tag2_ids]:
tran = id_to_tran[int(id)]
prediction2.append(tran)
prediction3 = []
for id in [model.vocab.get_token_from_index(i, 'labels') for i in tag3_ids]:
tran = id_to_tran[int(id)]
prediction3.append(tran)
return prediction, prediction2, prediction3, scores, scores2, scores3
def is_word_end(s):
"""
Checks if a sign finishes a word
:param s: the sign to check
:return: true if the sign finishes the word
"""
if s[-1] in "-.":
return False
return True
def compute_accuracy(texts, prediction_function, *args):
"""
Computes the accuracy of the predicted tags by the model
:param texts: the texts for the accuracy checks
:param prediction_function: the function which predicts the corresponding tag and we want to check
:*args: more arguements needed for prediction_function
:return: returns the computed accuracy with segmentation, without segmentation and the F1 score
"""
correct = 0
correct_without_segmentation = 0
total = 0
true_positives = 0
false_positives = 0
false_negatives = 0
for text in texts:
prediction = prediction_function(text, *args)
for i in range(len(prediction)):
total += 1
if text[i][1] == prediction[i]:
correct += 1
elif not is_word_end(text[i][1]) and text[i][1][:-1] == prediction[i]:
correct_without_segmentation += 1
elif not is_word_end(prediction[i]) and text[i][1] == prediction[i][:-1]:
correct_without_segmentation += 1
if is_word_end(prediction[i]) and is_word_end(text[i][1]):
true_positives += 1
if is_word_end(prediction[i]) and not is_word_end(text[i][1]):
false_positives += 1
if not is_word_end(prediction[i]) and is_word_end(text[i][1]):
false_negatives += 1
precision = float(true_positives) / (true_positives + false_positives)
recall = float(true_positives) / (true_positives + false_negatives)
F1 = (2 * precision * recall) / (precision + recall)
accuracy = float(correct) / total
accuracy_without_segmentation = float(correct + correct_without_segmentation) / total
return accuracy, accuracy_without_segmentation, F1
def compress_file(file_name):
with open(file_name, "rb") as fp:
file_data = fp.read()
bin_data = bytearray(file_data)
with gzip.open(file_name + ".gz", "wb") as f:
f.write(bin_data)
def decompress_file(gzip_name):
fp = open(gzip_name[:-len(".gz")], "wb")
with gzip.open(gzip_name, "rb") as f:
bin_data = f.read()
fp.write(bin_data)
fp.close()
def main():
# for file_name in ["bilstm_model_linux.pkl", "bilstm_model_windows.pkl", "hmm_model.pkl", "memm_model.pkl"]:
# compress_file("output\\" + file_name)
# for file_name in ["bilstm_model_linux.pkl", "bilstm_model_windows.pkl", "hmm_model.pkl", "memm_model.pkl"]:
# decompress_file("output\\" + file_name + ".gz")
pass
if __name__ == '__main__':
main()