-
Notifications
You must be signed in to change notification settings - Fork 10
/
DumpData.java
263 lines (247 loc) · 9.61 KB
/
DumpData.java
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
/**
* (C) Copyright 2012-2013 A-cube lab - Università di Pisa - Dipartimento di Informatica.
* BAT-Framework is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* BAT-Framework is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with BAT-Framework. If not, see <http://www.gnu.org/licenses/>.
*/
package it.unipi.di.acube.batframework.utils;
import it.unipi.di.acube.batframework.data.Annotation;
import it.unipi.di.acube.batframework.data.ScoredAnnotation;
import it.unipi.di.acube.batframework.data.ScoredTag;
import it.unipi.di.acube.batframework.data.Tag;
import it.unipi.di.acube.batframework.metrics.MatchRelation;
import java.io.IOException;
import java.util.*;
/**
* Utility methods to dump a dataset.
*/
public class DumpData {
/**
* Dump an entire dataset.
*
* @param texts
* the instances of the dataset.
* @param gs
* the gold standard (must be of the same size as {@code texts}).
* @param api
* the API to Wikipedia (needed to print information about
* annotations/tags).
* @param <T1>
* the type of this dataset's gold standard.
* @throws IOException
* if something went wrong while querying the Wikipedia API.
*/
public static <T1 extends Tag> void dumpDataset(List<String> texts,
List<HashSet<T1>> gs, WikipediaInterface api) throws IOException {
for (int i = 0; i < texts.size(); i++)
dumpCompare(texts.get(i), gs.get(i), null, api);
}
/**
* Dump an entire output of a tagger for a dataset.
*
* @param texts
* the instances of the dataset.
* @param output
* the output computed by a tagger (must be of the same size as
* {@code texts}).
* @param api
* the API to Wikipedia (needed to print information about
* annotations/tags).
* @param <T1>
* the type of this output's gold standard.
* @throws IOException
* if something went wrong while querying the Wikipedia API.
*/
public static <T1 extends Tag> void dumpOutput(List<String> texts,
List<HashSet<T1>> output, WikipediaInterface api)
throws IOException {
for (int i = 0; i < texts.size(); i++)
dumpCompare(texts.get(i), null, output.get(i), api);
}
/**
* Dump, for each document of a dataset, the expected output (gold standard)
* and the actual output (found by an annotator).
*
* @param texts
* the instances of the dataset.
* @param expectedResult
* the gold standard provided by a dataset, one for each instance
* (must have the same size as {@code texts}).
* @param computedResult
* the solution found by an annotator, one for each instance
* (must have the same size as {@code texts}).
* @param api
* the API to Wikipedia (needed to print information about
* annotations/tags).
* @param <T>
* the type of this list elements.
* @throws IOException
* if something went wrong while querying the Wikipedia API.
*/
public static <T extends Tag> void dumpCompareList(List<String> texts,
List<HashSet<T>> expectedResult, List<HashSet<T>> computedResult,
WikipediaInterface api) throws IOException {
dumpCompareList(texts, expectedResult, computedResult, api, true);
}
/**
* Dump, for each document of a dataset, the expected output (gold standard)
* and the actual output (found by an annotator).
*
* @param texts
* the instances of the dataset.
* @param expectedResult
* the gold standard provided by a dataset, one for each instance
* (must have the same size as {@code texts}).
* @param computedResult
* the solution found by an annotator, one for each instance
* (must have the same size as {@code texts}).
* @param api
* the API to Wikipedia (needed to print information about
* annotations/tags).
* @param printEmptyDocs
* whether or not to print documents with an empty gold standard
* and an empty solution.
* @param <T>
* the type of these lists elements.
* @throws IOException
* if something went wrong while querying the Wikipedia API.
*/
public static <T extends Tag> void dumpCompareList(List<String> texts,
List<HashSet<T>> expectedResult, List<HashSet<T>> computedResult,
WikipediaInterface api, boolean printEmptyDocs)
throws IOException {
dumpCompareList(texts, expectedResult, computedResult, api, printEmptyDocs, null);
}
/**
* Dump, for each document of a dataset, the expected output (gold standard)
* and the actual output (found by an annotator).
*
* @param texts
* the instances of the dataset.
* @param expectedResult
* the gold standard provided by a dataset, one for each instance
* (must have the same size as {@code texts}).
* @param computedResult
* the solution found by an annotator, one for each instance
* (must have the same size as {@code texts}).
* @param api
* the API to Wikipedia (needed to print information about
* annotations/tags).
* @param printEmptyDocs
* whether or not to print documents with an empty gold standard
* and an empty solution.
* @param mr
* match relation used to dump annotations about TP/FP/FN
* @param <T>
* the type of these lists elements.
* @throws IOException
* if something went wrong while querying the Wikipedia API.
*/
public static <T extends Tag> void dumpCompareList(List<String> texts,
List<HashSet<T>> expectedResult, List<HashSet<T>> computedResult,
WikipediaInterface api, boolean printEmptyDocs, MatchRelation<T> mr)
throws IOException {
for (int i = 0; i < texts.size(); i++) {
if (printEmptyDocs
|| (!printEmptyDocs && (!expectedResult.get(i).isEmpty() || !computedResult
.get(i).isEmpty()))) {
DumpData.dumpCompareMatch(texts.get(i), expectedResult.get(i),
computedResult.get(i), mr, api);
System.out.println();
}
}
}
/**
* Dumps the text, the annotations provided by the gold standard and those
* found by a tagger for a single document.
*
* @param text
* the document.
* @param expectedResult
* the expected results provided by a dataset (if {@code null},
* it is not printed).
* @param computedResult
* the results found by an annotator (if {@code null}, it is not
* printed).
* @param api
* the API to Wikipedia (needed to print information about
* annotations/tags).
* @param mr
* a match relation to compare the results.
* @param <T>
* the type of result.
* @throws IOException
* if something went wrong while querying the Wikipedia API.
*/
public static <T extends Tag> void dumpCompareMatch(String text,
HashSet<T> expectedResult, HashSet<T> computedResult,
MatchRelation<T> mr, WikipediaInterface api) throws IOException {
System.out.println("Text: " + text);
if (expectedResult != null) {
System.out.println();
System.out.println("Gold standard: ");
for (T a : expectedResult) {
String note = "";
if (mr != null) {
note = "FN";
for (T t : computedResult)
if (mr.match(t, a))
note = "";
}
printAnnotation(text, a, api, note);
}
}
if (computedResult != null) {
System.out.println();
System.out.println("System output: ");
List<T> list = new Vector<T>();
for (T t : computedResult)
list.add(t);
Collections.sort(list);
for (T a : list) {
String note = "";
if (mr != null) {
note = "FP";
for (T t : expectedResult)
if (mr.match(a, t))
note = "TP";
}
printAnnotation(text, a, api, note);
}
}
}
public static <T extends Tag> void dumpCompare(String text,
HashSet<T> expectedResult, HashSet<T> computedResult,
WikipediaInterface api) throws IOException {
dumpCompareMatch(text, expectedResult, computedResult, null, api);
}
private static <T extends Tag> void printAnnotation(String text, T a,
WikipediaInterface api, String note) throws IOException {
if (a instanceof ScoredAnnotation)
System.out.printf("\t%s: %s -> %s (wid=%d) (score=%.3f)%n", note,
text.substring(((ScoredAnnotation) a).getPosition(),
((Annotation) a).getPosition()
+ ((ScoredAnnotation) a).getLength()), api
.getTitlebyId(a.getConcept()), a.getConcept(),
((ScoredAnnotation) a).getScore());
else if (a instanceof Annotation)
System.out.printf(
"\t%s: %s (%d, %d) -> %s (%d)%n",
note,
text.substring(
((Annotation) a).getPosition(),
((Annotation) a).getPosition()
+ ((Annotation) a).getLength()),
((Annotation) a).getPosition(),
((Annotation) a).getPosition()
+ ((Annotation) a).getLength(), api.getTitlebyId(a
.getConcept()), a.getConcept());
else if (a instanceof ScoredTag)
System.out.printf("\t%s: %s (wid=%d) (score=%.3f)%n", note,
api.getTitlebyId(a.getConcept()), a.getConcept(),
((ScoredTag) a).getScore());
else if (a instanceof Tag)
System.out.printf("\t%s: %s (%d)", note,
api.getTitlebyId(a.getConcept()), a.getConcept());
}
}