Search in sources :

Example 36 with AnnotationLayer

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

the class ImportExportServiceImpl method exportCasToFile.

/**
 * A new directory is created using UUID so that every exported file will reside in its own
 * directory. This is useful as the written file can have multiple extensions based on the
 * Writer class used.
 */
@Override
public File exportCasToFile(CAS cas, SourceDocument aDocument, String aFileName, @SuppressWarnings("rawtypes") Class aWriter, boolean aStripExtension) throws IOException, UIMAException {
    // Update the source file name in case it is changed for some reason. This is necessary
    // for the writers to create the files under the correct names.
    Project project = aDocument.getProject();
    File currentDocumentUri = new File(dir.getAbsolutePath() + "/" + PROJECT_FOLDER + "/" + project.getId() + "/" + DOCUMENT_FOLDER + "/" + aDocument.getId() + "/" + SOURCE_FOLDER);
    DocumentMetaData documentMetadata = DocumentMetaData.get(cas.getJCas());
    documentMetadata.setDocumentUri(new File(currentDocumentUri, aFileName).toURI().toURL().toExternalForm());
    documentMetadata.setDocumentBaseUri(currentDocumentUri.toURI().toURL().toExternalForm());
    documentMetadata.setCollectionId(currentDocumentUri.toURI().toURL().toExternalForm());
    documentMetadata.setDocumentUri(new File(dir.getAbsolutePath() + "/" + PROJECT_FOLDER + "/" + project.getId() + "/" + DOCUMENT_FOLDER + "/" + aDocument.getId() + "/" + SOURCE_FOLDER + "/" + aFileName).toURI().toURL().toExternalForm());
    // update with the correct tagset name
    List<AnnotationFeature> features = annotationService.listAnnotationFeature(project);
    for (AnnotationFeature feature : features) {
        TagSet tagSet = feature.getTagset();
        if (tagSet == null) {
            continue;
        } else if (!feature.getLayer().getType().equals(WebAnnoConst.CHAIN_TYPE)) {
            updateCasWithTagSet(cas, feature.getLayer().getName(), tagSet.getName());
        }
    }
    File exportTempDir = File.createTempFile("webanno", "export");
    try {
        exportTempDir.delete();
        exportTempDir.mkdirs();
        AnalysisEngineDescription writer;
        if (aWriter.getName().equals("de.tudarmstadt.ukp.clarin.webanno.tsv.WebannoTsv3Writer")) {
            List<AnnotationLayer> layers = annotationService.listAnnotationLayer(aDocument.getProject());
            List<String> slotFeatures = new ArrayList<>();
            List<String> slotTargets = new ArrayList<>();
            List<String> linkTypes = new ArrayList<>();
            Set<String> spanLayers = new HashSet<>();
            Set<String> slotLayers = new HashSet<>();
            for (AnnotationLayer layer : layers) {
                if (layer.getType().contentEquals(WebAnnoConst.SPAN_TYPE)) {
                    // TSV will not use this
                    if (!annotationExists(cas, layer.getName())) {
                        continue;
                    }
                    boolean isslotLayer = false;
                    for (AnnotationFeature f : annotationService.listAnnotationFeature(layer)) {
                        if (MultiValueMode.ARRAY.equals(f.getMultiValueMode()) && LinkMode.WITH_ROLE.equals(f.getLinkMode())) {
                            isslotLayer = true;
                            slotFeatures.add(layer.getName() + ":" + f.getName());
                            slotTargets.add(f.getType());
                            linkTypes.add(f.getLinkTypeName());
                        }
                    }
                    if (isslotLayer) {
                        slotLayers.add(layer.getName());
                    } else {
                        spanLayers.add(layer.getName());
                    }
                }
            }
            spanLayers.addAll(slotLayers);
            List<String> chainLayers = new ArrayList<>();
            for (AnnotationLayer layer : layers) {
                if (layer.getType().contentEquals(WebAnnoConst.CHAIN_TYPE)) {
                    if (!chainAnnotationExists(cas, layer.getName() + "Chain")) {
                        continue;
                    }
                    chainLayers.add(layer.getName());
                }
            }
            List<String> relationLayers = new ArrayList<>();
            for (AnnotationLayer layer : layers) {
                if (layer.getType().contentEquals(WebAnnoConst.RELATION_TYPE)) {
                    // TSV will not use this
                    if (!annotationExists(cas, layer.getName())) {
                        continue;
                    }
                    relationLayers.add(layer.getName());
                }
            }
            writer = createEngineDescription(aWriter, JCasFileWriter_ImplBase.PARAM_TARGET_LOCATION, exportTempDir, JCasFileWriter_ImplBase.PARAM_STRIP_EXTENSION, aStripExtension, "spanLayers", spanLayers, "slotFeatures", slotFeatures, "slotTargets", slotTargets, "linkTypes", linkTypes, "chainLayers", chainLayers, "relationLayers", relationLayers);
        } else {
            writer = createEngineDescription(aWriter, JCasFileWriter_ImplBase.PARAM_TARGET_LOCATION, exportTempDir, JCasFileWriter_ImplBase.PARAM_STRIP_EXTENSION, aStripExtension);
        }
        runPipeline(cas, writer);
        // If the writer produced more than one file, we package it up as a ZIP file
        File exportFile;
        if (exportTempDir.listFiles().length > 1) {
            exportFile = new File(exportTempDir.getAbsolutePath() + ".zip");
            try {
                ZipUtils.zipFolder(exportTempDir, exportFile);
            } catch (Exception e) {
                try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(project.getId()))) {
                    log.info("Unable to create zip File");
                }
            }
        } else {
            exportFile = new File(exportTempDir.getParent(), exportTempDir.listFiles()[0].getName());
            FileUtils.copyFile(exportTempDir.listFiles()[0], exportFile);
        }
        return exportFile;
    } finally {
        if (exportTempDir != null) {
            FileUtils.forceDelete(exportTempDir);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) UIMAException(org.apache.uima.UIMAException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) TagSet(de.tudarmstadt.ukp.clarin.webanno.model.TagSet) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) DocumentMetaData(de.tudarmstadt.ukp.dkpro.core.api.metadata.type.DocumentMetaData) File(java.io.File) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature) HashSet(java.util.HashSet)

