Search in sources :

Example 31 with AnnotationDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument in project webanno by webanno.

the class AutomationUtil method repeateSpanAnnotation.

public static void repeateSpanAnnotation(AnnotatorState aState, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, int aStart, int aEnd, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
    AnnotationDocument annoDoc = aDocumentService.getAnnotationDocument(aState.getDocument(), aState.getUser());
    JCas annoCas = aDocumentService.readAnnotationCas(annoDoc);
    // get selected text, concatenations of tokens
    String selectedText = WebAnnoCasUtil.getSelectedText(annoCas, aStart, aEnd);
    SpanAdapter adapter = (SpanAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
    for (SourceDocument d : aDocumentService.listSourceDocuments(aState.getProject())) {
        loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aState.getUser());
        JCas jCas = aCorrectionDocumentService.readCorrectionCas(d);
        for (Sentence sentence : select(jCas, Sentence.class)) {
            String sentenceText = sentence.getCoveredText().toLowerCase();
            for (int i = -1; (i = sentenceText.indexOf(selectedText.toLowerCase(), i)) != -1; i = i + selectedText.length()) {
                if (selectCovered(jCas, Token.class, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length()).size() > 0) {
                    int addr = adapter.add(aState, jCas, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length() - 1);
                    adapter.setFeatureValue(aState, jCas, addr, aFeature, aValue);
                }
            }
        }
        aCorrectionDocumentService.writeCorrectionCas(jCas, d);
    }
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) SpanAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.SpanAdapter) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)

Example 32 with AnnotationDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument in project webanno by webanno.

the class CurationPage method actionLoadDocument.

/**
 * Open a document or to a different document. This method should be used only the first time
 * that a document is accessed. It reset the annotator state and upgrades the CAS.
 */
@Override
protected void actionLoadDocument(AjaxRequestTarget aTarget) {
    LOG.info("BEGIN LOAD_DOCUMENT_ACTION");
    AnnotatorState state = getModelObject();
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    state.setUser(user);
    try {
        // ANNOTATION_FINISHED
        if (!SourceDocumentState.CURATION_FINISHED.equals(state.getDocument().getState())) {
            documentService.transitionSourceDocumentState(state.getDocument(), ANNOTATION_IN_PROGRESS_TO_CURATION_IN_PROGRESS);
        }
        // Load user preferences
        PreferencesUtil.loadPreferences(username, settingsService, projectService, annotationService, state, state.getMode());
        // Re-render whole page as sidebar size preference may have changed
        aTarget.add(CurationPage.this);
        List<AnnotationDocument> finishedAnnotationDocuments = new ArrayList<>();
        for (AnnotationDocument annotationDocument : documentService.listAnnotationDocuments(state.getDocument())) {
            if (annotationDocument.getState().equals(AnnotationDocumentState.FINISHED)) {
                finishedAnnotationDocuments.add(annotationDocument);
            }
        }
        SuggestionBuilder cb = new SuggestionBuilder(casStorageService, documentService, correctionDocumentService, curationDocumentService, annotationService, userRepository);
        AnnotationDocument randomAnnotationDocument = null;
        if (finishedAnnotationDocuments.size() > 0) {
            randomAnnotationDocument = finishedAnnotationDocuments.get(0);
        }
        // annotation
        for (AnnotationDocument ad : finishedAnnotationDocuments) {
            upgradeCasAndSave(ad.getDocument(), ad.getUser());
        }
        Map<String, JCas> jCases = cb.listJcasesforCuration(finishedAnnotationDocuments, randomAnnotationDocument, state.getMode());
        JCas mergeJCas = cb.getMergeCas(state, state.getDocument(), jCases, randomAnnotationDocument, true);
        // (Re)initialize brat model after potential creating / upgrading CAS
        state.reset();
        state.getPreferences().setCurationWindowSize(WebAnnoCasUtil.getSentenceCount(mergeJCas));
        // Initialize the visible content
        state.setFirstVisibleUnit(WebAnnoCasUtil.getFirstSentence(mergeJCas));
        // if project is changed, reset some project specific settings
        if (currentprojectId != state.getProject().getId()) {
            state.clearRememberedFeatures();
        }
        currentprojectId = state.getProject().getId();
        SuggestionBuilder builder = new SuggestionBuilder(casStorageService, documentService, correctionDocumentService, curationDocumentService, annotationService, userRepository);
        curationContainer = builder.buildCurationContainer(state);
        curationContainer.setBratAnnotatorModel(state);
        curationPanel.editor.reset(aTarget);
        updatePanel(curationContainer, aTarget);
        updateSentenceNumber(mergeJCas, state.getFirstVisibleUnitAddress());
        curationPanel.init(aTarget, curationContainer);
        // curationPanel.updatePanel(aTarget, curationContainer);
        // Load constraints
        state.setConstraints(constraintsService.loadConstraints(state.getProject()));
        aTarget.add(getOrCreatePositionInfoLabel());
        aTarget.add(documentNamePanel);
        aTarget.add(remergeDocumentLink);
        aTarget.add(finishDocumentLink);
    } catch (Exception e) {
        handleException(aTarget, e);
    }
    LOG.info("END LOAD_DOCUMENT_ACTION");
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) ArrayList(java.util.ArrayList) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) SuggestionBuilder(de.tudarmstadt.ukp.clarin.webanno.ui.curation.component.model.SuggestionBuilder) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Example 33 with AnnotationDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument in project webanno by webanno.

