Search in sources :

Example 1 with UIMAException

use of org.apache.uima.UIMAException in project webanno by webanno.

the class AutomationCasStorageServiceImpl method readCas.

@Override
public JCas readCas(TrainingDocument aDocument) throws IOException {
    log.debug("Reading CAs for Automation document [{}] ({}) in project [{}] ({})", aDocument.getName(), aDocument.getId(), aDocument.getProject().getName(), aDocument.getProject().getId());
    synchronized (lock) {
        File annotationFolder = getAutomationFolder(aDocument);
        String file = aDocument.getName() + ".ser";
        try {
            File serializedCasFile = new File(annotationFolder, file);
            if (!serializedCasFile.exists()) {
                throw new FileNotFoundException("Annotation document of  Training document " + "[" + aDocument.getName() + "] (" + aDocument.getId() + ") not found in project[" + aDocument.getProject().getName() + "] (" + aDocument.getProject().getId() + ")");
            }
            CAS cas = CasCreationUtils.createCas((TypeSystemDescription) null, null, null);
            CasPersistenceUtils.readSerializedCas(cas.getJCas(), serializedCasFile);
            analyzeAndRepair(aDocument, cas);
            return cas.getJCas();
        } catch (UIMAException e) {
            throw new DataRetrievalFailureException("Unable to parse annotation", e);
        }
    }
}
Also used : CAS(org.apache.uima.cas.CAS) FileNotFoundException(java.io.FileNotFoundException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) File(java.io.File) UIMAException(org.apache.uima.UIMAException)

Example 2 with UIMAException

use of org.apache.uima.UIMAException in project webanno by webanno.

the class BratAnnotatorUtility method clearJcasAnnotations.

public static JCas clearJcasAnnotations(JCas aJCas, SourceDocument aSourceDocument, User aUser, DocumentService repository) throws IOException {
    JCas target;
    try {
        target = JCasFactory.createJCas();
    } catch (UIMAException e) {
        throw new IOException(e);
    }
    // Copy the CAS - basically we do this just to keep the full type system information
    CASCompleteSerializer serializer = serializeCASComplete(aJCas.getCasImpl());
    deserializeCASComplete(serializer, (CASImpl) target.getCas());
    // Re-init JCas
    try {
        target.getCas().getJCas();
    } catch (CASException e) {
        throw new IOException(e);
    }
    // Remove all annotations from the target CAS but we keep the type system!
    target.reset();
    // Copy over essential information
    DocumentMetaData.copy(aJCas, target);
    // DKPro Core Issue 435
    target.setDocumentLanguage(aJCas.getDocumentLanguage());
    target.setDocumentText(aJCas.getDocumentText());
    // Transfer token boundaries
    for (Token t : select(aJCas, Token.class)) {
        new Token(target, t.getBegin(), t.getEnd()).addToIndexes();
    }
    // Transfer sentence boundaries
    for (Sentence s : select(aJCas, Sentence.class)) {
        new Sentence(target, s.getBegin(), s.getEnd()).addToIndexes();
    }
    repository.writeAnnotationCas(target, aSourceDocument, aUser, false);
    return target;
}
Also used : CASCompleteSerializer(org.apache.uima.cas.impl.CASCompleteSerializer) JCas(org.apache.uima.jcas.JCas) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) IOException(java.io.IOException) CASException(org.apache.uima.cas.CASException) UIMAException(org.apache.uima.UIMAException) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)

Example 3 with UIMAException

use of org.apache.uima.UIMAException in project webanno by webanno.

the class CasStorageServiceImpl method realReadCas.

private JCas realReadCas(SourceDocument aDocument, String aUsername, boolean aAnalyzeAndRepair) throws IOException {
    log.debug("Reading annotation document [{}] ({}) for user [{}] in project [{}] ({})", aDocument.getName(), aDocument.getId(), aUsername, aDocument.getProject().getName(), aDocument.getProject().getId());
    File annotationFolder = getAnnotationFolder(aDocument);
    String file = aUsername + ".ser";
    JCas jcas;
    try {
        CAS cas = CasCreationUtils.createCas((TypeSystemDescription) null, null, null);
        File serializedCasFile = new File(annotationFolder, file);
        if (!serializedCasFile.exists()) {
            throw new FileNotFoundException("Annotation document of user [" + aUsername + "] for source document [" + aDocument.getName() + "] (" + aDocument.getId() + ") not found in project[" + aDocument.getProject().getName() + "] (" + aDocument.getProject().getId() + ")");
        }
        CasPersistenceUtils.readSerializedCas(cas, serializedCasFile);
        if (aAnalyzeAndRepair) {
            analyzeAndRepair(aDocument, aUsername, cas);
        }
        jcas = cas.getJCas();
    } catch (UIMAException e) {
        throw new DataRetrievalFailureException("Unable to parse annotation", e);
    }
    // Update the cache
    if (isCacheEnabled()) {
        JCasCacheEntry entry = new JCasCacheEntry();
        entry.jcas = jcas;
        entry.reads++;
        getCache().put(JCasCacheKey.of(aDocument, aUsername), entry);
        log.debug("Loaded CAS [{},{}] from disk and stored in cache", aDocument.getId(), aUsername);
    } else {
        log.debug("Loaded CAS [{},{}] from disk", aDocument.getId(), aUsername);
    }
    return jcas;
}
Also used : CAS(org.apache.uima.cas.CAS) FileNotFoundException(java.io.FileNotFoundException) JCas(org.apache.uima.jcas.JCas) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) File(java.io.File) UIMAException(org.apache.uima.UIMAException)

