Search in sources :

Example 36 with AnnotationDocument

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

the class SuggestionBuilder method buildCurationContainer.

public CurationContainer buildCurationContainer(AnnotatorState aBModel) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
    CurationContainer curationContainer = new CurationContainer();
    // initialize Variables
    SourceDocument sourceDocument = aBModel.getDocument();
    Map<Integer, Integer> segmentBeginEnd = new HashMap<>();
    Map<Integer, Integer> segmentNumber = new HashMap<>();
    Map<String, Map<Integer, Integer>> segmentAdress = new HashMap<>();
    // get annotation documents
    List<AnnotationDocument> finishedAnnotationDocuments = new ArrayList<>();
    for (AnnotationDocument annotationDocument : documentService.listAnnotationDocuments(aBModel.getDocument())) {
        if (annotationDocument.getState().equals(AnnotationDocumentState.FINISHED)) {
            finishedAnnotationDocuments.add(annotationDocument);
        }
    }
    Map<String, JCas> jCases = new HashMap<>();
    AnnotationDocument randomAnnotationDocument = null;
    JCas mergeJCas;
    // get the correction/automation JCas for the logged in user
    if (aBModel.getMode().equals(Mode.AUTOMATION) || aBModel.getMode().equals(Mode.CORRECTION)) {
        jCases = listJcasesforCorrection(randomAnnotationDocument, sourceDocument, aBModel.getMode());
        mergeJCas = getMergeCas(aBModel, sourceDocument, jCases, randomAnnotationDocument, false);
        String username = jCases.keySet().iterator().next();
        updateSegment(aBModel, segmentBeginEnd, segmentNumber, segmentAdress, jCases.get(username), username, aBModel.getWindowBeginOffset(), aBModel.getWindowEndOffset());
    } else {
        jCases = listJcasesforCuration(finishedAnnotationDocuments, randomAnnotationDocument, aBModel.getMode());
        mergeJCas = getMergeCas(aBModel, sourceDocument, jCases, randomAnnotationDocument, false);
        updateSegment(aBModel, segmentBeginEnd, segmentNumber, segmentAdress, mergeJCas, WebAnnoConst.CURATION_USER, WebAnnoCasUtil.getFirstSentence(mergeJCas).getBegin(), mergeJCas.getDocumentText().length());
    }
    List<Type> entryTypes = null;
    segmentAdress.put(WebAnnoConst.CURATION_USER, new HashMap<>());
    for (Sentence sentence : selectCovered(mergeJCas, Sentence.class, diffRangeBegin, diffRangeEnd)) {
        segmentAdress.get(WebAnnoConst.CURATION_USER).put(sentence.getBegin(), getAddr(sentence));
    }
    if (entryTypes == null) {
        entryTypes = getEntryTypes(mergeJCas, aBModel.getAnnotationLayers(), annotationService);
    }
    // for cross-sentences annotation, update the end of the segment
    if (firstload) {
        long start = System.currentTimeMillis();
        log.debug("Updating cross sentence annotation list...");
        updateCrossSentAnnoList(segmentBeginEnd, segmentNumber, jCases, entryTypes);
        firstload = false;
        log.debug("Cross sentence annotation list complete in {}ms", (System.currentTimeMillis() - start));
    }
    long diffStart = System.currentTimeMillis();
    log.debug("Calculating differences...");
    int count = 0;
    for (Integer begin : segmentBeginEnd.keySet()) {
        Integer end = segmentBeginEnd.get(begin);
        count++;
        if (count % 100 == 0) {
            log.debug("Processing differences: {} of {} sentences...", count, segmentBeginEnd.size());
        }
        DiffResult diff = CasDiff2.doDiffSingle(annotationService, aBModel.getProject(), entryTypes, LinkCompareBehavior.LINK_ROLE_AS_LABEL, jCases, begin, end);
        SourceListView curationSegment = new SourceListView();
        curationSegment.setBegin(begin);
        curationSegment.setEnd(end);
        curationSegment.setSentenceNumber(segmentNumber.get(begin));
        if (diff.hasDifferences() || !diff.getIncompleteConfigurationSets().isEmpty()) {
            // Is this confSet a diff due to stacked annotations (with same configuration)?
            boolean stackedDiff = false;
            stackedDiffSet: for (ConfigurationSet d : diff.getDifferingConfigurationSets().values()) {
                for (Configuration c : d.getConfigurations()) {
                    if (c.getCasGroupIds().size() != d.getCasGroupIds().size()) {
                        stackedDiff = true;
                        break stackedDiffSet;
                    }
                }
            }
            if (stackedDiff) {
                curationSegment.setSentenceState(SentenceState.DISAGREE);
            } else if (!diff.getIncompleteConfigurationSets().isEmpty()) {
                curationSegment.setSentenceState(SentenceState.DISAGREE);
            } else {
                curationSegment.setSentenceState(SentenceState.AGREE);
            }
        } else {
            curationSegment.setSentenceState(SentenceState.AGREE);
        }
        for (String username : segmentAdress.keySet()) {
            curationSegment.getSentenceAddress().put(username, segmentAdress.get(username).get(begin));
        }
        curationContainer.getCurationViewByBegin().put(begin, curationSegment);
    }
    log.debug("Difference calculation completed in {}ms", (System.currentTimeMillis() - diffStart));
    return curationContainer;
}
Also used : Configuration(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.Configuration) HashMap(java.util.HashMap) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ArrayList(java.util.ArrayList) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) ConfigurationSet(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.ConfigurationSet) Type(org.apache.uima.cas.Type) DiffResult(de.tudarmstadt.ukp.clarin.webanno.curation.casdiff.CasDiff2.DiffResult) HashMap(java.util.HashMap) Map(java.util.Map) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)