the class CurationPage method upgradeCasAndSave.

public void upgradeCasAndSave(SourceDocument aDocument, String aUsername) throws IOException {
    User user = userRepository.get(aUsername);
    if (documentService.existsAnnotationDocument(aDocument, user)) {
        AnnotationDocument annotationDocument = documentService.getAnnotationDocument(aDocument, user);
        try {
            CAS cas = documentService.readAnnotationCas(annotationDocument).getCas();
            annotationService.upgradeCas(cas, annotationDocument);
            documentService.writeAnnotationCas(cas.getJCas(), annotationDocument, false);
        } catch (Exception e) {
        // no need to catch, it is acceptable that no curation document
        // exists to be upgraded while there are annotation documents
        }
    }
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) CAS(org.apache.uima.cas.CAS) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Example 34 with AnnotationDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument in project webanno by webanno.

the class CorrectionPage method actionLoadDocument.

@Override
protected void actionLoadDocument(AjaxRequestTarget aTarget) {
    LOG.info("BEGIN LOAD_DOCUMENT_ACTION");
    AnnotatorState state = getModelObject();
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    state.setUser(user);
    state.setProject(state.getProject());
    state.setDocument(state.getDocument(), getListOfDocs());
    try {
        // Check if there is an annotation document entry in the database. If there is none,
        // create one.
        AnnotationDocument annotationDocument = documentService.createOrGetAnnotationDocument(state.getDocument(), user);
        // Read the correction CAS - if it does not exist yet, from the initial CAS
        JCas correctionCas;
        if (correctionDocumentService.existsCorrectionCas(state.getDocument())) {
            correctionCas = correctionDocumentService.readCorrectionCas(state.getDocument());
        } else {
            correctionCas = documentService.createOrReadInitialCas(state.getDocument());
        }
        // Read the annotation CAS or create an annotation CAS from the initial CAS by stripping
        // annotations
        JCas editorCas;
        if (documentService.existsCas(state.getDocument(), user.getUsername())) {
            editorCas = documentService.readAnnotationCas(annotationDocument);
        } else {
            editorCas = documentService.createOrReadInitialCas(state.getDocument());
            editorCas = BratAnnotatorUtility.clearJcasAnnotations(editorCas, state.getDocument(), user, documentService);
        }
        // Update the CASes
        annotationService.upgradeCas(editorCas.getCas(), annotationDocument);
        correctionDocumentService.upgradeCorrectionCas(correctionCas.getCas(), state.getDocument());
        // After creating an new CAS or upgrading the CAS, we need to save it
        documentService.writeAnnotationCas(editorCas.getCas().getJCas(), annotationDocument.getDocument(), user, false);
        correctionDocumentService.writeCorrectionCas(correctionCas, state.getDocument());
        // (Re)initialize brat model after potential creating / upgrading CAS
        state.reset();
        // Load constraints
        state.setConstraints(constraintsService.loadConstraints(state.getProject()));
        // Load user preferences
        PreferencesUtil.loadPreferences(username, settingsService, projectService, annotationService, state, state.getMode());
        // Initialize the visible content
        state.setFirstVisibleUnit(WebAnnoCasUtil.getFirstSentence(editorCas));
        // if project is changed, reset some project specific settings
        if (currentprojectId != state.getProject().getId()) {
            state.clearRememberedFeatures();
        }
        currentprojectId = state.getProject().getId();
        LOG.debug("Configured BratAnnotatorModel for user [" + state.getUser() + "] f:[" + state.getFirstVisibleUnitIndex() + "] l:[" + state.getLastVisibleUnitIndex() + "] s:[" + state.getFocusUnitIndex() + "]");
        gotoPageTextField.setModelObject(1);
        setCurationSegmentBeginEnd(editorCas);
        suggestionView.init(aTarget, curationContainer, annotationSelectionByUsernameAndAddress, curationSegment);
        update(aTarget);
        // Re-render the whole page because the font size
        if (aTarget != null) {
            aTarget.add(this);
        }
        // Update document state
        documentService.transitionSourceDocumentState(state.getDocument(), SourceDocumentStateTransition.NEW_TO_ANNOTATION_IN_PROGRESS);
        // Reset the editor
        detailEditor.reset(aTarget);
        // Populate the layer dropdown box
        detailEditor.loadFeatureEditorModels(editorCas, aTarget);
    } catch (Exception e) {
        handleException(aTarget, e);
    }
    LOG.info("END LOAD_DOCUMENT_ACTION");
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Example 35 with AnnotationDocument

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument in project webanno by webanno.

the class SuggestionViewPanel method getAnnotatorCas.

private JCas getAnnotatorCas(AnnotatorState aBModel, Map<String, Map<Integer, AnnotationSelection>> aAnnotationSelectionByUsernameAndAddress, SourceDocument sourceDocument, Map<String, JCas> jCases) throws UIMAException, IOException, ClassNotFoundException {
    JCas annotatorCas;
    if (aBModel.getMode().equals(Mode.AUTOMATION) || aBModel.getMode().equals(Mode.CORRECTION)) {
        // If this is a CORRECTION or AUTOMATION project, then we get the CORRECTION document
        // and put it in as the single document to compare with. Basically what we do is that
        // we treat consider this scenario as a curation scenario where the CORRECTION document
        // is the only document we compare with.
        // The CAS the user can edit is the one from the virtual CORRECTION USER
        annotatorCas = correctionDocumentService.readCorrectionCas(sourceDocument);
        User user = userRepository.getCurrentUser();
        AnnotationDocument annotationDocument = documentService.getAnnotationDocument(sourceDocument, user);
        jCases.put(user.getUsername(), documentService.readAnnotationCas(annotationDocument));
        aAnnotationSelectionByUsernameAndAddress.put(CURATION_USER, new HashMap<>());
    } else {
        // If this is a true CURATION then we get all the annotation documents from all the
        // active users.
        // The CAS the user can edit is the one from the virtual CURATION USER
        annotatorCas = curationDocumentService.readCurationCas(sourceDocument);
        // Now we get all the other CASes from the repository
        List<AnnotationDocument> annotationDocuments = documentService.listAnnotationDocuments(sourceDocument);
        for (AnnotationDocument annotationDocument : annotationDocuments) {
            String username = annotationDocument.getUser();
            if (annotationDocument.getState().equals(AnnotationDocumentState.FINISHED) || username.equals(CURATION_USER)) {
                JCas jCas = documentService.readAnnotationCas(annotationDocument);
                jCases.put(username, jCas);
                // cleanup annotationSelections
                aAnnotationSelectionByUsernameAndAddress.put(username, new HashMap<>());
            }
        }
    }
    return annotatorCas;
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)

Aggregations

AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)41 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)22 JCas (org.apache.uima.jcas.JCas)19 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)14 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)11 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)11 IOException (java.io.IOException)10 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)6 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)6 ArrayList (java.util.ArrayList)6 NoResultException (javax.persistence.NoResultException)6 UIMAException (org.apache.uima.UIMAException)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 File (java.io.File)4 Transactional (org.springframework.transaction.annotation.Transactional)4 AutomationTypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.AutomationTypeAdapter)3 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject)3 ApiOperation (io.swagger.annotations.ApiOperation)3 HashMap (java.util.HashMap)3 DataRetrievalFailureException (org.springframework.dao.DataRetrievalFailureException)3