-
Notifications
You must be signed in to change notification settings - Fork 0
/
writers.py
235 lines (218 loc) · 9.06 KB
/
writers.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
"""Writes EL responses"""
import codecs
import json
import os
import re
import string
import model as md
class DefWriter(object):
"""Default annotation writer"""
HEADERS = {
"single": ["doc", "mtn", "start", "end", "label", "service", "conf",
"sentence"],
"multi": ["doc", "mtn", "start", "end", "label", "service", "conf",
"sentence"]
}
def __init__(self, cfg):
self.cfg = cfg
def write_raw_responses(self, di, clname, runid="001",
outdir=None):
for ke in sorted(di):
if outdir is None:
outdir = self.cfg.resdir
outfn = os.path.join(outdir,
os.path.basename(os.path.splitext(ke)[0]) + \
"_" + clname + "_" + string.zfill(runid, 3) + ".txt")
print " - Writing raw response: {}".format(
os.path.realpath(outfn))
with codecs.open(outfn, "w", "utf8") as out:
if (isinstance(di[ke], dict) or
isinstance(di[ke], list)):
out.write(json.dumps(di[ke]))
elif isinstance(di[ke], str):
out.write(di[ke].decode("utf8"))
class Dict2TsvWriter(DefWriter):
"""
Takes dictionary of annotation-dict hashed by fn and position,
writes TSV.
"""
def __init__(self, cfg):
super(Dict2TsvWriter, self).__init__(cfg)
@staticmethod
def _parse_annotation_hash(di, clname, cps, mode="single", fn=None):
"""
Parse annotations in a way that can be written out
by other methods in the class.
@param di: hash of annotations
@param clname: name of the client (spotlight etc.)
@param cps: the corpus object whose results to write
@param mode: 'single' means single output file for all corpus,
'multi' means individual output files per input file.
@return: List of tsv strings representing annotations
@rtype: list
"""
assert mode in ("single", "multi")
# files with no annotations
if not di:
if mode == "single":
outlists = [fn]
else:
outlists = []
else:
outlists = []
# files with
for po in sorted(di):
if isinstance(di[po], dict):
outlist = [di[po]["fmention"],
str(di[po]["start"]), str(di[po]["end"]),
di[po]["link"], clname,
str(di[po]["confidence"])]
try:
if di[po]["snbr"]:
outlist.append(str(di[po]["snbr"]))
except KeyError:
print "! No snbr: {}".format(repr(di[po]))
try:
if cps.entities[di[po]["link"]].categs["normcat"]:
outlist.append(
cps.entities[di[po]["link"]].categs["normcat"])
except KeyError:
print "! No normcat: {}".format(repr(di[po]["link"]))
elif isinstance(di[po], md.Annotation):
outlist = [di[po].fmention,
str(di[po].start), str(di[po].end),
di[po].link, clname,
str(di[po].confidence)]
else:
outlist = []
print "! type error with var containing annotations"
if mode == "single":
outlist = [fn] + outlist
outlists.append("\t".join(outlist))
return outlists
def write_to_multi(self, di, clname, cps, runid="001", has_categ=False,
outdir=None):
"""
Write annotations from a hash to multiple output files
(one file per key in hash, keys are filenames).
@param di: hash with annotations by fn and position
@param clname: name of the client (babelfy, etc.)
@param runid: id for the run
"""
for ke in di:
if outdir is None:
outdir = self.cfg.outdir
outfn = os.path.join(outdir,
os.path.basename(os.path.splitext(ke)[0]) + \
"_" + clname + "_" + string.zfill(runid, 3) + ".txt")
print " - Writing annotations: {}".format(
os.path.realpath(outfn))
with codecs.open(outfn, "w", "utf8") as out:
outlists = ["\t".join(DefWriter.HEADERS["multi"])]
if has_categ:
outlists[0] += "\tcateg"
outlists.extend(self._parse_annotation_hash(di[ke], clname,
cps, mode="multi"))
out.write("\n".join(outlists))
def write_to_single(self, di, clname, cps, runid="001", has_categ=False,
write_header=False, outdir=None):
"""
Write annotations from a hash to a single output file
for the hash.
@param di: hash with annotations by fn and position
@param clname: name of the client (babelfy, etc.)
@param cps: the corpus object whose results to write
@param runid: id for the run
"""
if outdir is None:
outdir = self.cfg.outdir
outfn = os.path.join(outdir,
cps.name + "_" + clname + "_all_" +
string.zfill(runid, 3) + ".txt")
with codecs.open(outfn, "a", "utf8") as out:
if write_header:
myheader = "\t".join(DefWriter.HEADERS["single"])
if has_categ:
myheader += "\tcateg"
out.write("".join((myheader, "\n")))
for ke in sorted(di):
print " - Appends annotations: {}".format(
os.path.realpath(outfn))
outlists = self._parse_annotation_hash(di[ke], clname, cps,
mode="single", fn=ke)
try:
out.write("".join(("\n".join(outlists), "\n")))
except UnicodeDecodeError:
out.write("".join("\n".join(
[re.sub("\xef\xbb\xbf", "", item) for item in outlists])))
class Obj2TsvWriter(Dict2TsvWriter):
"""
Takes dictionary of L{model.Annotation} hashed by fn and position,
writes TSV.
"""
def __init__(self, cfg):
super(Obj2TsvWriter, self).__init__(cfg)
@staticmethod
def _parse_annotation_hash(di, clname, cps, mode="single", fn=None):
"""
Parse annotations in a way that can be written out
by other methods in the class.
@param di: hash of L{model.Annotation}
@param clname: name of the client (spotlight etc.)
@param cps: the corpus object whose results to write
@param mode: 'single' means single output file for all corpus,
'multi' means individual output files per input file.
@return: List of tsv strings representing annotations
@rtype: list
"""
assert mode in ("single", "multi")
# files with no annotations
if not di:
if mode == "single":
outlists = [fn]
else:
outlists = []
else:
outlists = []
# files with
for po in sorted(di):
# fmention to write out
if isinstance(di[po], dict):
outlist = [di[po].fmention,
str(di[po].start), str(di[po].end),
di[po].link, clname,
str(di[po].confidence)]
try:
if di[po].snbr:
outlist.append(str(di[po].snbr))
except NameError:
print "! No snbr: {}".format(repr(di[po]))
try:
outlist.append(cps.entities[di[po]].normcat)
except KeyError:
print "! No normcat: {}".format(repr(di[po].link))
elif isinstance(di[po], md.Annotation):
outlist = [di[po].fmention,
str(di[po].mention.start), str(di[po].mention.end),
di[po].enti.link, clname,
str(di[po].confidence)]
try:
if di[po].snbr:
outlist.append(str(di[po].snbr))
except NameError:
print "! No snbr: {}".format(repr(di[po]))
try:
outlist.append(cps.entities[di[po].enti.link].normcat)
except KeyError:
print "! No normcat: {}".format(repr(di[po].enti.link))
else:
outlist = []
print "! type error with var containing annotations"
if mode == "single":
outlist = [fn] + outlist
try:
outlists.append("\t".join(outlist))
except UnicodeDecodeError:
outlists.append("\t".join(
[re.sub("\xef\xbb\xbf", "", item) for item in outlist]))
return outlists