Example 37 with AnnotationLayer

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

the class CoreferenceLayerInitializer method configure.

@Override
public void configure(Project aProject) throws IOException {
    TagSet corefTypeTagSet = JsonImportUtil.importTagSetFromJson(aProject, new ClassPathResource("/tagsets/de-coref-type-bart.json").getInputStream(), annotationSchemaService);
    TagSet corefRelTagSet = JsonImportUtil.importTagSetFromJson(aProject, new ClassPathResource("/tagsets/de-coref-rel-tuebadz.json").getInputStream(), annotationSchemaService);
    AnnotationLayer base = new AnnotationLayer("de.tudarmstadt.ukp.dkpro.core.api.coref.type.Coreference", "Coreference", CHAIN_TYPE, aProject, true);
    base.setCrossSentence(true);
    base.setAllowStacking(true);
    base.setMultipleTokens(true);
    base.setLockToTokenOffset(false);
    annotationSchemaService.createLayer(base);
    annotationSchemaService.createFeature(new AnnotationFeature(aProject, base, "referenceType", "referenceType", CAS.TYPE_NAME_STRING, "Coreference type", corefTypeTagSet));
    annotationSchemaService.createFeature(new AnnotationFeature(aProject, base, "referenceRelation", "referenceRelation", CAS.TYPE_NAME_STRING, "Coreference relation", corefRelTagSet));
}
Also used : TagSet(de.tudarmstadt.ukp.clarin.webanno.model.TagSet) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) ClassPathResource(org.springframework.core.io.ClassPathResource) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 38 with AnnotationLayer

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

the class DependencyLayerInitializer method configure.

