Search in sources :

Example 36 with SourceDocument

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

the class DocumentServiceImpl method readAnnotationCas.

@Override
@Transactional
public JCas readAnnotationCas(AnnotationDocument aAnnotationDocument, boolean aAnalyzeAndRepair) throws IOException {
    // If there is no CAS yet for the annotation document, create one.
    JCas jcas = null;
    SourceDocument aDocument = aAnnotationDocument.getDocument();
    String user = aAnnotationDocument.getUser();
    if (!existsCas(aAnnotationDocument.getDocument(), user)) {
        // Convert the source file into an annotation CAS
        try {
            if (!existsInitialCas(aDocument)) {
                jcas = createInitialCas(aDocument, aAnalyzeAndRepair);
            }
            // or we know that we can load the existing initial CAS.
            if (jcas == null) {
                jcas = readInitialCas(aDocument, aAnalyzeAndRepair);
            }
        } catch (Exception e) {
            log.error("The reader for format [" + aDocument.getFormat() + "] is unable to digest data", e);
            throw new IOException("The reader for format [" + aDocument.getFormat() + "] is unable to digest data: " + e.getMessage());
        }
        casStorageService.writeCas(aDocument, jcas, user);
    } else {
        // Read existing CAS
        // We intentionally do not upgrade the CAS here because in general the IDs
        // must remain stable. If an upgrade is required the caller should do it
        jcas = casStorageService.readCas(aDocument, user, aAnalyzeAndRepair);
    }
    return jcas;
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) IOException(java.io.IOException) NoResultException(javax.persistence.NoResultException) CASException(org.apache.uima.cas.CASException) UIMAException(org.apache.uima.UIMAException) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) IOException(java.io.IOException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 37 with SourceDocument

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

the class AnnotationPage method handleParameters.

private void handleParameters(AjaxRequestTarget aTarget, StringValue aProjectParameter, StringValue aDocumentParameter, StringValue aFocusParameter, boolean aLockIfPreset) {
    // Get current project from parameters
    Project project = null;
    try {
        project = getProjectFromParameters(aProjectParameter);
    } catch (NoResultException e) {
        error("Project [" + aProjectParameter + "] does not exist");
        return;
    }
    // Get current document from parameters
    SourceDocument document = null;
    if (project != null) {
        try {
            document = getDocumentFromParameters(project, aDocumentParameter);
        } catch (NoResultException e) {
            error("Document [" + aDocumentParameter + "] does not exist in project [" + project.getId() + "]");
        }
    }
    // Get current focus unit from parameters
    int focus = 0;
    if (aFocusParameter != null) {
        focus = aFocusParameter.toInt(0);
    }
    // there is also a document change.
    if (document != null && document.equals(getModelObject().getDocument()) && focus == getModelObject().getFocusUnitIndex()) {
        return;
    }
    // Check access to project
    if (project != null && !isAnnotator(project, projectService, getModelObject().getUser())) {
        error("You have no permission to access project [" + project.getId() + "]");
        return;
    }
    // Check if document is locked for the user
    if (project != null && document != null && documentService.existsAnnotationDocument(document, getModelObject().getUser())) {
        AnnotationDocument adoc = documentService.getAnnotationDocument(document, getModelObject().getUser());
        if (AnnotationDocumentState.IGNORE.equals(adoc.getState())) {
            error("Document [" + document.getId() + "] in project [" + project.getId() + "] is locked for user [" + getModelObject().getUser().getUsername() + "]");
            return;
        }
    }
    // i.e. not only in the case that it was a URL fragment parameter.
    if (project != null) {
        getModelObject().setProject(project);
        if (aLockIfPreset) {
            getModelObject().setProjectLocked(true);
        }
    }
    if (document != null) {
        // or a change of focus (or both)
        if (!document.equals(getModelObject().getDocument())) {
            getModelObject().setDocument(document, getListOfDocs());
            actionLoadDocument(aTarget, focus);
        } else {
            try {
                getModelObject().moveToUnit(getEditorCas(), focus);
                actionRefreshDocument(aTarget);
            } catch (Exception e) {
                aTarget.addChildren(getPage(), IFeedback.class);
                LOG.info("Error reading CAS " + e.getMessage());
                error("Error reading CAS " + e.getMessage());
            }
        }
    }
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) NoResultException(javax.persistence.NoResultException) IFeedback(org.apache.wicket.feedback.IFeedback) NoResultException(javax.persistence.NoResultException) IOException(java.io.IOException)

Example 38 with SourceDocument

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

the class AutomationPage method getEditorCas.

@Override
protected JCas getEditorCas() throws IOException {
    AnnotatorState state = getModelObject();
    if (state.getDocument() == null) {
        throw new IllegalStateException("Please open a document first!");
    }
    SourceDocument aDocument = getModelObject().getDocument();
    AnnotationDocument annotationDocument = documentService.getAnnotationDocument(aDocument, state.getUser());
    // If there is no CAS yet for the annotation document, create one.
    return documentService.readAnnotationCas(annotationDocument);
}
Also used : AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)

Example 39 with SourceDocument

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

the class AutomationUtil method deleteSpanAnnotation.

public static void deleteSpanAnnotation(AnnotatorState aBModel, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, int aStart, int aEnd, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
    AnnotationDocument annoDoc = aDocumentService.getAnnotationDocument(aBModel.getDocument(), aBModel.getUser());
    JCas annoCas = aDocumentService.readAnnotationCas(annoDoc);
    // get selected text, concatenations of tokens
    String selectedText = WebAnnoCasUtil.getSelectedText(annoCas, aStart, aEnd);
    for (SourceDocument d : aDocumentService.listSourceDocuments(aBModel.getProject())) {
        loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aBModel.getUser());
        JCas jCas = aCorrectionDocumentService.readCorrectionCas(d);
        AutomationTypeAdapter adapter = (AutomationTypeAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
        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) {
                    adapter.delete(aBModel, jCas, aFeature, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length() - 1, aValue);
                }
            }
        }
        aCorrectionDocumentService.writeCorrectionCas(jCas, d);
    }
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) AutomationTypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.AutomationTypeAdapter)

Example 40 with SourceDocument

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

the class AutomationUtil method deleteRelationAnnotation.

public static void deleteRelationAnnotation(AnnotatorState aBModel, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, AnnotationFS fs, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
    for (SourceDocument d : aDocumentService.listSourceDocuments(aBModel.getProject())) {
        loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aBModel.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);
        }
        int beginOffset = 0;
        int endOffset = jCas.getDocumentText().length() - 1;
        String depCoveredText = dependentFs.getCoveredText();
        String govCoveredText = governorFs.getCoveredText();
        adapter.delete(aBModel, jCas, aFeature, beginOffset, endOffset, depCoveredText, govCoveredText, aValue);
        aCorrectionDocumentService.writeCorrectionCas(jCas, d);
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) ArcAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Aggregations

SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)59 JCas (org.apache.uima.jcas.JCas)24 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)22 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)22 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)19 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)14 File (java.io.File)13 RProject (de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)10 ApiOperation (io.swagger.annotations.ApiOperation)9 IOException (java.io.IOException)9 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)8 Map (java.util.Map)8 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)7 LinkedHashMap (java.util.LinkedHashMap)7 List (java.util.List)7 DiffResult (de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult)6 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)6 NoResultException (javax.persistence.NoResultException)6