NGramAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas aJCas) throws AnalysisEngineProcessException { for (Sentence s : select(aJCas, Sentence.class)) { for (NGram ngram : NGramIterable.create(selectCovered(Token.class, s), n)) { ngram.addToIndexes(); } } }  @Override void process(JCas aJCas); static final String PARAM_N; }
@Test public void ngramAnnotatorTest() throws Exception { AnalysisEngine ae = createEngine(NGramAnnotator.class); JCas jcas = ae.newJCas(); JCasBuilder jb = new JCasBuilder(jcas); int begin1 = jb.getPosition(); jb.add("example", Token.class); jb.add(" "); jb.add("sentence", Token.class); jb.add(" "); jb.add("funny", Token.class); jb.add(begin1, Sentence.class); jb.add("."); int begin2 = jb.getPosition(); jb.add("second", Token.class); jb.add(" "); jb.add("example", Token.class); jb.add(begin2, Sentence.class); jb.add("."); jb.close(); ae.process(jcas); int i = 0; for (NGram ngram : select(jcas, NGram.class)) { assertTrue(i != 0 || "example sentence funny".equals(ngram.getText())); assertTrue(i != 1 || "example sentence".equals(ngram.getText())); assertTrue(i != 2 || "example".equals(ngram.getText())); assertTrue(i != 3 || "sentence funny".equals(ngram.getText())); assertTrue(i != 4 || "sentence".equals(ngram.getText())); assertTrue(i != 5 || "funny".equals(ngram.getText())); assertTrue(i != 6 || "second example".equals(ngram.getText())); assertTrue(i != 7 || "second".equals(ngram.getText())); assertTrue(i != 8 || "example".equals(ngram.getText())); i++; } }