/** * Constructor with begin and end passed as arguments * @param jcas JCas * @param begin begin offset * @param end end offset */ public Annotation(JCas jcas, int begin, int end) { this(jcas); // forward to constructor this.setBegin(begin); this.setEnd(end); }
private void modifyEnd(Annotation ann, Annotation aAnnot, Annotation bAnnot) { switch (iv_endSrc) { case END_A: ann.setEnd(aAnnot.getEnd()); break; case END_B: ann.setEnd(bAnnot.getEnd()); break; case END_SMALLEST: ann.setEnd(Math.min(aAnnot.getEnd(), bAnnot.getEnd())); break; case END_LARGEST: ann.setEnd(Math.max(aAnnot.getEnd(), bAnnot.getEnd())); break; } }
private void modifyEnd(Annotation ann, Annotation aAnnot, Annotation bAnnot) { switch (iv_endSrc) { case END_A: ann.setEnd(aAnnot.getEnd()); break; case END_B: ann.setEnd(bAnnot.getEnd()); break; case END_SMALLEST: ann.setEnd(Math.min(aAnnot.getEnd(), bAnnot.getEnd())); break; case END_LARGEST: ann.setEnd(Math.max(aAnnot.getEnd(), bAnnot.getEnd())); break; } }
private static void postprocess(int spanBegin, Annotation anno, int begin, int end) { anno.setBegin(spanBegin + begin); anno.setEnd(spanBegin + end); anno.addToIndexes(); } }
private void trim(Annotation aAnnotation) { StringBuilder buffer = getBuffer(); int s = aAnnotation.getBegin(); int e = aAnnotation.getEnd(); while (Character.isWhitespace(buffer.charAt(s))) { s++; } while ((e > s + 1) && Character.isWhitespace(buffer.charAt(e - 1))) { e--; } aAnnotation.setBegin(s); aAnnotation.setEnd(e); } }
private void trim(Annotation aAnnotation) { StringBuilder buffer = getBuffer(); int s = aAnnotation.getBegin(); int e = aAnnotation.getEnd(); while (Character.isWhitespace(buffer.charAt(s))) { s++; } while ((e > s + 1) && Character.isWhitespace(buffer.charAt(e - 1))) { e--; } aAnnotation.setBegin(s); aAnnotation.setEnd(e); } }
/** * Sets the begin and end of the annotation against the document (rather than this block) * * @param <T> the generic type * @param annotation the annotation * @param begin the begin offset within this text block * @param end the end offset within this text block * @return the annotaiton (with begin and end set to the document offsets) */ public <T extends Annotation> T setBeginAndEnd( final T annotation, final int begin, final int end) { annotation.setBegin(toDocumentOffset(begin)); annotation.setEnd(toDocumentOffset(end)); return annotation; }
@Override public Annotation convert(JCas view, TopTreebankNode topNode) { Annotation annotation = new Annotation(view); List<Annotation> subAnnotations = new ArrayList<Annotation>(); for (PropbankRelation rel : this.relations) { subAnnotations.add(rel.convert(view, topNode)); } // annotation.setAnnotations(UIMAUtil.toFSArray(view, subAnnotations)); int[] span = AnnotationUtil.getAnnotationsExtent(subAnnotations); annotation.setBegin(span[0]); annotation.setEnd(span[1]); annotation.addToIndexes(); return annotation; }
@Override public Annotation convert(JCas view, TopTreebankNode topNode) { Annotation annotation = new Annotation(view); List<Annotation> subAnnotations = new ArrayList<Annotation>(); for (PropbankRelation rel : this.relations) { subAnnotations.add(rel.convert(view, topNode)); } // annotation.setAnnotations(UIMAUtil.toFSArray(view, subAnnotations)); int[] span = AnnotationUtil.getAnnotationsExtent(subAnnotations); annotation.setBegin(span[0]); annotation.setEnd(span[1]); annotation.addToIndexes(); return annotation; }
/** * Sets the begin and end of the annotation against the document (rather than this block) * * @param <T> the generic type * @param annotation the annotation * @param begin the begin offset within this text block * @param end the end offset within this text block * @return the annotaiton (with begin and end set to the document offsets) */ public <T extends Annotation> T setBeginAndEnd( final T annotation, final int begin, final int end) { annotation.setBegin(toDocumentOffset(begin)); annotation.setEnd(toDocumentOffset(end)); return annotation; }
default void addTextSpan( final T type, final Basic resource, final Logger logger ) { final Pair<Integer> textSpan = FhirElementParser.getTextSpan( resource.getExtension() ); if ( textSpan == null ) { logger.error( "Could not parse text span for basic " + resource.getId() ); return; } type.setBegin( textSpan.getValue1() ); type.setEnd( textSpan.getValue2() ); }
@Override public void process(JCas aJCas) throws AnalysisEngineProcessException { // Do nothing Pattern p = Pattern.compile("\\d+"); Matcher m = p.matcher(aJCas.getDocumentText()); while (m.find()) { Annotation a = new Annotation(aJCas); a.setBegin(m.start()); a.setEnd(m.end()); a.addToIndexes(); } } }
/** * Trim the offsets of the given annotation to remove leading/trailing whitespace. * <p> * <b>Note:</b> use this method if the document text of the CAS has not been set yet but you * have it available in a buffer. * <p> * <b>Note:</b> best use this method before adding the annotation to the indexes. * * @param aText * the document text (available so far). * @param aAnnotation * the annotation to trim. Offsets are updated. */ public static void trim(CharSequence aText, Annotation aAnnotation) { int[] offsets = { aAnnotation.getBegin(), aAnnotation.getEnd() }; trim(aText, offsets); aAnnotation.setBegin(offsets[0]); aAnnotation.setEnd(offsets[1]); }
private static void testAnnotationType(JCas jcas, Annotation annotation) throws Exception { Class<?> annotationClass = annotation.getClass(); Constructor<?> constructor = annotationClass.getConstructor(JCas.class); annotation = (Annotation) constructor.newInstance(jcas); Assert.assertEquals(0, annotation.getBegin()); Assert.assertEquals(0, annotation.getEnd()); annotation.setBegin(15); Assert.assertEquals(15, annotation.getBegin()); annotation.setEnd(12); Assert.assertEquals(12, annotation.getEnd()); constructor = annotationClass.getConstructor(JCas.class, int.class, int.class); annotation = (Annotation) constructor.newInstance(jcas, 4, 6); Assert.assertEquals(4, annotation.getBegin()); Assert.assertEquals(6, annotation.getEnd()); }
public void changeOffsets(AnnotationFS annotation, int begin, int end, AbstractRuleMatch<? extends AbstractRule> modifikator) { if (!(annotation instanceof Annotation)) { return; } Annotation a = (Annotation) annotation; if (annotation.getBegin() == begin && annotation.getEnd() == end) { return; } // TODO implement incremental reindexing removeAnnotation(a); a.setBegin(begin); a.setEnd(end); addAnnotation(a, true, modifikator); }
protected <T extends TOP> T getFeatureStructure(JCas jcas, XMLElement hAnno, Element elm, Rule<T> mapping) { T annotation = null; if (mapping.isUnique()) { annotation = getOrCreate(jcas, mapping.getTargetClass()); } else { annotation = (T) jcas.getCas().createFS(JCasUtil.getType(jcas, mapping.getTargetClass())); jcas.getCas().addFsToIndexes(annotation); if (Annotation.class.isAssignableFrom(mapping.getTargetClass())) { ((Annotation) annotation).setBegin(hAnno.getBegin()); ((Annotation) annotation).setEnd(hAnno.getEnd()); } if (elm.hasAttr("xml:id") && !exists(elm.attr("xml:id"))) { String id = elm.attr("xml:id"); idRegistry.put(id, new AbstractMap.SimpleEntry<Element, FeatureStructure>(elm, annotation)); } } return annotation; }
protected <T extends TOP> T getFeatureStructure(JCas jcas, XMLElement hAnno, Element elm, Rule<T> mapping) { T annotation = null; if (mapping.isUnique()) { annotation = getOrCreate(jcas, mapping.getTargetClass()); } else { annotation = jcas.getCas().createFS(JCasUtil.getType(jcas, mapping.getTargetClass())); jcas.getCas().addFsToIndexes(annotation); if (Annotation.class.isAssignableFrom(mapping.getTargetClass())) { ((Annotation) annotation).setBegin(hAnno.getBegin()); ((Annotation) annotation).setEnd(hAnno.getEnd()); } if (elm.hasAttr("xml:id") && !exists(elm.attr("xml:id"))) { String id = elm.attr("xml:id"); idRegistry.put(id, new AbstractMap.SimpleEntry<Element, FeatureStructure>(elm, annotation)); } } return annotation; }
private void addAnnotation(final int start, final int end) { final Annotation a = new WordToken(jCas); a.setBegin(start); a.setEnd(end); a.addToIndexes(); }
@Test public void testGetSingleCoveredMissing() { final Annotation a = new Annotation(jCas); a.setBegin(1); a.setEnd(12); final Optional<Annotation> missing = AnnotationUtils.getSingleCovered(Annotation.class, a); Assert.assertFalse(missing.isPresent()); }
@Test public void testGetSingleCovered() { final Annotation a = new Annotation(jCas); a.setBegin(0); a.setEnd(4); final Optional<Annotation> single = AnnotationUtils.getSingleCovered(Annotation.class, a); Assert.assertEquals("012", single.get().getCoveredText()); }