@Test public void testProcess() throws Exception { boolean annotationsOK = true; XMLInputSource posXML = null; ResourceSpecifier posSpec = null; AnalysisEngine posAnnotator = null; posXML = new XMLInputSource("src/test/resources/PosTagAnnotatorTest.xml"); posSpec = UIMAFramework.getXMLParser().parseResourceSpecifier(posXML); posAnnotator = UIMAFramework.produceAnalysisEngine(posSpec); JCas jcas = null; jcas = posAnnotator.newJCas(); initCas(jcas); posAnnotator.process(jcas, null); JFSIndexRepository indexes = jcas.getJFSIndexRepository(); Iterator tokIter = indexes.getAnnotationIndex(Token.type).iterator(); String predictedPOSTags = ""; while (tokIter.hasNext()) { Token t = (Token) tokIter.next(); PennBioIEPOSTag tag = (PennBioIEPOSTag) t.getPosTag().get(0); predictedPOSTags = predictedPOSTags + tag.getValue() + ";"; } LOGGER.debug("[testProcess]" + "\n Wanted: " + postags + "\n Predicted: " + predictedPOSTags); if (!predictedPOSTags.equals(postags)) { annotationsOK = false; } assertTrue(annotationsOK); }
@Override public void process(JCas aJCas) { LOGGER.debug("[OpenNLP POSTag Annotator] processing document ..."); ArrayList<Token> tokenList = new ArrayList<Token>(); ArrayList<String> tokenTextList = new ArrayList<String>(); FSIterator<Annotation> sentenceIterator = aJCas.getAnnotationIndex(Sentence.type).iterator(); AnnotationIndex<Annotation> tokenIndex = aJCas.getAnnotationIndex(Token.type); if (!sentenceIterator.hasNext()) LOGGER.debug("CAS has no sentences, no POS tags will be added"); while (sentenceIterator.hasNext()) { tokenList.clear(); tokenTextList.clear(); Sentence sentence = (Sentence) sentenceIterator.next(); FSIterator<Annotation> tokenIterator = tokenIndex.subiterator(sentence); if (!tokenIterator.hasNext()) LOGGER.debug("Sentence has no tokens, no POS tags will be added for this sentence"); while (tokenIterator.hasNext()) { Token token = (Token) tokenIterator.next(); tokenList.add(token); tokenTextList.add(token.getCoveredText()); } String[] tokenTagList = tagger.tag(tokenTextList.toArray(new String[tokenList.size()])); try { for (int i = 0; i < tokenList.size(); i++) { Token token = (Token) tokenList.get(i); String posTag = (String) tokenTagList[i]; POSTag pos = null; try { if (null == typeConstructor) { Class<?>[] parameterTypes = new Class[] { JCas.class }; Class<?> typeClass = Class.forName(postagset); typeConstructor = typeClass.getConstructor(parameterTypes); } pos = (POSTag) typeConstructor.newInstance(aJCas); pos.setBegin(token.getBegin()); pos.setEnd(token.getEnd()); pos.setValue(posTag); pos.setComponentId(PosTagAnnotator.class.getName()); pos.addToIndexes(); } catch (SecurityException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (IllegalArgumentException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (ClassNotFoundException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (NoSuchMethodException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (InstantiationException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (IllegalAccessException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (InvocationTargetException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } FSArray postags = token.getPosTag(); if (postags == null) { postags = new FSArray(aJCas, 1); try { postags.set(0, pos); } catch (CASRuntimeException e) { LOGGER.error("[OpenNLP POSTag Annotator]" + e.getMessage()); } token.setPosTag(postags); } else { int numPosTags = postags.size(); int lastElementIndex = numPosTags - 1; if (postags.get(lastElementIndex) != null) { FSArray extendedPosTags = new FSArray(aJCas, numPosTags + 1); extendedPosTags.copyFromArray(postags.toArray(), 0, 0, numPosTags); extendedPosTags.set(numPosTags, pos); postags = extendedPosTags; } else { while ((lastElementIndex > 0) && (postags.get(lastElementIndex - 1) == null)) { lastElementIndex--; } postags.set(lastElementIndex, pos); } } } } catch (CASRuntimeException e) { LOGGER.error("[OpenNLP POSTag Annotator]" + e.getMessage()); LOGGER.error("[OpenNLP POSTag Annotator] list of tags shorter than list of words"); } } }
PosTagAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas aJCas) { LOGGER.debug("[OpenNLP POSTag Annotator] processing document ..."); ArrayList<Token> tokenList = new ArrayList<Token>(); ArrayList<String> tokenTextList = new ArrayList<String>(); FSIterator<Annotation> sentenceIterator = aJCas.getAnnotationIndex(Sentence.type).iterator(); AnnotationIndex<Annotation> tokenIndex = aJCas.getAnnotationIndex(Token.type); if (!sentenceIterator.hasNext()) LOGGER.debug("CAS has no sentences, no POS tags will be added"); while (sentenceIterator.hasNext()) { tokenList.clear(); tokenTextList.clear(); Sentence sentence = (Sentence) sentenceIterator.next(); FSIterator<Annotation> tokenIterator = tokenIndex.subiterator(sentence); if (!tokenIterator.hasNext()) LOGGER.debug("Sentence has no tokens, no POS tags will be added for this sentence"); while (tokenIterator.hasNext()) { Token token = (Token) tokenIterator.next(); tokenList.add(token); tokenTextList.add(token.getCoveredText()); } String[] tokenTagList = tagger.tag(tokenTextList.toArray(new String[tokenList.size()])); try { for (int i = 0; i < tokenList.size(); i++) { Token token = (Token) tokenList.get(i); String posTag = (String) tokenTagList[i]; POSTag pos = null; try { if (null == typeConstructor) { Class<?>[] parameterTypes = new Class[] { JCas.class }; Class<?> typeClass = Class.forName(postagset); typeConstructor = typeClass.getConstructor(parameterTypes); } pos = (POSTag) typeConstructor.newInstance(aJCas); pos.setBegin(token.getBegin()); pos.setEnd(token.getEnd()); pos.setValue(posTag); pos.setComponentId(PosTagAnnotator.class.getName()); pos.addToIndexes(); } catch (SecurityException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (IllegalArgumentException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (ClassNotFoundException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (NoSuchMethodException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (InstantiationException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (IllegalAccessException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (InvocationTargetException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } FSArray postags = token.getPosTag(); if (postags == null) { postags = new FSArray(aJCas, 1); try { postags.set(0, pos); } catch (CASRuntimeException e) { LOGGER.error("[OpenNLP POSTag Annotator]" + e.getMessage()); } token.setPosTag(postags); } else { int numPosTags = postags.size(); int lastElementIndex = numPosTags - 1; if (postags.get(lastElementIndex) != null) { FSArray extendedPosTags = new FSArray(aJCas, numPosTags + 1); extendedPosTags.copyFromArray(postags.toArray(), 0, 0, numPosTags); extendedPosTags.set(numPosTags, pos); postags = extendedPosTags; } else { while ((lastElementIndex > 0) && (postags.get(lastElementIndex - 1) == null)) { lastElementIndex--; } postags.set(lastElementIndex, pos); } } } } catch (CASRuntimeException e) { LOGGER.error("[OpenNLP POSTag Annotator]" + e.getMessage()); LOGGER.error("[OpenNLP POSTag Annotator] list of tags shorter than list of words"); } } } }
PosTagAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas aJCas) { LOGGER.debug("[OpenNLP POSTag Annotator] processing document ..."); ArrayList<Token> tokenList = new ArrayList<Token>(); ArrayList<String> tokenTextList = new ArrayList<String>(); FSIterator<Annotation> sentenceIterator = aJCas.getAnnotationIndex(Sentence.type).iterator(); AnnotationIndex<Annotation> tokenIndex = aJCas.getAnnotationIndex(Token.type); if (!sentenceIterator.hasNext()) LOGGER.debug("CAS has no sentences, no POS tags will be added"); while (sentenceIterator.hasNext()) { tokenList.clear(); tokenTextList.clear(); Sentence sentence = (Sentence) sentenceIterator.next(); FSIterator<Annotation> tokenIterator = tokenIndex.subiterator(sentence); if (!tokenIterator.hasNext()) LOGGER.debug("Sentence has no tokens, no POS tags will be added for this sentence"); while (tokenIterator.hasNext()) { Token token = (Token) tokenIterator.next(); tokenList.add(token); tokenTextList.add(token.getCoveredText()); } String[] tokenTagList = tagger.tag(tokenTextList.toArray(new String[tokenList.size()])); try { for (int i = 0; i < tokenList.size(); i++) { Token token = (Token) tokenList.get(i); String posTag = (String) tokenTagList[i]; POSTag pos = null; try { if (null == typeConstructor) { Class<?>[] parameterTypes = new Class[] { JCas.class }; Class<?> typeClass = Class.forName(postagset); typeConstructor = typeClass.getConstructor(parameterTypes); } pos = (POSTag) typeConstructor.newInstance(aJCas); pos.setBegin(token.getBegin()); pos.setEnd(token.getEnd()); pos.setValue(posTag); pos.setComponentId(PosTagAnnotator.class.getName()); pos.addToIndexes(); } catch (SecurityException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (IllegalArgumentException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (ClassNotFoundException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (NoSuchMethodException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (InstantiationException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (IllegalAccessException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (InvocationTargetException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } FSArray postags = token.getPosTag(); if (postags == null) { postags = new FSArray(aJCas, 1); try { postags.set(0, pos); } catch (CASRuntimeException e) { LOGGER.error("[OpenNLP POSTag Annotator]" + e.getMessage()); } token.setPosTag(postags); } else { int numPosTags = postags.size(); int lastElementIndex = numPosTags - 1; if (postags.get(lastElementIndex) != null) { FSArray extendedPosTags = new FSArray(aJCas, numPosTags + 1); extendedPosTags.copyFromArray(postags.toArray(), 0, 0, numPosTags); extendedPosTags.set(numPosTags, pos); postags = extendedPosTags; } else { while ((lastElementIndex > 0) && (postags.get(lastElementIndex - 1) == null)) { lastElementIndex--; } postags.set(lastElementIndex, pos); } } } } catch (CASRuntimeException e) { LOGGER.error("[OpenNLP POSTag Annotator]" + e.getMessage()); LOGGER.error("[OpenNLP POSTag Annotator] list of tags shorter than list of words"); } } }  }
PosTagAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas aJCas) { LOGGER.debug("[OpenNLP POSTag Annotator] processing document ..."); ArrayList<Token> tokenList = new ArrayList<Token>(); ArrayList<String> tokenTextList = new ArrayList<String>(); FSIterator<Annotation> sentenceIterator = aJCas.getAnnotationIndex(Sentence.type).iterator(); AnnotationIndex<Annotation> tokenIndex = aJCas.getAnnotationIndex(Token.type); if (!sentenceIterator.hasNext()) LOGGER.debug("CAS has no sentences, no POS tags will be added"); while (sentenceIterator.hasNext()) { tokenList.clear(); tokenTextList.clear(); Sentence sentence = (Sentence) sentenceIterator.next(); FSIterator<Annotation> tokenIterator = tokenIndex.subiterator(sentence); if (!tokenIterator.hasNext()) LOGGER.debug("Sentence has no tokens, no POS tags will be added for this sentence"); while (tokenIterator.hasNext()) { Token token = (Token) tokenIterator.next(); tokenList.add(token); tokenTextList.add(token.getCoveredText()); } String[] tokenTagList = tagger.tag(tokenTextList.toArray(new String[tokenList.size()])); try { for (int i = 0; i < tokenList.size(); i++) { Token token = (Token) tokenList.get(i); String posTag = (String) tokenTagList[i]; POSTag pos = null; try { if (null == typeConstructor) { Class<?>[] parameterTypes = new Class[] { JCas.class }; Class<?> typeClass = Class.forName(postagset); typeConstructor = typeClass.getConstructor(parameterTypes); } pos = (POSTag) typeConstructor.newInstance(aJCas); pos.setBegin(token.getBegin()); pos.setEnd(token.getEnd()); pos.setValue(posTag); pos.setComponentId(PosTagAnnotator.class.getName()); pos.addToIndexes(); } catch (SecurityException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (IllegalArgumentException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (ClassNotFoundException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (NoSuchMethodException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (InstantiationException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (IllegalAccessException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (InvocationTargetException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } FSArray postags = token.getPosTag(); if (postags == null) { postags = new FSArray(aJCas, 1); try { postags.set(0, pos); } catch (CASRuntimeException e) { LOGGER.error("[OpenNLP POSTag Annotator]" + e.getMessage()); } token.setPosTag(postags); } else { int numPosTags = postags.size(); int lastElementIndex = numPosTags - 1; if (postags.get(lastElementIndex) != null) { FSArray extendedPosTags = new FSArray(aJCas, numPosTags + 1); extendedPosTags.copyFromArray(postags.toArray(), 0, 0, numPosTags); extendedPosTags.set(numPosTags, pos); postags = extendedPosTags; } else { while ((lastElementIndex > 0) && (postags.get(lastElementIndex - 1) == null)) { lastElementIndex--; } postags.set(lastElementIndex, pos); } } } } catch (CASRuntimeException e) { LOGGER.error("[OpenNLP POSTag Annotator]" + e.getMessage()); LOGGER.error("[OpenNLP POSTag Annotator] list of tags shorter than list of words"); } } }  @Override void initialize(UimaContext aContext); @Override void process(JCas aJCas); }
PosTagAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas aJCas) { LOGGER.debug("[OpenNLP POSTag Annotator] processing document ..."); ArrayList<Token> tokenList = new ArrayList<Token>(); ArrayList<String> tokenTextList = new ArrayList<String>(); FSIterator<Annotation> sentenceIterator = aJCas.getAnnotationIndex(Sentence.type).iterator(); AnnotationIndex<Annotation> tokenIndex = aJCas.getAnnotationIndex(Token.type); if (!sentenceIterator.hasNext()) LOGGER.debug("CAS has no sentences, no POS tags will be added"); while (sentenceIterator.hasNext()) { tokenList.clear(); tokenTextList.clear(); Sentence sentence = (Sentence) sentenceIterator.next(); FSIterator<Annotation> tokenIterator = tokenIndex.subiterator(sentence); if (!tokenIterator.hasNext()) LOGGER.debug("Sentence has no tokens, no POS tags will be added for this sentence"); while (tokenIterator.hasNext()) { Token token = (Token) tokenIterator.next(); tokenList.add(token); tokenTextList.add(token.getCoveredText()); } String[] tokenTagList = tagger.tag(tokenTextList.toArray(new String[tokenList.size()])); try { for (int i = 0; i < tokenList.size(); i++) { Token token = (Token) tokenList.get(i); String posTag = (String) tokenTagList[i]; POSTag pos = null; try { if (null == typeConstructor) { Class<?>[] parameterTypes = new Class[] { JCas.class }; Class<?> typeClass = Class.forName(postagset); typeConstructor = typeClass.getConstructor(parameterTypes); } pos = (POSTag) typeConstructor.newInstance(aJCas); pos.setBegin(token.getBegin()); pos.setEnd(token.getEnd()); pos.setValue(posTag); pos.setComponentId(PosTagAnnotator.class.getName()); pos.addToIndexes(); } catch (SecurityException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (IllegalArgumentException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (ClassNotFoundException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (NoSuchMethodException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (InstantiationException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (IllegalAccessException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } catch (InvocationTargetException e1) { LOGGER.error("[OpenNLP POSTag Annotator]" + e1.getMessage()); } FSArray postags = token.getPosTag(); if (postags == null) { postags = new FSArray(aJCas, 1); try { postags.set(0, pos); } catch (CASRuntimeException e) { LOGGER.error("[OpenNLP POSTag Annotator]" + e.getMessage()); } token.setPosTag(postags); } else { int numPosTags = postags.size(); int lastElementIndex = numPosTags - 1; if (postags.get(lastElementIndex) != null) { FSArray extendedPosTags = new FSArray(aJCas, numPosTags + 1); extendedPosTags.copyFromArray(postags.toArray(), 0, 0, numPosTags); extendedPosTags.set(numPosTags, pos); postags = extendedPosTags; } else { while ((lastElementIndex > 0) && (postags.get(lastElementIndex - 1) == null)) { lastElementIndex--; } postags.set(lastElementIndex, pos); } } } } catch (CASRuntimeException e) { LOGGER.error("[OpenNLP POSTag Annotator]" + e.getMessage()); LOGGER.error("[OpenNLP POSTag Annotator] list of tags shorter than list of words"); } } }  @Override void initialize(UimaContext aContext); @Override void process(JCas aJCas); static final String PARAM_MODEL_FILE; static final String PARAM_TAGSET; }