Search in sources :

Example 56 with AnnotationFeature

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature in project webanno by webanno.

the class AnnotationDetailEditorPanel method loadFeatureEditorModelsCommon.

private void loadFeatureEditorModelsCommon(AjaxRequestTarget aTarget, JCas aJCas, AnnotationLayer aLayer, FeatureStructure aFS, Map<AnnotationFeature, Serializable> aRemembered) {
    clearFeatureEditorModels(aTarget);
    AnnotatorState state = AnnotationDetailEditorPanel.this.getModelObject();
    // Populate from feature structure
    for (AnnotationFeature feature : annotationService.listAnnotationFeature(aLayer)) {
        if (!feature.isEnabled()) {
            continue;
        }
        Serializable value = null;
        if (aFS != null) {
            value = annotationService.getAdapter(aLayer).getFeatureValue(feature, aFS);
        } else if (aRemembered != null) {
            value = aRemembered.get(feature);
        }
        FeatureState featureState = null;
        if (WebAnnoConst.CHAIN_TYPE.equals(feature.getLayer().getType())) {
            if (state.getSelection().isArc()) {
                if (feature.getLayer().isLinkedListBehavior() && WebAnnoConst.COREFERENCE_RELATION_FEATURE.equals(feature.getName())) {
                    featureState = new FeatureState(feature, value);
                }
            } else {
                if (WebAnnoConst.COREFERENCE_TYPE_FEATURE.equals(feature.getName())) {
                    featureState = new FeatureState(feature, value);
                }
            }
        } else {
            featureState = new FeatureState(feature, value);
        }
        if (featureState != null) {
            state.getFeatureStates().add(featureState);
            // verification to check whether constraints exist for this project or NOT
            if (state.getConstraints() != null && state.getSelection().getAnnotation().isSet()) {
                // indicator.setRulesExist(true);
                populateTagsBasedOnRules(aJCas, featureState);
            } else {
                // indicator.setRulesExist(false);
                featureState.tagset = annotationService.listTags(featureState.feature.getTagset());
            }
        }
    }
}
Also used : Serializable(java.io.Serializable) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) FeatureState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.FeatureState) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 57 with AnnotationFeature

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature in project webanno by webanno.

the class AnnotationFeatureForm method updateLayersDropdown.

public void updateLayersDropdown() {
    editorPanel.getLog().trace("updateLayersDropdown()");
    AnnotatorState state = getModelObject();
    annotationLayers.clear();
    AnnotationLayer l = null;
    for (AnnotationLayer layer : state.getAnnotationLayers()) {
        if (!layer.isEnabled() || layer.isReadonly() || layer.getName().equals(Token.class.getName())) {
            continue;
        }
        if (layer.getType().equals(WebAnnoConst.SPAN_TYPE)) {
            annotationLayers.add(layer);
            l = layer;
        } else // manage chain type
        if (layer.getType().equals(WebAnnoConst.CHAIN_TYPE)) {
            for (AnnotationFeature feature : annotationService.listAnnotationFeature(layer)) {
                if (!feature.isEnabled()) {
                    continue;
                }
                if (feature.getName().equals(WebAnnoConst.COREFERENCE_TYPE_FEATURE)) {
                    annotationLayers.add(layer);
                }
            }
        }
    // chain
    }
    if (state.getDefaultAnnotationLayer() != null) {
        state.setSelectedAnnotationLayer(state.getDefaultAnnotationLayer());
    } else if (l != null) {
        state.setSelectedAnnotationLayer(l);
    }
}
Also used : AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 58 with AnnotationFeature

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature 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);
    }
}
Also used : AnnotationFS(org.apache.uima.cas.text.AnnotationFS) Type(org.apache.uima.cas.Type) CasUtil.getType(org.apache.uima.fit.util.CasUtil.getType) ArcAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter) SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) JCas(org.apache.uima.jcas.JCas) Feature(org.apache.uima.cas.Feature) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 59 with AnnotationFeature

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature in project webanno by webanno.

the class AutomationUtil method addOtherFeatureTrainDocument.

