use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.
the class DocumentServiceImpl method readAnnotationCas.
@Override
@Transactional
public JCas readAnnotationCas(AnnotationDocument aAnnotationDocument, boolean aAnalyzeAndRepair) throws IOException {
// If there is no CAS yet for the annotation document, create one.
JCas jcas = null;
SourceDocument aDocument = aAnnotationDocument.getDocument();
String user = aAnnotationDocument.getUser();
if (!existsCas(aAnnotationDocument.getDocument(), user)) {
// Convert the source file into an annotation CAS
try {
if (!existsInitialCas(aDocument)) {
jcas = createInitialCas(aDocument, aAnalyzeAndRepair);
}
// or we know that we can load the existing initial CAS.
if (jcas == null) {
jcas = readInitialCas(aDocument, aAnalyzeAndRepair);
}
} catch (Exception e) {
log.error("The reader for format [" + aDocument.getFormat() + "] is unable to digest data", e);
throw new IOException("The reader for format [" + aDocument.getFormat() + "] is unable to digest data: " + e.getMessage());
}
casStorageService.writeCas(aDocument, jcas, user);
} else {
// Read existing CAS
// We intentionally do not upgrade the CAS here because in general the IDs
// must remain stable. If an upgrade is required the caller should do it
jcas = casStorageService.readCas(aDocument, user, aAnalyzeAndRepair);
}
return jcas;
}
use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.
the class AnnotationPage method handleParameters.
private void handleParameters(AjaxRequestTarget aTarget, StringValue aProjectParameter, StringValue aDocumentParameter, StringValue aFocusParameter, boolean aLockIfPreset) {
// Get current project from parameters
Project project = null;
try {
project = getProjectFromParameters(aProjectParameter);
} catch (NoResultException e) {
error("Project [" + aProjectParameter + "] does not exist");
return;
}
// Get current document from parameters
SourceDocument document = null;
if (project != null) {
try {
document = getDocumentFromParameters(project, aDocumentParameter);
} catch (NoResultException e) {
error("Document [" + aDocumentParameter + "] does not exist in project [" + project.getId() + "]");
}
}
// Get current focus unit from parameters
int focus = 0;
if (aFocusParameter != null) {
focus = aFocusParameter.toInt(0);
}
// there is also a document change.
if (document != null && document.equals(getModelObject().getDocument()) && focus == getModelObject().getFocusUnitIndex()) {
return;
}
// Check access to project
if (project != null && !isAnnotator(project, projectService, getModelObject().getUser())) {
error("You have no permission to access project [" + project.getId() + "]");
return;
}
// Check if document is locked for the user
if (project != null && document != null && documentService.existsAnnotationDocument(document, getModelObject().getUser())) {
AnnotationDocument adoc = documentService.getAnnotationDocument(document, getModelObject().getUser());
if (AnnotationDocumentState.IGNORE.equals(adoc.getState())) {
error("Document [" + document.getId() + "] in project [" + project.getId() + "] is locked for user [" + getModelObject().getUser().getUsername() + "]");
return;
}
}
// i.e. not only in the case that it was a URL fragment parameter.
if (project != null) {
getModelObject().setProject(project);
if (aLockIfPreset) {
getModelObject().setProjectLocked(true);
}
}
if (document != null) {
// or a change of focus (or both)
if (!document.equals(getModelObject().getDocument())) {
getModelObject().setDocument(document, getListOfDocs());
actionLoadDocument(aTarget, focus);
} else {
try {
getModelObject().moveToUnit(getEditorCas(), focus);
actionRefreshDocument(aTarget);
} catch (Exception e) {
aTarget.addChildren(getPage(), IFeedback.class);
LOG.info("Error reading CAS " + e.getMessage());
error("Error reading CAS " + e.getMessage());
}
}
}
}
use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.
the class AutomationPage method getEditorCas.
@Override
protected JCas getEditorCas() throws IOException {
AnnotatorState state = getModelObject();
if (state.getDocument() == null) {
throw new IllegalStateException("Please open a document first!");
}
SourceDocument aDocument = getModelObject().getDocument();
AnnotationDocument annotationDocument = documentService.getAnnotationDocument(aDocument, state.getUser());
// If there is no CAS yet for the annotation document, create one.
return documentService.readAnnotationCas(annotationDocument);
}
use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.
the class AutomationUtil method deleteSpanAnnotation.
public static void deleteSpanAnnotation(AnnotatorState aBModel, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, int aStart, int aEnd, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
AnnotationDocument annoDoc = aDocumentService.getAnnotationDocument(aBModel.getDocument(), aBModel.getUser());
JCas annoCas = aDocumentService.readAnnotationCas(annoDoc);
// get selected text, concatenations of tokens
String selectedText = WebAnnoCasUtil.getSelectedText(annoCas, aStart, aEnd);
for (SourceDocument d : aDocumentService.listSourceDocuments(aBModel.getProject())) {
loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aBModel.getUser());
JCas jCas = aCorrectionDocumentService.readCorrectionCas(d);
AutomationTypeAdapter adapter = (AutomationTypeAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
for (Sentence sentence : select(jCas, Sentence.class)) {
String sentenceText = sentence.getCoveredText().toLowerCase();
for (int i = -1; (i = sentenceText.indexOf(selectedText.toLowerCase(), i)) != -1; i = i + selectedText.length()) {
if (selectCovered(jCas, Token.class, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length()).size() > 0) {
adapter.delete(aBModel, jCas, aFeature, sentence.getBegin() + i, sentence.getBegin() + i + selectedText.length() - 1, aValue);
}
}
}
aCorrectionDocumentService.writeCorrectionCas(jCas, d);
}
}
use of de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument in project webanno by webanno.
the class AutomationUtil method deleteRelationAnnotation.
public static void deleteRelationAnnotation(AnnotatorState aBModel, DocumentService aDocumentService, CorrectionDocumentService aCorrectionDocumentService, AnnotationSchemaService aAnnotationService, AnnotationFS fs, AnnotationFeature aFeature, String aValue) throws UIMAException, ClassNotFoundException, IOException, AnnotationException {
for (SourceDocument d : aDocumentService.listSourceDocuments(aBModel.getProject())) {
loadDocument(d, aAnnotationService, aDocumentService, aCorrectionDocumentService, aBModel.getUser());
JCas jCas = aCorrectionDocumentService.readCorrectionCas(d);
ArcAdapter adapter = (ArcAdapter) aAnnotationService.getAdapter(aFeature.getLayer());
String sourceFName = adapter.getSourceFeatureName();
String targetFName = adapter.getTargetFeatureName();
Type type = getType(jCas.getCas(), aFeature.getLayer().getName());
Type spanType = getType(jCas.getCas(), adapter.getAttachTypeName());
Feature arcSpanFeature = spanType.getFeatureByBaseName(adapter.getAttachFeatureName());
Feature dependentFeature = type.getFeatureByBaseName(targetFName);
Feature governorFeature = type.getFeatureByBaseName(sourceFName);
AnnotationFS dependentFs = null;
AnnotationFS governorFs = null;
if (adapter.getAttachFeatureName() != null) {
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature).getFeatureValue(arcSpanFeature);
governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature).getFeatureValue(arcSpanFeature);
} else {
dependentFs = (AnnotationFS) fs.getFeatureValue(dependentFeature);
governorFs = (AnnotationFS) fs.getFeatureValue(governorFeature);
}
int beginOffset = 0;
int endOffset = jCas.getDocumentText().length() - 1;
String depCoveredText = dependentFs.getCoveredText();
String govCoveredText = governorFs.getCoveredText();
adapter.delete(aBModel, jCas, aFeature, beginOffset, endOffset, depCoveredText, govCoveredText, aValue);
aCorrectionDocumentService.writeCorrectionCas(jCas, d);
}
}
Aggregations