WhiteSpaceExtractor implements NamedFeatureExtractor1<T> { public List<Feature> extract(JCas view, Annotation focusAnnotation) throws CleartkExtractorException { List<Feature> features = new ArrayList<Feature>(); String text = view.getDocumentText(); int begin = focusAnnotation.getBegin(); int end = focusAnnotation.getEnd(); if (begin == 0) { Feature feature = new Feature(FEATURE_NAME, ORIENTATION_LEFT); features.add(feature); } else { char leftChar = text.charAt(begin - 1); if (Character.isWhitespace(leftChar)) { Feature feature = new Feature(FEATURE_NAME, ORIENTATION_LEFT); features.add(feature); } } if (end == text.length()) { Feature feature = new Feature(FEATURE_NAME, ORIENTATION_RIGHT); features.add(feature); } else { char rightChar = text.charAt(end); if (Character.isWhitespace(rightChar)) { Feature feature = new Feature(FEATURE_NAME, ORIENTATION_RIGHT); features.add(feature); } } return features; }  @Override String getFeatureName(); List<Feature> extract(JCas view, Annotation focusAnnotation); static final String WhiteSpaceRegex; static final Pattern WhiteSpacePattern; static final String ORIENTATION_LEFT; static final String ORIENTATION_RIGHT; }
@Test public void testExtract() throws Exception { AnalysisEngine engine = AnalysisEngineFactory.createEngine(Annotator.class); jCas.reset(); jCas.setDocumentText("This is some test text."); engine.process(jCas); engine.collectionProcessComplete(); FSIndex<Annotation> fsIndex = jCas.getAnnotationIndex(Token.type); Token targetToken = new Token(jCas, 0, 4); Token t1 = (Token) fsIndex.find(targetToken); WhiteSpaceExtractor<Token> extractor = new WhiteSpaceExtractor<Token>(); List<Feature> features = extractor.extract(jCas, t1); assertEquals(2, features.size()); Feature feature = features.get(0); assertEquals(WhiteSpaceExtractor.ORIENTATION_LEFT, feature.getValue()); feature = features.get(1); assertEquals(WhiteSpaceExtractor.ORIENTATION_RIGHT, feature.getValue()); assertEquals("whitespace", feature.getName()); targetToken = new Token(jCas, 5, 7); t1 = (Token) fsIndex.find(targetToken); features = extractor.extract(jCas, t1); assertEquals(features.size(), 2); feature = features.get(0); assertEquals(WhiteSpaceExtractor.ORIENTATION_LEFT, feature.getValue()); feature = features.get(1); assertEquals(WhiteSpaceExtractor.ORIENTATION_RIGHT, feature.getValue()); assertEquals("whitespace", feature.getName()); targetToken = new Token(jCas, 18, 22); t1 = (Token) fsIndex.find(targetToken); features = extractor.extract(jCas, t1); assertEquals(features.size(), 1); feature = features.get(0); assertEquals(WhiteSpaceExtractor.ORIENTATION_LEFT, feature.getValue()); targetToken = new Token(jCas, 22, 23); t1 = (Token) fsIndex.find(targetToken); features = extractor.extract(jCas, t1); assertEquals(1, features.size()); feature = features.get(0); assertEquals(WhiteSpaceExtractor.ORIENTATION_RIGHT, feature.getValue()); targetToken = new Token(jCas, 10, 12); t1 = (Token) fsIndex.find(targetToken); features = extractor.extract(jCas, t1); assertEquals(1, features.size()); feature = features.get(0); assertEquals(WhiteSpaceExtractor.ORIENTATION_RIGHT, feature.getValue()); targetToken = new Token(jCas, 15, 20); t1 = (Token) fsIndex.find(targetToken); features = extractor.extract(jCas, t1); assertEquals(0, features.size()); targetToken = new Token(jCas, 0, 23); t1 = (Token) fsIndex.find(targetToken); features = extractor.extract(jCas, t1); assertEquals(features.size(), 2); feature = features.get(0); assertEquals(WhiteSpaceExtractor.ORIENTATION_LEFT, feature.getValue()); feature = features.get(1); assertEquals(WhiteSpaceExtractor.ORIENTATION_RIGHT, feature.getValue()); }