1   /*
2    *  AnnotationSetTransfer.java
3    *
4    *  Copyright (c) 1998-2001, 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.14 2002/08/30 13:14:28 nasso Exp $
14   */
15  
16  package gate.creole.annotransfer;
17  
18  import java.util.*;
19  import gate.*;
20  import gate.creole.*;
21  import gate.util.*;
22  import gate.corpora.*;
23  
24  /**
25   * This class is the implementation of the resource ACEPROCESSOR.
26   */
27  public class AnnotationSetTransfer extends AbstractLanguageAnalyser
28    implements ProcessingResource {
29  
30    public static final String
31      AST_DOCUMENT_PARAMETER_NAME = "document";
32  
33    public static final String
34      AST_INPUT_AS_PARAMETER_NAME = "inputASName";
35  
36    public static final String
37      AST_OUTPUT_AS_PARAMETER_NAME = "outputASName";
38  
39    public static final String
40      AST_TAG_AS_PARAMETER_NAME = "tagASName";
41  
42    public static final String
43      AST_TEXT_TAG_PARAMETER_NAME = "textTagName";
44  
45    public static final String DEFAULT_OUTPUT_SET_NAME = "Filtered";
46    public static final String DEFAULT_TEXT_TAG_NAME = "BODY";
47  
48    protected String   tagASName =  GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME;
49    protected String                outputASName = DEFAULT_OUTPUT_SET_NAME;
50    protected String                inputASName = null;
51    protected String                textTagName = DEFAULT_TEXT_TAG_NAME;
52    protected gate.AnnotationSet    bodyAnnotations = null;
53    protected List annotationTypes;
54  
55    /** Initialise this resource, and return it. */
56    public Resource init() throws ResourceInstantiationException
57    {
58      return super.init();
59    } // init()
60  
61    /**
62    * Reinitialises the processing resource. After calling this method the
63    * resource should be in the state it is after calling init.
64    * If the resource depends on external resources (such as rules files) then
65    * the resource will re-read those resources. If the data used to create
66    * the resource has changed since the resource has been created then the
67    * resource will change too after calling reInit().
68    */
69    public void reInit() throws ResourceInstantiationException
70    {
71      init();
72    } // reInit()
73  
74    /** Run the resource. */
75    public void execute() throws ExecutionException {
76  
77      if(document == null)
78        throw new GateRuntimeException("No document to process!");
79  
80      if(inputASName != null && inputASName.equals(""))
81        inputASName = null;
82      if(outputASName != null && outputASName.equals(""))
83        outputASName = null;
84  
85      //get the input annotation set and the output one
86      AnnotationSet inputAS = (inputASName == null) ?
87                              document.getAnnotations() :
88                              document.getAnnotations(inputASName);
89      AnnotationSet outputAS = (outputASName == null) ?
90                               document.getAnnotations() :
91                               document.getAnnotations(outputASName);
92      AnnotationSet tagAS = (tagASName == null) ?
93                              document.getAnnotations() :
94                              document.getAnnotations(tagASName);
95  
96      //if we want to transfer only some types, then select only annotations
97      //from these types
98  
99      if (annotationTypes != null && annotationTypes.size() > 0)
100       inputAS = inputAS.get(new HashSet(annotationTypes));
101       
102     // in case of no one annotation from some of annotationTypes   
103     if(inputAS == null) return;
104 
105     //check if we have a BODY annotation
106     //if not, just copy all
107     if (textTagName == null || textTagName.equals("")) {
108       outputAS.addAll(inputAS);
109       return;
110     }
111 
112     //get the BODY annotation
113     bodyAnnotations = tagAS.get(textTagName);
114     if (bodyAnnotations == null || bodyAnnotations.isEmpty()) {
115       Out.prln("AST Warning: No text annotations of type '" + textTagName 
116         + "' in document '" + document.getName()
117         + "' found, so transferring all annotations to the target set");
118       outputAS.addAll(inputAS);
119       return;
120     }
121 
122     List annots2Move = new ArrayList();
123     Iterator bodyIter = bodyAnnotations.iterator();
124     while (bodyIter.hasNext()) {
125       Annotation bodyAnn = (Annotation)bodyIter.next();
126       Long start = bodyAnn.getStartNode().getOffset();
127       Long end = bodyAnn.getEndNode().getOffset();
128 
129       //get all annotations we want transferred
130       AnnotationSet annots2Copy = inputAS.getContained(start, end);
131       //copy them to the new set and delete them from the old one
132       annots2Move.addAll(annots2Copy);
133     }
134     outputAS.addAll(annots2Move);
135     inputAS.removeAll(annots2Move);
136 
137 
138   } // execute()
139 
140   public void setTagASName(String newTagASName) {
141     //if given an empty string, set to the default set
142     if ("".equals(newTagASName))
143       tagASName = null;
144     else
145       tagASName = newTagASName;
146   }
147 
148   public String getTagASName() {
149     return tagASName;
150   }
151 
152   public void setInputASName(String newInputASName) {
153     inputASName = newInputASName;
154   }
155 
156   public String getInputASName() {
157     return inputASName;
158   }
159 
160   public void setOutputASName(String newOutputASName) {
161     outputASName = newOutputASName;
162   }
163 
164   public String getOutputASName() {
165     return outputASName;
166   }
167 
168   public void setTextTagName(String newTextTagName) {
169     textTagName = newTextTagName;
170   }
171 
172   public String getTextTagName() {
173     return textTagName;
174   }
175 
176   public List getAnnotationTypes() {
177     return this.annotationTypes;
178   }
179 
180   public void setAnnotationTypes(List newTypes) {
181     annotationTypes = newTypes;
182   }
183 
184 
185 } // class AnnotationSetTransfer
186