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