TreebankGoldAnnotator extends JCasAnnotator_ImplBase { @Override public void process(JCas jCas) throws AnalysisEngineProcessException { JCas docView; String tbText; try { docView = jCas.getView(CAS.NAME_DEFAULT_SOFA); tbText = jCas.getView(PennTreebankReader.TREEBANK_VIEW).getDocumentText(); } catch (CASException e) { throw new AnalysisEngineProcessException(e); } String docText = docView.getDocumentText(); if (docText == null) { docText = TreebankFormatParser.inferPlainText(tbText); docView.setSofaDataString(docText, "text/plain"); } List<org.cleartk.util.treebank.TopTreebankNode> topNodes; topNodes = TreebankFormatParser.parseDocument(tbText, 0, docText); for (org.cleartk.util.treebank.TopTreebankNode topNode : topNodes) { TopTreebankNode uimaNode = org.cleartk.corpus.penntreebank.TreebankNodeConverter.convert( topNode, docView, postTrees); Sentence uimaSentence = new Sentence(docView, uimaNode.getBegin(), uimaNode.getEnd()); uimaSentence.addToIndexes(); int tokenIndex = 0; for (TerminalTreebankNode terminal : JCasUtil.select( uimaNode.getTerminals(), TerminalTreebankNode.class)) { if (terminal.getBegin() != terminal.getEnd()) { terminal.setTokenIndex(tokenIndex++); Token uimaToken = new Token(docView, terminal.getBegin(), terminal.getEnd()); uimaToken.setPos(terminal.getNodeType()); uimaToken.addToIndexes(); } else { terminal.setTokenIndex(-1); } } } }  static AnalysisEngineDescription getDescription(); static AnalysisEngineDescription getDescriptionPOSTagsOnly(); @Override void initialize(UimaContext context); @Override void process(JCas jCas); static final String PARAM_POST_TREES; }
@Test public void craftTest1() throws Exception { String treebankParse = "( (X (NP (NP (NML (NN Complex ) (NN trait )) (NN analysis )) (PP (IN of ) (NP (DT the ) (NN mouse ) (NN striatum )))) (: : ) (S (NP-SBJ (JJ independent ) (NNS QTLs )) (VP (VBP modulate ) (NP (NP (NN volume )) (CC and ) (NP (NN neuron ) (NN number)))))) )"; String expectedText = "Complex trait analysis of the mouse striatum: independent QTLs modulate volume and neuron number"; AnalysisEngine engine = AnalysisEngineFactory.createEngine(TreebankGoldAnnotator.class); JCas tbView = jCas.createView(PennTreebankReader.TREEBANK_VIEW); tbView.setDocumentText(treebankParse); engine.process(jCas); JCas goldView = jCas.getView(CAS.NAME_DEFAULT_SOFA); FSIndex<Annotation> sentenceIndex = goldView.getAnnotationIndex(Sentence.type); assertEquals(1, sentenceIndex.size()); Sentence firstSentence = JCasUtil.selectByIndex(goldView, Sentence.class, 0); assertEquals(expectedText, firstSentence.getCoveredText()); FSIndex<Annotation> topNodeIndex = goldView.getAnnotationIndex(TopTreebankNode.type); TopTreebankNode topNode = (TopTreebankNode) topNodeIndex.iterator().next(); int i = 0; assertEquals("Complex", topNode.getTerminals(i++).getCoveredText()); assertEquals("trait", topNode.getTerminals(i++).getCoveredText()); assertEquals("analysis", topNode.getTerminals(i++).getCoveredText()); assertEquals("of", topNode.getTerminals(i++).getCoveredText()); assertEquals("the", topNode.getTerminals(i++).getCoveredText()); assertEquals("mouse", topNode.getTerminals(i++).getCoveredText()); assertEquals("striatum", topNode.getTerminals(i++).getCoveredText()); assertEquals(":", topNode.getTerminals(i++).getCoveredText()); assertEquals("independent", topNode.getTerminals(i++).getCoveredText()); assertEquals("QTLs", topNode.getTerminals(i++).getCoveredText()); assertEquals("modulate", topNode.getTerminals(i++).getCoveredText()); assertEquals("volume", topNode.getTerminals(i++).getCoveredText()); assertEquals("and", topNode.getTerminals(i++).getCoveredText()); assertEquals("neuron", topNode.getTerminals(i++).getCoveredText()); assertEquals("number", topNode.getTerminals(i++).getCoveredText()); }
@Test public void testWhenDefaultViewDocumentTextIsSet() throws Exception { String treebankParse = "( (X (NP (NP (NML (NN Complex ) (NN trait )) (NN analysis )) (PP (IN of ) (NP (DT the ) (NN mouse ) (NN striatum )))) (: : ) (S (NP-SBJ (JJ independent ) (NNS QTLs )) (VP (VBP modulate ) (NP (NP (NN volume )) (CC and ) (NP (NN neuron ) (NN number)))))) )"; String expectedText = "Complex trait analysis of the mouse striatum : independent QTLs modulate volume and neuron number"; JCas view = ViewCreatorAnnotator.createViewSafely(jCas, CAS.NAME_DEFAULT_SOFA); view.setSofaDataString(expectedText, "text/plain"); AnalysisEngine engine = AnalysisEngineFactory.createEngine(TreebankGoldAnnotator.class); JCas tbView = jCas.createView(PennTreebankReader.TREEBANK_VIEW); tbView.setDocumentText(treebankParse); engine.process(jCas); JCas goldView = jCas.getView(CAS.NAME_DEFAULT_SOFA); FSIndex<Annotation> sentenceIndex = goldView.getAnnotationIndex(Sentence.type); assertEquals(1, sentenceIndex.size()); Sentence firstSentence = JCasUtil.selectByIndex(goldView, Sentence.class, 0); assertEquals(expectedText, firstSentence.getCoveredText()); }