/** * @param anno an annotation * @param contextCharNum number of characters * @return contextCharNum characters after the given annotation */ public static String textAfter(AnnotationFS anno, int contextCharNum) { Preconditions.checkArgument(contextCharNum >= 0); String txt = anno.getCAS().getDocumentText(); int begin = anno.getEnd(); int end = Math.min(txt.length(), begin + contextCharNum); return txt.substring(begin, end); }
/** * update a base annotation with slot annotations * * @param linkFSesPerAnno * contains list of slot annotations per a base annotation * @param aLinkeF * The link slot annotation feature */ private void addSlotAnnotations(Map<AnnotationFS, List<FeatureStructure>> linkFSesPerAnno, Feature aLinkeF) { for (AnnotationFS anno : linkFSesPerAnno.keySet()) { ArrayFS array = anno.getCAS().createArrayFS(linkFSesPerAnno.get(anno).size()); array.copyFromArray( linkFSesPerAnno.get(anno).toArray(new FeatureStructure[linkFSesPerAnno.get(anno).size()]), 0, 0, linkFSesPerAnno.get(anno).size()); anno.setFeatureValue(aLinkeF, array); anno.getCAS().addFsToIndexes(anno); } }
/** * update a base annotation with slot annotations * * @param linkFSesPerAnno * contains list of slot annotations per a base annotation * @param aLinkeF * The link slot annotation feature */ private void addSlotAnnotations(Map<AnnotationFS, List<FeatureStructure>> linkFSesPerAnno, Feature aLinkeF) { for (AnnotationFS anno : linkFSesPerAnno.keySet()) { ArrayFS array = anno.getCAS().createArrayFS(linkFSesPerAnno.get(anno).size()); array.copyFromArray( linkFSesPerAnno.get(anno) .toArray(new FeatureStructure[linkFSesPerAnno.get(anno).size()]), 0, 0, linkFSesPerAnno.get(anno).size()); anno.setFeatureValue(aLinkeF, array); anno.getCAS().addFsToIndexes(anno); } }
/** * @param anno an annotation * @param contextCharNum number of characters * @return contextCharNum characters before the given annotation */ public static String textBefore(AnnotationFS anno, int contextCharNum) { Preconditions.checkArgument(contextCharNum >= 0); int begin = Math.max(0, anno.getBegin() - contextCharNum); int end = anno.getBegin(); return anno.getCAS().getDocumentText().substring(begin, end); }
/** * update a base annotation with slot annotations * * @param linkFSesPerAnno * contains list of slot annotations per a base annotation * @param aLinkeF * The link slot annotation feature */ private void addSlotAnnotations(Map<AnnotationFS, List<FeatureStructure>> linkFSesPerAnno, Feature aLinkeF) { for (AnnotationFS anno : linkFSesPerAnno.keySet()) { ArrayFS array = anno.getCAS().createArrayFS(linkFSesPerAnno.get(anno).size()); array.copyFromArray( linkFSesPerAnno.get(anno) .toArray(new FeatureStructure[linkFSesPerAnno.get(anno).size()]), 0, 0, linkFSesPerAnno.get(anno).size()); anno.setFeatureValue(aLinkeF, array); anno.getCAS().addFsToIndexes(anno); } }
public Feature getFeature(AnnotationFS annotation) { if(f==null) { /* * TODO Ensure that the Feature will be reset for a new TypeSystem */ f = annotation.getCAS().getTypeSystem().getFeatureByFullName(feature); } return f; }
private List<NGram> getNGrams(List<T> tokenList, int k) { List<NGram> nGrams = new ArrayList<NGram>(); int size = tokenList.size(); for (int i = 0; i < (size + 1 - k); i++) { try { NGram ngram = new NGram(tokenList.get(i).getCAS().getJCas(), tokenList.get(i) .getBegin(), tokenList.get(i + k - 1).getEnd()); ngram.setText(getTokenText(tokenList, i, i + k - 1)); nGrams.add(ngram); } catch (CASException e) { throw new IllegalStateException(e); } } return nGrams; }
private List<NGram> getNGrams(List<T> tokenList, int k) { List<NGram> nGrams = new ArrayList<NGram>(); int size = tokenList.size(); for (int i = 0; i < (size + 1 - k); i++) { try { NGram ngram = new NGram(tokenList.get(i).getCAS().getJCas(), tokenList.get(i) .getBegin(), tokenList.get(i + k - 1).getEnd()); ngram.setText(getTokenText(tokenList, i, i + k - 1)); nGrams.add(ngram); } catch (CASException e) { throw new IllegalStateException(e); } } return nGrams; }
/** * Return an annotation preceding or following of a given reference annotation. * * @param <T> * the JCas type. * @param aType * a type. * @param annotation * anchor annotation * @param index * 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 index points beyond the type index bounds. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ @SuppressWarnings("unchecked") public static <T extends Annotation> T selectSingleRelative(Class<T> aType, AnnotationFS annotation, int index) { CAS cas = annotation.getCAS(); Type t = CasUtil.getType(cas, aType); return (T) CasUtil.selectSingleRelative(cas, t, annotation, index); }
public static FeatureStructure[] getDebugLogicalStructure_SubAnnotations(AnnotationFS fs) { // uses sub iterators - may cause apparant skipping of initial annotations due to type // priorities. return getIndexContents(fs.getCAS().getAnnotationIndex().subiterator(fs)); // built-in // annotation // index }
/** * Returns the (one) annotation of a given type that is aligned with another annotation. * * @param type * The annotation type to be looked up. * @param annotation * An annotation. * @return The annotation aligned with another annotation. */ private AnnotationFS getAnnotation(Type type, AnnotationFS annotation) { List<AnnotationFS> annotations = CasUtil.selectCovered(annotation.getCAS(), type, annotation); if (annotations.size() != 1) { getLogger().debug( "Could not find matching annotation of type " + type + " for annotation: " + annotation.getCoveredText()); return null; } return annotations.get(0); } }
/** * Get a list of annotations of the given annotation type located between two annotations. Does * not use subiterators and does not respect type priorities. Zero-width annotations what lie on * the borders are included in the result, e.g. if the boundary annotations are [1..2] and [2..3] * then an annotation [2..2] is returned. If there is a non-zero overlap between the boundary * annotations, the result is empty. The method properly handles cases where the second boundary * annotations occurs before the first boundary annotation by switching their roles. * * @param <T> * the JCas type. * @param type * a UIMA type. * @param ann1 * the first boundary annotation. * @param ann2 * the second boundary annotation. * @return a return value. * @see Subiterator * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ public static <T extends Annotation> List<T> selectBetween(final Class<T> type, AnnotationFS ann1, AnnotationFS ann2) { return cast(CasUtil.selectBetween(CasUtil.getType(ann1.getCAS(), type), ann1, ann2)); }
/** * Convenience method to get a sub-iterator for the specified type. * * @param <T> * the iteration type. * @param container * the containing annotation. * @param type * the type. * @param ambiguous * If set to <code>false</code>, resulting iterator will be unambiguous. * @param strict * Controls if annotations that overlap to the right are considered in or out. * @return A sub-iterator. * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ @SuppressWarnings("unchecked") public static <T extends AnnotationFS> Iterator<T> iterator(AnnotationFS container, Class<T> type, boolean ambiguous, boolean strict) { CAS cas = container.getCAS(); return ((AnnotationIndex<T>) cas.getAnnotationIndex(CasUtil.getType(cas, type))).subiterator( container, ambiguous, strict); }
/** * Returns the n annotations preceding the given annotation * * @param <T> * the JCas type. * @param aType * a type. * @param annotation * anchor annotation * @param count * number of annotations to collect * @return List of aType annotations preceding anchor annotation * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ public static <T extends Annotation> List<T> selectPreceding(Class<T> aType, AnnotationFS annotation, int count) { Type t = CasUtil.getType(annotation.getCAS(), aType); return cast(CasUtil.selectPreceding(annotation.getView(), t, annotation, count)); }
/** * Returns the n annotations following the given annotation * * @param <T> * the JCas type. * @param aType * a type. * @param annotation * anchor annotation * @param count * number of annotations to collect * @return List of aType annotations following anchor annotation * @see <a href="package-summary.html#SortOrder">Order of selected feature structures</a> */ public static <T extends Annotation> List<T> selectFollowing(Class<T> aType, AnnotationFS annotation, int count) { Type t = CasUtil.getType(annotation.getCAS(), aType); return cast(CasUtil.selectFollowing(annotation.getView(), t, annotation, count)); }
private static void makeChainHead(Type aType, AnnotationFS first) { CAS cas = first.getCAS(); FeatureStructure h = cas.createFS(aType); FSUtil.setFeature(h, "first", first); cas.addFsToIndexes(h); }
private Set<AnnotationFS> getAttachedSpans(AnnotationFS aFs, AnnotationLayer aLayer) { CAS cas = aFs.getCAS(); Set<AnnotationFS> attachedSpans = new HashSet<>(); TypeAdapter adapter = annotationService.getAdapter(aLayer); if (adapter instanceof SpanAdapter && aLayer.getAttachType() != null) { Type spanType = CasUtil.getType(cas, aLayer.getAttachType().getName()); Feature attachFeature = spanType.getFeatureByBaseName(aLayer.getAttachFeature() .getName()); for (AnnotationFS attachedFs : selectAt(cas, spanType, aFs.getBegin(), aFs.getEnd())) { if (isSame(attachedFs.getFeatureValue(attachFeature), aFs)) { attachedSpans.add(attachedFs); } } } return attachedSpans; }
public void annotationAdded(AnnotationFS annotation, RuleMatch creator) { CAS cas = annotation.getCAS(); Type t = cas.getTypeSystem().getType(TYPE); Feature featureRule = t.getFeatureByBaseName(FEATURE_RULE); Feature featureAnnotation = t.getFeatureByBaseName(FEATURE_ANNOTATION); Feature featureScript = t.getFeatureByBaseName(FEATURE_SCRIPT); Feature featureId = t.getFeatureByBaseName(FEATURE_ID); String ruleString = "provided"; String ruleScript = ""; int ruleId = -1; if (creator != null) { ruleString = verbalizer.verbalize(creator.getRule()); ruleId = creator.getRule().getId(); ruleScript = creator.getRule().getParent().getScript().getRootBlock().getNamespace(); } FeatureStructure fs = cas.createFS(t); fs.setStringValue(featureRule, ruleString); fs.setFeatureValue(featureAnnotation, annotation); fs.setIntValue(featureId, ruleId); fs.setStringValue(featureScript, ruleScript); fsList.add(fs); }
public static FeatureStructure[] resolve(RelationAdapter aAdapter, AnnotationFS aRelation) { Type type = aRelation.getType(); Feature targetFeature = type.getFeatureByBaseName(aAdapter.getTargetFeatureName()); Feature sourceFeature = type.getFeatureByBaseName(aAdapter.getSourceFeatureName()); FeatureStructure targetFs; FeatureStructure sourceFs; if (aAdapter.getAttachFeatureName() != null) { Type spanType = getType(aRelation.getCAS(), aAdapter.getAttachTypeName()); Feature arcSpanFeature = spanType.getFeatureByBaseName(aAdapter.getAttachFeatureName()); targetFs = aRelation.getFeatureValue(targetFeature).getFeatureValue(arcSpanFeature); sourceFs = aRelation.getFeatureValue(sourceFeature).getFeatureValue(arcSpanFeature); } else { targetFs = aRelation.getFeatureValue(targetFeature); sourceFs = aRelation.getFeatureValue(sourceFeature); } return new FeatureStructure[] { sourceFs, targetFs }; } }
public static FeatureStructure[] resolve(RelationAdapter aAdapter, AnnotationFS aRelation) { Type type = aRelation.getType(); Feature targetFeature = type.getFeatureByBaseName(aAdapter.getTargetFeatureName()); Feature sourceFeature = type.getFeatureByBaseName(aAdapter.getSourceFeatureName()); FeatureStructure targetFs; FeatureStructure sourceFs; if (aAdapter.getAttachFeatureName() != null) { Type spanType = getType(aRelation.getCAS(), aAdapter.getAttachTypeName()); Feature arcSpanFeature = spanType.getFeatureByBaseName(aAdapter.getAttachFeatureName()); targetFs = aRelation.getFeatureValue(targetFeature).getFeatureValue(arcSpanFeature); sourceFs = aRelation.getFeatureValue(sourceFeature).getFeatureValue(arcSpanFeature); } else { targetFs = aRelation.getFeatureValue(targetFeature); sourceFs = aRelation.getFeatureValue(sourceFeature); } return new FeatureStructure[] { sourceFs, targetFs }; } }