1
13
14 package gate.creole.coref;
15
16 import java.util.*;
17
18 import gate.*;
19 import gate.creole.AbstractLanguageAnalyser;
20 import gate.creole.ResourceInstantiationException;
21 import gate.util.GateRuntimeException;
22 import gate.util.SimpleFeatureMapImpl;
23
24 public abstract class AbstractCoreferencer extends AbstractLanguageAnalyser
25 implements ProcessingResource{
26
27 public static final String COREF_DOCUMENT_PARAMETER_NAME = "document";
28
29 public static final String COREF_ANN_SET_PARAMETER_NAME = "annotationSetName";
30
31 public static final String COREF_TYPE_FEATURE_NAME = "ENTITY_MENTION_TYPE";
32 public static final String COREF_ANTECEDENT_FEATURE_NAME = "antecedent_offset";
33
34
35 private static final boolean DEBUG = false;
36
37 public String coreferenceType;
38
39
40 public AbstractCoreferencer(String type) {
41 this.coreferenceType = type;
42 }
43
44
45
46 public Resource init() throws ResourceInstantiationException {
47
48 Resource result = super.init();
49
50 return result;
51 }
53
54
62 public void reInit() throws ResourceInstantiationException {
63 init();
64 }
66
67 public void setDocument(Document newDocument) {
68 super.setDocument(newDocument);
69 }
70
71
72 public abstract void setAnnotationSetName(String annotationSetName);
73
74
75 public abstract String getAnnotationSetName();
76
77
78 protected void generateCorefChains(HashMap ana2ant)
79 throws GateRuntimeException{
80
81 String asName = getAnnotationSetName();
82 AnnotationSet outputSet = null;
83
84 if (null == asName || asName.equals("")) {
85 outputSet = getDocument().getAnnotations();
86 }
87 else {
88 outputSet = getDocument().getAnnotations(asName);
89 }
90
91 Iterator it = ana2ant.entrySet().iterator();
93 while (it.hasNext()) {
94 Map.Entry currLink = (Map.Entry)it.next();
95 Annotation anaphor = (Annotation)currLink.getKey();
96 Annotation antecedent = (Annotation)currLink.getValue();
97
98 if (DEBUG) {
99 AnnotationSet corefSet = getDocument().getAnnotations("COREF");
100 Long antOffset = new Long(0);
101
102 if (null != antecedent) {
103 antOffset = antecedent.getStartNode().getOffset();
104 }
105
106 FeatureMap features = new SimpleFeatureMapImpl();
107 features.put("antecedent",antOffset);
108 corefSet.add(anaphor.getStartNode(),anaphor.getEndNode(),"COREF",features);
109 }
110
111 if (null == antecedent) {
113 continue;
114 }
115
116 List matches = (List)antecedent.getFeatures().
118 get(ANNOTATION_COREF_FEATURE_NAME);
119 if (null == matches) {
120 matches = new ArrayList();
121 matches.add(antecedent.getId());
122 antecedent.getFeatures().
123 put(ANNOTATION_COREF_FEATURE_NAME,matches);
124 if (document.getFeatures().containsKey(
128 DOCUMENT_COREF_FEATURE_NAME)) {
129 Map matchesMap = (Map) document.getFeatures().get(
130 DOCUMENT_COREF_FEATURE_NAME);
131 List matchesList = (List) matchesMap.get(getAnnotationSetName());
132 if (matchesList == null) {
133 matchesList = new ArrayList();
134 matchesMap.put(getAnnotationSetName(), matchesList);
135 }
136 matchesList.add(matches);
137 } else {
138 Map matchesMap = new HashMap();
139 List matchesList = new ArrayList();
140 matchesMap.put(getAnnotationSetName(), matchesList);
141 matchesList.add(matches);
142 } }
145 FeatureMap features = new SimpleFeatureMapImpl();
146 features.put(COREF_TYPE_FEATURE_NAME, coreferenceType);
147 features.put(ANNOTATION_COREF_FEATURE_NAME, matches);
148 features.put(COREF_ANTECEDENT_FEATURE_NAME,
149 antecedent.getStartNode().getOffset());
150
151 Integer annID = outputSet.add(anaphor.getStartNode(),
152 anaphor.getEndNode(),
153 antecedent.getType(),
154 features);
155 matches.add(annID);
156 }
157 }
158
159 }
160