Search in sources :

Example 1 with LinkWithRoleModel

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;
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) Type(org.apache.uima.cas.Type) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) ArrayFS(org.apache.uima.cas.ArrayFS) ArrayList(java.util.ArrayList) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 2 with LinkWithRoleModel

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());
    }
}
Also used : LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) List(java.util.List)

Example 3 with LinkWithRoleModel

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!
    }
}
Also used : LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) List(java.util.List) Tag(de.tudarmstadt.ukp.clarin.webanno.model.Tag) PossibleValue(de.tudarmstadt.ukp.clarin.webanno.constraints.evaluator.PossibleValue) HashSet(java.util.HashSet)

Example 4 with LinkWithRoleModel

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);
}
Also used : FeatureStructure(org.apache.uima.cas.FeatureStructure) Type(org.apache.uima.cas.Type) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) ArrayList(java.util.ArrayList) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) HashSet(java.util.HashSet)

Example 5 with LinkWithRoleModel

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++;
        }
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) WebAnnoCasUtil.selectByAddr(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectByAddr) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) LinkMode(de.tudarmstadt.ukp.clarin.webanno.model.LinkMode) ArrayList(java.util.ArrayList) Type(org.apache.uima.cas.Type) VRange(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VRange) Map(java.util.Map) FeatureStructure(org.apache.uima.cas.FeatureStructure) JCas(org.apache.uima.jcas.JCas) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) MultiValueMode(de.tudarmstadt.ukp.clarin.webanno.model.MultiValueMode) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) Collectors(java.util.stream.Collectors) VSpan(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSpan) CasUtil.selectCovered(org.apache.uima.fit.util.CasUtil.selectCovered) List(java.util.List) TypeUtil(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) JCasUtil.selectCovered(org.apache.uima.fit.util.JCasUtil.selectCovered) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) FeatureSupportRegistry(de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry) LinkWithRoleModel(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel) VSpan(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VSpan) ArrayList(java.util.ArrayList) FeatureStructure(org.apache.uima.cas.FeatureStructure) VID(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) VArc(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc) VRange(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VRange) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Aggregations

LinkWithRoleModel (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel)11 List (java.util.List)7 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)6 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)5 Type (org.apache.uima.cas.Type)5 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)4 ArrayList (java.util.ArrayList)4 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)4 SpanAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter)3 HashSet (java.util.HashSet)3 UIMAException (org.apache.uima.UIMAException)3 Feature (org.apache.uima.cas.Feature)3 FeatureStructure (org.apache.uima.cas.FeatureStructure)3 ArcAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter)1 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)1 FeatureSupportRegistry (de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry)1 VID (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID)1 VArc (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VArc)1 VDocument (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument)1 VRange (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VRange)1