1   /*
2    *  AnnotationSetTransfer.java
3    *
4    *  Copyright (c) 1998-2004, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Kalina Bontcheva, 6/8/2001
12   *
13   *  $Id: AnnotationSetTransfer.java,v 1.17 2004/07/21 17:10:03 akshay Exp $
14   */
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  /**
26   * This class is the implementation of the resource ACEPROCESSOR.
27   */
28  public class AnnotationSetTransfer extends AbstractLanguageAnalyser
29    implements ProcessingResource {
30  
31    /** DEBUG flag */
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    /** Initialise this resource, and return it. */
60    public Resource init() throws ResourceInstantiationException
61    {
62      return super.init();
63    } // init()
64  
65    /**
66    * Reinitialises the processing resource. After calling this method the
67    * resource should be in the state it is after calling init.
68    * If the resource depends on external resources (such as rules files) then
69    * the resource will re-read those resources. If the data used to create
70    * the resource has changed since the resource has been created then the
71    * resource will change too after calling reInit().
72    */
73    public void reInit() throws ResourceInstantiationException
74    {
75      init();
76    } // reInit()
77  
78    /** Run the resource. */
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      //get the input annotation set and the output one
90      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     //if we want to transfer only some types, then select only annotations
101     //from these types
102 
103     if (annotationTypes != null && annotationTypes.size() > 0)
104       inputAS = inputAS.get(new HashSet(annotationTypes));
105 
106     // in case of no one annotation from some of annotationTypes
107     if(inputAS == null) return;
108 
109     //check if we have a BODY annotation
110     //if not, just copy all
111     if (textTagName == null || textTagName.equals("")) {
112       outputAS.addAll(inputAS);
113       return;
114     }
115 
116     //get the BODY annotation
117     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       //get all annotations we want transferred
136       AnnotationSet annots2Copy = inputAS.getContained(start, end);
137       //copy them to the new set and delete them from the old one
138       annots2Move.addAll(annots2Copy);
139     }
140     outputAS.addAll(annots2Move);
141     inputAS.removeAll(annots2Move);
142 
143 
144   } // execute()
145 
146   public void setTagASName(String newTagASName) {
147     //if given an empty string, set to the default set
148     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 } // class AnnotationSetTransfer
192