Search in sources :

Example 6 with AnnotationDocument

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

the class DocumentServiceImpl method createOrGetAnnotationDocument.

@Override
@Transactional(noRollbackFor = NoResultException.class)
public AnnotationDocument createOrGetAnnotationDocument(SourceDocument aDocument, User aUser) {
    // Check if there is an annotation document entry in the database. If there is none,
    // create one.
    AnnotationDocument annotationDocument = null;
    if (!existsAnnotationDocument(aDocument, aUser)) {
        annotationDocument = new AnnotationDocument();
        annotationDocument.setDocument(aDocument);
        annotationDocument.setName(aDocument.getName());
        annotationDocument.setUser(aUser.getUsername());
        annotationDocument.setProject(aDocument.getProject());
        createAnnotationDocument(annotationDocument);
    } else {
        annotationDocument = getAnnotationDocument(aDocument, aUser);
    }
    return annotationDocument;
}
Also used : AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with AnnotationDocument

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

the class DocumentServiceImpl method removeSourceDocument.

@Override
@Transactional
public void removeSourceDocument(SourceDocument aDocument) throws IOException {
    // BeforeDocumentRemovedEvent is triggered first, since methods that rely
    // on it might need to have access to the associated annotation documents
    applicationEventPublisher.publishEvent(new BeforeDocumentRemovedEvent(this, aDocument));
    for (AnnotationDocument annotationDocument : listAllAnnotationDocuments(aDocument)) {
        removeAnnotationDocument(annotationDocument);
    }
    entityManager.remove(entityManager.contains(aDocument) ? aDocument : entityManager.merge(aDocument));
    String path = dir.getAbsolutePath() + "/" + PROJECT_FOLDER + "/" + aDocument.getProject().getId() + "/" + DOCUMENT_FOLDER + "/" + aDocument.getId();
    // remove from file both source and related annotation file
    if (new File(path).exists()) {
        FileUtils.forceDelete(new File(path));
    }
    try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(aDocument.getProject().getId()))) {
        Project project = aDocument.getProject();
        log.info("Removed source document [{}]({}) from project [{}]({})", aDocument.getName(), aDocument.getId(), project.getName(), project.getId());
    }
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) BeforeDocumentRemovedEvent(de.tudarmstadt.ukp.clarin.webanno.api.event.BeforeDocumentRemovedEvent) File(java.io.File) MDC(org.slf4j.MDC) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with AnnotationDocument

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

the class RemoteApiController2 method annotationsDelete.

@ApiOperation(value = "Delete a user's annotations of one document from a project")
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS + "/{" + PARAM_ANNOTATOR_ID + "}", method = RequestMethod.DELETE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<Void>> annotationsDelete(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aDocumentId, @PathVariable(PARAM_ANNOTATOR_ID) String aAnnotatorId) throws Exception {
    // Get project (this also ensures that it exists and that the current user can access it
    Project project = getProject(aProjectId);
    SourceDocument doc = getDocument(project, aDocumentId);
    AnnotationDocument anno = getAnnotation(doc, aAnnotatorId, false);
    documentService.removeAnnotationDocument(anno);
    documentService.deleteAnnotationCas(anno);
    return ResponseEntity.ok(new RResponse<>(INFO, "Annotations of user [" + aAnnotatorId + "] on document [" + aDocumentId + "] deleted from project [" + aProjectId + "]."));
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with AnnotationDocument

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

the class RemoteApiController2 method annotationsCreate.

@ApiOperation(value = "Create annotations for a document in a project")
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS + "/{" + PARAM_ANNOTATOR_ID + "}", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<RResponse<RAnnotation>> annotationsCreate(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aDocumentId, @PathVariable(PARAM_ANNOTATOR_ID) String aAnnotatorId, @RequestParam(value = PARAM_CONTENT) MultipartFile aFile, @RequestParam(value = PARAM_FORMAT) Optional<String> aFormat, @RequestParam(value = PARAM_STATE) Optional<String> aState, UriComponentsBuilder aUcb) throws Exception {
    User annotator = getUser(aAnnotatorId);
    Project project = getProject(aProjectId);
    SourceDocument document = getDocument(project, aDocumentId);
    AnnotationDocument anno = getAnnotation(document, aAnnotatorId, true);
    JCas annotationCas = createCompatibleCas(aProjectId, aDocumentId, aFile, aFormat);
    // If they are compatible, then we can store the new annotations
    documentService.writeAnnotationCas(annotationCas, document, annotator, false);
    // Set state if one was provided
    if (aState.isPresent()) {
        anno.setState(parseAnnotationDocumentState(aState.get()));
        documentService.createAnnotationDocument(anno);
    }
    RResponse<RAnnotation> response = new RResponse<>(new RAnnotation(anno));
    if (aState.isPresent()) {
        response.addMessage(INFO, "State of annotations of user [" + aAnnotatorId + "] on document [" + document.getId() + "] set to [" + aState.get() + "]");
    }
    return ResponseEntity.created(aUcb.path(API_BASE + "/" + PROJECTS + "/{pid}/" + DOCUMENTS + "/{did}/" + ANNOTATIONS + "/{aid}").buildAndExpand(project.getId(), document.getId(), annotator.getUsername()).toUri()).body(response);
}
Also used : RProject(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RProject) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) RAnnotation(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RAnnotation) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) RResponse(de.tudarmstadt.ukp.clarin.webanno.webapp.remoteapi.v2.model.RResponse) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with AnnotationDocument

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

the class AutomationUtil method generatePredictDocument.

// TODO: rename to predictDocument
public static void generatePredictDocument(MiraTemplate aTemplate, DocumentService aRepository, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, AutomationService aAutomationService, UserDao aUserDao) throws IOException, UIMAException, ClassNotFoundException {
    File miraDir = aAutomationService.getMiraDir(aTemplate.getTrainFeature());
    if (!miraDir.exists()) {
        FileUtils.forceMkdir(miraDir);
    }
    User user = aUserDao.getCurrentUser();
    AnnotationFeature feature = aTemplate.getTrainFeature();
    AutomationTypeAdapter adapter = (AutomationTypeAdapter) aAnnotationService.getAdapter(feature.getLayer());
    for (SourceDocument document : aRepository.listSourceDocuments(feature.getProject())) {
        File predFile = new File(miraDir, document.getId() + ".pred.ft");
        BufferedWriter predOut = new BufferedWriter(new FileWriter(predFile));
        JCas jCas;
        try {
            jCas = aCorrectionDocumentService.readCorrectionCas(document);
        } catch (Exception e) {
            AnnotationDocument annoDoc = aRepository.createOrGetAnnotationDocument(document, user);
            jCas = aRepository.readAnnotationCas(annoDoc);
        }
        for (Sentence sentence : select(jCas, Sentence.class)) {
            predOut.append(getMiraLine(sentence, null, adapter).toString()).append("\n");
        }
        predOut.close();
    }
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) FileWriter(java.io.FileWriter) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) File(java.io.File) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) NoResultException(javax.persistence.NoResultException) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) CASException(org.apache.uima.cas.CASException) UIMAException(org.apache.uima.UIMAException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) IOException(java.io.IOException) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) AutomationTypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.AutomationTypeAdapter) BufferedWriter(java.io.BufferedWriter)

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