CleartkExtractor implements
    FeatureExtractor1<FOCUS_T>, FeatureExtractor2<FOCUS_T, FOCUS_T> { @Override public List<Feature> extract(JCas view, FOCUS_T focusAnnotation) throws CleartkExtractorException { return this.extract(view, focusAnnotation, new NoBounds()); } CleartkExtractor(
      Class<SEARCH_T> annotationClass,
      FeatureExtractor1<SEARCH_T> extractor,
      Context... contexts); @Override List<Feature> extract(JCas view, FOCUS_T focusAnnotation); List<Feature> extractWithin(JCas view, FOCUS_T focusAnnotation, Annotation boundsAnnotation); List<Feature> extractBetween(JCas view, FOCUS_T annotation1, FOCUS_T annotation2); @Override java.util.List<Feature> extract(JCas view, FOCUS_T annotation1, FOCUS_T annotation2);  }
@Test public void testBasic() throws Exception { CleartkExtractor<Chunk, Token> extractor = new CleartkExtractor<Chunk, Token>( Token.class, new CoveredTextExtractor<Token>(), new Preceding(2), new Preceding(3, 6), new Covered(), new FirstCovered(1), new FirstCovered(1, 3), new LastCovered(1), new LastCovered(1, 3), new Following(1, 3), new Following(3, 5), new Preceding(5, 6), new Following(5, 6)); this.tokenBuilder.buildTokens( this.jCas, "The quick brown fox jumped over the lazy dog.", "The quick brown fox jumped over the lazy dog .", "DT JJ JJ NN VBD IN DT JJ NN ."); Chunk chunk = new Chunk(this.jCas, 20, 31); chunk.addToIndexes(); Assert.assertEquals("jumped over", chunk.getCoveredText()); List<Feature> features = extractor.extract(this.jCas, chunk); Assert.assertEquals(19, features.size()); Iterator<Feature> iter = features.iterator(); this.assertFeature("Preceding_0_2_1", "brown", iter.next()); this.assertFeature("Preceding_0_2_0", "fox", iter.next()); this.assertFeature("Preceding_3_6_5", "OOB2", iter.next()); this.assertFeature("Preceding_3_6_4", "OOB1", iter.next()); this.assertFeature("Preceding_3_6_3", "The", iter.next()); this.assertFeature("Covered_0", "jumped", iter.next()); this.assertFeature("Covered_1", "over", iter.next()); this.assertFeature("FirstCovered_0_1_0", "jumped", iter.next()); this.assertFeature("FirstCovered_1_3_1", "over", iter.next()); this.assertFeature("FirstCovered_1_3_2", "OOB1", iter.next()); this.assertFeature("LastCovered_0_1_0", "over", iter.next()); this.assertFeature("LastCovered_1_3_2", "OOB1", iter.next()); this.assertFeature("LastCovered_1_3_1", "jumped", iter.next()); this.assertFeature("Following_1_3_1", "lazy", iter.next()); this.assertFeature("Following_1_3_2", "dog", iter.next()); this.assertFeature("Following_3_5_3", ".", iter.next()); this.assertFeature("Following_3_5_4", "OOB1", iter.next()); this.assertFeature("Preceding_5_6_5", "OOB2", iter.next()); this.assertFeature("Following_5_6_5", "OOB2", iter.next()); }
@Test public void testBag() throws Exception { CleartkExtractor<Chunk, Token> extractor = new CleartkExtractor<Chunk, Token>( Token.class, new TypePathExtractor<Token>(Token.class, "pos"), new Bag(new Preceding(2)), new Bag(new Preceding(3, 6)), new Bag(new FirstCovered(1), new LastCovered(1)), new Bag(new Following(1, 3)), new Bag(new Following(3, 5)), new Bag(new Preceding(1), new Following(1)), new Bag(new Bag(new Bag(new Following(1, 3))))); this.tokenBuilder.buildTokens( this.jCas, "The quick brown fox jumped over the lazy dog.", "The quick brown fox jumped over the lazy dog .", "DT JJ JJ NN VBD IN DT JJ NN ."); Chunk chunk = new Chunk(this.jCas, 20, 31); chunk.addToIndexes(); Assert.assertEquals("jumped over", chunk.getCoveredText()); List<Feature> features = extractor.extract(this.jCas, chunk); Assert.assertEquals(15, features.size()); Iterator<Feature> iter = features.iterator(); this.assertFeature("Bag_Preceding_0_2_TypePath(Pos)", "JJ", iter.next()); this.assertFeature("Bag_Preceding_0_2_TypePath(Pos)", "NN", iter.next()); this.assertFeature("Bag_Preceding_3_6_TypePath(Pos)", "OOB2", iter.next()); this.assertFeature("Bag_Preceding_3_6_TypePath(Pos)", "OOB1", iter.next()); this.assertFeature("Bag_Preceding_3_6_TypePath(Pos)", "DT", iter.next()); this.assertFeature("Bag_FirstCovered_0_1_LastCovered_0_1_TypePath(Pos)", "VBD", iter.next()); this.assertFeature("Bag_FirstCovered_0_1_LastCovered_0_1_TypePath(Pos)", "IN", iter.next()); this.assertFeature("Bag_Following_1_3_TypePath(Pos)", "JJ", iter.next()); this.assertFeature("Bag_Following_1_3_TypePath(Pos)", "NN", iter.next()); this.assertFeature("Bag_Following_3_5_TypePath(Pos)", ".", iter.next()); this.assertFeature("Bag_Following_3_5_TypePath(Pos)", "OOB1", iter.next()); this.assertFeature("Bag_Preceding_0_1_Following_0_1_TypePath(Pos)", "NN", iter.next()); this.assertFeature("Bag_Preceding_0_1_Following_0_1_TypePath(Pos)", "DT", iter.next()); this.assertFeature("Bag_Bag_Bag_Following_1_3_TypePath(Pos)", "JJ", iter.next()); this.assertFeature("Bag_Bag_Bag_Following_1_3_TypePath(Pos)", "NN", iter.next()); }
@Test public void testCounts() throws Exception { CleartkExtractor<Chunk, Token> extractor = new CleartkExtractor<Chunk, Token>( Token.class, new CoveredTextExtractor<Token>(), new Count(new Preceding(2)), new Count(new Covered()), new Count(new Following(1, 5)), new Count(new Preceding(3), new Following(4)), new Count(new Ngrams(2, new Preceding(3), new Following(4))), new Count(new Ngram(new Preceding(2), new Following(2))), new Count(new Bag(new Preceding(2))), new Count(new Count(new Preceding(2))), new Count(new Count(new Count(new Preceding(2))))); this.tokenBuilder.buildTokens(this.jCas, "aa bb cc bb aa cc bb cc aa"); Chunk chunk = new Chunk(this.jCas, 9, 11); chunk.addToIndexes(); Assert.assertEquals("bb", chunk.getCoveredText()); List<Feature> features = extractor.extract(this.jCas, chunk); Assert.assertEquals(21, features.size()); Iterator<Feature> iter = features.iterator(); this.assertFeature("Count_Preceding_0_2_bb", 1, iter.next()); this.assertFeature("Count_Preceding_0_2_cc", 1, iter.next()); this.assertFeature("Count_Covered_bb", 1, iter.next()); this.assertFeature("Count_Following_1_5_cc", 2, iter.next()); this.assertFeature("Count_Following_1_5_bb", 1, iter.next()); this.assertFeature("Count_Following_1_5_aa", 1, iter.next()); this.assertFeature("Count_Preceding_0_3_Following_0_4_aa", 2, iter.next()); this.assertFeature("Count_Preceding_0_3_Following_0_4_bb", 2, iter.next()); this.assertFeature("Count_Preceding_0_3_Following_0_4_cc", 3, iter.next()); this.assertFeature("Count_2grams_Preceding_0_3_Following_0_4_aa_bb", 1, iter.next()); this.assertFeature("Count_2grams_Preceding_0_3_Following_0_4_bb_cc", 2, iter.next()); this.assertFeature("Count_2grams_Preceding_0_3_Following_0_4_cc_aa", 1, iter.next()); this.assertFeature("Count_2grams_Preceding_0_3_Following_0_4_aa_cc", 1, iter.next()); this.assertFeature("Count_2grams_Preceding_0_3_Following_0_4_cc_bb", 1, iter.next()); this.assertFeature("Count_Ngram_Preceding_0_2_Following_0_2_bb_cc_aa_cc", 1, iter.next()); this.assertFeature("Count_Bag_Preceding_0_2_bb", 1, iter.next()); this.assertFeature("Count_Bag_Preceding_0_2_cc", 1, iter.next()); this.assertFeature("Count_Count_Preceding_0_2_bb_1", 1, iter.next()); this.assertFeature("Count_Count_Preceding_0_2_cc_1", 1, iter.next()); this.assertFeature("Count_Count_Count_Preceding_0_2_bb_1_1", 1, iter.next()); this.assertFeature("Count_Count_Count_Preceding_0_2_cc_1_1", 1, iter.next()); }
@Test public void testCounts2() throws Exception { CleartkExtractor<Chunk, Token> extractor = new CleartkExtractor<Chunk, Token>( Token.class, new TypePathExtractor<Token>(Token.class, "pos"), new Count(new Preceding(2)), new Count(new Covered()), new Count(new Following(1, 5)), new Count(new Preceding(3), new Following(4)), new Count(new Ngrams(2, new Preceding(3), new Following(4))), new Count(new Ngram(new Preceding(2), new Following(2))), new Count(new Bag(new Preceding(2)))); this.tokenBuilder.buildTokens( this.jCas, "aa bb cc bb aa cc bb cc aa", "aa bb cc bb aa cc bb cc aa", "p1 p2 p2 p3 p3 p3 p3 p4 p5"); Chunk chunk = new Chunk(this.jCas, 9, 11); chunk.addToIndexes(); Assert.assertEquals("bb", chunk.getCoveredText()); List<Feature> features = extractor.extract(this.jCas, chunk); Assert.assertEquals(16, features.size()); Iterator<Feature> iter = features.iterator(); this.assertFeature("Count_Preceding_0_2_TypePath(Pos)_p2", 2, iter.next()); this.assertFeature("Count_Covered_TypePath(Pos)_p3", 1, iter.next()); this.assertFeature("Count_Following_1_5_TypePath(Pos)_p3", 2, iter.next()); this.assertFeature("Count_Following_1_5_TypePath(Pos)_p4", 1, iter.next()); this.assertFeature("Count_Following_1_5_TypePath(Pos)_p5", 1, iter.next()); this.assertFeature("Count_Preceding_0_3_Following_0_4_TypePath(Pos)_p1", 1, iter.next()); this.assertFeature("Count_Preceding_0_3_Following_0_4_TypePath(Pos)_p2", 2, iter.next()); this.assertFeature("Count_Preceding_0_3_Following_0_4_TypePath(Pos)_p3", 3, iter.next()); this.assertFeature("Count_Preceding_0_3_Following_0_4_TypePath(Pos)_p4", 1, iter.next()); this.assertFeature( "Count_2grams_Preceding_0_3_Following_0_4_TypePath(Pos)_p1_p2", 1, iter.next()); this.assertFeature( "Count_2grams_Preceding_0_3_Following_0_4_TypePath(Pos)_p2_p2", 1, iter.next()); this.assertFeature( "Count_2grams_Preceding_0_3_Following_0_4_TypePath(Pos)_p2_p3", 1, iter.next()); this.assertFeature( "Count_2grams_Preceding_0_3_Following_0_4_TypePath(Pos)_p3_p3", 2, iter.next()); this.assertFeature( "Count_2grams_Preceding_0_3_Following_0_4_TypePath(Pos)_p3_p4", 1, iter.next()); this.assertFeature( "Count_Ngram_Preceding_0_2_Following_0_2_TypePath(Pos)_p2_p2_p3_p3", 1, iter.next()); this.assertFeature("Count_Bag_Preceding_0_2_TypePath(Pos)_p2", 2, iter.next()); }
@Test public void testNgram() throws Exception { CleartkExtractor<Chunk, Token> extractor = new CleartkExtractor<Chunk, Token>( Token.class, new CoveredTextExtractor<Token>(), new Ngram(new Preceding(2)), new Ngram(new Preceding(3, 6)), new Ngram(new Preceding(1), new FirstCovered(1), new LastCovered(1)), new Ngram(new Following(1, 3)), new Ngram(new Following(3, 5)), new Ngram(new Preceding(2), new Following(1, 2))); this.tokenBuilder.buildTokens( this.jCas, "The quick brown fox jumped over the lazy dog.", "The quick brown fox jumped over the lazy dog .", "DT JJ JJ NN VBD IN DT JJ NN ."); Chunk chunk = new Chunk(this.jCas, 20, 31); chunk.addToIndexes(); Assert.assertEquals("jumped over", chunk.getCoveredText()); List<Feature> features = extractor.extract(this.jCas, chunk); Assert.assertEquals(6, features.size()); Iterator<Feature> iter = features.iterator(); this.assertFeature("Ngram_Preceding_0_2", "brown_fox", iter.next()); this.assertFeature("Ngram_Preceding_3_6", "OOB2_OOB1_The", iter.next()); this.assertFeature( "Ngram_Preceding_0_1_FirstCovered_0_1_LastCovered_0_1", "fox_jumped_over", iter.next()); this.assertFeature("Ngram_Following_1_3", "lazy_dog", iter.next()); this.assertFeature("Ngram_Following_3_5", "._OOB1", iter.next()); this.assertFeature("Ngram_Preceding_0_2_Following_1_2", "brown_fox_lazy", iter.next()); extractor = new CleartkExtractor<Chunk, Token>( Token.class, new CoveredTextExtractor<Token>(), new Ngram(new Preceding(2), new Following(2))); jCas = JCasFactory.createJCas(); this.tokenBuilder.buildTokens(this.jCas, "A B C D E"); chunk = new Chunk(this.jCas, 4, 5); chunk.addToIndexes(); Assert.assertEquals("C", chunk.getCoveredText()); features = extractor.extract(this.jCas, chunk); Assert.assertEquals(1, features.size()); this.assertFeature("Ngram_Preceding_0_2_Following_0_2", "A_B_D_E", features.get(0)); }
@Test public void testNgrams() throws Exception { CleartkExtractor<Chunk, Token> extractor = new CleartkExtractor<Chunk, Token>( Token.class, new CoveredTextExtractor<Token>(), new Ngrams(2, new Preceding(3)), new Ngrams(2, new Following(3)), new Ngrams(4, new Preceding(3), new Following(3)), new Ngrams(3, new Preceding(1, 5)), new Ngrams(2, new Covered()), new Ngrams(3, new Covered())); this.tokenBuilder.buildTokens( this.jCas, "The quick brown fox jumped over the lazy dog.", "The quick brown fox jumped over the lazy dog .", "DT JJ JJ NN VBD IN DT JJ NN ."); Chunk chunk = new Chunk(this.jCas, 20, 31); chunk.addToIndexes(); Assert.assertEquals("jumped over", chunk.getCoveredText()); List<Feature> features = extractor.extract(this.jCas, chunk); Assert.assertEquals(10, features.size()); Iterator<Feature> iter = features.iterator(); this.assertFeature("2grams_Preceding_0_3", "quick_brown", iter.next()); this.assertFeature("2grams_Preceding_0_3", "brown_fox", iter.next()); this.assertFeature("2grams_Following_0_3", "the_lazy", iter.next()); this.assertFeature("2grams_Following_0_3", "lazy_dog", iter.next()); this.assertFeature("4grams_Preceding_0_3_Following_0_3", "quick_brown_fox_the", iter.next()); this.assertFeature("4grams_Preceding_0_3_Following_0_3", "brown_fox_the_lazy", iter.next()); this.assertFeature("4grams_Preceding_0_3_Following_0_3", "fox_the_lazy_dog", iter.next()); this.assertFeature("3grams_Preceding_1_5", "OOB1_The_quick", iter.next()); this.assertFeature("3grams_Preceding_1_5", "The_quick_brown", iter.next()); this.assertFeature("2grams_Covered", "jumped_over", iter.next()); }
@Test public void testFocus() throws Exception { CleartkExtractor<Token, Token> extractor = new CleartkExtractor<Token, Token>( Token.class, new CoveredTextExtractor<Token>(), new Focus(), new Bag(new Preceding(1), new Focus()), new Ngram(new Following(2), new Focus())); this.tokenBuilder.buildTokens( this.jCas, "The quick brown fox jumped over the lazy dog.", "The quick brown fox jumped over the lazy dog .", "DT JJ JJ NN VBD IN DT JJ NN ."); Token jumped = JCasUtil.selectByIndex(this.jCas, Token.class, 4); Assert.assertEquals("jumped", jumped.getCoveredText()); List<Feature> features = extractor.extract(this.jCas, jumped); Assert.assertEquals(4, features.size()); Iterator<Feature> iter = features.iterator(); this.assertFeature("Focus", "jumped", iter.next()); this.assertFeature("Bag_Preceding_0_1_Focus", "fox", iter.next()); this.assertFeature("Bag_Preceding_0_1_Focus", "jumped", iter.next()); this.assertFeature("Ngram_Following_0_2_Focus", "over_the_jumped", iter.next()); CleartkExtractor<Token, Chunk> chunkExtractor = new CleartkExtractor<Token, Chunk>( Chunk.class, new CoveredTextExtractor<Chunk>(), new Focus()); try { chunkExtractor.extract(this.jCas, jumped); Assert.fail("Expected exception from Focus of wrong type"); } catch (ClassCastException e) { } }
@Test public void testNestedNames() throws Exception { CleartkExtractor<Chunk, Token> extractor = new CleartkExtractor<Chunk, Token>( Token.class, new TypePathExtractor<Token>(Token.class, "pos"), new Count(new Preceding(1, 5), new Covered()), new Bag(new Preceding(3)), new Ngram(new Following(2)), new Ngrams(3, new Following(1, 6))); this.tokenBuilder.buildTokens( this.jCas, "The quick brown fox jumped over the lazy dog.", "The quick brown fox jumped over the lazy dog .", "DT JJ JJ NN VBD IN DT JJ NN ."); Chunk chunk = new Chunk(this.jCas, 20, 31); chunk.addToIndexes(); Assert.assertEquals("jumped over", chunk.getCoveredText()); List<Feature> features = extractor.extract(this.jCas, chunk); Iterator<Feature> iter = features.iterator(); this.assertFeature("Count_Preceding_1_5_Covered_TypePath(Pos)_OOB1", 1, iter.next()); this.assertFeature("Count_Preceding_1_5_Covered_TypePath(Pos)_DT", 1, iter.next()); this.assertFeature("Count_Preceding_1_5_Covered_TypePath(Pos)_JJ", 2, iter.next()); this.assertFeature("Count_Preceding_1_5_Covered_TypePath(Pos)_VBD", 1, iter.next()); this.assertFeature("Count_Preceding_1_5_Covered_TypePath(Pos)_IN", 1, iter.next()); this.assertFeature("Bag_Preceding_0_3_TypePath(Pos)", "JJ", iter.next()); this.assertFeature("Bag_Preceding_0_3_TypePath(Pos)", "JJ", iter.next()); this.assertFeature("Bag_Preceding_0_3_TypePath(Pos)", "NN", iter.next()); this.assertFeature("Ngram_Following_0_2_TypePath(Pos)", "DT_JJ", iter.next()); this.assertFeature("3grams_Following_1_6_TypePath(Pos)", "JJ_NN_.", iter.next()); this.assertFeature("3grams_Following_1_6_TypePath(Pos)", "NN_._OOB1", iter.next()); this.assertFeature("3grams_Following_1_6_TypePath(Pos)", "._OOB1_OOB2", iter.next()); Assert.assertFalse(iter.hasNext()); }