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