Java Code Examples for org.apache.uima.cas.text.AnnotationFS#getBegin()
The following examples show how to use
org.apache.uima.cas.text.AnnotationFS#getBegin() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebAnnoCasUtil.java From webanno with Apache License 2.0 | 6 votes |
/** * Return true if these two annotations agree on every non slot features */ public static boolean isEquivalentAnnotation(AnnotationFS aFs1, AnnotationFS aFs2) { // Check offsets (because they are excluded by shouldIgnoreFeatureOnMerge()) if (aFs1.getBegin() != aFs2.getBegin() || aFs1.getEnd() != aFs2.getEnd()) { return false; } // Check the features (basically limiting to the primitive features) for (Feature f : aFs1.getType().getFeatures()) { if (shouldIgnoreFeatureOnMerge(aFs1, f)) { continue; } Object value1 = getFeatureValue(aFs1, f); Object value2 = getFeatureValue(aFs2, f); if (!Objects.equals(value1, value2)) { return false; } } return true; }
Example 2
Source File: CasTreeViewer.java From uima-uimaj with Apache License 2.0 | 6 votes |
/** * Recursive method called by {@link buildTree(DefaultMutableTreeNode,FSIterator)}. * * @param aParentNode * root node of tree to be built * @param aIterator * iterator over all annotation in CAS * @param aStartPos * text position at which to begin processing * @param aEndPos * text position at which to end processing */ private void _buildTree(DefaultMutableTreeNode aParentNode, FSIterator aIterator, int aStartPos, int aEndPos) { while (aIterator.isValid()) { AnnotationFS curAnnot = (AnnotationFS) aIterator.get(); int curAnnotStart = curAnnot.getBegin(); int curAnnotEnd = curAnnot.getEnd(); if (curAnnotEnd <= aEndPos) { // move iterator to next annotation aIterator.moveToNext(); if (curAnnotStart < curAnnotEnd) // account for bug in JTalent { // add this annotation as a child of aParentNode DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new AnnotationTreeNodeObject( curAnnot)); aParentNode.add(newNode); // recursively add children to this node _buildTree(newNode, aIterator, curAnnotStart, curAnnotEnd); } } else break; } }
Example 3
Source File: DataMajorityNerRecommender.java From inception with Apache License 2.0 | 6 votes |
private List<Annotation> predict(Collection<AnnotationFS> candidates, DataMajorityModel aModel) { List<Annotation> result = new ArrayList<>(); for (AnnotationFS token : candidates) { String tokenText = token.getCoveredText(); if (tokenText.length() > 0 && !Character.isUpperCase(tokenText.codePointAt(0))) { continue; } int begin = token.getBegin(); int end = token.getEnd(); Annotation annotation = new Annotation(aModel.majorityLabel, aModel.confidence, aModel.numberOfAnnotations, begin, end); result.add(annotation); } return result; }
Example 4
Source File: WebAnnoCasUtil.java From webanno with Apache License 2.0 | 6 votes |
/** * Get overlapping annotations where selection overlaps with annotations.<br> * Example: if annotation is (5, 13) and selection covered was from (7, 12); the annotation (5, * 13) is returned as overlapped selection <br> * If multiple annotations are [(3, 8), (9, 15), (16, 21)] and selection covered was from (10, * 18), overlapped annotation [(9, 15), (16, 21)] should be returned * * @param aCas * a CAS containing the annotation. * @param aType * a UIMA type. * @param aBegin * begin offset. * @param aEnd * end offset. * @return a return value. */ public static List<AnnotationFS> selectOverlapping(CAS aCas, Type aType, int aBegin, int aEnd) { List<AnnotationFS> annotations = new ArrayList<>(); for (AnnotationFS t : select(aCas, aType)) { if (t.getBegin() >= aEnd) { break; } // not yet there if (t.getEnd() <= aBegin) { continue; } annotations.add(t); } return annotations; }
Example 5
Source File: NamedEntityLinker.java From inception with Apache License 2.0 | 5 votes |
@Override public void predict(RecommenderContext aContext, CAS aCas) throws RecommendationException { Type predictedType = getPredictedType(aCas); for (AnnotationFS sentence : selectSentences(aCas)) { for (AnnotationFS annotation : CasUtil.selectCovered(aCas, predictedType, sentence)) { int begin = annotation.getBegin(); int end = annotation.getEnd(); predictSingle(annotation.getCoveredText(), begin, end, aCas); } } }
Example 6
Source File: WebAnnoCasUtil.java From webanno with Apache License 2.0 | 5 votes |
/** * Get the current sentence based on the annotation begin/end offset * * @param aCas * the CAS. * @param aBegin * the begin offset. * @param aEnd * the end offset. * @return the sentence. */ public static AnnotationFS getCurrentSentence(CAS aCas, int aBegin, int aEnd) { AnnotationFS currentSentence = null; for (AnnotationFS sentence : selectSentences(aCas)) { if (sentence.getBegin() <= aBegin && sentence.getEnd() > aBegin && sentence.getEnd() <= aEnd) { currentSentence = sentence; break; } } return currentSentence; }
Example 7
Source File: RelationOffsetsCheck.java From webanno with Apache License 2.0 | 5 votes |
@Override public boolean check(Project aProject, CAS aCas, List<LogMessage> aMessages) { boolean ok = true; for (AnnotationLayer layer : annotationService.listAnnotationLayer(aProject)) { if (!WebAnnoConst.RELATION_TYPE.equals(layer.getType())) { continue; } Type type; try { type = getType(aCas, layer.getName()); } catch (IllegalArgumentException e) { // If the type does not exist, the CAS has not been upgraded. In this case, we // can skip checking the layer because there will be no annotations anyway. continue; } for (AnnotationFS rel : select(aCas, type)) { AnnotationFS target = getFeature(rel, WebAnnoConst.FEAT_REL_TARGET, AnnotationFS.class); if ((rel.getBegin() != target.getBegin()) || (rel.getEnd() != target.getEnd())) { aMessages.add(new LogMessage(this, LogLevel.ERROR, "Relation offsets [%d,%d] to not match target offsets [%d,%d]", rel.getBegin(), rel.getEnd(), target.getBegin(), target.getEnd())); ok = false; } } } return ok; }
Example 8
Source File: AgreementUtils.java From webanno with Apache License 2.0 | 5 votes |
private static Object extractLinkFeatureValueForAgreement(FeatureStructure aFs, String aFeature, int aLinkIndex, LinkCompareBehavior aLCB) { ArrayFS links = (ArrayFS) aFs.getFeatureValue(aFs.getType().getFeatureByBaseName( aFeature)); FeatureStructure link = links.get(aLinkIndex); switch (aLCB) { case LINK_TARGET_AS_LABEL: // FIXME The target feature name should be obtained from the feature // definition! AnnotationFS target = (AnnotationFS) link.getFeatureValue(link.getType() .getFeatureByBaseName("target")); return target.getBegin() + "-" + target.getEnd() + " [" + target.getCoveredText() + "]"; case LINK_ROLE_AS_LABEL: // FIXME The role feature name should be obtained from the feature // definition! String role = link.getStringValue(link.getType().getFeatureByBaseName( "role")); return role; default: throw new IllegalStateException("Unknown link target comparison mode [" + aLCB + "]"); } }
Example 9
Source File: LowerRightAnnotationSideAction.java From uima-uimaj with Apache License 2.0 | 5 votes |
/** * Lower right annotation side. * * @param document the document * @param annotation the annotation */ public static void lowerRightAnnotationSide(ICasDocument document, AnnotationFS annotation) { Type annotationType = annotation.getType(); Feature endFeature = annotationType.getFeatureByBaseName("end"); if (annotation.getBegin() < annotation.getEnd()) { annotation.setIntValue(endFeature, annotation.getEnd() - 1); } document.update(annotation); }
Example 10
Source File: LowerLeftAnnotationSideAction.java From uima-uimaj with Apache License 2.0 | 5 votes |
/** * Lower left annotation side. * * @param document the document * @param annotation the annotation */ public static void lowerLeftAnnotationSide(ICasDocument document, AnnotationFS annotation) { Type annotationType = annotation.getType(); Feature beginFeature = annotationType.getFeatureByBaseName("begin"); if (annotation.getBegin() < annotation.getEnd()) { annotation.setIntValue(beginFeature, annotation.getBegin() + 1); } document.update(annotation); }
Example 11
Source File: TableAnnotationDivider.java From biomedicus with Apache License 2.0 | 5 votes |
private void divideAnnotation(AnnotationFS annotation) { Objects.requireNonNull(typeToCreate); Objects.requireNonNull(dividers); FSIterator<AnnotationFS> subiterator = dividers.subiterator(annotation); int begin = annotation.getBegin(); while (subiterator.hasNext()) { int end = subiterator.next().getBegin(); cas.addFsToIndexes(cas.createAnnotation(typeToCreate, begin, end)); begin = end; } }
Example 12
Source File: AnnotationComparator.java From webanno with Apache License 2.0 | 5 votes |
@Override public int compare(AnnotationFS arg0, AnnotationFS arg1) { int beginDiff = arg0.getBegin() - arg1.getBegin(); if (beginDiff == 0) { return arg1.getEnd() - arg0.getEnd(); } else { return beginDiff; } }
Example 13
Source File: PdfAnnotationEditor.java From inception with Apache License 2.0 | 5 votes |
private void selectSpanAnnotation( AjaxRequestTarget aTarget, VID paramId, CAS aCas) { try { if (paramId.isSynthetic()) { extensionRegistry.fireAction(getActionHandler(), getModelObject(), aTarget, aCas, paramId, "spanOpenDialog"); return; } AnnotationFS fs = selectByAddr(aCas, AnnotationFS.class, paramId.getId()); Offset offset = new Offset(fs.getBegin(), fs.getEnd()); if (offset.getBegin() > -1 && offset.getEnd() > -1) { AnnotatorState state = getModelObject(); if (state.isSlotArmed()) { // When filling a slot, the current selection is *NOT* changed. The // Span annotation which owns the slot that is being filled remains // selected! getActionHandler().actionFillSlot(aTarget, aCas, offset.getBegin(), offset.getEnd(), paramId); } else { state.getSelection().selectSpan(paramId, aCas, offset.getBegin(), offset.getEnd()); getActionHandler().actionSelect(aTarget); } } else { handleError("Unable to select span annotation: No match was found", aTarget); } } catch (AnnotationException | IOException e) { handleError("Unable to select span annotation", e, aTarget); } }
Example 14
Source File: SpanDiffAdapter.java From webanno with Apache License 2.0 | 5 votes |
@Override public Position getPosition(int aCasId, FeatureStructure aFS, String aFeature, String aRole, int aLinkTargetBegin, int aLinkTargetEnd, LinkCompareBehavior aLinkCompareBehavior) { AnnotationFS annoFS = (AnnotationFS) aFS; String collectionId = null; String documentId = null; try { FeatureStructure dmd = WebAnnoCasUtil.getDocumentMetadata(aFS.getCAS()); collectionId = FSUtil.getFeature(dmd, "collectionId", String.class); documentId = FSUtil.getFeature(dmd, "documentId", String.class); } catch (IllegalArgumentException e) { // We use this information only for debugging - so we can ignore if the information // is missing. } String linkTargetText = null; if (aLinkTargetBegin != -1 && aFS.getCAS().getDocumentText() != null) { linkTargetText = aFS.getCAS().getDocumentText() .substring(aLinkTargetBegin, aLinkTargetEnd); } return new SpanPosition(collectionId, documentId, aCasId, getType(), annoFS.getBegin(), annoFS.getEnd(), annoFS.getCoveredText(), aFeature, aRole, aLinkTargetBegin, aLinkTargetEnd, linkTargetText, aLinkCompareBehavior); }
Example 15
Source File: WebAnnoCasUtil.java From webanno with Apache License 2.0 | 5 votes |
/** * Get the sentence based on the annotation begin offset * * @param aCas * the CAS. * @param aBegin * the begin offset. * @return the sentence. */ public static AnnotationFS selectSentenceCovering(CAS aCas, int aBegin) { AnnotationFS currentSentence = null; for (AnnotationFS sentence : select(aCas, getType(aCas, Sentence.class))) { if (sentence.getBegin() <= aBegin && sentence.getEnd() > aBegin) { currentSentence = sentence; break; } } return currentSentence; }
Example 16
Source File: SymbolIndexedDocument.java From biomedicus with Apache License 2.0 | 4 votes |
/** * Indexes all the symbols from an original document. * * @param originalDocumentView jCas original document view. * @return The newly created symbol indexed document. */ public static SymbolIndexedDocument fromView(CAS originalDocumentView) { Type viewIndexType = originalDocumentView.getTypeSystem() .getType("edu.umn.biomedicus.rtfuima.type.ViewIndex"); Feature destinationNameFeature = viewIndexType .getFeatureByBaseName("destinationName"); Feature destinationIndexFeature = viewIndexType .getFeatureByBaseName("destinationIndex"); AnnotationIndex<AnnotationFS> viewIndexAI = originalDocumentView .getAnnotationIndex(viewIndexType); List<SymbolLocation> symbolLocations = new ArrayList<>(); Map<String, Map<Integer, Integer>> destinationMap = new HashMap<>(); int index = 0; int lastEnd = 0; for (AnnotationFS annotation : viewIndexAI) { int begin = annotation.getBegin(); int end = annotation.getEnd(); String destinationName = annotation.getStringValue(destinationNameFeature); SymbolLocation symbolLocation = new SymbolLocation( destinationName, begin - lastEnd, end - begin, index++ ); symbolLocations.add(symbolLocation); int destinationIndex = annotation.getIntValue(destinationIndexFeature); destinationMap.compute(destinationName, (String key, @Nullable Map<Integer, Integer> value) -> { if (value == null) { value = new HashMap<>(); } value.put(destinationIndex, symbolLocations.size() - 1); return value; }); lastEnd = end; } return new SymbolIndexedDocument(symbolLocations, destinationMap, originalDocumentView.getDocumentText()); }
Example 17
Source File: BlueCasUtil.java From bluima with Apache License 2.0 | 4 votes |
/** * REM: modified from CasUtils.. * * Get a list of annotations constraint by a certain annotation. Iterates * over all annotations to find the covered annotations. Does not use * subiterators and does not respect type prioritites. Was adapted from * {@link Subiterator}. Uses the same approach except that type priorities * are ignored. * * @param cas * a CAS. * @param coveringAnnotation * the covering annotation. * @see Subiterator */ public static List<Annotation> selectCovered(CAS cas, AnnotationFS coveringAnnotation) { final int begin = coveringAnnotation.getBegin(); final int end = coveringAnnotation.getEnd(); final List<Annotation> list = new ArrayList<Annotation>(); final FSIterator<AnnotationFS> it = cas.getAnnotationIndex().iterator(); // Try to seek the insertion point. it.moveTo(coveringAnnotation); // If the insertion point is beyond the index, move back to the last. if (!it.isValid()) { it.moveToLast(); if (!it.isValid()) { return list; } } // Ignore type priorities by seeking to the first that has the same // begin boolean moved = false; while (it.isValid() && (it.get()).getBegin() >= begin) { it.moveToPrevious(); moved = true; } // If we moved, then we are now on one starting before the requested // begin, so we have to // move one ahead. if (moved) { it.moveToNext(); } // If we managed to move outside the index, start at first. if (!it.isValid()) { it.moveToFirst(); } // Skip annotations whose start is before the start parameter. while (it.isValid() && (it.get()).getBegin() < begin) { it.moveToNext(); } boolean strict = true; while (it.isValid()) { AnnotationFS a = it.get(); if (!(a instanceof Annotation)) continue; // If the start of the current annotation is past the end parameter, // we're done. if (a.getBegin() > end) { break; } it.moveToNext(); if (strict && a.getEnd() > end) { continue; } checkArgument(a.getBegin() >= coveringAnnotation.getBegin(), "Illegal begin " + a.getBegin() + " in [" + coveringAnnotation.getBegin() + ".." + coveringAnnotation.getEnd() + "]"); checkArgument( a.getEnd() <= coveringAnnotation.getEnd(), "Illegal end " + a.getEnd() + " in [" + coveringAnnotation.getBegin() + ".." + coveringAnnotation.getEnd() + "]"); if (!a.equals(coveringAnnotation) && !BlueCasUtil.isDocAnnot(a)) { list.add((Annotation) a); } } return unmodifiableList(list); }
Example 18
Source File: WebannoTsv2Writer.java From webanno with Apache License 2.0 | 4 votes |
private void setTokenAnnos(CAS aCas, Map<Integer, String> aTokenAnnoMap, Type aType, Feature aFeature) { LowLevelCAS llCas = aCas.getLowLevelCAS(); for (AnnotationFS annoFs : CasUtil.select(aCas, aType)) { boolean first = true; boolean previous = false; // exists previous annotation, place-holed O-_ should be kept for (Token token : selectCovered(Token.class, annoFs)) { if (annoFs.getBegin() <= token.getBegin() && annoFs.getEnd() >= token.getEnd()) { String annotation = annoFs.getFeatureValueAsString(aFeature); if (annotation == null) { annotation = aType.getName() + "_"; } if (aTokenAnnoMap.get(llCas.ll_getFSRef(token)) == null) { if (previous) { if (!multipleSpans.contains(aType.getName())) { aTokenAnnoMap.put(llCas.ll_getFSRef(token), annotation); } else { aTokenAnnoMap.put(llCas.ll_getFSRef(token), "O-_|" + (first ? "B-" : "I-") + annotation); first = false; } } else { if (!multipleSpans.contains(aType.getName())) { aTokenAnnoMap.put(llCas.ll_getFSRef(token), annotation); } else { aTokenAnnoMap.put(llCas.ll_getFSRef(token), (first ? "B-" : "I-") + annotation); first = false; } } } else { if (!multipleSpans.contains(aType.getName())) { aTokenAnnoMap.put(llCas.ll_getFSRef(token), aTokenAnnoMap.get(llCas.ll_getFSRef(token)) + "|" + annotation); previous = true; } else { aTokenAnnoMap.put(llCas.ll_getFSRef(token), aTokenAnnoMap.get(llCas.ll_getFSRef(token)) + "|" + (first ? "B-" : "I-") + annotation); first = false; previous = true; } } } } } }
Example 19
Source File: CasUtil.java From uima-uimafit with Apache License 2.0 | 4 votes |
/** * Return an annotation preceding or following of a given reference annotation. If the type * parameter corresponds to the type or a subtype of the anchor annotation and the relative * position is 0, then the anchor annotation is returned. * * @param cas * a CAS containing the annotation. * @param type * a type. * @param aAnchor * anchor annotation * @param aPosition * relative position to access. A negative value selects a preceding annotation while a * positive number selects a following annotation. * @return the addressed annotation. * @throws IndexOutOfBoundsException * if the relative position points beyond the type index bounds. * @throws IllegalArgumentException * if the relative position is {@code 0} and the anchor type does not subsume the * selected type. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ public static AnnotationFS selectSingleRelative(CAS cas, Type type, AnnotationFS aAnchor, int aPosition) { if (!cas.getTypeSystem().subsumes(cas.getAnnotationType(), type)) { throw new IllegalArgumentException("Type [" + type.getName() + "] is not an annotation type"); } // move to first previous annotation FSIterator<AnnotationFS> itr = cas.getAnnotationIndex(type).iterator(); itr.moveTo(aAnchor); if (aPosition < 0) { // If the insertion point is beyond the index, move back to the last. if (!itr.isValid()) { itr.moveToLast(); if (!itr.isValid()) { throw new IndexOutOfBoundsException("Reached end of index while seeking."); } } // No need to do additional seeks here (as done in selectCovered) because the current method // does not have to worry about type priorities - it never returns annotations that have // the same offset as the reference annotation. // make sure we're past the beginning of the reference annotation while (itr.isValid() && itr.get().getEnd() > aAnchor.getBegin()) { itr.moveToPrevious(); } for (int i = 0; i < (-aPosition - 1) && itr.isValid(); ++i, itr.moveToPrevious()) { // Seeking } if (!itr.isValid()) { throw new IndexOutOfBoundsException("Reached end of index while seeking."); } else { return itr.get(); } } else if (aPosition > 0) { // When seeking forward, there is no need to check if the insertion point is beyond the // index. If it was, there would be nothing beyond it that could be found and returned. // The moveTo operation also does not yield an iterator being invalid because it points // *before the first* index entry, at max it points *to the first* index entry, so this // case also does not need to be handled. // No need to do additional seeks here (as done in selectCovered) because the current method // does not have to worry about type priorities - it never returns annotations that have // the same offset as the reference annotation. // make sure we're past the end of the reference annotation while (itr.isValid() && itr.get().getBegin() < aAnchor.getEnd()) { itr.moveToNext(); } for (int i = 0; i < (aPosition - 1) && itr.isValid(); ++i, itr.moveToPrevious()) { // Seeking } if (!itr.isValid()) { throw new IndexOutOfBoundsException("Reached end of index while seeking."); } else { return itr.get(); } } else if (cas.getTypeSystem().subsumes(aAnchor.getType(), type)) { return aAnchor; } else { throw new IllegalArgumentException( "Relative position cannot be 0 if the type of the anchor annotator does not subsume " + "the selected type."); } }
Example 20
Source File: ContainingConstraint.java From uima-uimaj with Apache License 2.0 | 2 votes |
/** * Checks if is containing. * * @param annotation the annotation * @param containing the containing * @return true, if is containing */ private boolean isContaining(AnnotationFS annotation, AnnotationFS containing) { return containing.getBegin() <= annotation.getBegin() && containing.getEnd() >= annotation.getEnd(); }