-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
75 lines (65 loc) · 2.24 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
import logging, sys, argparse
def str2bool(v):
# copy from StackOverflow
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def get_entity(tag_seq, char_seq):
ENT = get_ENT_entity(tag_seq, char_seq)
HYPER = get_HYPER_entity(tag_seq, char_seq)
# ORG = get_ORG_entity(tag_seq, char_seq)
return ENT, HYPER
def get_ENT_entity(tag_seq, char_seq):
length = len(char_seq)
ENT = []
for i, (char, tag) in enumerate(zip(char_seq, tag_seq)):
if tag == 'B-ENT':
if 'ent' in locals().keys():
ENT.append(ent)
del ent
ent = char
if i+1 == length:
ENT.append(ent)
if tag == 'I-ENT':
ent += char
if i+1 == length:
ENT.append(ent)
if tag not in ['B-ENT', 'I-ENT']:
if 'ent' in locals().keys():
ENT.append(ent)
del ent
continue
return ENT
def get_HYPER_entity(tag_seq, char_seq):
length = len(char_seq)
HYPER = []
for i, (char, tag) in enumerate(zip(char_seq, tag_seq)):
if tag == 'B-HYPER':
if 'hyper' in locals().keys():
HYPER.append(hyper)
del hyper
hyper = char
if i+1 == length:
HYPER.append(hyper)
if tag == 'I-HYPER':
hyper += char
if i+1 == length:
HYPER.append(hyper)
if tag not in ['B-HYPER', 'I-HYPER']:
if 'hyper' in locals().keys():
HYPER.append(hyper)
del hyper
continue
return HYPER
def get_logger(filename):
logger = logging.getLogger('logger')
logger.setLevel(logging.DEBUG)
logging.basicConfig(format='%(message)s', level=logging.DEBUG)
handler = logging.FileHandler(filename)
handler.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s: %(message)s'))
logging.getLogger().addHandler(handler)
return logger