Search in sources :

Example 51 with Sentence

use of de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence in project webanno by webanno.

the class AnnotatorViewState method moveToNextPage.

default void moveToNextPage(JCas aJCas) {
    int nextSentenceAddress = WebAnnoCasUtil.getNextPageFirstSentenceAddress(aJCas, getFirstVisibleUnitAddress(), getPreferences().getWindowSize());
    if (getFirstVisibleUnitAddress() == nextSentenceAddress) {
        throw new IllegalStateException("This is last page!");
    }
    Sentence sentence = selectByAddr(aJCas, Sentence.class, nextSentenceAddress);
    setFirstVisibleUnit(sentence);
    setFocusUnitIndex(WebAnnoCasUtil.getSentenceNumber(aJCas, sentence.getBegin()));
}
Also used : Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) WebAnnoCasUtil.getFirstSentence(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.getFirstSentence)

Example 52 with Sentence

use of de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence 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)

Example 53 with Sentence

use of de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence in project webanno by webanno.

the class AnnotatorStateImpl method setFirstVisibleUnit.

@Override
public void setFirstVisibleUnit(Sentence aFirstVisibleUnit) {
    JCas jcas;
    try {
        jcas = aFirstVisibleUnit.getCAS().getJCas();
    } catch (CASException e) {
        throw new IllegalStateException("Unable to fetch JCas from CAS", e);
    }
    firstVisibleUnitAddress = WebAnnoCasUtil.getAddr(aFirstVisibleUnit);
    firstVisibleUnitBegin = aFirstVisibleUnit.getBegin();
    firstVisibleUnitEnd = aFirstVisibleUnit.getEnd();
    Sentence lastVisibleUnit = getLastSentenceInDisplayWindow(jcas, getAddr(aFirstVisibleUnit), getPreferences().getWindowSize());
    firstVisibleUnitIndex = WebAnnoCasUtil.getSentenceNumber(jcas, aFirstVisibleUnit.getBegin());
    lastVisibleUnitIndex = WebAnnoCasUtil.getSentenceNumber(jcas, lastVisibleUnit.getBegin());
    unitCount = select(jcas, Sentence.class).size();
    windowBeginOffset = aFirstVisibleUnit.getBegin();
    windowEndOffset = lastVisibleUnit.getEnd();
}
Also used : JCas(org.apache.uima.jcas.JCas) CASException(org.apache.uima.cas.CASException) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)

Example 54 with Sentence

use of de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence in project webanno by webanno.

the class AnnotatorViewState method moveForward.

default void moveForward(JCas aJCas) {
    // Fetch the first sentence on screen
    Sentence sentence = selectByAddr(aJCas, Sentence.class, getFirstVisibleUnitAddress());
    // Find the following one
    int address = getNextSentenceAddress(aJCas, sentence);
    // Move to it
    setFirstVisibleUnit(selectByAddr(aJCas, Sentence.class, address));
}
Also used : Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) WebAnnoCasUtil.getFirstSentence(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.getFirstSentence)

Example 55 with Sentence

use of de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence in project webanno by webanno.

the class AnnotatorViewState method moveToPreviousPage.

// ---------------------------------------------------------------------------------------------
// Navigation within a document
// ---------------------------------------------------------------------------------------------
default void moveToPreviousPage(JCas aJCas) {
    int firstSentenceAddress = WebAnnoCasUtil.getFirstSentenceAddress(aJCas);
    int previousSentenceAddress = WebAnnoCasUtil.getPreviousDisplayWindowSentenceBeginAddress(aJCas, getFirstVisibleUnitAddress(), getPreferences().getWindowSize());
    // if there are not much sentences to go back to as defined in windowSize
    if (previousSentenceAddress == getFirstVisibleUnitAddress() && // Check whether it's not the beginning of document
    getFirstVisibleUnitAddress() != firstSentenceAddress) {
        previousSentenceAddress = firstSentenceAddress;
    }
    if (getFirstVisibleUnitAddress() == previousSentenceAddress) {
        throw new IllegalStateException("This is First Page!");
    }
    Sentence sentence = selectByAddr(aJCas, Sentence.class, previousSentenceAddress);
    setFirstVisibleUnit(sentence);
    setFocusUnitIndex(WebAnnoCasUtil.getSentenceNumber(aJCas, sentence.getBegin()));
}
Also used : Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) WebAnnoCasUtil.getFirstSentence(de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.getFirstSentence)

Aggregations

Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)90 JCas (org.apache.uima.jcas.JCas)41 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)34 ArrayList (java.util.ArrayList)22 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)14 Type (org.apache.uima.cas.Type)12 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)12 IOException (java.io.IOException)9 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)8 POS (de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.pos.POS)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 TokenBuilder (org.apache.uima.fit.testing.factory.TokenBuilder)7 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)6 WebAnnoCasUtil.getFirstSentence (de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.getFirstSentence)6 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)6 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)6 FrequencyDistribution (de.tudarmstadt.ukp.dkpro.core.api.frequency.util.FrequencyDistribution)6 CASException (org.apache.uima.cas.CASException)6 AutomationTypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.AutomationTypeAdapter)5