use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature in project webanno by webanno.
the class TagSetEditorPanel method actionDelete.
private void actionDelete(AjaxRequestTarget aTarget) {
confirmationDialog.setContentModel(new StringResourceModel("DeleteDialog.text", this).setParameters(selectedTagSet.getObject().getName()));
confirmationDialog.show(aTarget);
confirmationDialog.setConfirmAction((_target) -> {
// the tagset is deleted!
for (AnnotationFeature ft : annotationSchemaService.listAnnotationFeature(selectedProject.getObject())) {
if (ft.getTagset() != null && ft.getTagset().equals(selectedTagSet.getObject())) {
ft.setTagset(null);
annotationSchemaService.createFeature(ft);
}
}
annotationSchemaService.removeTagSet(selectedTagSet.getObject());
_target.add(getPage());
actionCancel(_target);
});
}
use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature in project webanno by webanno.
the class AutomationPage method createDetailEditor.
private AnnotationDetailEditorPanel createDetailEditor() {
return new AnnotationDetailEditorPanel("annotationDetailEditorPanel", getModel()) {
private static final long serialVersionUID = 2857345299480098279L;
@Override
protected void onChange(AjaxRequestTarget aTarget) {
AnnotatorState state = getModelObject();
aTarget.addChildren(getPage(), IFeedback.class);
try {
annotationEditor.requestRender(aTarget);
} catch (Exception e) {
handleException(this, aTarget, e);
return;
}
try {
SuggestionBuilder builder = new SuggestionBuilder(casStorageService, documentService, correctionDocumentService, curationDocumentService, annotationService, userRepository);
curationContainer = builder.buildCurationContainer(state);
setCurationSegmentBeginEnd(getEditorCas());
curationContainer.setBratAnnotatorModel(state);
suggestionView.updatePanel(aTarget, curationContainer, annotationSelectionByUsernameAndAddress, curationSegment);
update(aTarget);
} catch (Exception e) {
handleException(this, aTarget, e);
}
}
@Override
public void onAnnotate(AjaxRequestTarget aTarget) {
AnnotatorState state = getModelObject();
if (state.isForwardAnnotation()) {
return;
}
AnnotationLayer layer = state.getSelectedAnnotationLayer();
int address = state.getSelection().getAnnotation().getId();
try {
JCas jCas = getEditorCas();
AnnotationFS fs = selectByAddr(jCas, address);
for (AnnotationFeature f : annotationService.listAnnotationFeature(layer)) {
Type type = CasUtil.getType(fs.getCAS(), layer.getName());
Feature feat = type.getFeatureByBaseName(f.getName());
if (!automationService.existsMiraTemplate(f)) {
continue;
}
if (!automationService.getMiraTemplate(f).isAnnotateAndRepeat()) {
continue;
}
TagSet tagSet = f.getTagset();
boolean isRepeatable = false;
// repeat only if the value is in the tagset
for (Tag tag : annotationService.listTags(tagSet)) {
if (fs.getFeatureValueAsString(feat) == null) {
// this is new annotation without values
break;
}
if (fs.getFeatureValueAsString(feat).equals(tag.getName())) {
isRepeatable = true;
break;
}
}
if (automationService.getMiraTemplate(f) != null && isRepeatable) {
if (layer.getType().endsWith(WebAnnoConst.RELATION_TYPE)) {
AutomationUtil.repeateRelationAnnotation(state, documentService, correctionDocumentService, annotationService, fs, f, fs.getFeatureValueAsString(feat));
update(aTarget);
break;
} else if (layer.getType().endsWith(WebAnnoConst.SPAN_TYPE)) {
AutomationUtil.repeateSpanAnnotation(state, documentService, correctionDocumentService, annotationService, fs.getBegin(), fs.getEnd(), f, fs.getFeatureValueAsString(feat));
update(aTarget);
break;
}
}
}
} catch (Exception e) {
handleException(this, aTarget, e);
}
}
@Override
protected void onAutoForward(AjaxRequestTarget aTarget) {
annotationEditor.requestRender(aTarget);
}
@Override
public void onDelete(AjaxRequestTarget aTarget, AnnotationFS aFS) {
AnnotatorState state = getModelObject();
AnnotationLayer layer = state.getSelectedAnnotationLayer();
for (AnnotationFeature f : annotationService.listAnnotationFeature(layer)) {
if (!automationService.existsMiraTemplate(f)) {
continue;
}
if (!automationService.getMiraTemplate(f).isAnnotateAndRepeat()) {
continue;
}
try {
Type type = CasUtil.getType(aFS.getCAS(), layer.getName());
Feature feat = type.getFeatureByBaseName(f.getName());
if (layer.getType().endsWith(WebAnnoConst.RELATION_TYPE)) {
AutomationUtil.deleteRelationAnnotation(state, documentService, correctionDocumentService, annotationService, aFS, f, aFS.getFeatureValueAsString(feat));
} else {
AutomationUtil.deleteSpanAnnotation(state, documentService, correctionDocumentService, annotationService, aFS.getBegin(), aFS.getEnd(), f, aFS.getFeatureValueAsString(feat));
}
update(aTarget);
} catch (Exception e) {
handleException(this, aTarget, e);
}
}
}
};
}
use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature in project webanno by webanno.
the class AutomationUtil method generateFinalClassifier.
/**
* Based on the other layer, predict features for the training document
*
* @param aTemplate
* the template.
* @param aRepository
* the repository.
* @return the prediction.
* @throws UIMAException
* hum?
* @throws ClassNotFoundException
* hum?
* @throws IOException
* hum?
* @throws AnnotationException
* hum?
*
* @throws AutomationException
* if an error occurs.
*/
public static String generateFinalClassifier(MiraTemplate aTemplate, DocumentService aRepository, CurationDocumentService aCurationDocumentService, AnnotationSchemaService aAnnotationService, AutomationService aAutomationService, UserDao aUserDao) throws UIMAException, ClassNotFoundException, IOException, AnnotationException, AutomationException {
int frequency = 2;
double sigma = 1;
int iterations = 10;
int beamSize = 0;
boolean maxPosteriors = false;
AnnotationFeature layerFeature = aTemplate.getTrainFeature();
List<List<String>> predictions = new ArrayList<>();
File miraDir = aAutomationService.getMiraDir(layerFeature);
Mira mira = new Mira();
File predFile = new File(miraDir, layerFeature.getLayer().getId() + "-" + layerFeature.getId() + ".train.ft");
File predcitedFile = new File(predFile.getAbsolutePath() + "-pred");
boolean trainingDocumentUpdated = false;
// A. training document for other train layers were changed
for (AnnotationFeature feature : aTemplate.getOtherFeatures()) {
for (TrainingDocument document : aAutomationService.listTrainingDocuments(aTemplate.getTrainFeature().getProject())) {
if (!document.isProcessed() && document.getFeature() != null && document.getFeature().equals(feature)) {
trainingDocumentUpdated = true;
break;
}
}
}
// B. Training document for the main training layer were changed
for (TrainingDocument document : aAutomationService.listTrainingDocuments(layerFeature.getProject())) {
if (!document.isProcessed() && (document.getFeature() != null && document.getFeature().equals(layerFeature))) {
trainingDocumentUpdated = true;
break;
}
}
// C. New Curation document arrives
for (SourceDocument document : aRepository.listSourceDocuments(layerFeature.getProject())) {
if (document.getState().equals(SourceDocumentState.CURATION_FINISHED)) {
trainingDocumentUpdated = true;
break;
}
}
// D. tab-sep training documents
for (TrainingDocument document : aAutomationService.listTabSepDocuments(aTemplate.getTrainFeature().getProject())) {
if (!document.isProcessed() && document.getFeature() != null && document.getFeature().equals(layerFeature)) {
trainingDocumentUpdated = true;
break;
}
}
if (!trainingDocumentUpdated) {
return aTemplate.getResult();
}
// if no other layer is used, use this as main train document,
// otherwise, add all the
// predictions and modify template
File baseTrainFile = new File(miraDir, layerFeature.getLayer().getId() + "-" + layerFeature.getId() + ".train.base");
File trainFile = new File(miraDir, layerFeature.getLayer().getId() + "-" + layerFeature.getId() + ".train");
// generate final classifier, using all features generated
String trainName = trainFile.getAbsolutePath();
String finalClassifierModelName = aAutomationService.getMiraModel(layerFeature, false, null).getAbsolutePath();
getFeatureOtherLayer(aTemplate, aRepository, aAutomationService, aAnnotationService, aUserDao, beamSize, maxPosteriors, predictions, mira, predFile, predcitedFile, null);
getFeaturesTabSep(aTemplate, aAutomationService, beamSize, maxPosteriors, layerFeature, predictions, mira, predFile, predcitedFile);
generateTrainDocument(aTemplate, aRepository, aCurationDocumentService, aAnnotationService, aAutomationService, aUserDao, false);
String trainTemplate;
if (predictions.size() == 0) {
trainTemplate = createTemplate(aTemplate.getTrainFeature(), getMiraTemplateFile(layerFeature, aAutomationService), 0);
FileUtils.copyFile(baseTrainFile, trainFile);
} else {
trainTemplate = createTemplate(aTemplate.getTrainFeature(), getMiraTemplateFile(layerFeature, aAutomationService), predictions.size());
buildTrainFile(baseTrainFile, trainFile, predictions);
}
boolean randomInit = false;
if (!layerFeature.getLayer().isLockToTokenOffset()) {
mira.setIobScorer();
}
mira.loadTemplates(trainTemplate);
mira.setClip(sigma);
mira.maxPosteriors = maxPosteriors;
mira.beamSize = beamSize;
int numExamples = mira.count(trainName, frequency);
mira.initModel(randomInit);
String trainResult = "";
for (int i = 0; i < iterations; i++) {
trainResult = mira.train(trainName, iterations, numExamples, i);
mira.averageWeights(iterations * numExamples);
}
mira.saveModel(finalClassifierModelName);
// all training documents are processed by now
for (TrainingDocument document : aAutomationService.listTrainingDocuments(layerFeature.getProject())) {
document.setProcessed(true);
}
for (TrainingDocument document : aAutomationService.listTabSepDocuments(layerFeature.getProject())) {
document.setProcessed(true);
}
return trainResult;
}
use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature in project webanno by webanno.
the class AutomationUtil method addOtherFeatureToPredictDocument.
/**
* Based on the other layer, add features for the prediction document
*
* @param aTemplate
* the template.
* @param aRepository
* the repository.
* @throws UIMAException
* hum?
* @throws ClassNotFoundException
* hum?
* @throws IOException
* hum?
* @throws AnnotationException
* hum?
* @throws AutomationException
* hum?
*/
public static void addOtherFeatureToPredictDocument(MiraTemplate aTemplate, DocumentService aRepository, AnnotationSchemaService aAnnotationService, AutomationService aAutomationService, UserDao aUserDao) throws UIMAException, ClassNotFoundException, IOException, AnnotationException, AutomationException {
AnnotationFeature layerFeature = aTemplate.getTrainFeature();
File miraDir = aAutomationService.getMiraDir(layerFeature);
for (SourceDocument document : aRepository.listSourceDocuments(layerFeature.getProject())) {
List<List<String>> predictions = new ArrayList<>();
File predFtFile = new File(miraDir, document.getId() + ".pred.ft");
Mira mira = new Mira();
int beamSize = 0;
boolean maxPosteriors = false;
File predcitedFile = new File(predFtFile.getAbsolutePath() + "-pred");
getFeatureOtherLayer(aTemplate, aRepository, aAutomationService, aAnnotationService, aUserDao, beamSize, maxPosteriors, predictions, mira, predFtFile, predcitedFile, document);
getFeaturesTabSep(aTemplate, aAutomationService, beamSize, maxPosteriors, layerFeature, predictions, mira, predFtFile, predcitedFile);
File basePredFile = new File(miraDir, document.getId() + ".pred");
if (predictions.size() == 0) {
createTemplate(aTemplate.getTrainFeature(), getMiraTemplateFile(layerFeature, aAutomationService), 0);
FileUtils.copyFile(predFtFile, basePredFile);
} else {
createTemplate(aTemplate.getTrainFeature(), getMiraTemplateFile(layerFeature, aAutomationService), predictions.size());
buildPredictFile(predFtFile, basePredFile, predictions, aTemplate.getTrainFeature());
}
}
}
use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature 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();
}
}
Aggregations