forked from v-mipeng/LexiconAugmentedNER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
316 lines (275 loc) · 13.3 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
# -*- coding: utf-8 -*-
import sys
import numpy as np
from utils.alphabet import Alphabet
from utils.functions import *
from utils.gazetteer import Gazetteer
START = "</s>"
UNKNOWN = "</unk>"
PADDING = "</pad>"
NULLKEY = "-null-"
class Data:
def __init__(self):
self.MAX_SENTENCE_LENGTH = 250
self.MAX_WORD_LENGTH = -1
self.number_normalized = True
self.norm_word_emb = True
self.norm_biword_emb = True
self.norm_gaz_emb = False
self.word_alphabet = Alphabet('word')
self.biword_alphabet = Alphabet('biword')
self.char_alphabet = Alphabet('character')
self.label_alphabet = Alphabet('label', True)
self.gaz_lower = False
self.gaz = Gazetteer(self.gaz_lower)
self.gaz_alphabet = Alphabet('gaz')
self.gaz_count = {}
self.gaz_split = {}
self.biword_count = {}
self.HP_fix_gaz_emb = False
self.HP_use_gaz = True
self.HP_use_count = False
self.tagScheme = "NoSeg"
self.char_features = "LSTM"
self.train_texts = []
self.dev_texts = []
self.test_texts = []
self.raw_texts = []
self.train_Ids = []
self.dev_Ids = []
self.test_Ids = []
self.raw_Ids = []
self.train_split_index = []
self.dev_split_index = []
self.use_bigram = True
self.word_emb_dim = 50
self.biword_emb_dim = 50
self.char_emb_dim = 30
self.gaz_emb_dim = 50
self.gaz_dropout = 0.5
self.pretrain_word_embedding = None
self.pretrain_biword_embedding = None
self.pretrain_gaz_embedding = None
self.label_size = 0
self.word_alphabet_size = 0
self.biword_alphabet_size = 0
self.char_alphabet_size = 0
self.label_alphabet_size = 0
### hyperparameters
self.HP_iteration = 100
self.HP_batch_size = 10
self.HP_char_hidden_dim = 50
self.HP_hidden_dim = 128
self.HP_dropout = 0.5
self.HP_lstm_layer = 1
self.HP_bilstm = True
self.HP_use_char = False
self.HP_gpu = True
self.HP_lr = 0.015
self.HP_lr_decay = 0.05
self.HP_clip = 5.0
self.HP_momentum = 0
self.HP_num_layer = 4
def show_data_summary(self):
print("DATA SUMMARY START:")
print(" Tag scheme: %s"%(self.tagScheme))
print(" MAX SENTENCE LENGTH: %s"%(self.MAX_SENTENCE_LENGTH))
print(" MAX WORD LENGTH: %s"%(self.MAX_WORD_LENGTH))
print(" Number normalized: %s"%(self.number_normalized))
print(" Use bigram: %s"%(self.use_bigram))
print(" Word alphabet size: %s"%(self.word_alphabet_size))
print(" Biword alphabet size: %s"%(self.biword_alphabet_size))
print(" Char alphabet size: %s"%(self.char_alphabet_size))
print(" Gaz alphabet size: %s"%(self.gaz_alphabet.size()))
print(" Label alphabet size: %s"%(self.label_alphabet_size))
print(" Word embedding size: %s"%(self.word_emb_dim))
print(" Biword embedding size: %s"%(self.biword_emb_dim))
print(" Char embedding size: %s"%(self.char_emb_dim))
print(" Gaz embedding size: %s"%(self.gaz_emb_dim))
print(" Norm word emb: %s"%(self.norm_word_emb))
print(" Norm biword emb: %s"%(self.norm_biword_emb))
print(" Norm gaz emb: %s"%(self.norm_gaz_emb))
print(" Norm gaz dropout: %s"%(self.gaz_dropout))
print(" Train instance number: %s"%(len(self.train_texts)))
print(" Dev instance number: %s"%(len(self.dev_texts)))
print(" Test instance number: %s"%(len(self.test_texts)))
print(" Raw instance number: %s"%(len(self.raw_texts)))
print(" Hyperpara iteration: %s"%(self.HP_iteration))
print(" Hyperpara batch size: %s"%(self.HP_batch_size))
print(" Hyperpara lr: %s"%(self.HP_lr))
print(" Hyperpara lr_decay: %s"%(self.HP_lr_decay))
print(" Hyperpara HP_clip: %s"%(self.HP_clip))
print(" Hyperpara momentum: %s"%(self.HP_momentum))
print(" Hyperpara hidden_dim: %s"%(self.HP_hidden_dim))
print(" Hyperpara dropout: %s"%(self.HP_dropout))
print(" Hyperpara lstm_layer: %s"%(self.HP_lstm_layer))
print(" Hyperpara bilstm: %s"%(self.HP_bilstm))
print(" Hyperpara GPU: %s"%(self.HP_gpu))
print(" Hyperpara use_gaz: %s"%(self.HP_use_gaz))
print(" Hyperpara fix gaz emb: %s"%(self.HP_fix_gaz_emb))
print(" Hyperpara use_char: %s"%(self.HP_use_char))
if self.HP_use_char:
print(" Char_features: %s"%(self.char_features))
print("DATA SUMMARY END.")
sys.stdout.flush()
def refresh_label_alphabet(self, input_file):
old_size = self.label_alphabet_size
self.label_alphabet.clear(True)
in_lines = open(input_file,'r',encoding="utf-8").readlines()
for line in in_lines:
if len(line) > 2:
pairs = line.strip().split()
label = pairs[-1]
self.label_alphabet.add(label)
self.label_alphabet_size = self.label_alphabet.size()
startS = False
startB = False
for label,_ in self.label_alphabet.iteritems():
if "S-" in label.upper():
startS = True
elif "B-" in label.upper():
startB = True
if startB:
if startS:
self.tagScheme = "BMES"
else:
self.tagScheme = "BIO"
self.fix_alphabet()
print("Refresh label alphabet finished: old:%s -> new:%s"%(old_size, self.label_alphabet_size))
def build_alphabet(self, input_file):
in_lines = open(input_file,'r',encoding="utf-8").readlines()
seqlen = 0
for idx in range(len(in_lines)):
line = in_lines[idx]
if len(line) > 2:
pairs = line.strip().split()
word = pairs[0]
if self.number_normalized:
word = normalize_word(word)
label = pairs[-1]
self.label_alphabet.add(label)
self.word_alphabet.add(word)
if idx < len(in_lines) - 1 and len(in_lines[idx+1]) > 2:
biword = word + in_lines[idx+1].strip().split()[0]
else:
biword = word + NULLKEY
self.biword_alphabet.add(biword)
# biword_index = self.biword_alphabet.get_index(biword)
self.biword_count[biword] = self.biword_count.get(biword,0) + 1
for char in word:
self.char_alphabet.add(char)
seqlen += 1
else:
seqlen = 0
self.word_alphabet_size = self.word_alphabet.size()
self.biword_alphabet_size = self.biword_alphabet.size()
self.char_alphabet_size = self.char_alphabet.size()
self.label_alphabet_size = self.label_alphabet.size()
startS = False
startB = False
for label,_ in self.label_alphabet.iteritems():
if "S-" in label.upper():
startS = True
elif "B-" in label.upper():
startB = True
if startB:
if startS:
self.tagScheme = "BMES"
else:
self.tagScheme = "BIO"
def build_gaz_file(self, gaz_file):
## build gaz file,initial read gaz embedding file
if gaz_file:
fins = open(gaz_file, 'r',encoding="utf-8").readlines()
for fin in fins:
fin = fin.strip().split()[0]
if fin:
self.gaz.insert(fin, "one_source")
print ("Load gaz file: ", gaz_file, " total size:", self.gaz.size())
else:
print ("Gaz file is None, load nothing")
def build_gaz_alphabet(self, input_file, count=False):
in_lines = open(input_file,'r',encoding="utf-8").readlines()
word_list = []
for line in in_lines:
if len(line) > 3:
word = line.split()[0]
if self.number_normalized:
word = normalize_word(word)
word_list.append(word)
else:
w_length = len(word_list)
entitys = []
for idx in range(w_length):
matched_entity = self.gaz.enumerateMatchList(word_list[idx:])
entitys += matched_entity
for entity in matched_entity:
# print entity, self.gaz.searchId(entity),self.gaz.searchType(entity)
self.gaz_alphabet.add(entity)
index = self.gaz_alphabet.get_index(entity)
self.gaz_count[index] = self.gaz_count.get(index,0) ## initialize gaz count
if count:
entitys.sort(key=lambda x:-len(x))
while entitys:
longest = entitys[0]
longest_index = self.gaz_alphabet.get_index(longest)
self.gaz_count[longest_index] = self.gaz_count.get(longest_index, 0) + 1
gazlen = len(longest)
for i in range(gazlen):
for j in range(i+1,gazlen+1):
covering_gaz = longest[i:j]
if covering_gaz in entitys:
entitys.remove(covering_gaz)
# print('remove:',covering_gaz)
word_list = []
print("gaz alphabet size:", self.gaz_alphabet.size())
def fix_alphabet(self):
self.word_alphabet.close()
self.biword_alphabet.close()
self.char_alphabet.close()
self.label_alphabet.close()
self.gaz_alphabet.close()
def build_word_pretrain_emb(self, emb_path):
print ("build word pretrain emb...")
self.pretrain_word_embedding, self.word_emb_dim = build_pretrain_embedding(emb_path, self.word_alphabet, self.word_emb_dim, self.norm_word_emb)
def build_biword_pretrain_emb(self, emb_path):
print ("build biword pretrain emb...")
self.pretrain_biword_embedding, self.biword_emb_dim = build_pretrain_embedding(emb_path, self.biword_alphabet, self.biword_emb_dim, self.norm_biword_emb)
def build_gaz_pretrain_emb(self, emb_path):
print ("build gaz pretrain emb...")
self.pretrain_gaz_embedding, self.gaz_emb_dim = build_pretrain_embedding(emb_path, self.gaz_alphabet, self.gaz_emb_dim, self.norm_gaz_emb)
def generate_instance_with_gaz(self, input_file, name):
self.fix_alphabet()
if name == "train":
self.train_texts, self.train_Ids = read_instance_with_gaz(self.HP_num_layer, input_file, self.gaz, self.word_alphabet, self.biword_alphabet, self.biword_count, self.char_alphabet, self.gaz_alphabet, self.gaz_count, self.gaz_split, self.label_alphabet, self.number_normalized, self.MAX_SENTENCE_LENGTH)
elif name == "dev":
self.dev_texts, self.dev_Ids = read_instance_with_gaz(self.HP_num_layer, input_file, self.gaz,self.word_alphabet, self.biword_alphabet, self.biword_count, self.char_alphabet, self.gaz_alphabet, self.gaz_count, self.gaz_split, self.label_alphabet, self.number_normalized, self.MAX_SENTENCE_LENGTH)
elif name == "test":
self.test_texts, self.test_Ids = read_instance_with_gaz(self.HP_num_layer, input_file, self.gaz, self.word_alphabet, self.biword_alphabet, self.biword_count, self.char_alphabet, self.gaz_alphabet, self.gaz_count, self.gaz_split, self.label_alphabet, self.number_normalized, self.MAX_SENTENCE_LENGTH)
elif name == "raw":
self.raw_texts, self.raw_Ids = read_instance_with_gaz(self.HP_num_layer, input_file, self.gaz, self.word_alphabet,self.biword_alphabet, self.biword_count, self.char_alphabet, self.gaz_alphabet, self.gaz_count, self.gaz_split, self.label_alphabet, self.number_normalized, self.MAX_SENTENCE_LENGTH)
else:
print("Error: you can only generate train/dev/test instance! Illegal input:%s"%(name))
def write_decoded_results(self, output_file, predict_results, name):
fout = open(output_file,'w')
sent_num = len(predict_results)
content_list = []
if name == 'raw':
content_list = self.raw_texts
elif name == 'test':
content_list = self.test_texts
elif name == 'dev':
content_list = self.dev_texts
elif name == 'train':
content_list = self.train_texts
else:
print("Error: illegal name during writing predict result, name should be within train/dev/test/raw !")
assert(sent_num == len(content_list))
for idx in range(sent_num):
sent_length = len(predict_results[idx])
for idy in range(sent_length):
## content_list[idx] is a list with [word, char, label]
fout.write(content_list[idx][0][idy].encode('utf-8') + " " + predict_results[idx][idy] + '\n')
fout.write('\n')
fout.close()
print("Predict %s result has been written into file. %s"%(name, output_file))