1
15
16 package gate.creole.annotdelete;
17
18 import java.util.*;
19
20 import gate.*;
21 import gate.creole.*;
22 import gate.util.GateRuntimeException;
23
24
30 public class AnnotationDeletePR extends AbstractLanguageAnalyser
31 implements ProcessingResource {
32
33 public static final String
34 TRANSD_DOCUMENT_PARAMETER_NAME = "document";
35
36 public static final String
37 TRANSD_ANNOT_TYPES_PARAMETER_NAME = "annotationTypes";
38
39 public static final String
40 TRANSD_SETS_KEEP_PARAMETER_NAME = "setsToKeep";
41
42 protected String markupSetName = GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME;
43 protected List annotationTypes;
44 protected List setsToKeep;
45
46
47 public Resource init() throws ResourceInstantiationException
48 {
49 return super.init();
50 }
52
60 public void reInit() throws ResourceInstantiationException
61 {
62 init();
63 }
65
66 public void execute() throws ExecutionException {
67
68 if(document == null)
69 throw new GateRuntimeException("No document to process!");
70
71
72 Map matchesMap = null;
73 Object matchesMapObject = document.getFeatures().get(ANNIEConstants.DOCUMENT_COREF_FEATURE_NAME);
74 if(matchesMapObject instanceof Map) {
75 matchesMap = (Map) matchesMapObject;
78 }
79
80
81 if (annotationTypes == null || annotationTypes.isEmpty()) {
83 document.getAnnotations().clear();
84
85 removeFromDocumentCorefData( (String)null, matchesMap);
86
87 } else {
88 removeSubSet(document.getAnnotations(), matchesMap );
89 }
90
91 Map namedSets = document.getNamedAnnotationSets();
93 if (namedSets == null || namedSets.isEmpty())
95 return;
96
97 List setNames = new ArrayList(namedSets.keySet());
99 Iterator iter = setNames.iterator();
100 String setName;
101
102 while (iter.hasNext()) {
103 setName = (String) iter.next();
104 if (setName != null && !setName.equals(markupSetName) ) {
107 if(setsToKeep != null && setsToKeep.contains(setName)) continue;
109
110 if (annotationTypes == null || annotationTypes.isEmpty()) {
111 document.removeAnnotationSet(setName);
112
113 removeFromDocumentCorefData( (String) setName, matchesMap);
114
115 } else {
116 removeSubSet(document.getAnnotations(setName), matchesMap );
117 }
118 } }
120
121
122 if(matchesMap != null) {
124 document.getFeatures().put(ANNIEConstants.DOCUMENT_COREF_FEATURE_NAME,
125 matchesMap);
126 }
127
128
129 }
131
132 private void removeFromDocumentCorefData(String currentSet, Map matchesMap) {
134 if(matchesMap == null)
135 return;
136
137 if(currentSet == null) {
139 java.util.List matches = (java.util.List) matchesMap.get(currentSet);
140 if (matches == null || matches.size() == 0) {
141 return;
143 }
144 else {
145 matchesMap.put(currentSet, new java.util.ArrayList());
146 }
147 } else {
148 matchesMap.remove(currentSet);
150 }
151 }
152
153 private void removeAnnotationsFromCorefData(AnnotationSet annotations, String setName, Map matchesMap) {
155 java.util.List matches = (java.util.List) matchesMap.get(setName);
156 if(matches == null)
157 return;
158
159 ArrayList annots = new ArrayList(annotations);
163 for(int i=0;i<annots.size();i++) {
164 Annotation toRemove = (Annotation) annots.get(i);
165 Iterator idIters = matches.iterator();
166 ArrayList ids = new ArrayList();
167 while(idIters.hasNext()) {
168 ids = (ArrayList) idIters.next();
169 if(ids.remove(toRemove.getId())) {
170 break;
172 }
173 }
174 if(ids.size()==0) {
175 matches.remove(ids);
176 }
177 }
178 if(matches.size()==0) {
180 matchesMap.remove(setName);
181 }
182 }
183
184
185
186 private void removeSubSet(AnnotationSet theSet, Map matchMap) {
187 AnnotationSet toRemove = theSet.get(new HashSet(annotationTypes));
188 if (toRemove == null || toRemove.isEmpty())
189 return;
190 theSet.removeAll(toRemove);
191
192 removeAnnotationsFromCorefData(toRemove, theSet.getName(), matchMap);
193
194 }
196 public void setMarkupASName(String newMarkupASName) {
197 markupSetName = newMarkupASName;
198 }
199
200 public String getMarkupASName() {
201 return markupSetName;
202 }
203
204 public List getAnnotationTypes() {
205 return this.annotationTypes;
206 }
207
208 public void setAnnotationTypes(List newTypes) {
209 annotationTypes = newTypes;
210 }
211
212 public List getSetsToKeep() {
213 return this.setsToKeep;
214 }
215
216 public void setSetsToKeep(List newSetNames) {
217 setsToKeep = newSetNames;
218 }
219
220
221 }