Example 37 with AnnotationDocument

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

the class ProjectCasDoctorPanel method actionRepair.

private void actionRepair(AjaxRequestTarget aTarget, Form<?> aForm) throws IOException, UIMAException, ClassNotFoundException {
    casStorageService.disableCache();
    CasDoctor casDoctor = new CasDoctor();
    casDoctor.setApplicationContext(ApplicationContextProvider.getApplicationContext());
    casDoctor.setFatalChecks(false);
    casDoctor.setRepairClasses(formModel.repairs);
    Project project = getModelObject();
    formModel.messageSets = new ArrayList<>();
    for (SourceDocument sd : documentService.listSourceDocuments(project)) {
        {
            LogMessageSet messageSet = new LogMessageSet(sd.getName() + " [INITIAL]");
            JCas initialCas;
            if (documentService.existsInitialCas(sd)) {
                initialCas = documentService.readInitialCas(sd, false);
            } else {
                messageSet.messages.add(new LogMessage(getClass(), LogLevel.INFO, "Created initial CAS for [" + sd.getName() + "]"));
                initialCas = documentService.createInitialCas(sd, false);
            }
            casDoctor.repair(project, initialCas.getCas(), messageSet.messages);
            CasPersistenceUtils.writeSerializedCas(initialCas, documentService.getCasFile(sd, INITIAL_CAS_PSEUDO_USER));
            noticeIfThereAreNoMessages(messageSet);
            formModel.messageSets.add(messageSet);
        }
        for (AnnotationDocument ad : documentService.listAnnotationDocuments(sd)) {
            if (documentService.existsAnnotationCas(ad)) {
                LogMessageSet messageSet = new LogMessageSet(sd.getName() + " [" + ad.getUser() + "]");
                JCas userCas = documentService.readAnnotationCas(ad, false);
                casDoctor.repair(project, userCas.getCas(), messageSet.messages);
                CasPersistenceUtils.writeSerializedCas(userCas, documentService.getCasFile(ad.getDocument(), ad.getUser()));
                noticeIfThereAreNoMessages(messageSet);
                formModel.messageSets.add(messageSet);
            }
        }
    }
    aTarget.add(this);
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) LogMessage(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage) CasDoctor(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)

Example 38 with AnnotationDocument

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

the class ProjectCasDoctorPanel method actionCheck.

