Search in sources :

Example 11 with AnnotationDocument

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

the class SuggestionViewPanel method mergeArc.

private void mergeArc(IRequestParameters aRequest, UserAnnotationSegment aCurationUserSegment, JCas aJcas) throws AnnotationException, IOException, UIMAException, ClassNotFoundException {
    int addressOriginClicked = aRequest.getParameterValue(PARAM_ORIGIN_SPAN_ID).toInt();
    int addressTargetClicked = aRequest.getParameterValue(PARAM_TARGET_SPAN_ID).toInt();
    String arcType = removePrefix(aRequest.getParameterValue(PARAM_TYPE).toString());
    String fsArcaddress = aRequest.getParameterValue(PARAM_ARC_ID).toString();
    AnnotatorState bModel = aCurationUserSegment.getAnnotatorState();
    SourceDocument sourceDocument = bModel.getDocument();
    // for correction and automation, the lower panel is the clickedJcase, from the suggestions
    JCas clickedJCas;
    if (!aCurationUserSegment.getAnnotatorState().getMode().equals(Mode.CURATION)) {
        clickedJCas = correctionDocumentService.readCorrectionCas(sourceDocument);
    } else {
        User user = userRepository.get(aCurationUserSegment.getUsername());
        AnnotationDocument clickedAnnotationDocument = documentService.getAnnotationDocument(sourceDocument, user);
        clickedJCas = getJCas(bModel, clickedAnnotationDocument);
    }
    long layerId = TypeUtil.getLayerId(arcType);
    AnnotationLayer layer = annotationService.getLayer(layerId);
    TypeAdapter adapter = annotationService.getAdapter(layer);
    int address = Integer.parseInt(fsArcaddress.split("\\.")[0]);
    AnnotationFS clickedFS = selectByAddr(clickedJCas, address);
    if (isCorefType(clickedFS)) {
        throw new AnnotationException(" Coreference Annotation not supported in curation");
    }
    MergeCas.addArcAnnotation(adapter, aJcas, addressOriginClicked, addressTargetClicked, fsArcaddress, clickedJCas, clickedFS);
    writeEditorCas(bModel, aJcas);
    int sentenceNumber = getSentenceNumber(clickedJCas, clickedFS.getBegin());
    bModel.setFocusUnitIndex(sentenceNumber);
    // Update timestamp
    bModel.getDocument().setSentenceAccessed(sentenceNumber);
    if (bModel.getPreferences().isScrollPage()) {
        Sentence sentence = selectSentenceAt(aJcas, bModel.getFirstVisibleUnitBegin(), bModel.getFirstVisibleUnitEnd());
        sentence = findWindowStartCenteringOnSelection(aJcas, sentence, clickedFS.getBegin(), bModel.getProject(), bModel.getDocument(), bModel.getPreferences().getWindowSize());
        bModel.setFirstVisibleUnit(sentence);
    }
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) AnnotationFS(org.apache.uima.cas.text.AnnotationFS) TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)

Example 12 with AnnotationDocument

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

the class SuggestionViewPanel method mergeSpan.

private void mergeSpan(IRequestParameters aRequest, UserAnnotationSegment aCurationUserSegment, JCas aJcas) throws AnnotationException, UIMAException, ClassNotFoundException, IOException {
    User user = userRepository.get(aCurationUserSegment.getUsername());
    SourceDocument sourceDocument = aCurationUserSegment.getAnnotatorState().getDocument();
    AnnotationDocument clickedAnnotationDocument = documentService.getAnnotationDocument(sourceDocument, user);
    Integer address = aRequest.getParameterValue(PARAM_ID).toInteger();
    String spanType = removePrefix(aRequest.getParameterValue(PARAM_TYPE).toString());
    createSpan(spanType, aCurationUserSegment.getAnnotatorState(), aJcas, clickedAnnotationDocument, address);
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)

Example 13 with AnnotationDocument

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

the class SuggestionBuilder method listJcasesforCuration.

public Map<String, JCas> listJcasesforCuration(List<AnnotationDocument> annotationDocuments, AnnotationDocument randomAnnotationDocument, Mode aMode) throws UIMAException, ClassNotFoundException, IOException {
    Map<String, JCas> jCases = new HashMap<>();
    for (AnnotationDocument annotationDocument : annotationDocuments) {
        String username = annotationDocument.getUser();
        if (!annotationDocument.getState().equals(AnnotationDocumentState.FINISHED)) {
            continue;
        }
        if (randomAnnotationDocument == null) {
            randomAnnotationDocument = annotationDocument;
        }
        // Upgrading should be an explicit action during the opening of a document at the end
        // of the open dialog - it must not happen during editing because the CAS addresses
        // are used as IDs in the UI
        // repository.upgradeCasAndSave(annotationDocument.getDocument(), aMode, username);
        JCas jCas = documentService.readAnnotationCas(annotationDocument);
        jCases.put(username, jCas);
    }
    return jCases;
}
Also used : HashMap(java.util.HashMap) JCas(org.apache.uima.jcas.JCas) AnnotationDocument(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)

Example 14 with AnnotationDocument

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

the class AutomationUtil method predict.

