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.11 2002/03/06 17:15:41 kalina 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  
54    /** Initialise this resource, and return it. */
55    public Resource init() throws ResourceInstantiationException
56    {
57      return super.init();
58    } // init()
59  
60    /**
61    * Reinitialises the processing resource. After calling this method the
62    * resource should be in the state it is after calling init.
63    * If the resource depends on external resources (such as rules files) then
64    * the resource will re-read those resources. If the data used to create
65    * the resource has changed since the resource has been created then the
66    * resource will change too after calling reInit().
67    */
68    public void reInit() throws ResourceInstantiationException
69    {
70      init();
71    } // reInit()
72  
73    /** Run the resource. */
74    public void execute() throws ExecutionException {
75  
76      if(document == null)
77        throw new GateRuntimeException("No document to process!");
78  
79      if(inputASName != null && inputASName.equals(""))
80        inputASName = null;
81      if(outputASName != null && outputASName.equals(""))
82        outputASName = null;
83  
84      //get the input annotation set and the output one
85      AnnotationSet inputAS = (inputASName == null) ?
86                              document.getAnnotations() :
87                              document.getAnnotations(inputASName);
88      AnnotationSet outputAS = (outputASName == null) ?
89                               document.getAnnotations() :
90                               document.getAnnotations(outputASName);
91      AnnotationSet tagAS = (tagASName == null) ?
92                              document.getAnnotations() :
93                              document.getAnnotations(tagASName);
94  
95      //check if we have a BODY annotation
96      //if not, just copy all
97      if (textTagName == null || textTagName.equals("")) {
98        outputAS.addAll(inputAS);
99        return;
100     }
101 
102     //get the BODY annotation
103     bodyAnnotations = tagAS.get(textTagName);
104     if (bodyAnnotations == null || bodyAnnotations.isEmpty()) {
105       Out.prln("AST Warning: No text annotations of type " + textTagName +
106         " found, so transferring all annotations to the target set");
107       outputAS.addAll(inputAS);
108       return;
109     }
110 
111     List annots2Move = new ArrayList();
112     Iterator bodyIter = bodyAnnotations.iterator();
113     while (bodyIter.hasNext()) {
114       Annotation bodyAnn = (Annotation)bodyIter.next();
115       Long start = bodyAnn.getStartNode().getOffset();
116       Long end = bodyAnn.getEndNode().getOffset();
117 
118       //get all annotations we want transferred
119       AnnotationSet annots2Copy = inputAS.getContained(start, end);
120       //copy them to the new set and delete them from the old one
121       annots2Move.addAll(annots2Copy);
122     }
123     outputAS.addAll(annots2Move);
124     inputAS.removeAll(annots2Move);
125 
126 
127   } // execute()
128 
129   public void setTagASName(String newTagASName) {
130     //if given an empty string, set to the default set
131     if ("".equals(newTagASName))
132       tagASName = null;
133     else
134       tagASName = newTagASName;
135   }
136 
137   public String getTagASName() {
138     return tagASName;
139   }
140 
141   public void setInputASName(String newInputASName) {
142     inputASName = newInputASName;
143   }
144 
145   public String getInputASName() {
146     return inputASName;
147   }
148 
149   public void setOutputASName(String newOutputASName) {
150     outputASName = newOutputASName;
151   }
152 
153   public String getOutputASName() {
154     return outputASName;
155   }
156 
157   public void setTextTagName(String newTextTagName) {
158     textTagName = newTextTagName;
159   }
160 
161   public String getTextTagName() {
162     return textTagName;
163   }
164 
165 
166 } // class AnnotationSetTransfer
167