-
Notifications
You must be signed in to change notification settings - Fork 44
/
toolbox.py
1091 lines (940 loc) · 33.1 KB
/
toolbox.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import codecs
import os
import numpy as np
import sys
import tensorflow as tf
import math
import re
import pygame
import copy
from evaluation import score, score_boundaries
sys.stdout = codecs.getwriter('utf8')(sys.stdout)
def get_ngrams(raw, gram):
gram_set = set()
li = gram/2
ri = gram - li - 1
p = '<PAD>'
for line in raw:
for i in range(len(line)):
if i - li < 0:
lp = p * (li - i) + line[:i]
else:
lp = line[i - li:i]
if i + ri + 1 > len(line):
rp = line[i:] + p*(i + ri + 1 - len(line))
else:
rp = line[i:i+ri+1]
ch = lp + rp
gram_set.add(ch)
return gram_set
def get_vocab_tag(path, filelist, ngram=1):
out_char = codecs.open(path + '/chars.txt', 'w', encoding='utf-8')
out_tag = codecs.open(path + '/tags.txt', 'w', encoding='utf-8')
char_set = set()
tag_set = {}
raw = []
for file_name in filelist:
for line in codecs.open(path + '/' + file_name, 'rb', encoding='utf-8'):
line = line.strip()
raw_l = ''
sets = line.split(' ')
if len(sets) > 0:
for seg in sets:
spos = seg.split('_')
if len(spos) == 2:
for ch in spos[0]:
char_set.add(ch)
raw_l += ch
if spos[1] in tag_set:
if tag_set[spos[1]] < len(spos[0]):
tag_set[spos[1]] = len(spos[0])
else:
tag_set[spos[1]] = len(spos[0])
raw.append(raw_l)
elif len(line) == 0:
continue
else:
print line
raise Exception('Check your text file.')
char_set = list(char_set)
#tag_set = list(tag_set)
if ngram > 1:
for i in range(2, ngram + 1):
out_gram = codecs.open(path + '/' + str(i) + 'gram.txt', 'w', encoding='utf-8')
grams = get_ngrams(raw, i)
for g in grams:
out_gram.write(g + '\n')
out_gram.close()
for item in char_set:
out_char.write(item + '\n')
out_char.close()
for k, v in tag_set.items():
out_tag.write(k + ' ' + str(v) + '\n')
out_tag.close()
def read_vocab_tag(path, ngrams=1):
char_set = set()
tag_set = {}
ngram_set = None
for line in codecs.open(path + '/chars.txt', 'rb', encoding='utf-8'):
char_set.add(line.strip())
for line in codecs.open(path + '/tags.txt', 'rb', encoding='utf-8'):
line = line.strip()
sp = line.split(' ')
tag_set[sp[0]] = int(sp[1])
char_set = list(char_set)
if ngrams > 1:
ngram_set = []
for i in range(2, ngrams + 1):
ng_set = set()
for line in codecs.open(path + '/' + str(i) + 'gram.txt', 'rb', encoding='utf-8'):
line = line.strip()
ng_set.add(line)
ngram_set.append(ng_set)
return char_set, tag_set, ngram_set
def get_sample_embedding(path, emb, chars, default='unk'):
short_emb = emb[emb.index('/') + 1: emb.index('.')]
emb_dic = {}
for line in codecs.open(emb, 'rb', encoding='utf-8'):
line = line.strip()
sets = line.split(' ')
emb_dic[sets[0]] = np.asarray(sets[1:], dtype='float32')
emb_dim = len(emb_dic.values()[0])
fout = codecs.open(path + '/' + short_emb + '_sub.txt', 'w', encoding='utf-8')
p_line = '<P>'
if '<P>' in emb_dic:
for emb in emb_dic['<P>']:
p_line += ' ' + unicode(emb)
else:
rand_emb = np.random.uniform(-math.sqrt(float(3)/emb_dim), math.sqrt(float(3)/ emb_dim), emb_dim)
for emb in rand_emb:
p_line += ' ' + unicode(emb)
fout.write(p_line + '\n')
p_line = '<UNK>'
if '<UNK>' in emb_dic:
for emb in emb_dic['<UNK>']:
p_line += ' ' + unicode(emb)
else:
rand_emb = np.random.uniform(-math.sqrt(float(3) / emb_dim), math.sqrt(float(3) / emb_dim), emb_dim)
emb_dic['<UNK>'] = rand_emb
for emb in rand_emb:
p_line += ' ' + unicode(emb)
fout.write(p_line + '\n')
p_line = '<NUM>'
if '<NUM>' in emb_dic:
for emb in emb_dic['<NUM>']:
p_line += ' ' + unicode(emb)
else:
rand_emb = np.random.uniform(-math.sqrt(float(3) / emb_dim), math.sqrt(float(3) / emb_dim), emb_dim)
for emb in rand_emb:
p_line += ' ' + unicode(emb)
fout.write(p_line + '\n')
p_line = '<FW>'
if '<FW>' in emb_dic:
for emb in emb_dic['<FW>']:
p_line += ' ' + unicode(emb)
else:
rand_emb = np.random.uniform(-math.sqrt(float(3) / emb_dim), math.sqrt(float(3) / emb_dim), emb_dim)
for emb in rand_emb:
p_line += ' ' + unicode(emb)
fout.write(p_line + '\n')
for ch in chars:
p_line = ch
if ch in emb_dic:
for emb in emb_dic[ch]:
p_line += ' ' + unicode(emb)
else:
if default == 'unk':
for emb in emb_dic['<UNK>']:
p_line += ' ' + unicode(emb)
else:
rand_emb = np.random.uniform(-math.sqrt(float(3) / emb_dim), math.sqrt(float(3) / emb_dim), emb_dim)
for emb in rand_emb:
p_line += ' ' + unicode(emb)
fout.write(p_line + '\n')
fout.close()
def get_ngram_embedding(path, emb, ngrams, default='unk'):
for gram in ngrams:
n = len(list(gram)[0])
real_emb = emb + '_' + str(n) + 'gram.txt'
get_sample_embedding(path, real_emb, gram, default=default)
def read_sample_embedding(path, short_emb):
emb_values = []
for line in codecs.open(path + '/' + short_emb + '_sub.txt', 'rb', encoding='utf-8'):
line = line.strip()
sets = line.split(' ')
emb_values.append(np.asarray(sets[1:], dtype='float32'))
emb_dim = len(emb_values[0])
return emb_dim, emb_values
def read_ngram_embedding(path, short_emb, n):
embs = []
for i in range(2, n + 1):
_, emb_v = read_sample_embedding(path, short_emb + '_' + str(i) + 'gram')
embs.append(emb_v)
return embs
def get_chars_pixels(path, chars, font, pt_size, utf8=True):
pix_dic = {}
pygame.init()
ft = pygame.font.Font('fonts/' + font, pt_size)
font_name = font[:font.index('.')]
fout = codecs.open(path + '/' + font_name + str(pt_size) + '_pixels.txt', 'w', encoding='utf-8')
zeros = np.zeros((pt_size, pt_size), dtype='float32').flatten()
pix_dic['<P>'] = zeros
p_line = '<P>'
for n in zeros:
p_line += ' ' + unicode(n)
fout.write(p_line + '\n')
p_line = '<UNK>'
pix_dic['<UNK>'] = zeros
for n in zeros:
p_line += ' ' + unicode(n)
fout.write(p_line + '\n')
p_line = '<NUM>'
ch = '0'
if utf8:
u_ch = ch
else:
u_ch = ch.decode('utf-8')
rtext = ft.render(u_ch, True, (0, 0, 0), (255, 255, 255))
ch_ary = pygame.surfarray.array2d(rtext)
ch_ary = ch_ary[:, :pt_size]
ch_ary = ch_ary[: pt_size]
if ch_ary.shape[0] < pt_size:
ch_ary = np.pad(ch_ary, ((pt_size - ch_ary.shape[0], 0), (0, 0)), 'constant', constant_values=0)
ch_ary = ch_ary.flatten()
for n in ch_ary:
p_line += ' ' + unicode(float(n)/255)
fout.write(p_line + '\n')
p_line = '<FW>'
ch = 'a'
if utf8:
u_ch = ch
else:
u_ch = ch.decode('utf-8')
rtext = ft.render(u_ch, True, (0, 0, 0), (255, 255, 255))
ch_ary = pygame.surfarray.array2d(rtext)
ch_ary = ch_ary[:, :pt_size]
ch_ary = ch_ary[: pt_size]
if ch_ary.shape[0] < pt_size:
ch_ary = np.pad(ch_ary, ((pt_size - ch_ary.shape[0], 0), (0, 0)), 'constant', constant_values=0)
ch_ary = ch_ary.flatten()
for n in ch_ary:
p_line += ' ' + unicode(float(n) / 255)
fout.write(p_line + '\n')
for ch in chars:
p_line = ch
if utf8:
u_ch = ch
else:
u_ch = ch.decode('utf-8')
rtext = ft.render(u_ch, True, (0, 0, 0), (255, 255, 255))
ch_ary = pygame.surfarray.array2d(rtext)
ch_ary = ch_ary[:, :pt_size]
ch_ary = ch_ary[: pt_size]
if ch_ary.shape[0] < pt_size:
ch_ary = np.pad(ch_ary, ((pt_size - ch_ary.shape[0], 0), (0, 0)), 'constant', constant_values=0)
ch_ary = ch_ary.flatten()
pix_dic[ch] = ch_ary.astype('float32')
for n in ch_ary:
p_line += ' ' + unicode(float(n)/255)
fout.write(p_line + '\n')
fout.close()
def read_chars_pixels(path, font_name, pt_size):
pix_dic = []
for line in codecs.open(path + '/' + font_name + str(pt_size) + '_pixels.txt', 'rb', encoding='utf-8'):
line = line.strip()
sets = line.split(' ')
ch_ary = np.array(sets[1:], dtype='float32')
pix_dic.append(ch_ary)
return pix_dic
def update_char_dict(char2idx, new_chars, valid_chars=None):
dim = len(char2idx)
unk_dim = dim
o_dim = dim
unk_char2idx = copy.copy(char2idx)
unk_idx = char2idx['<UNK>']
for char in new_chars:
if char not in char2idx:
char2idx[char] = dim
if valid_chars is not None and char in valid_chars and unk_dim - o_dim < 500:
unk_char2idx[char] = unk_dim
else:
unk_char2idx[char] = unk_idx
dim += 1
idx2char = {k: v for v, k in char2idx.items()}
return char2idx, idx2char, unk_char2idx
def update_gram_dicts(gram2idx, new_grams):
assert len(gram2idx) == len(new_grams)
new_gram2idx = []
for dic, n_gram in zip(gram2idx, new_grams):
assert len(dic.keys()[0]) == len(n_gram[0])
new_dic, _, _ = update_char_dict(dic, n_gram)
new_gram2idx.append(new_dic)
return new_gram2idx
def get_radical_dic(path='radical.txt'):
rad_dic= {}
for line in codecs.open(path, 'r', encoding='utf-8'):
line = line.strip()
rad_dic[ord(line)] = line
return rad_dic
def get_radical_idx(ch, rad_dic, keys=None):
if keys is None:
keys = rad_dic.keys()
keys = sorted(keys)
idx = ord(ch)
if idx < keys[0] or idx > keys[-1] + 6:
return '<NULL>'
else:
pre = 0
for k in keys:
if k > idx:
break
pre = k
return pre
def get_new_chars(path, char2idx, type='ctb'):
new_chars = set()
for line in codecs.open(path, 'rb', encoding='utf-8'):
line = line.strip()
if type == 'ctb':
segs = line.split(' ')
for seg in segs:
items = seg.split('_')
assert len(items) == 2
for ch in items[0]:
if ch not in char2idx:
new_chars.add(ch)
else:
line = re.sub('[\s+]', '', line)
for ch in line:
if ch not in char2idx:
new_chars.add(ch)
return new_chars
def get_new_chars_raw(lines, char2idx, type='ctb'):
new_chars = set()
for line in lines:
line = line.strip()
if type == 'ctb':
segs = line.split(' ')
for seg in segs:
items = seg.split('_')
assert len(items) == 2
for ch in items[0]:
if ch not in char2idx:
new_chars.add(ch)
else:
line = re.sub('[\s+]', '', line)
for ch in line:
if ch not in char2idx:
new_chars.add(ch)
return new_chars
def get_new_grams(path, gram2idx, type='ctb'):
raw = []
for line in codecs.open(path, 'rb', encoding='utf-8'):
line = line.strip()
raw_l = ''
if type == 'ctb':
segs = line.split(' ')
for seg in segs:
items = seg.split('_')
assert len(items) == 2
raw_l += items[0]
else:
line = re.sub('[\s+]', '', line)
raw_l = line
raw.append(raw_l)
new_grams = []
for g_dic in gram2idx:
new_g = []
n = len(g_dic.keys()[0])
grams = get_ngrams(raw, n)
for g in grams:
if g not in g_dic:
new_g.append(g)
new_grams.append(new_g)
return new_grams
def get_new_embeddings(new_chars, emb_dim, emb_path=None):
if emb_path is None:
return tf.random_uniform([len(new_chars), emb_dim], -math.sqrt(3 / emb_dim), math.sqrt(3 / emb_dim))
else:
assert os.path.isfile(emb_path)
emb = {}
new_emb = []
for line in codecs.open(emb_path, 'rb', encoding='utf-8'):
line = line.strip()
sets = line.split(' ')
emb[sets[0]] = np.asarray(sets[1:], dtype='float32')
if '<UNK>' not in emb:
unk = np.random.uniform(-math.sqrt(float(3) / emb_dim), math.sqrt(float(3) / emb_dim), emb_dim)
emb['<UNK>'] = np.asarray(unk, dtype='float32')
for ch in new_chars:
if ch in emb:
new_emb.append(emb[ch])
else:
new_emb.append(emb['<UNK>'])
return tf.stack(new_emb)
def get_new_ng_embeddings(new_grams, emb_dim, emb_path=None):
new_embs = []
for i in range(len(new_grams)):
n = len(new_grams[i][0])
if emb_path is not None:
real_path = emb_path + '_' + str(n) + 'gram.txt'
else:
real_path = None
n_emb = get_new_embeddings(new_grams[i], emb_dim, real_path)
new_embs.append(n_emb)
return new_embs
def get_valid_chars(chars, emb_path):
valid_chars = []
total = []
for line in codecs.open(emb_path, 'rb', encoding='utf-8'):
line = line.strip()
sets = line.split(' ')
total.append(sets[0])
for ch in chars:
if ch in total:
valid_chars.append(ch)
return valid_chars
def get_valid_grams(ngram, emb_path):
valid_grams = []
for gram in ngram:
valid = []
n = len(gram[0])
real_path = emb_path + '_' + str(n) + 'gram.txt'
total = []
for line in codecs.open(real_path, 'rb', encoding='utf-8'):
line = line.strip()
sets = line.split(' ')
total.append(sets[0])
for g in gram:
if g in total:
valid.append(g)
valid_grams.append(valid)
return valid_grams
def get_new_pixels(new_chars, font, pt_size):
new_pixels = []
pygame.init()
ft = pygame.font.Font('fonts/' + font, pt_size)
for ch in new_chars:
rtext = ft.render(ch.decode('utf-8'), True, (0, 0, 0), (255, 255, 255))
ch_ary = pygame.surfarray.array2d(rtext)
ch_ary = ch_ary[:, :pt_size]
if ch_ary.shape[0] < pt_size:
ch_ary = np.pad(ch_ary, ((pt_size - ch_ary.shape[0], 0), (0, 0)), 'constant', constant_values=0)
ch_ary = ch_ary.flatten()
new_pixels.append(np.asarray(ch_ary, dtype='float32')/255)
return new_pixels
def down_pool(pixel_dim, pooling_size):
if pixel_dim % pooling_size == 0:
p_size = pixel_dim / pooling_size
else:
p_size = pixel_dim / pooling_size + 1
return p_size
def next_batch(x, y, start_idx, batch_size):
last_idx = start_idx + batch_size
batch_x = x[start_idx:last_idx]
batch_y = y[start_idx:last_idx]
return batch_x, batch_y
def viterbi(max_scores, max_scores_pre, length, batch_size):
best_paths = []
for m in range(batch_size):
path = []
last_max_node = np.argmax(max_scores[m][length[m] - 1])
path.append(last_max_node)
for t in range(1, length[m])[::-1]:
last_max_node = max_scores_pre[m][t][last_max_node]
path.append(last_max_node)
path = path[::-1]
best_paths.append(path)
return best_paths
def get_comb_tags(tags, tag_type):
tag2index = {}
tag2index['<P>'] = 0
idx = 1
for k, v in tags.items():
real_tag_type = tag_type
if v == 1:
if tag_type == 'BIES':
real_tag_type = tag_type[-1:]
else:
real_tag_type = tag_type[0]
elif v == 2:
if tag_type == 'BIES' or tag_type == 'BIE':
real_tag_type = tag_type[: 1] + tag_type[-2:]
for t_type in real_tag_type:
tag2index[str(t_type + '-' + k)] = idx
idx += 1
return tag2index
def get_dic(chars, tags):
char2index = {}
char2index['<P>'] = 0
char2index['<UNK>'] = 1
char2index['<NUM>'] = 2
char2index['<FW>'] = 3
idx = 4
for ch in chars:
char2index[ch] = idx
idx += 1
index2char = {v: k for k, v in char2index.items()}
#0.seg BIES 1. BI; 2. BIE; 3. BIES
seg_tags2index = {'<P>':0, 'B': 1, 'I': 2, 'E': 3, 'S': 4}
tag2index = {'seg': seg_tags2index, 'BI': get_comb_tags(tags, 'BI'), 'BIE': get_comb_tags(tags, 'BIE'), 'BIES':
get_comb_tags(tags, 'BIES')}
index2tag = {}
for dic_keys in tag2index:
index2tag[dic_keys] = {v: k for k, v in tag2index[dic_keys].items()}
return char2index, index2char, tag2index, index2tag
def get_ngram_dic(ngrams):
gram_dics = []
for i, gram in enumerate(ngrams):
g_dic = {}
g_dic['<P>'] = 0
g_dic['<UNK>'] = 1
idx = 2
for g in gram:
g_dic[g] = idx
idx += 1
gram_dics.append(g_dic)
return gram_dics
def sub_num(x, char2index):
num_k = 0
fw_k = 0
num_set = set()
fw_set = set()
for k in char2index.keys():
if k == '<NUM>':
num_k = char2index[k]
elif k == '<FW>':
fw_k = char2index[k]
elif ('0' <= k <= '9') | ('0' <= k <= '9'):
num_set.add(char2index[k])
elif ('A' <= k <= 'Z') | ('a' <= k <= 'z') | ('A' <= k <= 'Z') | ('a' <= k <= 'z'):
fw_set.add(char2index[k])
if num_k == 0:
raise Exception('<NUM> key is not contained in the dictionary')
if fw_k == 0:
raise Exception('<FW> key is not contained in the dictionary')
for l in x:
for idx, ch in enumerate(l):
if ch in num_set:
l[idx] = num_k
elif ch in fw_set:
l[idx] = fw_k
return x
def get_input_vec(path, fname, char2index, tag2index, rad_dic=None, tag_scheme='BIES'):
max_sent_len_c = 0
max_sent_len_w = 0
max_word_len = 0
t_len = 0
key_map = {}
keys = []
if rad_dic is None:
x_m = [[]]
else:
x_m = [[], []]
keys = sorted(rad_dic.keys())
key_map['<NULL>'] = 0
idx = 1
for k in keys:
key_map[k] = idx
idx += 1
y_m = [[]]
for line in codecs.open(path + '/' + fname, 'r', encoding='utf-8'):
charIndices = []
raw_l = ''
if rad_dic is not None:
radIndices = []
tagIndices = {}
for k in tag2index.keys():
tagIndices[k] = []
line = line.strip()
segs = line.split(' ')
if len(segs) > max_sent_len_w:
max_sent_len_w = len(segs)
if len(segs) > 0 and len(line) > 0:
for seg in segs:
splits = seg.split('_')
assert len(splits) == 2
w_len = len(splits[0])
raw_l += splits[0]
if w_len > max_word_len:
max_word_len = w_len
t_len += w_len
if w_len == 1:
charIndices.append(char2index[splits[0]])
if rad_dic is not None:
radIndices.append(key_map[get_radical_idx(splits[0], rad_dic, keys)])
tagIndices['seg'].append(tag2index['seg']['S'])
tagIndices['BI'].append(tag2index['BI']['B-' + splits[1]])
tagIndices['BIE'].append(tag2index['BIE']['B-' + splits[1]])
tagIndices['BIES'].append(tag2index['BIES']['S-' + splits[1]])
else:
for x in range(w_len):
c_ch = splits[0][x]
charIndices.append(char2index[c_ch])
if rad_dic is not None:
radIndices.append(key_map[get_radical_idx(c_ch, rad_dic, keys)])
if x == 0:
tagIndices['seg'].append(tag2index['seg']['B'])
tagIndices['BI'].append(tag2index['BI']['B-' + splits[1]])
tagIndices['BIE'].append(tag2index['BIE']['B-' + splits[1]])
tagIndices['BIES'].append(tag2index['BIES']['B-' + splits[1]])
elif x == len(splits[0]) - 1:
tagIndices['seg'].append(tag2index['seg']['E'])
tagIndices['BI'].append(tag2index['BI']['I-' + splits[1]])
tagIndices['BIE'].append(tag2index['BIE']['E-' + splits[1]])
tagIndices['BIES'].append(tag2index['BIES']['E-' + splits[1]])
else:
tagIndices['seg'].append(tag2index['seg']['I'])
tagIndices['BI'].append(tag2index['BI']['I-' + splits[1]])
tagIndices['BIE'].append(tag2index['BIE']['I-' + splits[1]])
tagIndices['BIES'].append(tag2index['BIES']['I-' + splits[1]])
if t_len > max_sent_len_c:
max_sent_len_c = t_len
t_len = 0
x_m[0].append(charIndices)
if rad_dic is not None:
x_m[1].append(radIndices)
y_m[0].append(tagIndices[tag_scheme])
return x_m, y_m, max_sent_len_c, max_sent_len_w, max_word_len
def gram_vec(raw, dic):
out = []
ngram = len(dic.keys()[0])
li = ngram/2
ri = ngram - li - 1
p = '<PAD>'
for line in raw:
indices = []
for i in range(len(line)):
if i - li < 0:
lp = p * (li - i) + line[:i]
else:
lp = line[i - li:i]
if i + ri + 1 > len(line):
rp = line[i:] + p*(i + ri + 1 - len(line))
else:
rp = line[i:i+ri+1]
ch = lp + rp
if ch in dic:
indices.append(dic[ch])
else:
indices.append(dic['<UNK>'])
out.append(indices)
return out
def get_gram_vec(path, fname, gram2index, is_raw=False):
raw = []
if path is None:
real_path = fname
else:
real_path = path + '/' + fname
if is_raw:
for line in codecs.open(real_path, 'r', encoding='utf-8'):
line = re.sub('[\s+]', '', line)
raw.append(line)
else:
for line in codecs.open(real_path, 'r', encoding='utf-8'):
line = line.strip()
segs = line.split(' ')
if len(segs) > 0 and len(line) > 0:
raw_l = ''
for seg in segs:
sp = seg.split('_')
if len(sp) == 2:
raw_l += sp[0]
raw.append(raw_l)
out = []
for g_dic in gram2index:
out.append(gram_vec(raw, g_dic))
return out
def get_gram_vec_raw(raw, gram2index):
out = []
for g_dic in gram2index:
out.append(gram_vec(raw, g_dic))
return out
def get_input_vec_raw(path, fname, char2index, rad_dic=None):
max_len = 0
key_map = {}
keys = []
if rad_dic is None:
x_m = [[]]
else:
x_m = [[], []]
keys = sorted(rad_dic.keys())
key_map['<NULL>'] = 0
idx = 1
for k in keys:
key_map[k] = idx
idx += 1
if path is None:
real_path = fname
else:
real_path = path + '/' + fname
for line in codecs.open(real_path, 'r', encoding='utf-8'):
charIndices = []
radIndices = []
line = re.sub('[\s+]', '', line)
if len(line) > max_len:
max_len = len(line)
for ch in line:
charIndices.append(char2index[ch])
if rad_dic is not None:
radIndices.append(key_map[get_radical_idx(ch, rad_dic, keys)])
x_m[0].append(charIndices)
if rad_dic is not None:
x_m[1].append(radIndices)
return x_m, max_len
def get_input_vec_line(lines, char2index, rad_dic=None):
max_len = 0
key_map = {}
keys = []
if rad_dic is None:
x_m = [[]]
else:
x_m = [[], []]
keys = sorted(rad_dic.keys())
key_map['<NULL>'] = 0
idx = 1
for k in keys:
key_map[k] = idx
idx += 1
for line in lines:
charIndices = []
radIndices = []
line = re.sub('[\s+]', '', line)
if len(line) > max_len:
max_len = len(line)
for ch in line:
charIndices.append(char2index[ch])
if rad_dic is not None:
radIndices.append(key_map[get_radical_idx(ch, rad_dic, keys)])
x_m[0].append(charIndices)
if rad_dic is not None:
x_m[1].append(radIndices)
return x_m, max_len
def pad_zeros(l, max_len):
if type(l) is list:
return [np.pad(item, (0, max_len - len(item)), 'constant', constant_values=0) for item in l]
elif type(l) is dict:
padded = {}
for k, v in l.iteritems():
padded[k] = [np.pad(item, (0, max_len - len(item)), 'constant', constant_values=0) for item in v]
return padded
def unpad_zeros(l):
out = []
for tags in l:
out.append([np.trim_zeros(line) for line in tags])
return out
def decode_tags(idx, index2tags, tag_scheme):
out = []
dic = index2tags[tag_scheme]
for id in idx:
sents = []
for line in id:
sent = []
for item in line:
tag = dic[item]
if '-' in tag:
tag = tag.replace('E-', 'I-')
tag = tag.replace('S-', 'B-')
else:
tag = tag.replace('E', 'I')
tag = tag.replace('S', 'B')
sent.append(tag)
sents.append(sent)
out.append(sents)
return out
def decode_chars(idx, idx2chars):
out = []
for line in idx:
line = np.trim_zeros(line)
out.append([idx2chars[item] for item in line])
return out
def trim_output(out, length):
assert len(out) == len(length)
trimmed_out = []
for item, l in zip(out, length):
trimmed_out.append(item[:l])
return trimmed_out
def get_nums_tags(tag2idx, tag_scheme):
nums_tags = [len(tag2idx[tag_scheme])]
return nums_tags
def generate_output(chars, tags, tag_scheme):
out = []
for i, tag in enumerate(tags):
assert len(chars) == len(tag)
sub_out = []
for chs, tgs in zip(chars, tag):
#print len(chs), len(tgs)
assert len(chs) == len(tgs)
c_word = ''
c_tag = ''
p_line = ''
for ch, tg in zip(chs, tgs):
if tag_scheme == 'seg':
if tg == 'I':
c_word += ch
else:
p_line += ' ' + c_word
c_word = ch
else:
tg_sets = tg.split('-')
if tg_sets[0] == 'I' and tg_sets[1] == c_tag:
c_word += ch
else:
p_line += ' ' + c_word + '_' + c_tag
c_word = ch
if len(tg_sets) < 2:
c_tag = '<UNK>'
else:
c_tag = tg_sets[1]
if len(c_word) > 0:
if tag_scheme == 'seg':
p_line += ' ' + c_word
elif len(c_tag) > 0:
p_line += ' ' + c_word + '_' + c_tag
if tag_scheme == 'seg':
sub_out.append(p_line.strip())
else:
sub_out.append(p_line[1:].strip())
out.append(sub_out)
return out
def evaluator(prediction, gold, metric='F1-score', tag_num=1, verbose=False):
assert len(prediction) == len(gold)
scores = (0, 0, 0, 0, 0, 0)
scores_b = (0, 0, 0, 0, 0, 0)
if metric in ['F1-score', 'Precision', 'Recall', 'All']:
scores = score(gold[0], prediction[0], tag_num, verbose)
if metric in ['Boundary-F1-score', 'All']:
scores_b = score_boundaries(gold[0], prediction[0], verbose)
return scores + scores_b
def printer(predictions, out_path):
fout = codecs.open(out_path, 'w', encoding='utf-8')
for line in predictions:
fout.write(line + '\n')
fout.close()
def buckets(x, y, size=10):
assert len(x[0]) == len(y[0])
num_inputs = len(x)
samples = x + y
num_items = len(samples)
xy = zip(*samples)
xy.sort(key=lambda i: len(i[0]))
t_len = size
idx = 0
bucks = [[[]] for _ in range(num_items)]
for item in xy:
if len(item[0]) > t_len:
if len(bucks[0][idx]) > 0:
for buck in bucks:
buck.append([])
idx += 1
while len(item[0]) > t_len:
t_len += size
for i in range(num_items):
bucks[i][idx].append(item[i])
return bucks[:num_inputs], bucks[num_inputs:]
def pad_bucket(x, y, bucket_len_c=None):
assert len(x[0]) == len(y[0])
num_inputs = len(x)
num_tags = len(y)
padded = [[] for _ in range(num_tags + num_inputs)]
bucket_counts = []
samples = x + y
xy = zip(*samples)
if bucket_len_c is None:
bucket_len_c = []
for item in xy:
max_len = len(item[0][-1])
bucket_len_c.append(max_len)
bucket_counts.append(len(item[0]))
for idx in range(num_tags + num_inputs):
padded[idx].append(pad_zeros(item[idx], max_len))
print 'Number of buckets: ', len(bucket_len_c)
else:
idy = 0
for item in xy:
max_len = len(item[0][-1])
while idy < len(bucket_len_c) and max_len > bucket_len_c[idy]:
idy += 1
bucket_counts.append(len(item[0]))
if idy >= len(bucket_len_c):
for idx in range(num_tags + num_inputs):
padded[idx].append(pad_zeros(item[idx], max_len))
bucket_len_c.append(max_len)
else:
for idx in range(num_tags + num_inputs):