use of org.apache.uima.cas.Feature in project webanno by webanno.
the class AutomationUtil method repeateRelationAnnotation.
public static void repeateRelationAnnotation(AnnotatorState aState, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, AnnotationFS fs, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
for (SourceDocument d : aDocumentService.listSourceDocuments(aState.getProject())) {
loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aState.getUser());
JCas jCas = aCorrectionDocumentService.readCorrectionCas(d);
ArcAdapter adapter = (ArcAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
String sourceFName = adapter.getSourceFeatureName();
String targetFName = adapter.getTargetFeatureName();
Type type = getType(jCas.getCas(), aFeature.getLayer().getName());
Type spanType = getType(jCas.getCas(), adapter.getAttachTypeName());
Feature arcSpanFeature = spanType.getFeatureByBaseName(adapter.getAttachFeatureName());
Feature dependentFeature = type.getFeatureByBaseName(targetFName);
Feature governorFeature = type.getFeatureByBaseName(sourceFName);
AnnotationFS dependentFs = null;
AnnotationFS governorFs = null;
if (adapter.getAttachFeatureName() != null) {
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
} else {
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature);
}
if (adapter.isCrossMultipleSentence()) {
List<AnnotationFS> mSpanAnnos = new ArrayList<>(getAllAnnoFss(jCas, governorFs.getType()));
repeatRelation(aState, 0, jCas.getDocumentText().length() - 1, aFeature, aValue, jCas, adapter, dependentFs, governorFs, mSpanAnnos);
} else {
for (Sentence sent : select(jCas, Sentence.class)) {
List<AnnotationFS> spanAnnos = selectCovered(governorFs.getType(), sent);
repeatRelation(aState, sent.getBegin(), sent.getEnd(), aFeature, aValue, jCas, adapter, dependentFs, governorFs, spanAnnos);
}
}
aCorrectionDocumentService.writeCorrectionCas(jCas, d);
}
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class ImportExportServiceImpl method updateCasWithTagSet.
/**
* A Helper method to add {@link TagsetDescription} to {@link CAS}
*
* @param aCas
* the CAA.
* @param aLayer
* the layer.
* @param aTagSetName
* the tagset.
*/
private static void updateCasWithTagSet(CAS aCas, String aLayer, String aTagSetName) {
Type TagsetType = CasUtil.getType(aCas, TagsetDescription.class);
Feature layerFeature = TagsetType.getFeatureByBaseName("layer");
Feature nameFeature = TagsetType.getFeatureByBaseName("name");
boolean tagSetModified = false;
// modify existing tagset Name
for (FeatureStructure fs : CasUtil.select(aCas, TagsetType)) {
String layer = fs.getStringValue(layerFeature);
String tagSetName = fs.getStringValue(nameFeature);
if (layer.equals(aLayer)) {
// only if the tagset name is changed
if (!aTagSetName.equals(tagSetName)) {
fs.setStringValue(nameFeature, aTagSetName);
aCas.addFsToIndexes(fs);
}
tagSetModified = true;
break;
}
}
if (!tagSetModified) {
FeatureStructure fs = aCas.createFS(TagsetType);
fs.setStringValue(layerFeature, aLayer);
fs.setStringValue(nameFeature, aTagSetName);
aCas.addFsToIndexes(fs);
}
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class ChainRenderer method render.
@Override
public void render(JCas aJcas, List<AnnotationFeature> aFeatures, VDocument aResponse, AnnotatorState aState) {
List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
// Find the features for the arc and span labels - it is possible that we do not find a
// feature for arc/span labels because they may have been disabled.
AnnotationFeature spanLabelFeature = null;
AnnotationFeature arcLabelFeature = null;
for (AnnotationFeature f : visibleFeatures) {
if (WebAnnoConst.COREFERENCE_TYPE_FEATURE.equals(f.getName())) {
spanLabelFeature = f;
}
if (WebAnnoConst.COREFERENCE_RELATION_FEATURE.equals(f.getName())) {
arcLabelFeature = f;
}
}
// At this point arc and span feature labels must have been found! If not, the later code
// will crash.
ChainAdapter typeAdapter = getTypeAdapter();
Type chainType = typeAdapter.getAnnotationType(aJcas.getCas());
Feature chainFirst = chainType.getFeatureByBaseName(typeAdapter.getChainFirstFeatureName());
int colorIndex = 0;
// Iterate over the chains
for (FeatureStructure chainFs : selectFS(aJcas.getCas(), chainType)) {
AnnotationFS linkFs = (AnnotationFS) chainFs.getFeatureValue(chainFirst);
AnnotationFS prevLinkFs = null;
// Iterate over the links of the chain
while (linkFs != null) {
Feature linkNext = linkFs.getType().getFeatureByBaseName(typeAdapter.getLinkNextFeatureName());
AnnotationFS nextLinkFs = (AnnotationFS) linkFs.getFeatureValue(linkNext);
// Is link after window? If yes, we can skip the rest of the chain
if (linkFs.getBegin() >= aState.getWindowEndOffset()) {
// Go to next chain
break;
}
// end within the window
if (!(linkFs.getBegin() >= aState.getWindowBeginOffset()) && (linkFs.getEnd() <= aState.getWindowEndOffset())) {
// prevLinkFs remains null until we enter the window
linkFs = nextLinkFs;
// Go to next link
continue;
}
String bratTypeName = TypeUtil.getUiTypeName(typeAdapter);
// Render span
{
String bratLabelText = TypeUtil.getUiLabelText(typeAdapter, linkFs, (spanLabelFeature != null) ? asList(spanLabelFeature) : emptyList());
String bratHoverText = TypeUtil.getUiHoverText(typeAdapter, linkFs, (spanLabelFeature != null) ? asList(spanLabelFeature) : emptyList());
VRange offsets = new VRange(linkFs.getBegin() - aState.getWindowBeginOffset(), linkFs.getEnd() - aState.getWindowBeginOffset());
aResponse.add(new VSpan(typeAdapter.getLayer(), linkFs, bratTypeName, offsets, colorIndex, singletonMap("label", bratLabelText), singletonMap("label", bratHoverText)));
}
// and last link are within the window ;)
if (prevLinkFs != null) {
String bratLabelText = null;
if (typeAdapter.isLinkedListBehavior() && arcLabelFeature != null) {
// Render arc label
bratLabelText = TypeUtil.getUiLabelText(typeAdapter, prevLinkFs, asList(arcLabelFeature));
} else {
// Render only chain type
bratLabelText = TypeUtil.getUiLabelText(typeAdapter, prevLinkFs, emptyList());
}
aResponse.add(new VArc(typeAdapter.getLayer(), new VID(prevLinkFs, 1, VID.NONE, VID.NONE), bratTypeName, prevLinkFs, linkFs, colorIndex, singletonMap("label", bratLabelText)));
}
// Render errors if required features are missing
renderRequiredFeatureErrors(visibleFeatures, linkFs, aResponse);
// if (BratAjaxCasUtil.isSame(linkFs, nextLinkFs)) {
// log.error("Loop in CAS detected, aborting rendering of chains");
// break;
// }
prevLinkFs = linkFs;
linkFs = nextLinkFs;
}
// The color index is updated even for chains that have no visible links in the current
// window because we would like the chain color to be independent of visibility. In
// particular the color of a chain should not change when switching pages/scrolling.
colorIndex++;
}
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class Renderer method getHoverFeatures.
default Map<String, String> getHoverFeatures(TypeAdapter aAdapter, AnnotationFS aFs, List<AnnotationFeature> aFeatures) {
FeatureSupportRegistry fsr = getFeatureSupportRegistry();
Map<String, String> hoverfeatures = new LinkedHashMap<>();
if (aAdapter.getLayer().isShowTextInHover())
hoverfeatures.put("__spantext__", aFs.getCoveredText());
for (AnnotationFeature feature : aFeatures) {
if (!feature.isEnabled() || !feature.isIncludeInHover() || !MultiValueMode.NONE.equals(feature.getMultiValueMode())) {
continue;
}
Feature labelFeature = aFs.getType().getFeatureByBaseName(feature.getName());
String text = defaultString(fsr.getFeatureSupport(feature).renderFeatureValue(feature, aFs, labelFeature));
hoverfeatures.put(feature.getName(), text);
}
return hoverfeatures;
}
use of org.apache.uima.cas.Feature in project webanno by webanno.
the class ArcAdapter method interalAddToCas.
/**
* @param aWindowBegin
* begin offset of the first visible sentence
* @param aWindowEnd
* end offset of the last visible sentence
*/
private AnnotationFS interalAddToCas(JCas aJCas, int aWindowBegin, int aWindowEnd, AnnotationFS aOriginFs, AnnotationFS aTargetFs) throws AnnotationException {
Type type = getType(aJCas.getCas(), annotationTypeName);
Feature dependentFeature = type.getFeatureByBaseName(targetFeatureName);
Feature governorFeature = type.getFeatureByBaseName(sourceFeatureName);
Type spanType = getType(aJCas.getCas(), attachType);
AnnotationFS dependentFs = null;
AnnotationFS governorFs = null;
// presently visible on screen is sufficient - we don't have to scan the whole CAS.
for (AnnotationFS fs : selectCovered(aJCas.getCas(), type, aWindowBegin, aWindowEnd)) {
if (attachFeatureName != null) {
Feature arcSpanFeature = spanType.getFeatureByBaseName(attachFeatureName);
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
} else {
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature);
}
if (dependentFs == null || governorFs == null) {
log.warn("Relation [" + getLayer().getName() + "] with id [" + getAddr(fs) + "] has loose ends - ignoring during while checking for duplicates.");
continue;
}
// update the label of the existing arc
if (!allowStacking && isDuplicate(governorFs, aOriginFs, dependentFs, aTargetFs)) {
throw new AnnotationException("Cannot create another annotation of layer [" + getLayer().getUiName() + "] at this location - stacking is not " + "enabled for this layer.");
}
}
// It is new ARC annotation, create it
dependentFs = aTargetFs;
governorFs = aOriginFs;
// for POS annotation, since custom span layers do not have attach feature
if (attachFeatureName != null) {
dependentFs = selectCovered(aJCas.getCas(), spanType, dependentFs.getBegin(), dependentFs.getEnd()).get(0);
governorFs = selectCovered(aJCas.getCas(), spanType, governorFs.getBegin(), governorFs.getEnd()).get(0);
}
if (dependentFs == null || governorFs == null) {
throw new AnnotationException("Relation must have a source and a target!");
}
// Set the relation offsets in DKPro Core style - the relation recieves the offsets from
// the dependent
// If origin and target spans are multiple tokens, dependentFS.getBegin will be the
// the begin position of the first token and dependentFS.getEnd will be the End
// position of the last token.
AnnotationFS newAnnotation = aJCas.getCas().createAnnotation(type, dependentFs.getBegin(), dependentFs.getEnd());
newAnnotation.setFeatureValue(dependentFeature, dependentFs);
newAnnotation.setFeatureValue(governorFeature, governorFs);
aJCas.addFsToIndexes(newAnnotation);
return newAnnotation;
}
Aggregations