@Override
public void configure(Project aProject) throws IOException {
    TagSet depTagSet = JsonImportUtil.importTagSetFromJson(aProject, new ClassPathResource("/tagsets/mul-dep-ud.json").getInputStream(), annotationSchemaService);
    // Dependency Layer
    AnnotationLayer depLayer = new AnnotationLayer(Dependency.class.getName(), "Dependency", RELATION_TYPE, aProject, true);
    AnnotationLayer tokenLayer = annotationSchemaService.getLayer(Token.class.getName(), aProject);
    List<AnnotationFeature> tokenFeatures = annotationSchemaService.listAnnotationFeature(tokenLayer);
    AnnotationFeature tokenPosFeature = null;
    for (AnnotationFeature feature : tokenFeatures) {
        if (feature.getName().equals("pos")) {
            tokenPosFeature = feature;
            break;
        }
    }
    depLayer.setAttachType(tokenLayer);
    depLayer.setAttachFeature(tokenPosFeature);
    annotationSchemaService.createLayer(depLayer);
    annotationSchemaService.createFeature(new AnnotationFeature(aProject, depLayer, "DependencyType", "Relation", CAS.TYPE_NAME_STRING, "Dependency relation", depTagSet));
    String[] flavors = { DependencyFlavor.BASIC, DependencyFlavor.ENHANCED };
    String[] flavorDesc = { DependencyFlavor.BASIC, DependencyFlavor.ENHANCED };
    TagSet flavorsTagset = annotationSchemaService.createTagSet("Dependency flavors", "Dependency flavors", "mul", flavors, flavorDesc, aProject);
    annotationSchemaService.createFeature(new AnnotationFeature(aProject, depLayer, "flavor", "Flavor", CAS.TYPE_NAME_STRING, "Dependency relation", flavorsTagset));
}
Also used : TagSet(de.tudarmstadt.ukp.clarin.webanno.model.TagSet) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) Dependency(de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) ClassPathResource(org.springframework.core.io.ClassPathResource) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 39 with AnnotationLayer

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

the class MorphologicalFeaturesLayerInitializer method configure.

@Override
public void configure(Project aProject) throws IOException {
    AnnotationLayer tokenLayer = annotationSchemaService.getLayer(Token.class.getName(), aProject);
    AnnotationFeature tokenMorphFeature = new AnnotationFeature(aProject, tokenLayer, "morph", "morph", Lemma.class.getName());
    annotationSchemaService.createFeature(tokenMorphFeature);
    AnnotationLayer morphLayer = new AnnotationLayer(MorphologicalFeatures.class.getName(), "Morphological features", SPAN_TYPE, aProject, true);
    morphLayer.setAttachType(tokenLayer);
    morphLayer.setAttachFeature(tokenMorphFeature);
    annotationSchemaService.createLayer(morphLayer);
    AnnotationFeature valueFeature = new AnnotationFeature();
    valueFeature.setDescription("Morphological features");
    valueFeature.setName("value");
    valueFeature.setType(CAS.TYPE_NAME_STRING);
    valueFeature.setProject(aProject);
    valueFeature.setUiName("Features");
    valueFeature.setLayer(morphLayer);
    valueFeature.setIncludeInHover(true);
    valueFeature.setVisible(false);
    annotationSchemaService.createFeature(valueFeature);
}
Also used : MorphologicalFeatures(de.tudarmstadt.ukp.dkpro.core.api.lexmorph.type.morph.MorphologicalFeatures) Lemma(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Lemma) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Example 40 with AnnotationLayer

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

the class PreRenderer method render.

public void render(VDocument aResponse, AnnotatorState aState, JCas aJCas, List<AnnotationLayer> aLayers) {
    // Render (custom) layers
    for (AnnotationLayer layer : aLayers) {
        List<AnnotationFeature> features = annotationService.listAnnotationFeature(layer);
        TypeAdapter adapter = annotationService.getAdapter(layer);
        Renderer renderer = getRenderer(adapter);
        renderer.render(aJCas, features, aResponse, aState);
    }
}
Also used : TypeAdapter(de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer) AnnotationFeature(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)

Aggregations

AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)67 AnnotationFeature (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature)35 AnnotationFS (org.apache.uima.cas.text.AnnotationFS)22 ArrayList (java.util.ArrayList)14 TagSet (de.tudarmstadt.ukp.clarin.webanno.model.TagSet)13 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)12 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)11 LogMessage (de.tudarmstadt.ukp.clarin.webanno.diag.CasDoctor.LogMessage)8 TypeAdapter (de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter)7 Type (org.apache.uima.cas.Type)7 JCas (org.apache.uima.jcas.JCas)6 AnnotationException (de.tudarmstadt.ukp.clarin.webanno.api.annotation.exception.AnnotationException)5 FeatureStructure (org.apache.uima.cas.FeatureStructure)5 TypeSystemDescription (org.apache.uima.resource.metadata.TypeSystemDescription)5 Test (org.junit.Test)5 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)4 HashMap (java.util.HashMap)4 TypeSystemDescriptionFactory.createTypeSystemDescription (org.apache.uima.fit.factory.TypeSystemDescriptionFactory.createTypeSystemDescription)4 SoftAssertions (org.assertj.core.api.SoftAssertions)4 Tag (de.tudarmstadt.ukp.clarin.webanno.model.Tag)3