VerbClauseTemporalAnnotator extends CleartkAnnotator<String> { public void process(JCas jCas) throws AnalysisEngineProcessException { int docEnd = jCas.getDocumentText().length(); Map<String, TemporalLink> tlinks = null; if (this.isTraining()) { tlinks = this.getTemporalLinks(jCas); } for (Sentence sentence : JCasUtil.select(jCas, Sentence.class)) { TopTreebankNode tree = AnnotationUtil.selectFirstMatching( jCas, TopTreebankNode.class, sentence); if (tree == null) { String fmt = "missing syntactic parse for sentence: %s"; String msg = String.format(fmt, sentence.getCoveredText()); this.getContext().getLogger().log(Level.WARNING, msg); continue; } List<TreebankNodeLink> links = new ArrayList<TreebankNodeLink>(); this.collectVerbClausePairs(tree, links); for (TreebankNodeLink link : links) { Token sourceToken = JCasUtil.selectCovered(jCas, Token.class, link.source).get(0); Token targetToken = JCasUtil.selectCovered(jCas, Token.class, link.target).get(0); int firstEnd = Math.min(sourceToken.getEnd(), targetToken.getEnd()); int lastBegin = Math.max(sourceToken.getBegin(), targetToken.getBegin()); Instance<String> instance = new Instance<String>(); for (FeatureExtractor1<Token> extractor : this.sourceFeatureExtractors) { instance.addAll(extractor.extract(jCas, sourceToken)); } for (FeatureExtractor1<Token> extractor : this.targetFeatureExtractors) { instance.addAll(extractor.extract(jCas, targetToken)); } Annotation windowAnnotation = new Annotation(jCas, firstEnd, lastBegin); for (FeatureExtractor1<Annotation> extractor : this.betweenAnchorsFeatureExtractors) { instance.addAll(extractor.extract(jCas, windowAnnotation)); } instance.addAll(this.pathExtractor.extract(jCas, link.source, link.target)); Anchor source = AnnotationUtil.selectFirstMatching(jCas, Anchor.class, link.source); Anchor target = AnnotationUtil.selectFirstMatching(jCas, Anchor.class, link.target); if (this.isTraining()) { if (source != null && target != null) { String key = String.format("%s:%s", source.getId(), target.getId()); TemporalLink tlink = tlinks.remove(key); if (tlink != null) { instance.setOutcome(tlink.getRelationType()); this.dataWriter.write(instance); } } } else { source = this.getOrCreateEvent(jCas, source, link.source); target = this.getOrCreateEvent(jCas, target, link.target); if (source != null && target != null) { String relationType = this.classifier.classify(instance.getFeatures()); TemporalLink tlink = new TemporalLink(jCas, docEnd, docEnd); tlink.setSource(source); tlink.setTarget(target); tlink.setRelationType(relationType); tlink.addToIndexes(); } } } } } VerbClauseTemporalAnnotator(); void process(JCas jCas); static final CleartkInternalModelFactory FACTORY; static final String PARAM_CREATE_EVENTS; }
@Test public void test() throws UIMAException { AnalysisEngine engine = AnalysisEngineFactory.createEngine( VerbClauseTemporalAnnotator.class, CleartkAnnotator.PARAM_DATA_WRITER_FACTORY_CLASS_NAME, PublicFieldDataWriter.StringFactory.class.getName(), DirectoryDataWriterFactory.PARAM_OUTPUT_DIRECTORY, "."); tokenBuilder.buildTokens( jCas, "He said she bought milk.", "He said she bought milk .", "PRP VBD PRP VBD NN .", "he say she buy milk ."); List<Token> tokens = new ArrayList<Token>(JCasUtil.select(jCas, Token.class)); Event source = new Event(jCas, tokens.get(1).getBegin(), tokens.get(1).getEnd()); Event target = new Event(jCas, tokens.get(3).getBegin(), tokens.get(3).getEnd()); TemporalLink tlink = new TemporalLink(jCas); tlink.setSource(source); tlink.setTarget(target); tlink.setRelationType("AFTER"); Annotation[] timemlAnnotations = new Annotation[] { source, target, tlink }; for (Annotation annotation : timemlAnnotations) { annotation.addToIndexes(); } TreebankNode root = TreebankNodeUtil.newNode(jCas, "S", TreebankNodeUtil.newNode( jCas, "NP", this.newNode(jCas, tokens.get(0))), TreebankNodeUtil.newNode( jCas, "VP", this.newNode(jCas, tokens.get(1)), TreebankNodeUtil.newNode(jCas, "SBAR", TreebankNodeUtil.newNode( jCas, "NP", this.newNode(jCas, tokens.get(2))), TreebankNodeUtil.newNode( jCas, "VP", this.newNode(jCas, tokens.get(3)), TreebankNodeUtil.newNode(jCas, "NP", this.newNode(jCas, tokens.get(4))))))); Sentence sentence = JCasUtil.selectSingle(jCas, Sentence.class); TopTreebankNode tree = new TopTreebankNode(jCas, sentence.getBegin(), sentence.getEnd()); tree.setNodeType("TOP"); tree.setChildren(new FSArray(jCas, 1)); tree.setChildren(0, root); tree.addToIndexes(); List<Instance<String>> instances; instances = PublicFieldDataWriter.StringFactory.collectInstances(engine, jCas); Assert.assertEquals(1, instances.size()); Assert.assertEquals("AFTER", instances.get(0).getOutcome()); Collection<Event> events; Collection<TemporalLink> tlinks; for (Annotation annotation : timemlAnnotations) { annotation.removeFromIndexes(); } events = JCasUtil.select(jCas, Event.class); tlinks = JCasUtil.select(jCas, TemporalLink.class); Assert.assertEquals(0, events.size()); Assert.assertEquals(0, tlinks.size()); engine = AnalysisEngineFactory.createEngine( VerbClauseTemporalAnnotator.class, CleartkAnnotator.PARAM_CLASSIFIER_FACTORY_CLASS_NAME, AfterNewClassifier.class.getName()); engine.process(jCas); engine.collectionProcessComplete(); events = JCasUtil.select(jCas, Event.class); tlinks = JCasUtil.select(jCas, TemporalLink.class); Assert.assertEquals(0, events.size()); Assert.assertEquals(0, tlinks.size()); engine = AnalysisEngineFactory.createEngine( VerbClauseTemporalAnnotator.class, CleartkAnnotator.PARAM_CLASSIFIER_FACTORY_CLASS_NAME, AfterNewClassifier.class.getName(), VerbClauseTemporalAnnotator.PARAM_CREATE_EVENTS, true); engine.process(jCas); engine.collectionProcessComplete(); events = JCasUtil.select(jCas, Event.class); tlinks = JCasUtil.select(jCas, TemporalLink.class); Iterator<Event> eventsIter = events.iterator(); Event event0 = eventsIter.next(); Event event1 = eventsIter.next(); TemporalLink tlink0 = tlinks.iterator().next(); Assert.assertEquals(2, events.size()); Assert.assertEquals(1, tlinks.size()); Assert.assertEquals("said", event0.getCoveredText()); Assert.assertEquals("bought", event1.getCoveredText()); Assert.assertEquals(event0, tlink0.getSource()); Assert.assertEquals(event1, tlink0.getTarget()); Assert.assertEquals("AFTER-NEW", tlink0.getRelationType()); }