DirectedDistanceExtractor implements
    FeatureExtractor2<T, U> { public List<Feature> extract(JCas jCas, Annotation annotation1, Annotation annotation2) { String featureName = Feature.createName(this.name, "DDistance", this.unitClass.getSimpleName()); Annotation firstAnnotation, secondAnnotation; boolean negate = false; if (annotation1.getBegin() <= annotation2.getBegin()) { firstAnnotation = annotation1; secondAnnotation = annotation2; } else { firstAnnotation = annotation2; secondAnnotation = annotation1; negate = true; } int featureValue = 0; if (AnnotationUtil.overlaps(annotation1, annotation2)) { featureValue = 0; } else { List<? extends Annotation> annotations = JCasUtil.selectCovered( jCas, unitClass, firstAnnotation.getEnd(), secondAnnotation.getBegin()); featureValue = annotations.size() + 1; } if (negate) featureValue = -featureValue; return Collections.singletonList(new Feature(featureName, featureValue)); } DirectedDistanceExtractor(String name, Class<? extends Annotation> unitClass); List<Feature> extract(JCas jCas, Annotation annotation1, Annotation annotation2);  }
@Test public void test1() throws Exception { tokenBuilder.buildTokens( jCas, "A simple sentence to test the distance of tokens from each other.", "A simple sentence to test the distance of tokens from each other ."); DirectedDistanceExtractor<Token, Token> extractor = new DirectedDistanceExtractor<Token, Token>( null, Token.class); Token token1 = JCasUtil.selectByIndex(jCas, Token.class, 0); Token token2 = JCasUtil.selectByIndex(jCas, Token.class, 1); Token token3 = JCasUtil.selectByIndex(jCas, Token.class, 2); Token token4 = JCasUtil.selectByIndex(jCas, Token.class, 3); Token token5 = JCasUtil.selectByIndex(jCas, Token.class, 4); Feature feature = extractor.extract(jCas, token1, token2).get(0); assertEquals(1, feature.getValue()); feature = extractor.extract(jCas, token2, token1).get(0); assertEquals(-1, feature.getValue()); feature = extractor.extract(jCas, token1, token3).get(0); assertEquals(2, feature.getValue()); feature = extractor.extract(jCas, token3, token1).get(0); assertEquals(-2, feature.getValue()); feature = extractor.extract(jCas, token1, token5).get(0); assertEquals(4, feature.getValue()); feature = extractor.extract(jCas, token5, token1).get(0); assertEquals(-4, feature.getValue()); feature = extractor.extract(jCas, token4, token5).get(0); assertEquals(1, feature.getValue()); feature = extractor.extract(jCas, token5, token4).get(0); assertEquals(-1, feature.getValue()); feature = extractor.extract(jCas, token5, token5).get(0); assertEquals(0, feature.getValue()); feature = extractor.extract(jCas, new Annotation(jCas, 0, 3), token1).get(0); assertEquals(0, feature.getValue()); feature = extractor.extract(jCas, new Annotation(jCas, 0, 3), token2).get(0); assertEquals(0, feature.getValue()); feature = extractor.extract(jCas, new Annotation(jCas, 0, 3), token3).get(0); assertEquals(1, feature.getValue()); feature = extractor.extract(jCas, new Annotation(jCas, 0, 3), token4).get(0); assertEquals(2, feature.getValue()); feature = extractor.extract(jCas, new Annotation(jCas, 0, 3), token5).get(0); assertEquals(3, feature.getValue()); Annotation annotation = new Annotation(jCas, 64, 65); assertEquals(".", annotation.getCoveredText()); feature = extractor.extract(jCas, annotation, token1).get(0); assertEquals(-12, feature.getValue()); }