Example 4 with UIMAException

use of org.apache.uima.UIMAException 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)

Example 5 with UIMAException

use of org.apache.uima.UIMAException in project webanno by webanno.

the class SuggestionBuilder method getMergeCas.

/**
 * Fetches the CAS that the user will be able to edit. In AUTOMATION/CORRECTION mode, this is
 * the CAS for the CORRECTION_USER and in CURATION mode it is the CAS for the CURATION user.
 *
 * @param aBratAnnotatorModel
 *            the model.
 * @param aDocument
 *            the source document.
 * @param jCases
 *            the JCases.
 * @param randomAnnotationDocument
 *            an annotation document.
 * @return the JCas.
 * @throws UIMAException
 *             hum?
 * @throws ClassNotFoundException
 *             hum?
 * @throws IOException
 *             if an I/O error occurs.
 * @throws AnnotationException
 *             hum?
 */
public JCas getMergeCas(AnnotatorState aBratAnnotatorModel, SourceDocument aDocument, Map<String, JCas> jCases, AnnotationDocument randomAnnotationDocument, boolean aUpgrade) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
    JCas mergeJCas = null;
    try {
        if (aBratAnnotatorModel.getMode().equals(Mode.AUTOMATION) || aBratAnnotatorModel.getMode().equals(Mode.CORRECTION)) {
            // 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(aDocument, aBratAnnotatorModel.getMode(),
            // aBratAnnotatorModel.getUser().getUsername());
            mergeJCas = correctionDocumentService.readCorrectionCas(aDocument);
            if (aUpgrade) {
                correctionDocumentService.upgradeCorrectionCas(mergeJCas.getCas(), aDocument);
                correctionDocumentService.writeCorrectionCas(mergeJCas, aDocument);
            }
        } else {
            // 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(aDocument, aBratAnnotatorModel.getMode(),
            // aBratAnnotatorModel.getUser().getUsername());
            mergeJCas = curationDocumentService.readCurationCas(aDocument);
            if (aUpgrade) {
                curationDocumentService.upgradeCurationCas(mergeJCas.getCas(), aDocument);
                curationDocumentService.writeCurationCas(mergeJCas, aDocument, true);
            }
        }
    }// Create jcas, if it could not be loaded from the file system
     catch (Exception e) {
        if (aBratAnnotatorModel.getMode().equals(Mode.AUTOMATION) || aBratAnnotatorModel.getMode().equals(Mode.CORRECTION)) {
            mergeJCas = createCorrectionCas(mergeJCas, aBratAnnotatorModel, randomAnnotationDocument);
        } else {
            mergeJCas = createCurationCas(aBratAnnotatorModel.getProject(), randomAnnotationDocument, jCases, aBratAnnotatorModel.getAnnotationLayers());
        }
    }
    return mergeJCas;
}
Also used : JCas(org.apache.uima.jcas.JCas) AnnotationException(de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException) UIMAException(org.apache.uima.UIMAException) IOException(java.io.IOException)

Aggregations

UIMAException (org.apache.uima.UIMAException)9 IOException (java.io.IOException)7 JCas (org.apache.uima.jcas.JCas)6 File (java.io.File)5 FileNotFoundException (java.io.FileNotFoundException)4 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)3 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)3 DataRetrievalFailureException (org.springframework.dao.DataRetrievalFailureException)3 AnnotationDocument (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationDocument)2 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)2 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)2 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)2 CAS (org.apache.uima.cas.CAS)2 CASException (org.apache.uima.cas.CASException)2 AutomationTypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.AutomationTypeAdapter)1 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)1 AnnotatorStateImpl (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorStateImpl)1 BratAnnotationEditor (de.tudarmstadt.ukp.clarin.webanno.brat.annotation.BratAnnotationEditor)1 CasDoctor (de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor)1 LogMessage (de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage)1