-
Notifications
You must be signed in to change notification settings - Fork 40
/
CorefBrackets.java
214 lines (185 loc) · 6.82 KB
/
CorefBrackets.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
// Dstl (c) Crown Copyright 2017
// Modified by NCA (c) Crown Copyright 2017
package uk.gov.dstl.baleen.annotators.cleaners;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.uima.UIMAException;
import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.factory.JCasFactory;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import uk.gov.dstl.baleen.core.pipelines.orderers.AnalysisEngineAction;
import uk.gov.dstl.baleen.core.utils.ReflectionUtils;
import uk.gov.dstl.baleen.exceptions.BaleenException;
import uk.gov.dstl.baleen.types.semantic.Entity;
import uk.gov.dstl.baleen.types.semantic.ReferenceTarget;
import uk.gov.dstl.baleen.uima.BaleenAnnotator;
import uk.gov.dstl.baleen.uima.utils.TypeSystemSingleton;
import uk.gov.dstl.baleen.uima.utils.TypeUtils;
/**
* Coreference entities where a series of entities of the same type appears in brackets.
*
* <p>For example, "William Tell (Bill)" would coreference William Tell and Bill if they were both
* person entities.
*
* @baleen.javadoc
*/
public class CorefBrackets extends BaleenAnnotator {
/**
* If two entities are thought to be coreferences, but they have different existing reference
* targets, should we merge them?
*
* @baleen.config false
*/
public static final String PARAM_MERGE_REFERENTS = "mergeReferents";
@ConfigurationParameter(name = PARAM_MERGE_REFERENTS, defaultValue = "false")
private boolean mergeReferents = false;
/**
* A list of the excluded entity types.
*
* @baleen.config
*/
public static final String PARAM_TYPE = "excludedTypes";
@ConfigurationParameter(
name = PARAM_TYPE,
defaultValue = {})
String[] excludedTypes;
List<Class<? extends Entity>> classTypes = new ArrayList<>();
private static final Pattern BRACKETS = Pattern.compile("^\\s*\\((.*?)\\)");
@Override
public void doInitialize(UimaContext aContext) throws ResourceInitializationException {
JCas jCas;
try {
jCas = JCasFactory.createJCas(TypeSystemSingleton.getTypeSystemDescriptionInstance());
} catch (UIMAException e) {
throw new ResourceInitializationException(e);
}
for (String type : excludedTypes) {
try {
classTypes.add(TypeUtils.getEntityClass(type, jCas));
} catch (BaleenException e) {
getMonitor().error("Couldn't parse type {} - type will not be excluded", type, e);
}
}
}
@Override
public void doProcess(JCas jCas) throws AnalysisEngineProcessException {
Collection<Entity> entities = JCasUtil.select(jCas, Entity.class);
Multimap<Integer, Entity> entityMap = ArrayListMultimap.create();
entities.forEach(e -> entityMap.put(e.getBegin(), e));
for (Entity e : entities) {
String followingText = jCas.getDocumentText().substring(e.getEnd());
Matcher m = BRACKETS.matcher(followingText);
Integer offset = e.getEnd();
List<Entity> matched = new ArrayList<>();
while (m.find()) {
final Integer end = offset + m.end(1);
matched.addAll(
entityMap.get(offset + m.start(1)).stream()
.filter(
f ->
f.getClass().isAssignableFrom(e.getClass())
|| e.getClass().isAssignableFrom(f.getClass()))
.filter(f -> f.getEnd() == end)
.collect(Collectors.toList()));
// Prepare matcher to look for next bracket
followingText = followingText.substring(m.end());
offset += m.end();
m = BRACKETS.matcher(followingText);
}
if (!matched.isEmpty()) {
matched.add(e);
makeCoref(jCas, matched);
}
}
}
@Override
public AnalysisEngineAction getAction() {
Set<Class<? extends Entity>> types = ReflectionUtils.getSubTypes(Entity.class);
types.removeAll(classTypes);
Set<Class<? extends Annotation>> annotations = new HashSet<>();
annotations.addAll(types);
return new AnalysisEngineAction(annotations, Collections.emptySet());
}
private void makeCoref(JCas jCas, Collection<Entity> entities) {
Set<ReferenceTarget> rts = new HashSet<>();
for (Entity e : entities) {
if (e.getReferent() != null) {
rts.add(e.getReferent());
}
}
if (rts.isEmpty()) {
setAllReferents(createReferenceTarget(jCas), entities);
} else if (rts.size() == 1) {
setAllReferents(rts.toArray(new ReferenceTarget[0])[0], entities);
} else {
if (mergeReferents) {
ReferenceTarget rt = createReferenceTarget(jCas);
setReferents(rt, entities, rts);
removeFromJCasIndex(rts);
} else {
getMonitor()
.warn(
"Multiple existing referents found, only those entities without existing referents will be modified");
setNewReferentIfNull(jCas, entities);
}
}
}
/**
* Set the referents of all entities in <em>entities</em> to the ReferenceTarget specified by
* <em>target</em>
*/
private void setAllReferents(ReferenceTarget target, Collection<Entity> entities) {
for (Entity e : entities) {
e.setReferent(target);
}
}
/**
* For all entities in <em>entities</em> that don't have an existing referent, set them to the
* same new ReferenceTarget. This ReferenceTarget is returned if created, else null is returned.
*/
private ReferenceTarget setNewReferentIfNull(JCas jCas, Collection<Entity> entities) {
ReferenceTarget rt = null;
for (Entity e : entities) {
if (e.getReferent() == null) {
if (rt == null) {
rt = createReferenceTarget(jCas);
}
e.setReferent(rt);
}
}
return rt;
}
/**
* Sets the referents to <em>target</em> on all entities that either have a null referent, or a
* referent that matches one of the targets specified in <em>condition</em>
*/
private void setReferents(
ReferenceTarget target, Collection<Entity> entities, Collection<ReferenceTarget> condition) {
for (Entity e : entities) {
if (e.getReferent() == null || condition.contains(e.getReferent())) {
e.setReferent(target);
}
}
}
private ReferenceTarget createReferenceTarget(JCas jCas) {
ReferenceTarget rt = new ReferenceTarget(jCas);
rt.setBegin(0);
rt.setEnd(jCas.getDocumentText().length());
addToJCasIndex(rt);
return rt;
}
}