-
Notifications
You must be signed in to change notification settings - Fork 17
/
tagging.py
82 lines (68 loc) · 2.73 KB
/
tagging.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
#!/usr/bin/env python3
#coding=utf-8
import os, sys
I_BAR, I_TILDE, i_BAR, i_TILDE = 'I_', 'I~', 'i_', 'i~'
def sent_tags(nWords, anno, smwes, wmwes):
"""Convert a sentence's MWE analysis to a BIO-style tag sequence."""
tagging = []
parents = {}
gapstrength = {} # offset -> kind of gap ('_' or '~'), if the offset lies with a gap
# process strong groups
for grp in smwes:
g = sorted(grp)
skip = False
for i,j in zip(g[:-1],g[1:]):
if j>i+1:
if i in gapstrength: # gap within a gap
print('Simplifying: removing gappy group that is wholly contained within another gap:', g, anno, file=sys.stderr)
skip = True
break
if skip: continue
for i,j in zip(g[:-1],g[1:]):
assert j not in parents
parents[j] = i, '_'
if j>i+1:
for h in range(i+1,j):
gapstrength[h] = '_'
# process weak groups, skipping any that interleave with (are only partially contained in a gap of)
# a strong group
for grp in wmwes:
g = sorted(grp)
skip = False
for i in g:
if i in gapstrength and any(j for j in g if j not in gapstrength):
print('Simplifying: removing weak group that interleaves with a strong gap:', g, anno, file=sys.stderr)
skip = True
break
if skip: continue
for i,j in zip(g[:-1],g[1:]):
if j>i+1:
if i in gapstrength: # gap within a gap
print('Simplifying: removing gappy group that is wholly contained within another gap:', g, anno, file=sys.stderr)
skip = True
break
if skip: continue
for i,j in zip(g[:-1],g[1:]):
if j not in parents:
parents[j] = i, '~'
else:
assert parents[j][0]==i,(j,parents[j],i,g,anno)
if j>i+1:
for h in range(i+1,j):
gapstrength.setdefault(h,'~')
allparents = set(list(zip(*parents.values()))[0]) if parents else set()
for i in range(nWords):
parent, strength = parents.get(i+1,(0,''))
amInGap = (i+1 in gapstrength)
if parent==0:
if i+1 in allparents:
tag = ('b' if amInGap else 'B') #+labelFlag
else:
tag = ('o' if amInGap else 'O') #+labelFlag
elif strength=='_': # do not attach label to strong MWE continuations
tag = i_BAR if amInGap else I_BAR
else:
assert strength=='~'
tag = (i_TILDE if amInGap else I_TILDE) #+labelFlag
tagging.append(tag)
return tagging