/** {@inheritDoc} */ @Override public void onMatching( String docUri, Type type, SortedSet<Annotation> goldAnnos, SortedSet<Annotation> sysAnnos) { if (goldAnnos.size() == 1 && sysAnnos.size() == 1) { Annotation goldAnno = goldAnnos.iterator().next(); Annotation sysAnno = sysAnnos.iterator().next(); if (goldAnno.getBegin() == sysAnno.getBegin() && goldAnno.getEnd() == sysAnno.getEnd()) { printRow( type.getShortName(), "Exact", goldAnno.getCoveredText(), String.valueOf(goldAnno.getBegin()), sysAnno.getCoveredText(), String.valueOf(sysAnno.getBegin()), docUri); return; } } printRow( type.getShortName(), "Partial", Joiner.on(" /// ").join(transform(goldAnnos, annoToTxt)), Joiner.on(", ").join(transform(goldAnnos, annoToOffset)), Joiner.on(" /// ").join(transform(sysAnnos, annoToTxt)), Joiner.on(", ").join(transform(sysAnnos, annoToOffset)), docUri); }
@Override public void process(JCas cas) throws AnalysisEngineProcessException { this.setSource(cas); this.setTarget(cas); AnnotationIndex<Annotation> index = cas.getAnnotationIndex(this.getSourceType()); FSIterator<Annotation> iter = index.iterator(); while (iter.hasNext()) { Annotation annotation = iter.next(); String source = null; if (this.getSourceFeature() == null) { source = annotation.getCoveredText(); } else { source = annotation.getStringValue(this.getSourceFeature()); } if (source != null) { String target = this.getMapping().get(source); if (target != null) { if (this.update().booleanValue()) { this.update(cas, annotation, this.getTargetFeature(), target); } else { this.create( cas, this.getTargetFeature(), annotation.getBegin(), annotation.getEnd(), target); } } } } }
@Override public void process(JCas jcas) throws AnalysisEngineProcessException { Type type = jcas.getCas().getTypeSystem().getType(TYPE_NAME); Feature entityFeature = type.getFeatureByBaseName(ENTITY_FEATURE); Feature nameFeature = type.getFeatureByBaseName(NAME_FEATURE); for (Annotation annotation : jcas.getAnnotationIndex(TokenAnnotation.type)) { String tokenPOS = ((TokenAnnotation) annotation).getPosTag(); if (NP.equals(tokenPOS) || NPS.equals(tokenPOS)) { AnnotationFS entityAnnotation = jcas.getCas().createAnnotation(type, annotation.getBegin(), annotation.getEnd()); entityAnnotation.setStringValue(entityFeature, annotation.getCoveredText()); String name = "OTHER"; // "OTHER" makes no sense. In practice, "PERSON", "COUNTRY", "E-MAIL", etc. if (annotation.getCoveredText().equals("Apache")) name = "ORGANIZATION"; entityAnnotation.setStringValue(nameFeature, name); jcas.addFsToIndexes(entityAnnotation); } } }
/** Outputs Trigrams for an input Annotation. */ public void extractNgramsFromAnnotation(Annotation annotation, JCas aJCas) { AnnotationIndex<Annotation> tokens = aJCas.getAnnotationIndex(Token.type); Iterator<Annotation> tIterator = tokens.subiterator(annotation); Annotation token = null; Annotation prev = null; Annotation prev_prev = null; while (tIterator.hasNext()) { prev_prev = prev; prev = token; token = tIterator.next(); if (tokenIsInAnnotation(annotation, prev) && tokenIsInAnnotation(annotation, prev_prev)) { NGram ngram = new NGram(aJCas); FSArray ngArray = new FSArray(aJCas, 3); ngArray.set(0, prev_prev); ngArray.set(1, prev); ngArray.set(2, token); ngram.setBegin(prev_prev.getBegin()); ngram.setEnd(token.getEnd()); ngram.setElements(ngArray); ngram.setElementType("edu.cmu.deiis.types.Token"); ngram.setConfidence(1D); ngram.setCasProcessorId(PROCESSOR_ID); ngram.addToIndexes(); } } }
public static boolean sameIndexes(Annotation anno1, Annotation anno2) { return anno1.getBegin() == anno2.getBegin() && anno1.getEnd() == anno2.getEnd(); }
public static boolean contains(Annotation container, Annotation subAnnotation) { return container.getBegin() <= subAnnotation.getBegin() && container.getEnd() >= subAnnotation.getEnd(); }
/** * Recreates a Stanford Tree from the StanfordParser annotations and saves all * non-StanfordParser-Annotations within the scope of the sentence in the label of the best * fitting node. * * <p><strong>CAUTION: </strong><i>This method is intended for the use by CAS Multipliers, which * create new CASes from this tree. The annotation-spans in the source-CAS will be changed!!!!!! * You do NOT want to use the source CAS after this method has been called. The * createStanfordTree()-method does not change the CAS, so use this instead, if the annotations do * not have to be recovered or accessed in the tree.</i> * * <p>TODO: This behavior could be changed by making COPIES of the annotations and changing the * copied instead of the originals. However, in order to being able to make copies, a dummy CAS * must be introduced to which the annotations can be copied. When they are recovered, they will * be copied to the new destination CAS anyway. * * @param root the ROOT annotation * @return an {@link Tree} object representing the syntax structure of the sentence * @throws CASException if the JCas cannot be accessed. */ public static Tree createStanfordTreeWithAnnotations(Annotation root) throws CASException { JCas aJCas = root.getCAS().getJCas(); // Create tree Tree tree = createStanfordTree(root); // Get all non-parser related annotations // and all tokens (needed for span-calculations later on) List<Annotation> nonParserAnnotations = new ArrayList<Annotation>(); List<Token> tokens = new ArrayList<Token>(); // Using getCoveredAnnotations instead of iterate, because subiterators did not work in all // cases List<Annotation> annosWithinRoot = JCasUtil.selectCovered(aJCas, Annotation.class, root); for (Annotation curAnno : annosWithinRoot) { if (!(curAnno instanceof POS) && !(curAnno instanceof Constituent) && !(curAnno instanceof Dependency) && !(curAnno instanceof PennTree) && !(curAnno instanceof Lemma) && !(curAnno instanceof Token) && !(curAnno instanceof DocumentMetaData)) { nonParserAnnotations.add(curAnno); } else if (curAnno instanceof Token) { tokens.add((Token) curAnno); } } // create wrapper for tree and its tokens TreeWithTokens annoTree = new TreeWithTokens(tree, tokens); /* * Add annotations to the best-fitting nodes. The best-fitting node for an annotation is the * deepest node in the tree that still completely contains the annotation. */ for (Annotation curAnno : nonParserAnnotations) { // get best fitting node Tree bestFittingNode = annoTree.getBestFit(curAnno); // Add annotation to node if (bestFittingNode != null) { // translate annotation span to a value relative to the // node-span IntPair span = annoTree.getSpan(bestFittingNode); curAnno.setBegin(curAnno.getBegin() - span.getSource()); curAnno.setEnd(curAnno.getEnd() - span.getSource()); // get the collection from the label of the best-fitting node in which we store UIMA // annotations or create it, if it does not exist Collection<Annotation> annotations = ((CoreLabel) bestFittingNode.label()).get(UIMAAnnotations.class); if (annotations == null) { annotations = new ArrayList<Annotation>(); } // add annotation + checksum of annotated text to list and write it back to node // label annotations.add(curAnno); ((CoreLabel) bestFittingNode.label()).set(UIMAAnnotations.class, annotations); } } return tree; }
/** * Static method that changes the annotation passed by off-setting it offset from zero * * @param annotation * @param offset - The offset as computed by (copy - original) */ private static void updateAnnotation(Annotation annotation, int offset) { if (annotation == null) return; annotation.setBegin(annotation.getBegin() + offset); annotation.setEnd(annotation.getEnd() + offset); }