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);
}
}
}
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;
}
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;
}
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();
}
}
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;
}
Aggregations