DictionaryAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas jcas) throws AnalysisEngineProcessException { Type type = getType(jcas.getCas(), annotationType); Feature f = null; if ((valueFeature != null) && (value != null)) { f = type.getFeatureByBaseName(valueFeature); if (f == null) { throw new IllegalArgumentException("Undeclared feature [" + valueFeature + "] in type [" + annotationType + "]"); } } for (Sentence currSentence : select(jcas, Sentence.class)) { List<Token> tokens = new ArrayList<>(selectCovered(Token.class, currSentence)); for (int i = 0; i < tokens.size(); i++) { List<Token> tokensToSentenceEnd = tokens.subList(i, tokens.size()); String[] sentenceToEnd = new String[tokens.size()]; for (int j = 0; j < tokensToSentenceEnd.size(); j++) { sentenceToEnd[j] = tokensToSentenceEnd.get(j).getText(); } String[] longestMatch = phrases.getLongestMatch(sentenceToEnd); if (longestMatch != null) { Token beginToken = tokens.get(i); Token endToken = tokens.get(i + longestMatch.length - 1); AnnotationFS newFound = jcas.getCas().createAnnotation(type, beginToken.getBegin(), endToken.getEnd()); if (f != null) { newFound.setFeatureValueFromString(f, value); } jcas.getCas().addFsToIndexes(newFound); } } } }  @Override void initialize(UimaContext aContext); @Override void process(JCas jcas); static final String PARAM_MODEL_LOCATION; static final String PARAM_MODEL_ENCODING; static final String PARAM_ANNOTATION_TYPE; static final String PARAM_VALUE_FEATURE; static final String PARAM_VALUE; }
@Test public void test() throws Exception { AnalysisEngine ae = createEngine(DictionaryAnnotator.class, DictionaryAnnotator.PARAM_ANNOTATION_TYPE, NamedEntity.class, DictionaryAnnotator.PARAM_MODEL_LOCATION, "src/test/resources/persons.txt"); JCas jcas = JCasFactory.createJCas(); TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class, Sentence.class); tb.buildTokens(jcas, "I am John Silver 's ghost ."); ae.process(jcas); NamedEntity ne = selectSingle(jcas, NamedEntity.class); assertEquals("John Silver", ne.getCoveredText()); }
@Test public void testWithValue() throws Exception { AnalysisEngine ae = createEngine(DictionaryAnnotator.class, DictionaryAnnotator.PARAM_ANNOTATION_TYPE, NamedEntity.class, DictionaryAnnotator.PARAM_VALUE, "PERSON", DictionaryAnnotator.PARAM_MODEL_LOCATION, "src/test/resources/persons.txt"); JCas jcas = JCasFactory.createJCas(); TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class, Sentence.class); tb.buildTokens(jcas, "I am John Silver 's ghost ."); ae.process(jcas); NamedEntity ne = selectSingle(jcas, NamedEntity.class); assertEquals("PERSON", ne.getValue()); assertEquals("John Silver", ne.getCoveredText()); }
@Test public void testWithWrongType() throws Exception { try { AnalysisEngine ae = createEngine(DictionaryAnnotator.class, DictionaryAnnotator.PARAM_ANNOTATION_TYPE, "lala", DictionaryAnnotator.PARAM_VALUE, "PERSON", DictionaryAnnotator.PARAM_MODEL_LOCATION, "src/test/resources/persons.txt"); JCas jcas = JCasFactory.createJCas(); TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class, Sentence.class); tb.buildTokens(jcas, "I am John Silver 's ghost ."); ae.process(jcas); fail("An exception for an undeclared type should have been thrown"); } catch (AnalysisEngineProcessException e) { assertTrue(ExceptionUtils.getRootCauseMessage(e).contains("Undeclared type")); } }
@Test public void testWithWrongValueFeature() throws Exception { try { AnalysisEngine ae = createEngine(DictionaryAnnotator.class, DictionaryAnnotator.PARAM_ANNOTATION_TYPE, NamedEntity.class, DictionaryAnnotator.PARAM_VALUE_FEATURE, "lala", DictionaryAnnotator.PARAM_VALUE, "PERSON", DictionaryAnnotator.PARAM_MODEL_LOCATION, "src/test/resources/persons.txt"); JCas jcas = JCasFactory.createJCas(); TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class, Sentence.class); tb.buildTokens(jcas, "I am John Silver 's ghost ."); ae.process(jcas); fail("An exception for an undeclared type should have been thrown"); } catch (AnalysisEngineProcessException e) { assertTrue(ExceptionUtils.getRootCauseMessage(e).contains("Undeclared feature")); } }
@Test public void testMatchesAtEndOfSentence() throws Exception { AnalysisEngine ae = createEngine(DictionaryAnnotator.class, DictionaryAnnotator.PARAM_ANNOTATION_TYPE, NamedEntity.class, DictionaryAnnotator.PARAM_VALUE, "PERSON", DictionaryAnnotator.PARAM_MODEL_LOCATION, "src/test/resources/persons.txt"); JCas jcas = JCasFactory.createJCas(); TokenBuilder<Token, Sentence> tb = new TokenBuilder<>(Token.class, Sentence.class); tb.buildTokens(jcas, "I am John Silver"); ae.process(jcas); NamedEntity ne = selectSingle(jcas, NamedEntity.class); assertEquals("PERSON", ne.getValue()); assertEquals("John Silver", ne.getCoveredText()); }