private void actionCheck(AjaxRequestTarget aTarget, Form<?> aForm) throws IOException, UIMAException, ClassNotFoundException {
    casStorageService.disableCache();
    CasDoctor casDoctor = new CasDoctor();
    casDoctor.setApplicationContext(ApplicationContextProvider.getApplicationContext());
    casDoctor.setFatalChecks(false);
    casDoctor.setCheckClasses(CasDoctor.scanChecks());
    Project project = getModelObject();
    formModel.messageSets = new ArrayList<>();
    for (SourceDocument sd : documentService.listSourceDocuments(project)) {
        {
            LogMessageSet messageSet = new LogMessageSet(sd.getName() + " [INITIAL]");
            JCas initialCas;
            try {
                if (documentService.existsInitialCas(sd)) {
                    initialCas = documentService.readInitialCas(sd, false);
                } else {
                    messageSet.messages.add(new LogMessage(getClass(), LogLevel.INFO, "No initial CAS for [" + sd.getName() + "]"));
                    initialCas = documentService.createInitialCas(sd, false);
                }
                casDoctor.analyze(project, initialCas.getCas(), messageSet.messages);
            } catch (Exception e) {
                messageSet.messages.add(new LogMessage(getClass(), LogLevel.ERROR, "Error reading initial CAS for [" + sd.getName() + "]: " + e.getMessage()));
                LOG.error("Error reading initial CAS for [" + sd.getName() + "]", e);
            }
            noticeIfThereAreNoMessages(messageSet);
            formModel.messageSets.add(messageSet);
        }
        for (AnnotationDocument ad : documentService.listAnnotationDocuments(sd)) {
            if (documentService.existsAnnotationCas(ad)) {
                LogMessageSet messageSet = new LogMessageSet(sd.getName() + " [" + ad.getUser() + "]");
                JCas userCas = documentService.readAnnotationCas(ad, false);
                casDoctor.analyze(project, userCas.getCas(), messageSet.messages);
                noticeIfThereAreNoMessages(messageSet);
                formModel.messageSets.add(messageSet);
            }
        }
    }
    aTarget.add(this);
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) LogMessage(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage) CasDoctor(de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Example 39 with AnnotationDocument

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

the class RemoteApiController method annotationDocumentList.

/**
 * List annotation documents for a source document in a projects where user is ADMIN
 *
 * Test when running in Eclipse: Open your browser, paste following URL with appropriate values:
 *
 * http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs/{
 * aSourceDocumentId}/annos
 *
 * @param aProjectId
 *            {@link Project} ID
 * @param aSourceDocumentId
 *            {@link SourceDocument} ID
 * @return JSON string of all the annotation documents with their projects.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS, method = RequestMethod.GET)
public ResponseEntity<String> annotationDocumentList(@PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId) throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        return ResponseEntity.badRequest().body("User [" + username + "] not found.");
    }
    // Get project
    Project project;
    try {
        project = projectRepository.getProject(aProjectId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Project [" + aProjectId + "] not found.");
    }
    // Check for the access
    boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
    if (!hasAccess) {
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body("User [" + username + "] is not allowed to access project [" + aProjectId + "]");
    }
    // Get source document
    SourceDocument srcDocument;
    try {
        srcDocument = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
    } catch (NoResultException e) {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Source document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "] not found.");
    }
    List<AnnotationDocument> annList = documentRepository.listAllAnnotationDocuments(srcDocument);
    JSONArray annDocArr = new JSONArray();
    for (AnnotationDocument annDoc : annList) {
        if (annDoc.getState().equals(AnnotationDocumentState.FINISHED) || annDoc.getState().equals(AnnotationDocumentState.IN_PROGRESS)) {
            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd'T'HH:mm:ssZ");
            JSONObject annDocObj = new JSONObject();
            annDocObj.put("user", annDoc.getUser());
            annDocObj.put("state", annDoc.getState().getId());
            if (annDoc.getTimestamp() != null) {
                annDocObj.put("timestamp", sdf.format(annDoc.getTimestamp()));
            }
            annDocArr.put(annDocObj);
        }
    }
    JSONObject returnJSON = new JSONObject();
    returnJSON.put(srcDocument.getName(), annDocArr);
    return ResponseEntity.ok(returnJSON.toString());
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) JSONObject(org.apache.wicket.ajax.json.JSONObject) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JSONArray(org.apache.wicket.ajax.json.JSONArray) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) NoResultException(javax.persistence.NoResultException) SimpleDateFormat(java.text.SimpleDateFormat) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 40 with AnnotationDocument

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

the class RemoteApiController method annotationDocumentRead.

/**
 * Download annotation document with requested parameters
 *
 * Test when running in Eclipse: Open your browser, paste following URL with appropriate values:
 *
 * http://USERNAME:PASSWORD@localhost:8080/webanno-webapp/api/projects/{aProjectId}/sourcedocs/{
 * aSourceDocumentId}/annos/{annotatorName}?format="text"
 *
 * @param response
 *            HttpServletResponse.
 * @param aProjectId
 *            {@link Project} ID.
 * @param aSourceDocumentId
 *            {@link SourceDocument} ID.
 * @param annotatorName
 *            {@link User} name.
 * @param format
 *            Export format.
 * @throws Exception
 *             if there was an error.
 */
@RequestMapping(value = "/" + PROJECTS + "/{" + PARAM_PROJECT_ID + "}/" + DOCUMENTS + "/{" + PARAM_DOCUMENT_ID + "}/" + ANNOTATIONS + "/{" + PARAM_USERNAME + "}", method = RequestMethod.GET)
public void annotationDocumentRead(HttpServletResponse response, @PathVariable(PARAM_PROJECT_ID) long aProjectId, @PathVariable(PARAM_DOCUMENT_ID) long aSourceDocumentId, @PathVariable(PARAM_USERNAME) String annotatorName, @RequestParam(value = PARAM_FORMAT, required = false) String format) throws Exception {
    // Get current user
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userRepository.get(username);
    if (user == null) {
        response.sendError(HttpStatus.BAD_REQUEST.value(), "User [" + username + "] not found.");
        return;
    }
    // Get project
    Project project;
    try {
        project = projectRepository.getProject(aProjectId);
    } catch (NoResultException e) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Project" + aProjectId + "] not found.");
        return;
    }
    // Check for the access
    boolean hasAccess = SecurityUtil.isProjectAdmin(project, projectRepository, user) || SecurityUtil.isSuperAdmin(projectRepository, user);
    if (!hasAccess) {
        response.sendError(HttpStatus.FORBIDDEN.value(), "User [" + username + "] is not allowed to access project [" + aProjectId + "]");
        return;
    }
    // Get annotator user
    User annotator = userRepository.get(annotatorName);
    if (annotator == null) {
        response.sendError(HttpStatus.BAD_REQUEST.value(), "Annotator user [" + annotatorName + "] not found.");
        return;
    }
    // Get source document
    SourceDocument srcDoc;
    try {
        srcDoc = documentRepository.getSourceDocument(aProjectId, aSourceDocumentId);
    } catch (NoResultException e) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Document [" + aSourceDocumentId + "] not found in project [" + aProjectId + "].");
        return;
    }
    // Get annotation document
    AnnotationDocument annDoc;
    try {
        annDoc = documentRepository.getAnnotationDocument(srcDoc, annotator);
    } catch (NoResultException e) {
        response.sendError(HttpStatus.NOT_FOUND.value(), "Annotations for user [" + annotatorName + "] not found on document [" + aSourceDocumentId + "] in project [" + aProjectId + "].");
        return;
    }
    String formatId;
    if (format == null) {
        formatId = srcDoc.getFormat();
    } else {
        formatId = format;
    }
    Class<?> writer = importExportService.getWritableFormats().get(formatId);
    if (writer == null) {
        String msg = "[" + srcDoc.getName() + "] No writer found for format [" + formatId + "] - exporting as WebAnno TSV instead.";
        LOG.info(msg);
        writer = WebannoTsv3XWriter.class;
    }
    // Temporary file of annotation document
    File downloadableFile = importExportService.exportAnnotationDocument(srcDoc, annotatorName, writer, annDoc.getName(), Mode.ANNOTATION);
    try {
        // Set mime type
        String mimeType = URLConnection.guessContentTypeFromName(downloadableFile.getName());
        if (mimeType == null) {
            LOG.info("mimetype is not detectable, will take default");
            mimeType = "application/octet-stream";
        }
        // Set response
        response.setContentType(mimeType);
        response.setContentType("application/force-download");
        response.setHeader("Content-Disposition", "inline; filename=\"" + downloadableFile.getName() + "\"");
        response.setContentLength((int) downloadableFile.length());
        InputStream inputStream = new BufferedInputStream(new FileInputStream(downloadableFile));
        FileCopyUtils.copy(inputStream, response.getOutputStream());
    } catch (Exception e) {
        LOG.info("Exception occured" + e.getMessage());
    } finally {
        if (downloadableFile.exists()) {
            downloadableFile.delete();
        }
    }
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) NoResultException(javax.persistence.NoResultException) FileInputStream(java.io.FileInputStream) NoResultException(javax.persistence.NoResultException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) BufferedInputStream(java.io.BufferedInputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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