-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsuc_to_iob.py
181 lines (152 loc) · 5.18 KB
/
suc_to_iob.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
"""
Conversion tool to transform SUC3's xml format to IOB
Copyright 2017-2022, Emil Stenström
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
from bz2 import BZ2File
from xml.etree.ElementTree import iterparse
import argparse
from collections import Counter
import sys
def parse(fp, skiptypes=[]):
root = None
ne_prefix = ""
ne_type = "O"
name_prefix = ""
name_type = "O"
for event, elem in iterparse(fp, events=("start", "end")):
if root is None:
root = elem
if event == "start":
if elem.tag == "name":
_type = name_type_to_label(elem.attrib["type"])
if (
_type not in skiptypes and
not (_type == "ORG" and ne_type == "LOC")
):
name_type = _type
name_prefix = "B-"
elif elem.tag == "ne":
_type = ne_type_to_label(elem.attrib["type"])
if "/" in _type:
_type = ne_type_to_label(_type[_type.index("/") + 1:])
if _type not in skiptypes:
ne_type = _type
ne_prefix = "B-"
elif elem.tag == "w":
if name_type == "PER" and elem.attrib["pos"] == "NN":
name_type = "O"
name_prefix = ""
elif event == "end":
if elem.tag == "sentence":
yield
elif elem.tag == "name":
name_type = "O"
name_prefix = ""
elif elem.tag == "ne":
ne_type = "O"
ne_prefix = ""
elif elem.tag == "w":
if name_type != "O" and name_type != "OTH":
yield elem.text, name_prefix, name_type
elif ne_type != "O":
yield elem.text, ne_prefix, ne_type
else:
yield elem.text, "", "O"
if ne_type != "O":
ne_prefix = "I-"
if name_type != "O":
name_prefix = "I-"
root.clear()
def ne_type_to_label(ne_type):
mapping = {
"PRS": "PER",
}
return mapping.get(ne_type, ne_type)
def name_type_to_label(name_type):
mapping = {
"inst": "ORG",
"product": "OBJ",
"other": "OTH",
"place": "LOC",
"myth": "PER",
"person": "PER",
"event": "EVN",
"work": "WRK",
"animal": "PER",
}
return mapping.get(name_type)
def main(args=None):
parser = argparse.ArgumentParser()
parser.add_argument(
"infile",
help="""
Input for that contains the full SUC 3.0 XML.
Can be the bz2-zipped version or the xml version.
"""
)
parser.add_argument(
"outfile",
nargs="?",
help="""
Output file for IOB format.
Optional - will print to stdout otherwise
"""
)
parser.add_argument(
"--skiptypes",
help="Entity types that should be skipped in output.",
nargs="+",
default=[]
)
parser.add_argument(
"--stats_only",
help="Show statistics of found labels at the end of output.",
action='store_true',
default=False
)
args = parser.parse_args(args)
MAGIC_BZ2_FILE_START = b"\x42\x5a\x68"
fp = open(args.infile, "rb")
is_bz2 = (fp.read(len(MAGIC_BZ2_FILE_START)) == MAGIC_BZ2_FILE_START)
if is_bz2:
fp = BZ2File(args.infile, "rb")
else:
fp = open(args.infile, "rb")
if args.outfile is not None:
fout = open(args.outfile, "w", encoding="utf-8")
else:
fout = sys.stdout
type_stats = Counter()
for token in parse(fp, skiptypes=args.skiptypes):
if not token:
if not args.stats_only:
fout.write("\n")
else:
word, prefix, label = token
if args.stats_only:
type_stats[label] += 1
else:
fout.write("%s\t%s%s\n" % (word, prefix, label))
if args.stats_only:
fout.write(str(type_stats) + "\n")
fp.close()
if args.outfile is not None:
fout.close()
if __name__ == '__main__':
main()