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