NamedReader.java
4.09 KB
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
package ipipan.clarin.tei.impl.io.read;
import ipipan.clarin.tei.api.entities.AnnotationLayer;
import ipipan.clarin.tei.api.entities.TEIMorph;
import ipipan.clarin.tei.api.entities.TEINamedEntity;
import ipipan.clarin.tei.api.entities.TEINamedEntityChild;
import ipipan.clarin.tei.api.entities.TEINamedEntityDerivation;
import ipipan.clarin.tei.api.entities.TEIParagraph;
import ipipan.clarin.tei.api.entities.TEISentence;
import ipipan.clarin.tei.api.exceptions.TEIException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.stream.XMLStreamException;
/**
*
* @author mlenart
*/
class NamedReader extends BodyReader {
NamedReader(InWrapper in) {
super(in);
}
@Override
protected void readNextParagraph(TEIParagraph par) throws TEIException {
try {
while (!in.isStartParagraph()) {
in.next();
}
String parId = in.getXmlId();
for (TEISentence sent : par.getSentences()) {
in.nextTag();
in.requireStart("s");
readNextSent(sent);
}
in.nextTag();
in.requireEnd(); // p
par.setId(AnnotationLayer.NAMES, parId);
} catch (Exception ex) {
throw new TEIException("Error in named entities: "
+ ex.getMessage(), ex);
}
}
private TEISentence readNextSent(TEISentence sent)
throws XMLStreamException, TEIException {
List<NEBuilder> builders = new LinkedList<NEBuilder>();
String sentId = in.getXmlId();
in.nextTag();
while (!in.isEnd()) {
in.requireStart("seg");
builders.add(readNEBuilder());
in.nextTag();
}
in.requireEnd(); // s
sent.setNERResult(getNEsFromBuilders(builders, sent));
sent.setId(AnnotationLayer.NAMES, sentId);
return sent;
}
private List<TEINamedEntity> getNEsFromBuilders(List<NEBuilder> builders,
TEISentence sent) throws TEIException {
LinkedList<TEINamedEntity> res = new LinkedList<TEINamedEntity>();
Map<String, TEINamedEntityChild> ptr2Child = new LinkedHashMap<String, TEINamedEntityChild>();
for (TEIMorph morph : sent.getMorphs()) {
ptr2Child.put(morph.getId(), morph);
}
Collections.reverse(builders);
for (NEBuilder b : builders) {
TEINamedEntity ne = b.getNe(ptr2Child);
res.addFirst(ne);
ptr2Child.put(ne.getId(), ne);
}
filterNEs(res);
return res;
}
private void filterNEs(LinkedList<TEINamedEntity> nes) {
Set<TEINamedEntityChild> nonRootNEs = new LinkedHashSet<TEINamedEntityChild>();
for (TEINamedEntity ne : nes) {
nonRootNEs.addAll(ne.getChildren());
}
nes.removeAll(nonRootNEs);
}
private NEBuilder readNEBuilder() throws XMLStreamException {
NEBuilder b = new NEBuilder(in.getXmlId());
in.nextTag();
in.requireStartFS("named");
in.nextTag();
if (in.isStartF("derived")) {
b.setDeriv(readDeriv());
in.nextTag();
}
in.requireStartF("type");
b.setType(in.readSymbolF().getValue());
in.nextTag();
if (in.isStartF("subtype")) {
b.setSubtype(in.readSymbolF().getValue());
in.nextTag();
}
in.requireStartF("orth");
b.setOrth(in.readStringF().getValue());
in.nextTag();
if (in.isStartF("base")) {
b.setBase(in.readStringF().getValue());
in.nextTag();
}
if (in.isStartF("when")) {
b.setBase(in.readStringF().getValue());
in.nextTag();
}
if (in.isStartF("certainty")) {
b.setCertainty(in.readSymbolF().getValue());
in.nextTag();
if (in.isStartF("comment")) {
b.setComment(in.readStringF().getValue());
in.nextTag();
}
}
in.requireEnd(); // fs named
in.nextTag();
b.setPtrs(PtrHelper.readPtrs(in));
in.requireEnd(); // seg
return b;
}
private TEINamedEntityDerivation readDeriv() throws XMLStreamException {
in.requireStartF("derived");
in.nextTag();
in.requireStartFS("derivation");
in.nextTag();
in.requireStartF("derivType");
String type = in.readSymbolF().getValue();
in.nextTag();
String from = null;
if (in.isStartF("derivedFrom")) {
from = in.readStringF().getValue();
in.nextTag();
}
in.requireEnd(); // fs
in.nextTag();
in.requireEnd(); // f derived
return ef.createNamedEntityDerivation(type, from);
}
}