use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel in project webanno by webanno.
the class SlotFeatureSupport method getFeatureValue.
@Override
public <T> T getFeatureValue(AnnotationFeature aFeature, FeatureStructure aFS) {
// Get type and features - we need them later in the loop
Feature linkFeature = aFS.getType().getFeatureByBaseName(aFeature.getName());
Type linkType = aFS.getCAS().getTypeSystem().getType(aFeature.getLinkTypeName());
Feature roleFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeRoleFeatureName());
Feature targetFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeTargetFeatureName());
List<LinkWithRoleModel> links = new ArrayList<>();
ArrayFS array = (ArrayFS) aFS.getFeatureValue(linkFeature);
if (array != null) {
for (FeatureStructure link : array.toArray()) {
LinkWithRoleModel m = new LinkWithRoleModel();
m.role = link.getStringValue(roleFeat);
m.targetAddr = WebAnnoCasUtil.getAddr(link.getFeatureValue(targetFeat));
m.label = ((AnnotationFS) link.getFeatureValue(targetFeat)).getCoveredText();
links.add(m);
}
}
return (T) links;
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel in project webanno by webanno.
the class LinkFeatureEditor method actionAdd.
private void actionAdd(AjaxRequestTarget aTarget) {
if (StringUtils.isBlank((String) field.getModelObject())) {
error("Must set slot label before adding!");
aTarget.addChildren(getPage(), IFeedback.class);
} else {
@SuppressWarnings("unchecked") List<LinkWithRoleModel> links = (List<LinkWithRoleModel>) LinkFeatureEditor.this.getModelObject().value;
AnnotatorState state = LinkFeatureEditor.this.stateModel.getObject();
LinkWithRoleModel m = new LinkWithRoleModel();
m.role = (String) field.getModelObject();
links.add(m);
state.setArmedSlot(LinkFeatureEditor.this.getModelObject().feature, links.size() - 1);
// Need to re-render the whole form because a slot in another
// link editor might get unarmed
aTarget.add(getOwner());
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel in project webanno by webanno.
the class LinkFeatureEditor method autoAddImportantTags.
private void autoAddImportantTags(List<Tag> aTagset, List<PossibleValue> aPossibleValues) {
if (aTagset == null || aTagset.isEmpty() || aPossibleValues == null || aPossibleValues.isEmpty()) {
return;
}
// Construct a quick index for tags
Set<String> tagset = new HashSet<>();
for (Tag t : aTagset) {
tagset.add(t.getName());
}
// Get links list and build role index
@SuppressWarnings("unchecked") List<LinkWithRoleModel> links = (List<LinkWithRoleModel>) getModelObject().value;
Set<String> roles = new HashSet<>();
for (LinkWithRoleModel l : links) {
roles.add(l.role);
}
// Loop over values to see which of the tags are important and add them.
for (PossibleValue value : aPossibleValues) {
if (!value.isImportant() || !tagset.contains(value.getValue())) {
continue;
}
// Check if there is already a slot with the given name
if (roles.contains(value.getValue())) {
continue;
}
// Add empty slot in UI with that name.
LinkWithRoleModel m = new LinkWithRoleModel();
m.role = value.getValue();
// Marking so that can be ignored later.
m.autoCreated = true;
links.add(m);
// NOT arming the slot here!
}
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel in project webanno by webanno.
the class WebAnnoCasUtil method setLinkFeature.
private static void setLinkFeature(FeatureStructure aFS, AnnotationFeature aFeature, List<LinkWithRoleModel> aValue, Feature feature) {
Type linkType = aFS.getCAS().getTypeSystem().getType(aFeature.getLinkTypeName());
Feature roleFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeRoleFeatureName());
Feature targetFeat = linkType.getFeatureByBaseName(aFeature.getLinkTypeTargetFeatureName());
// Create all the links
// FIXME: actually we could re-use existing link link feature structures
List<FeatureStructure> linkFSes = new ArrayList<>();
if (aValue != null) {
// remove duplicate links
Set<LinkWithRoleModel> links = new HashSet<>(aValue);
for (LinkWithRoleModel e : links) {
// set
if (e.targetAddr == -1) {
continue;
}
FeatureStructure link = aFS.getCAS().createFS(linkType);
link.setStringValue(roleFeat, e.role);
link.setFeatureValue(targetFeat, selectByAddr(aFS.getCAS(), e.targetAddr));
linkFSes.add(link);
}
}
setLinkFeatureValue(aFS, feature, linkFSes);
}
use of de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel in project webanno by webanno.
the class SpanRenderer method render.
@Override
public void render(JCas aJcas, List<AnnotationFeature> aFeatures, VDocument aResponse, AnnotatorState aBratAnnotatorModel) {
List<AnnotationFeature> visibleFeatures = aFeatures.stream().filter(f -> f.isVisible() && f.isEnabled()).collect(Collectors.toList());
SpanAdapter typeAdapter = getTypeAdapter();
Type type = getType(aJcas.getCas(), typeAdapter.getAnnotationTypeName());
int windowBegin = aBratAnnotatorModel.getWindowBeginOffset();
int windowEnd = aBratAnnotatorModel.getWindowEndOffset();
List<Sentence> visibleSentences = selectCovered(aJcas, Sentence.class, windowBegin, windowEnd);
for (AnnotationFS fs : selectCovered(aJcas.getCas(), type, windowBegin, windowEnd)) {
String bratTypeName = TypeUtil.getUiTypeName(typeAdapter);
Map<String, String> features = getFeatures(typeAdapter, fs, visibleFeatures);
Map<String, String> hoverFeatures = getHoverFeatures(typeAdapter, fs, aFeatures);
Sentence beginSent = null;
Sentence endSent = null;
// the visible window
for (Sentence sent : visibleSentences) {
if (beginSent == null) {
// offset of the current annotation.
if (sent.getBegin() <= fs.getBegin() && fs.getBegin() <= sent.getEnd()) {
beginSent = sent;
}
// second sentence.
if (fs.getBegin() == fs.getEnd()) {
endSent = sent;
}
}
if (endSent == null) {
if (sent.getBegin() <= fs.getEnd() && fs.getEnd() <= sent.getEnd()) {
endSent = sent;
}
}
if (beginSent != null && endSent != null) {
break;
}
}
if (beginSent == null || endSent == null) {
throw new IllegalStateException("Unable to determine sentences in which the annotation starts/ends: " + fs);
}
List<Sentence> sentences = selectCovered(aJcas, Sentence.class, beginSent.getBegin(), endSent.getEnd());
List<VRange> ranges = new ArrayList<>();
if (sentences.size() > 1) {
for (Sentence sentence : sentences) {
if (sentence.getBegin() <= fs.getBegin() && fs.getBegin() < sentence.getEnd()) {
ranges.add(new VRange(fs.getBegin() - windowBegin, sentence.getEnd() - windowBegin));
} else if (sentence.getBegin() <= fs.getEnd() && fs.getEnd() <= sentence.getEnd()) {
ranges.add(new VRange(sentence.getBegin() - windowBegin, fs.getEnd() - windowBegin));
} else {
ranges.add(new VRange(sentence.getBegin() - windowBegin, sentence.getEnd() - windowBegin));
}
}
aResponse.add(new VSpan(typeAdapter.getLayer(), fs, bratTypeName, ranges, features, hoverFeatures));
} else {
// FIXME It should be possible to remove this case and the if clause because
// the case that a FS is inside a single sentence is just a special case
aResponse.add(new VSpan(typeAdapter.getLayer(), fs, bratTypeName, new VRange(fs.getBegin() - windowBegin, fs.getEnd() - windowBegin), features, hoverFeatures));
}
// Render errors if required features are missing
renderRequiredFeatureErrors(visibleFeatures, fs, aResponse);
// Render slots
int fi = 0;
for (AnnotationFeature feat : typeAdapter.listFeatures()) {
if (MultiValueMode.ARRAY.equals(feat.getMultiValueMode()) && LinkMode.WITH_ROLE.equals(feat.getLinkMode())) {
List<LinkWithRoleModel> links = typeAdapter.getFeatureValue(feat, fs);
for (int li = 0; li < links.size(); li++) {
LinkWithRoleModel link = links.get(li);
FeatureStructure targetFS = selectByAddr(fs.getCAS(), link.targetAddr);
aResponse.add(new VArc(typeAdapter.getLayer(), new VID(fs, fi, li), bratTypeName, fs, targetFS, link.role, features));
}
}
fi++;
}
}
}
Aggregations