1
15
16 package gate.creole.annotransfer;
17
18 import java.util.*;
19
20 import gate.*;
21 import gate.creole.*;
22 import gate.util.GateRuntimeException;
23 import gate.util.Out;
24
25
28 public class AnnotationSetTransfer extends AbstractLanguageAnalyser
29 implements ProcessingResource {
30
31
32 private static final boolean DEBUG = false;
33
34 public static final String
35 AST_DOCUMENT_PARAMETER_NAME = "document";
36
37 public static final String
38 AST_INPUT_AS_PARAMETER_NAME = "inputASName";
39
40 public static final String
41 AST_OUTPUT_AS_PARAMETER_NAME = "outputASName";
42
43 public static final String
44 AST_TAG_AS_PARAMETER_NAME = "tagASName";
45
46 public static final String
47 AST_TEXT_TAG_PARAMETER_NAME = "textTagName";
48
49 public static final String DEFAULT_OUTPUT_SET_NAME = "Filtered";
50 public static final String DEFAULT_TEXT_TAG_NAME = "BODY";
51
52 protected String tagASName = GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME;
53 protected String outputASName = DEFAULT_OUTPUT_SET_NAME;
54 protected String inputASName = null;
55 protected String textTagName = DEFAULT_TEXT_TAG_NAME;
56 protected gate.AnnotationSet bodyAnnotations = null;
57 protected List annotationTypes;
58
59
60 public Resource init() throws ResourceInstantiationException
61 {
62 return super.init();
63 }
65
73 public void reInit() throws ResourceInstantiationException
74 {
75 init();
76 }
78
79 public void execute() throws ExecutionException {
80
81 if(document == null)
82 throw new GateRuntimeException("No document to process!");
83
84 if(inputASName != null && inputASName.equals(""))
85 inputASName = null;
86 if(outputASName != null && outputASName.equals(""))
87 outputASName = null;
88
89 AnnotationSet inputAS = (inputASName == null) ?
91 document.getAnnotations() :
92 document.getAnnotations(inputASName);
93 AnnotationSet outputAS = (outputASName == null) ?
94 document.getAnnotations() :
95 document.getAnnotations(outputASName);
96 AnnotationSet tagAS = (tagASName == null) ?
97 document.getAnnotations() :
98 document.getAnnotations(tagASName);
99
100
103 if (annotationTypes != null && annotationTypes.size() > 0)
104 inputAS = inputAS.get(new HashSet(annotationTypes));
105
106 if(inputAS == null) return;
108
109 if (textTagName == null || textTagName.equals("")) {
112 outputAS.addAll(inputAS);
113 return;
114 }
115
116 bodyAnnotations = tagAS.get(textTagName);
118 if (bodyAnnotations == null || bodyAnnotations.isEmpty()) {
119 if(DEBUG) {
120 Out.prln("AST Warning: No text annotations of type '" + textTagName
121 + "' in document '" + document.getName()
122 + "' found, so transferring all annotations to the target set");
123 }
124 outputAS.addAll(inputAS);
125 return;
126 }
127
128 List annots2Move = new ArrayList();
129 Iterator bodyIter = bodyAnnotations.iterator();
130 while (bodyIter.hasNext()) {
131 Annotation bodyAnn = (Annotation)bodyIter.next();
132 Long start = bodyAnn.getStartNode().getOffset();
133 Long end = bodyAnn.getEndNode().getOffset();
134
135 AnnotationSet annots2Copy = inputAS.getContained(start, end);
137 annots2Move.addAll(annots2Copy);
139 }
140 outputAS.addAll(annots2Move);
141 inputAS.removeAll(annots2Move);
142
143
144 }
146 public void setTagASName(String newTagASName) {
147 if ("".equals(newTagASName))
149 tagASName = null;
150 else
151 tagASName = newTagASName;
152 }
153
154 public String getTagASName() {
155 return tagASName;
156 }
157
158 public void setInputASName(String newInputASName) {
159 inputASName = newInputASName;
160 }
161
162 public String getInputASName() {
163 return inputASName;
164 }
165
166 public void setOutputASName(String newOutputASName) {
167 outputASName = newOutputASName;
168 }
169
170 public String getOutputASName() {
171 return outputASName;
172 }
173
174 public void setTextTagName(String newTextTagName) {
175 textTagName = newTextTagName;
176 }
177
178 public String getTextTagName() {
179 return textTagName;
180 }
181
182 public List getAnnotationTypes() {
183 return this.annotationTypes;
184 }
185
186 public void setAnnotationTypes(List newTypes) {
187 annotationTypes = newTypes;
188 }
189
190
191 }