private List<AnnotationFS> getRelationArgs(AnnotationFS anno, Set<Feature> argFeatures) { List<AnnotationFS> result = Lists.newLinkedList(); assert argFeatures.size() == 2; for (Feature f : argFeatures) { AnnotationFS argAnno = (AnnotationFS) anno.getFeatureValue(f); if (argAnno == null) { throw new IllegalStateException(); } result.add(argAnno); } return result; }
private static boolean isSamAnno(Type aType, AnnotationFS aMFs, AnnotationFS aFs) { for (Feature f : aType.getFeatures()) { // anywhere is ok if (f.getName().equals(CAS.FEATURE_FULL_NAME_BEGIN)) { continue; } // anywhere is ok if (f.getName().equals(CAS.FEATURE_FULL_NAME_END)) { continue; } if (!f.getRange().isPrimitive() && aMFs.getFeatureValue(f) instanceof SofaFS) { continue; } // do not attach relation on empty span annotations if (aMFs.getFeatureValueAsString(f) == null) { continue; } if (aFs.getFeatureValueAsString(f) == null) { continue; } if (!aMFs.getFeatureValueAsString(f).equals(aFs.getFeatureValueAsString(f))) { return false; } } return true; }
private static boolean isSamAnno(Type aType, AnnotationFS aMFs, AnnotationFS aFs) { for (Feature f : aType.getFeatures()) { // anywhere is ok if (f.getName().equals(CAS.FEATURE_FULL_NAME_BEGIN)) { continue; } // anywhere is ok if (f.getName().equals(CAS.FEATURE_FULL_NAME_END)) { continue; } if (!f.getRange().isPrimitive() && aMFs.getFeatureValue(f) instanceof SofaFS) { continue; } // do not attach relation on empty span annotations if (aMFs.getFeatureValueAsString(f) == null) { continue; } if (aFs.getFeatureValueAsString(f) == null) { continue; } if (!aMFs.getFeatureValueAsString(f).equals(aFs.getFeatureValueAsString(f))) { return false; } } return true; }
/** * Get the link following the current link. */ private AnnotationFS getNextLink(AnnotationFS aLink) { return (AnnotationFS) aLink.getFeatureValue(aLink.getType().getFeatureByBaseName( linkNextFeatureName)); }
private void copyFeatures(AnnotationFS oldFS, FeatureStructure newFS, CAS cas) { List<?> features = oldFS.getType().getFeatures(); Type newType = newFS.getType(); for (Object object : features) { Feature feature = (Feature) object; String shortName = feature.getShortName(); Feature newFeature = newType.getFeatureByBaseName(shortName); if (newFeature != null) { if (feature.getRange().isPrimitive()) { String value = oldFS.getFeatureValueAsString(feature); newFS.setFeatureValueFromString(newFeature, value); } else { FeatureStructure value = oldFS.getFeatureValue(feature); newFS.setFeatureValue(newFeature, value); } } } }
private Map<String, BratEntity> makeArgMap(AnnotationFS uAnno, BratRelationType bratType, Map<String, Feature> argFeatMap) { Map<String, BratEntity> argAnnotations = Maps.newHashMapWithExpectedSize(2); for (String argName : argFeatMap.keySet()) { Feature argFeat = argFeatMap.get(argName); FeatureStructure argFS = uAnno.getFeatureValue(argFeat); if (argFS == null) { getLogger().warn(String.format( "Can't map %s to Brat relation. Its feature '%s' is not set.", toPrettyString(uAnno), argFeat)); return null; } BratEntity argValue = context.demandEntity(argFS); argAnnotations.put(argName, argValue); } return argAnnotations; }
private void copyFeatures(AnnotationFS oldFS, FeatureStructure newFS, CAS cas) { List<?> features = oldFS.getType().getFeatures(); Type newType = newFS.getType(); for (Object object : features) { Feature feature = (Feature) object; String shortName = feature.getShortName(); Feature newFeature = newType.getFeatureByBaseName(shortName); if (newFeature != null) { if (feature.getRange().isPrimitive()) { String value = oldFS.getFeatureValueAsString(newFeature); newFS.setFeatureValueFromString(newFeature, value); } else { FeatureStructure value = oldFS.getFeatureValue(feature); newFS.setFeatureValue(newFeature, value); } } } }
private static List<AnnotationFS> getRelAnnosOnPosition(AnnotationFS aFs, AnnotationFS aOriginFs, AnnotationFS aTargetFs, JCas aJcas) { Type type = aFs.getType(); Feature sourceFeat = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE); Feature targetFeat = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET); return selectCovered(aJcas.getCas(), type, aFs.getBegin(), aFs.getEnd()).stream() .filter(fs -> fs.getFeatureValue(sourceFeat).equals(aOriginFs) && fs.getFeatureValue(targetFeat).equals(aTargetFs)) .collect(Collectors.toList()); }
private static List<AnnotationFS> getRelAnnosOnPosition(AnnotationFS aFs, AnnotationFS aOriginFs, AnnotationFS aTargetFs, JCas aJcas) { Type type = aFs.getType(); Feature sourceFeat = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_SOURCE); Feature targetFeat = type.getFeatureByBaseName(WebAnnoConst.FEAT_REL_TARGET); return selectCovered(aJcas.getCas(), type, aFs.getBegin(), aFs.getEnd()).stream() .filter(fs -> fs.getFeatureValue(sourceFeat).equals(aOriginFs) && fs.getFeatureValue(targetFeat).equals(aTargetFs)) .collect(Collectors.toList()); }
FeatureStructure source = fs.getFeatureValue(sourceFeat); FeatureStructure target = fs.getFeatureValue(targetFeat);
/** * Get the link following the current link. */ private AnnotationFS getNextLink(AnnotationFS aLink) { return (AnnotationFS) aLink.getFeatureValue(aLink.getType().getFeatureByBaseName( getLinkNextFeatureName())); }
/** * Get the link following the current link. */ private AnnotationFS getNextLink(AnnotationFS aLink) { return (AnnotationFS) aLink.getFeatureValue(aLink.getType().getFeatureByBaseName( getLinkNextFeatureName())); }
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 }; } }
/** * Gets the feature as int. * * @param aCas the a cas * @param aFeature the a feature * @param aName the a name * @return the feature as int * @throws Exception the exception */ public static int getFeatureAsInt(CAS aCas, Feature aFeature, String aName) throws Exception { Feature seqNo2 = aFeature.getRange().getFeatureByBaseName(aName); FeatureStructure documentMetaData = aCas.getView(CAS.NAME_DEFAULT_SOFA).getDocumentAnnotation() .getFeatureValue(aFeature); return documentMetaData.getIntValue(seqNo2); }
@Override public List<Pair<LogMessage, AnnotationFS>> onValidate(TypeAdapter aAdapter, JCas aJCas) { if (aAdapter.getLayer().isCrossSentence()) { emptyList(); } RelationAdapter adapter = (RelationAdapter) aAdapter; CAS cas = aJCas.getCas(); Type type = getType(cas, adapter.getAnnotationTypeName()); Feature targetFeature = type.getFeatureByBaseName(adapter.getTargetFeatureName()); Feature sourceFeature = type.getFeatureByBaseName(adapter.getSourceFeatureName()); List<Pair<LogMessage, AnnotationFS>> messages = new ArrayList<>(); for (AnnotationFS fs : select(cas, type)) { AnnotationFS targetFs = (AnnotationFS) fs.getFeatureValue(targetFeature); AnnotationFS sourceFs = (AnnotationFS) fs.getFeatureValue(sourceFeature); if (!isBeginInSameSentence(aJCas, targetFs.getBegin(), sourceFs.getBegin())) { messages.add(Pair.of( LogMessage.error(this, "Crossing sentence bounardies is not permitted."), fs)); } } return messages; } }
/** * Returns a value associated with a given feature. * * @param aCas - * Cas containing data to extract * @param aFeature - * feature to locate in the CAS * @param aName - * name of the feature * @return - value as String * @throws Exception the exception */ public static String getFeatureAsString(CAS aCas, Feature aFeature, String aName) throws Exception { Feature seqNo2 = aFeature.getRange().getFeatureByBaseName(aName); FeatureStructure documentMetaData = aCas.getView(CAS.NAME_DEFAULT_SOFA).getDocumentAnnotation() .getFeatureValue(aFeature); return documentMetaData.getStringValue(seqNo2); }
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; }
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature) .getFeatureValue(arcSpanFeature); governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature) .getFeatureValue(arcSpanFeature); dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature); governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature);
@Override public List<? extends Position> generateSubPositions(int aCasId, AnnotationFS aFs, LinkCompareBehavior aLinkCompareBehavior) { List<Position> subPositions = new ArrayList<>(); for (LinkFeatureDecl decl : linkFeatures) { Feature linkFeature = aFs.getType().getFeatureByBaseName(decl.name); ArrayFS array = (ArrayFS) aFs.getFeatureValue(linkFeature); if (array == null) { continue; } for (FeatureStructure linkFS : array.toArray()) { String role = linkFS.getStringValue(linkFS.getType().getFeatureByBaseName( decl.roleFeature)); AnnotationFS target = (AnnotationFS) linkFS.getFeatureValue(linkFS.getType() .getFeatureByBaseName(decl.targetFeature)); Position pos = getPosition(aCasId, aFs, decl.name, role, target.getBegin(), target.getEnd(), aLinkCompareBehavior); subPositions.add(pos); } } return subPositions; } }