// generates training document that will be used to predict the training document
// to add extra features, for example add POS tag as a feature for NE classifier
public static void addOtherFeatureTrainDocument(MiraTemplate aTemplate, AnnotationSchemaService aAnnotationService, AutomationService aAutomationService, UserDao aUserDao) throws IOException, UIMAException, ClassNotFoundException {
    File miraDir = aAutomationService.getMiraDir(aTemplate.getTrainFeature());
    if (!miraDir.exists()) {
        FileUtils.forceMkdir(miraDir);
    }
    AutomationStatus status = aAutomationService.getAutomationStatus(aTemplate);
    for (AnnotationFeature feature : aTemplate.getOtherFeatures()) {
        File trainFile = new File(miraDir, feature.getId() + ".train");
        boolean documentChanged = false;
        for (TrainingDocument document : aAutomationService.listTrainingDocuments(feature.getProject())) {
            if (!document.isProcessed() && (document.getFeature() != null && document.getFeature().equals(feature))) {
                documentChanged = true;
                break;
            }
        }
        if (!documentChanged && trainFile.exists()) {
            continue;
        }
        BufferedWriter trainOut = new BufferedWriter(new FileWriter(trainFile));
        AutomationTypeAdapter adapter = (AutomationTypeAdapter) aAnnotationService.getAdapter(feature.getLayer());
        for (TrainingDocument trainingDocument : aAutomationService.listTrainingDocuments(feature.getProject())) {
            if ((trainingDocument.getFeature() != null && trainingDocument.getFeature().equals(feature))) {
                JCas jCas = aAutomationService.readTrainingAnnotationCas(trainingDocument);
                for (Sentence sentence : select(jCas, Sentence.class)) {
                    trainOut.append(getMiraLine(sentence, feature, adapter).toString()).append("\n");
                }
                trainingDocument.setProcessed(false);
                status.setTrainDocs(status.getTrainDocs() - 1);
            }
        }
        trainOut.close();
    }
}
Also used : FileWriter(java.io.FileWriter) JCas(org.apache.uima.jcas.JCas) File(java.io.File) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence) AutomationStatus(de.tudarmstadt.ukp.clarin.webanno.automation.model.AutomationStatus) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) TrainingDocument(de.tudarmstadt.ukp.clarin.webanno.model.TrainingDocument) BufferedWriter(java.io.BufferedWriter) AutomationTypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.AutomationTypeAdapter)

Example 60 with AnnotationFeature

use of de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature in project webanno by webanno.

the class AutomationUtil method getFeatureOtherLayer.

private static void getFeatureOtherLayer(MiraTemplate aTemplate, DocumentService aRepository, AutomationService aAutomationService, AnnotationSchemaService aAnnotationService, UserDao aUserDao, int beamSize, boolean maxPosteriors, List<List<String>> predictions, Mira mira, File predFtFile, File predcitedFile, SourceDocument document) throws IOException, ClassNotFoundException, UIMAException {
    // other layers as training document
    for (AnnotationFeature feature : aTemplate.getOtherFeatures()) {
        int shiftColumns = 0;
        int nbest = 1;
        String modelName = aAutomationService.getMiraModel(feature, true, null).getAbsolutePath();
        if (!new File(modelName).exists()) {
            addOtherFeatureFromAnnotation(feature, aRepository, aAutomationService, aAnnotationService, aUserDao, predictions, document);
            continue;
        }
        String testName = predFtFile.getAbsolutePath();
        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);
        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);
        }
        predictions.add(annotations);
    }
}
Also used : PrintStream(java.io.PrintStream) StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) FileReader(java.io.FileReader) File(java.io.File) LineIterator(org.apache.commons.io.LineIterator) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Aggregations

AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)73 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)34 Feature (org.apache.uima.cas.Feature)20 ArrayList (java.util.ArrayList)16 Type (org.apache.uima.cas.Type)16 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)15 TagSet (de.tudarmstadt.ukp.clarin.webanno.model.TagSet)12 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)11 JCas (org.apache.uima.jcas.JCas)10 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)9 File (java.io.File)9 CasUtil.getType (org.apache.uima.fit.util.CasUtil.getType)8 LinkWithRoleModel (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.LinkWithRoleModel)6 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)6 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)6 List (java.util.List)6 FeatureStructure (org.apache.uima.cas.FeatureStructure)6 ArcAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.ArcAdapter)5 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)5 FeatureSupportRegistry (de.tudarmstadt.ukp.clarin.webanno.api.annotation.feature.FeatureSupportRegistry)5