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.8 2001/11/08 17:23:30 cursu 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    protected String   tagASName =  GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME;
31    protected String                outputASName = "Filtered";
32    protected String                inputASName = null;
33    protected String                textTagName = "BODY";
34    protected gate.AnnotationSet    bodyAnnotations = null;
35  
36    /** Initialise this resource, and return it. */
37    public Resource init() throws ResourceInstantiationException
38    {
39      return super.init();
40    } // init()
41  
42    /**
43    * Reinitialises the processing resource. After calling this method the
44    * resource should be in the state it is after calling init.
45    * If the resource depends on external resources (such as rules files) then
46    * the resource will re-read those resources. If the data used to create
47    * the resource has changed since the resource has been created then the
48    * resource will change too after calling reInit().
49    */
50    public void reInit() throws ResourceInstantiationException
51    {
52      init();
53    } // reInit()
54  
55    /** Run the resource. */
56    public void execute() throws ExecutionException {
57  
58      if(document == null)
59        throw new GateRuntimeException("No document to process!");
60  
61      if(inputASName != null && inputASName.equals(""))
62        inputASName = null;
63      if(outputASName != null && outputASName.equals(""))
64        outputASName = null;
65  
66      //get the input annotation set and the output one
67      AnnotationSet inputAS = (inputASName == null) ?
68                              document.getAnnotations() :
69                              document.getAnnotations(inputASName);
70      AnnotationSet outputAS = (outputASName == null) ?
71                               document.getAnnotations() :
72                               document.getAnnotations(outputASName);
73      AnnotationSet tagAS = (tagASName == null) ?
74                              document.getAnnotations() :
75                              document.getAnnotations(tagASName);
76  
77      //check if we have a BODY annotation
78      //if not, just copy all
79      if (textTagName == null || textTagName.equals("")) {
80        outputAS.addAll(inputAS);
81        return;
82      }
83  
84      //get the BODY annotation
85      bodyAnnotations = tagAS.get(textTagName);
86      if (bodyAnnotations == null || bodyAnnotations.isEmpty()) {
87        Out.prln("AST Warning: No text annotations of type " + textTagName +
88          " found, so transferring all annotations to the target set");
89        outputAS.addAll(inputAS);
90        return;
91      }
92  
93      Iterator bodyIter = bodyAnnotations.iterator();
94      while (bodyIter.hasNext()) {
95        Annotation bodyAnn = (Annotation)bodyIter.next();
96        Long start = bodyAnn.getStartNode().getOffset();
97        Long end = bodyAnn.getEndNode().getOffset();
98  
99        //get all annotations we want transferred
100       AnnotationSet annots2Copy = inputAS.getContained(start, end);
101       //copy them to the new set and delete them from the old one
102       outputAS.addAll(annots2Copy);
103       inputAS.removeAll(annots2Copy);
104     }
105 
106 
107   } // execute()
108 
109   public void setTagASName(String newTagASName) {
110     tagASName = newTagASName;
111   }
112 
113   public String getTagASName() {
114     return tagASName;
115   }
116 
117   public void setInputASName(String newInputASName) {
118     inputASName = newInputASName;
119   }
120 
121   public String getInputASName() {
122     return inputASName;
123   }
124 
125   public void setOutputASName(String newOutputASName) {
126     outputASName = newOutputASName;
127   }
128 
129   public String getOutputASName() {
130     return outputASName;
131   }
132 
133   public void setTextTagName(String newTextTagName) {
134     textTagName = newTextTagName;
135   }
136 
137   public String getTextTagName() {
138     return textTagName;
139   }
140 
141 
142 } // class AnnotationSetTransfer
143