Revision: 18602
http://sourceforge.net/p/gate/code/18602
Author: johann_p
Date: 2015-03-20 18:14:18 +0000 (Fri, 20 Mar 2015)
Log Message:
-----------
Add Utils methods for removing/adding/intersecting annotations.
This should make it easier to get e.g. the annotations which
are coextensive with some annotation x, without that annotation x.
Utils.minus(Utils.getCoextensiveAnnotations(set,x),x)
Modified Paths:
--------------
gate/trunk/src/main/gate/Utils.java
gate/trunk/src/test/gate/TestGate.java
Added Paths:
-----------
gate/trunk/src/test/gate/TestUtils.java
Modified: gate/trunk/src/main/gate/Utils.java
===================================================================
--- gate/trunk/src/main/gate/Utils.java 2015-03-20 16:24:59 UTC (rev 18601)
+++ gate/trunk/src/main/gate/Utils.java 2015-03-20 18:14:18 UTC (rev 18602)
@@ -16,6 +16,7 @@
package gate;
import gate.annotation.AnnotationSetImpl;
+import gate.annotation.ImmutableAnnotationSetImpl;
import gate.creole.ConditionalSerialController;
import gate.creole.RunningStrategy;
import gate.util.FeatureBearer;
@@ -25,6 +26,7 @@
import java.io.File;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
@@ -1167,7 +1169,7 @@
}
return sb.toString();
}
- static private Pattern varnamePattern =
Pattern.compile("(\\$\\$?)([a-zA-Z]*)\\{([^}]+)\\}");
+ private static final Pattern varnamePattern =
Pattern.compile("(\\$\\$?)([a-zA-Z]*)\\{([^}]+)\\}");
/**
* Load a plugin from the default GATE plugins directory.
@@ -1175,6 +1177,7 @@
* This will load the plugin with the specified directory name from the
* default GATE plugins path, if GATE knows its own location.
*
+ * @param dirName The directory name of the plugin within the standard GATE
plugins directory.
*/
public static void loadPlugin(String dirName) {
File gatehome = Gate.getGateHome();
@@ -1198,5 +1201,141 @@
throw new GateRuntimeException("Could not register plugin directory
"+pluginDir,ex);
}
}
+
+ /**
+ * Return the given set with the given annotations removed.
+ *
+ * This returns a new immutable annotation set, which contains all the
annotations from origSet
+ * except the given annotations. The removal is not based on equality but on
the id of the
+ * annotation: an annotation in origSet which has the same id as the
annotation except is removed
+ * in the returned set.
+ * <p>
+ * NOTE: Annotation ids are only unique within a document, so you should
never mix annotations
+ * from different documents when using this method!
+ *
+ * @param origSet The annotation set from which to remove the given
annotation
+ * @param except The annotation to remove from the given set
+ * @return A new immutable annotation set with the given annotation removed
from the original set
+ */
+ public static AnnotationSet minus(AnnotationSet origSet, Annotation...
except) {
+ return minus(origSet, Arrays.asList(except));
+ }
+ /**
+ * Return the given set with the given annotations removed.
+ *
+ * This returns a new immutable annotation set, which contains all the
annotations from origSet
+ * except the annotations given in the collection of exceptions.
+ * The removal is not based on equality but on the id of the
+ * annotations: an annotation in origSet which has the same id as an
annotation from the exceptions
+ * is removed in the returned set.
+ * <p>
+ * NOTE: Annotation ids are only unique within a document, so you should
never mix annotations
+ * from different documents when using this method!
+ *
+ * @param origSet The annotation set from which to remove the given
exceptions
+ * @param exceptions The annotations to remove from the given set
+ * @return A new immutable annotation set with the exceptions removed from
the original set
+ */
+ public static AnnotationSet minus(AnnotationSet origSet,
Collection<Annotation> exceptions) {
+ Set<Integer> ids = new HashSet<Integer>();
+ for(Annotation exception : exceptions) {
+ ids.add(exception.getId());
+ }
+ List<Annotation> tmp = new ArrayList<Annotation>();
+ for(Annotation ann : origSet) {
+ if(!ids.contains(ann.getId())) {
+ tmp.add(ann);
+ }
+ }
+ return new ImmutableAnnotationSetImpl(origSet.getDocument(),tmp);
+ }
+
+ /**
+ * Return the given set with the given annotations added.
+ *
+ * This returns a new immutable annotation set, which contains all the
annotations from origSet
+ * plus the given annotations to add. The addition is not based on equality
but on the id of the
+ * annotations: any new annotation is added if its annotation id differs
from all the ids
+ * already in the set.
+ * <p>
+ * NOTE: Annotation ids are only unique within a document, so you should
never mix annotations
+ * from different documents when using this method!
+ *
+ * @param origSet The annotation set from which to remove the given
exceptions
+ * @param toAdd The annotations to add to the given set
+ * @return A new immutable annotation set with the given annotations added
+ */
+ public static AnnotationSet plus(AnnotationSet origSet, Annotation... toAdd)
{
+ return plus(origSet,Arrays.asList(toAdd));
+ }
+
+ /**
+ * Return the given set with the given annotations added.
+ *
+ * This returns a new immutable annotation set, which contains all the
annotations from origSet
+ * plus the given annotations added. The addition is not based on equality
but on the id of the
+ * annotations: any new annotation is added if its annotation id differs
from all the ids
+ * already in the set.
+ * <p>
+ * NOTE: Annotation ids are only unique within a document, so you should
never mix annotations
+ * from different documents when using this method!
+ *
+ * @param origSet The annotation set from which to remove the given
exceptions
+ * @param toAdd A collection of annotations to add to the original set
+ * @return A new immutable annotation set with the annotations from the
collection added.
+ */
+ public static AnnotationSet plus(AnnotationSet origSet,
Collection<Annotation> toAdd) {
+ Set<Integer> ids = new HashSet<Integer>();
+ for(Annotation orig : origSet) {
+ ids.add(orig.getId());
+ }
+ List<Annotation> tmp = new ArrayList<Annotation>();
+ tmp.addAll(origSet);
+ for(Annotation ann : toAdd) {
+ if(!ids.contains(ann.getId())) {
+ tmp.add(ann);
+ }
+ }
+ return new ImmutableAnnotationSetImpl(origSet.getDocument(),tmp);
+ }
+
+
+ /**
+ * Return the subset from the original set that matches one of the given
annotations.
+ *
+ * This returns a new immutable annotation set, which contains all the
annotations from origSet
+ * which are also among the annotations given. The check for matching
annotations is not based
+ * on equality but on the id of the
+ * annotations: an annotation from the original set is included in the
returned set if its
+ * annotation id matches the annotation id of any of the annotations given.
+ * <p>
+ * NOTE: Annotation ids are only unique within a document, so you should
never mix annotations
+ * from different documents when using this method!
+ *
+ * @param origSet The annotation set from which to select only the given
annotations.
+ * @param others the given annotations
+ * @return A new immutable annotation set with the interesection between
original set and given annotations
+ */
+ public static AnnotationSet intersect(AnnotationSet origSet, Annotation...
others) {
+ return intersect(origSet,Arrays.asList(others));
+ }
+
+ public static AnnotationSet intersect(AnnotationSet origSet,
Collection<Annotation> others) {
+ if(others.isEmpty()) {
+ return new ImmutableAnnotationSetImpl(origSet.getDocument(),null);
+ }
+ Set<Integer> ids = new HashSet<Integer>();
+ for(Annotation other : others) {
+ ids.add(other.getId());
+ }
+ List<Annotation> tmp = new ArrayList<Annotation>();
+ for(Annotation ann : origSet) {
+ if(ids.contains(ann.getId())) {
+ tmp.add(ann);
+ }
+ }
+ return new ImmutableAnnotationSetImpl(origSet.getDocument(),tmp);
+ }
+
}
Modified: gate/trunk/src/test/gate/TestGate.java
===================================================================
--- gate/trunk/src/test/gate/TestGate.java 2015-03-20 16:24:59 UTC (rev
18601)
+++ gate/trunk/src/test/gate/TestGate.java 2015-03-20 18:14:18 UTC (rev
18602)
@@ -143,8 +143,9 @@
Test theSuite = (Test)suiteMethod.invoke(null);
suite.addTest(theSuite);
} else {
+ // no test name specified, so run them all
+ suite.addTest(TestUtils.suite());
suite.addTest(TestAnnic.suite());
- // no test name specified, so run them all
//WordNet has been moved into a plugin along with the test
//suite.addTest(TestWordNet.suite());
Copied: gate/trunk/src/test/gate/TestUtils.java (from rev 18595,
gate/trunk/src/test/gate/annotation/TestAnnotation.java)
===================================================================
--- gate/trunk/src/test/gate/TestUtils.java (rev 0)
+++ gate/trunk/src/test/gate/TestUtils.java 2015-03-20 18:14:18 UTC (rev
18602)
@@ -0,0 +1,102 @@
+/*
+ * TestAnnotation.java
+ *
+ * Copyright (c) 1995-2015, The University of Sheffield. See the file
+ * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
+ *
+ * This file is part of GATE (see http://gate.ac.uk/), and is free
+ * software, licenced under the GNU Library General Public License,
+ * Version 2, June 1991 (in the distribution as file licence.html,
+ * and also available at http://gate.ac.uk/gate/licence.html).
+ *
+ */
+
+package gate;
+
+import junit.framework.*;
+
+import static gate.Utils.*;
+import gate.creole.ResourceInstantiationException;
+import gate.util.InvalidOffsetException;
+
+/**
+ * Tests for the gate.Utils methods
+ */
+public class TestUtils extends TestCase
+{
+ /** Construction */
+ public TestUtils(String name) { super(name); }
+
+ /**
+ * Fixture set up.
+ *
+ * @throws java.lang.Exception
+ */
+ @Override
+ public void setUp() throws Exception
+ {
+ Gate.setNetConnected(false);
+ if (Gate.getGateHome() == null) Gate.init();
+ } // setUp
+
+
+ // test getting and combining annotations
+ public void testAnnotationSetHandling()
+ throws InvalidOffsetException, ResourceInstantiationException {
+ // create a new document of 100 spaces
+ Document doc = Factory.newDocument(new String(new char[100]).replace("\0",
" "));
+ // get an annotation set for the first couple of tests
+ AnnotationSet set1 = doc.getAnnotations("s1");
+ // add 3 coextensive annotations on top of each other
+ Annotation ann1_1 = set1.get(addAnn(set1,0,5,"t1.1",featureMap()));
+ Annotation ann1_2 = set1.get(addAnn(set1,0,5,"t1.2",featureMap()));
+ Annotation ann1_3 = set1.get(addAnn(set1,0,5,"t1.3",featureMap()));
+ // add 3 overlapping annotations
+ Annotation ann2_1 = set1.get(addAnn(set1,10,15,"t2.1",featureMap()));
+ Annotation ann2_2 = set1.get(addAnn(set1,11,16,"t2.2",featureMap()));
+ Annotation ann2_3 = set1.get(addAnn(set1,12,17,"t2.3",featureMap()));
+
+ AnnotationSet ret = getCoextensiveAnnotations(set1,ann1_1);
+ assertEquals(3, ret.size());
+
+ ret = minus(ret,ann1_1);
+ assertEquals(2, ret.size());
+
+ ret = minus(ret,ann2_1);
+ assertEquals(2, ret.size());
+
+ ret = minus(ret);
+ assertEquals(2, ret.size());
+
+ ret = minus(ret,ann1_2);
+ assertEquals(1, ret.size());
+
+ ret = getOverlappingAnnotations(set1, ann2_1);
+ assertEquals(3, ret.size());
+
+ ret = plus(ret,ann1_2);
+ assertEquals(4, ret.size());
+
+ ret = intersect(getCoextensiveAnnotations(set1,ann1_1),ret);
+ assertEquals(1, ret.size());
+
+
+ } // testAnnotationSetHandling
+
+ public static Test suite() {
+ return new TestSuite(TestUtils.class);
+ } // suite
+
+
+ public static void main(String[] args){
+
+ try{
+ TestUtils testUtils = new TestUtils("");
+ testUtils.setUp();
+ testUtils.testAnnotationSetHandling();
+ }catch(Throwable t){
+ t.printStackTrace(System.err);
+ }
+ }
+} // class TestAnnotation
+
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Dive into the World of Parallel Programming The Go Parallel Website, sponsored
by Intel and developed in partnership with Slashdot Media, is your hub for all
things parallel software development, from weekly thought leadership blogs to
news, videos, case studies, tutorials and more. Take a look and join the
conversation now. http://goparallel.sourceforge.net/
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs