DistanceExtractor implements
    FeatureExtractor2<T, U> { public List<Feature> extract(JCas jCas, Annotation annotation1, Annotation annotation2) { Annotation firstAnnotation, secondAnnotation; if (annotation1.getBegin() <= annotation2.getBegin()) { firstAnnotation = annotation1; secondAnnotation = annotation2; } else { firstAnnotation = annotation2; secondAnnotation = annotation1; } String featureName = Feature.createName(this.name, "Distance", this.unitClass.getSimpleName()); int featureValue; if (secondAnnotation.getBegin() <= firstAnnotation.getEnd()) { featureValue = 0; } else { List<? extends Annotation> annotations = JCasUtil.selectCovered( jCas, unitClass, firstAnnotation.getEnd(), secondAnnotation.getBegin()); featureValue = annotations.size(); } return Collections.singletonList(new Feature(featureName, featureValue)); } DistanceExtractor(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 ."); DistanceExtractor<Token, Token> extractor = new DistanceExtractor<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); List<Feature> features = extractor.extract(jCas, token1, token2); assertEquals(0, features.get(0).getValue()); features = extractor.extract(jCas, token1, token3); assertEquals(1, features.get(0).getValue()); features = extractor.extract(jCas, token3, token1); assertEquals(1, features.get(0).getValue()); features = extractor.extract(jCas, token1, token5); assertEquals(3, features.get(0).getValue()); features = extractor.extract(jCas, token5, token1); assertEquals(3, features.get(0).getValue()); features = extractor.extract(jCas, token4, token5); assertEquals(0, features.get(0).getValue()); features = extractor.extract(jCas, token5, token4); assertEquals(0, features.get(0).getValue()); features = extractor.extract(jCas, token5, token5); assertEquals(0, features.get(0).getValue()); features = extractor.extract(jCas, new Annotation(jCas, 0, 3), token1); assertEquals(0, features.get(0).getValue()); features = extractor.extract(jCas, new Annotation(jCas, 0, 3), token2); assertEquals(0, features.get(0).getValue()); features = extractor.extract(jCas, new Annotation(jCas, 0, 3), token3); assertEquals(0, features.get(0).getValue()); features = extractor.extract(jCas, new Annotation(jCas, 0, 3), token4); assertEquals(1, features.get(0).getValue()); features = extractor.extract(jCas, new Annotation(jCas, 0, 3), token5); assertEquals(2, features.get(0).getValue()); Annotation annotation = new Annotation(jCas, 64, 65); assertEquals(".", annotation.getCoveredText()); features = extractor.extract(jCas, annotation, token1); assertEquals(11, features.get(0).getValue()); }