AnnotationByLengthFilter extends JCasAnnotator_ImplBase { @Override public void process(JCas aJCas) throws AnalysisEngineProcessException { for (String filterType : filterTypes) { try { Collection<Annotation> toRemove = new ArrayList<Annotation>(); for (Entry<AnnotationFS, String> entry : FeaturePathFactory.select(aJCas.getCas(), filterType)) { int length = entry.getKey().getCoveredText().length(); if (length < minTokenLength || length > maxTokenLength) { toRemove.add((Annotation) entry.getKey()); } } for (Annotation anno : toRemove) { anno.removeFromIndexes(); } } catch (FeaturePathException e) { throw new AnalysisEngineProcessException(e); } } }  @Override void process(JCas aJCas); static final String PARAM_FILTER_ANNOTATION_TYPES; static final String PARAM_MIN_LENGTH; static final String PARAM_MAX_LENGTH; }
@Test public void testMin() throws Exception { AnalysisEngine filter = createEngine( AnnotationByLengthFilter.class, AnnotationByLengthFilter.PARAM_FILTER_ANNOTATION_TYPES, Token.class, AnnotationByLengthFilter.PARAM_MIN_LENGTH, 5); JCas jcas = filter.newJCas(); TokenBuilder<Token, Annotation> tb = new TokenBuilder<Token, Annotation>(Token.class, Annotation.class); tb.buildTokens(jcas, content); filter.process(jcas); assertEquals("55555 666666 7777777 88888888 999999999", StringUtils.join(toText(select(jcas, Token.class)), " ")); }
@Test public void testMax() throws Exception { AnalysisEngine filter = createEngine( AnnotationByLengthFilter.class, AnnotationByLengthFilter.PARAM_FILTER_ANNOTATION_TYPES, Token.class, AnnotationByLengthFilter.PARAM_MAX_LENGTH, 5); JCas jcas = filter.newJCas(); TokenBuilder<Token, Annotation> tb = new TokenBuilder<Token, Annotation>(Token.class, Annotation.class); tb.buildTokens(jcas, content); filter.process(jcas); assertEquals("1 22 333 4444 55555", StringUtils.join(toText(select(jcas, Token.class)), " ")); }
@Test public void testMinMax() throws Exception { AnalysisEngine filter = createEngine( AnnotationByLengthFilter.class, AnnotationByLengthFilter.PARAM_FILTER_ANNOTATION_TYPES, Token.class, AnnotationByLengthFilter.PARAM_MIN_LENGTH, 3, AnnotationByLengthFilter.PARAM_MAX_LENGTH, 5); JCas jcas = filter.newJCas(); TokenBuilder<Token, Annotation> tb = new TokenBuilder<Token, Annotation>(Token.class, Annotation.class); tb.buildTokens(jcas, content); filter.process(jcas); assertEquals("333 4444 55555", StringUtils.join(toText(select(jcas, Token.class)), " ")); }
@Test public void testMinMaxTokenStem() throws Exception { AnalysisEngine filter = createEngine( AnnotationByLengthFilter.class, AnnotationByLengthFilter.PARAM_FILTER_ANNOTATION_TYPES, new String[] { Token.class.getName(), Stem.class.getName()}, AnnotationByLengthFilter.PARAM_MIN_LENGTH, 3, AnnotationByLengthFilter.PARAM_MAX_LENGTH, 5); JCas jcas = filter.newJCas(); TokenBuilder<Token, Annotation> tb = new TokenBuilder<Token, Annotation>(Token.class, Annotation.class); tb.buildTokens(jcas, content); for (Token token : JCasUtil.select(jcas, Token.class)) { Stem stem = new Stem(jcas, token.getBegin(), token.getEnd()); stem.addToIndexes(); } filter.process(jcas); assertEquals("333 4444 55555", StringUtils.join(toText(select(jcas, Token.class)), " ")); assertEquals("333 4444 55555", StringUtils.join(toText(select(jcas, Stem.class)), " ")); }