public static void predict(MiraTemplate aTemplate, DocumentService aRepository, CorrectionDocumentService aCorrectionDocumentService, AutomationService aAutomationService, UserDao aUserDao) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
    AnnotationFeature layerFeature = aTemplate.getTrainFeature();
    File miraDir = aAutomationService.getMiraDir(layerFeature);
    AutomationStatus status = aAutomationService.getAutomationStatus(aTemplate);
    for (SourceDocument document : aRepository.listSourceDocuments(layerFeature.getProject())) {
        File predFile = new File(miraDir, document.getId() + ".pred");
        Mira mira = new Mira();
        int shiftColumns = 0;
        int nbest = 1;
        int beamSize = 0;
        boolean maxPosteriors = false;
        String modelName = aAutomationService.getMiraModel(layerFeature, false, null).getAbsolutePath();
        String testName = predFile.getAbsolutePath();
        File predcitedFile = new File(predFile.getAbsolutePath() + "-pred");
        PrintStream stream = new PrintStream(predcitedFile);
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        if (testName != null) {
            input = new BufferedReader(new FileReader(testName));
        }
        mira.loadModel(modelName);
        mira.setShiftColumns(shiftColumns);
        mira.nbest = nbest;
        mira.beamSize = beamSize;
        mira.maxPosteriors = maxPosteriors;
        mira.test(input, stream);
        LOG.info("Prediction is wrtten to a MIRA File. To be done is writing back to the CAS");
        LineIterator it = IOUtils.lineIterator(new FileReader(predcitedFile));
        List<String> annotations = new ArrayList<>();
        while (it.hasNext()) {
            String line = it.next();
            if (line.trim().equals("")) {
                continue;
            }
            StringTokenizer st = new StringTokenizer(line, " ");
            String tag = "";
            while (st.hasMoreTokens()) {
                tag = st.nextToken();
            }
            annotations.add(tag);
        }
        LOG.info(annotations.size() + " Predictions found to be written to the CAS");
        JCas jCas = null;
        User user = aUserDao.getCurrentUser();
        try {
            AnnotationDocument annoDocument = aRepository.getAnnotationDocument(document, user);
            jCas = aRepository.readAnnotationCas(annoDocument);
            automate(jCas, layerFeature, annotations);
        } catch (DataRetrievalFailureException e) {
            automate(jCas, layerFeature, annotations);
            LOG.info("Predictions found are written to the CAS");
            aCorrectionDocumentService.writeCorrectionCas(jCas, document);
            status.setAnnoDocs(status.getAnnoDocs() - 1);
        }
        automate(jCas, layerFeature, annotations);
        LOG.info("Predictions found are written to the CAS");
        aCorrectionDocumentService.writeCorrectionCas(jCas, document);
        status.setAnnoDocs(status.getAnnoDocs() - 1);
    }
}
Also used : PrintStream(java.io.PrintStream) User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) InputStreamReader(java.io.InputStreamReader) 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) LineIterator(org.apache.commons.io.LineIterator) AutomationStatus(de.tudarmstadt.ukp.clarin.webanno.automation.model.AutomationStatus) Mira(edu.lium.mira.Mira) StringTokenizer(java.util.StringTokenizer) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) File(java.io.File) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 15 with AnnotationDocument

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

the class AutomationUtil method addOtherFeatureFromAnnotation.

/**
 * If the training file or the test file already contain the "Other layer" annotations, get the
 * UIMA annotation and add it as a feature - no need to train and predict for this "other layer"
 */
private static void addOtherFeatureFromAnnotation(AnnotationFeature aFeature, DocumentService aRepository, AutomationService aAutomationServic, AnnotationSchemaService aAnnotationService, UserDao aUserDao, List<List<String>> aPredictions, SourceDocument aSourceDocument) throws UIMAException, ClassNotFoundException, IOException {
    AutomationTypeAdapter adapter = (AutomationTypeAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
    List<String> annotations = new ArrayList<>();
    // this is training - all training documents will be converted to a single training file
    if (aSourceDocument == null) {
        for (TrainingDocument trainingDocument : aAutomationServic.listTrainingDocuments(aFeature.getProject())) {
            JCas jCas = aAutomationServic.readTrainingAnnotationCas(trainingDocument);
            for (Sentence sentence : select(jCas, Sentence.class)) {
                if (aFeature.getLayer().isMultipleTokens()) {
                    annotations.addAll(((SpanAdapter) adapter).getMultipleAnnotation(sentence, aFeature).values());
                } else {
                    annotations.addAll(adapter.getAnnotation(sentence, aFeature));
                }
            }
        }
        aPredictions.add(annotations);
    } else // This is SourceDocument to predict (in the suggestion pane)
    {
        User user = aUserDao.getCurrentUser();
        AnnotationDocument annodoc = aRepository.createOrGetAnnotationDocument(aSourceDocument, user);
        JCas jCas = aRepository.readAnnotationCas(annodoc);
        for (Sentence sentence : select(jCas, Sentence.class)) {
            if (aFeature.getLayer().isMultipleTokens()) {
                annotations.addAll(((SpanAdapter) adapter).getMultipleAnnotation(sentence, aFeature).values());
            } else {
                annotations.addAll(adapter.getAnnotation(sentence, aFeature));
            }
        }
        aPredictions.add(annotations);
    }
}
Also used : User(de.tudarmstadt.ukp.clarin.webanno.security.model.User) ArrayList(java.util.ArrayList) 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) AutomationTypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.AutomationTypeAdapter) TrainingDocument(de.tudarmstadt.ukp.clarin.webanno.model.TrainingDocument)

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