|
AnnotationDeletePR |
|
1 /* 2 * AnnotationDeletePR.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, 19/10/2001 12 * 13 * $Id: AnnotationDeletePR.java,v 1.6 2002/03/06 17:15:40 kalina Exp $ 14 */ 15 16 package gate.creole.annotdelete; 17 18 import java.util.*; 19 import gate.*; 20 import gate.creole.*; 21 import gate.util.*; 22 23 /** 24 * This class is the implementation of a processing resource which 25 * deletes all annotations and sets other than 'original markups'. 26 * If put at the start of an application, it'll ensure that the 27 * document is restored to its clean state before being processed. 28 */ 29 public class AnnotationDeletePR extends AbstractLanguageAnalyser 30 implements ProcessingResource { 31 32 public static final String 33 TRANSD_DOCUMENT_PARAMETER_NAME = "document"; 34 35 public static final String 36 TRANSD_ANNOT_TYPES_PARAMETER_NAME = "annotationTypes"; 37 38 protected String markupSetName = GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME; 39 protected List annotationTypes; 40 41 /** Initialise this resource, and return it. */ 42 public Resource init() throws ResourceInstantiationException 43 { 44 return super.init(); 45 } // init() 46 47 /** 48 * Reinitialises the processing resource. After calling this method the 49 * resource should be in the state it is after calling init. 50 * If the resource depends on external resources (such as rules files) then 51 * the resource will re-read those resources. If the data used to create 52 * the resource has changed since the resource has been created then the 53 * resource will change too after calling reInit(). 54 */ 55 public void reInit() throws ResourceInstantiationException 56 { 57 init(); 58 } // reInit() 59 60 /** Run the resource. */ 61 public void execute() throws ExecutionException { 62 63 if(document == null) 64 throw new GateRuntimeException("No document to process!"); 65 66 //first clear the default set, which cannot be removed 67 if (annotationTypes == null || annotationTypes.isEmpty()) 68 document.getAnnotations().clear(); 69 else 70 removeSubSet(document.getAnnotations()); 71 72 //get the names of all sets 73 Map namedSets = document.getNamedAnnotationSets(); 74 //nothing left to do if there are no named sets 75 if (namedSets == null || namedSets.isEmpty()) 76 return; 77 78 //loop through the sets and delete them all unless they're original markups 79 List setNames = new ArrayList(namedSets.keySet()); 80 Iterator iter = setNames.iterator(); 81 while (iter.hasNext()) { 82 String setName = (String) iter.next(); 83 if (! setName.equals(markupSetName)) { 84 if(annotationTypes == null || annotationTypes.isEmpty()) 85 document.removeAnnotationSet(setName); 86 else 87 removeSubSet(document.getAnnotations(setName)); 88 }//if 89 } 90 91 } // execute() 92 93 private void removeSubSet(AnnotationSet theSet) { 94 AnnotationSet toRemove = theSet.get(new HashSet(annotationTypes)); 95 if (toRemove == null || toRemove.isEmpty()) 96 return; 97 theSet.removeAll(toRemove); 98 99 }//removeSubSet 100 101 public void setMarkupASName(String newMarkupASName) { 102 markupSetName = newMarkupASName; 103 } 104 105 public String getMarkupASName() { 106 return markupSetName; 107 } 108 109 public List getAnnotationTypes() { 110 return this.annotationTypes; 111 } 112 113 public void setAnnotationTypes(List newTypes) { 114 annotationTypes = newTypes; 115 } 116 117 } // class AnnotationSetTransfer 118
|
AnnotationDeletePR |
|