Search in sources :

Example 1 with GetDocumentResponse

use of de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse in project webanno by webanno.

the class SuggestionViewPanel method render.

private String render(JCas aJcas, AnnotatorState aBratAnnotatorModel, ColoringStrategy aCurationColoringStrategy) throws IOException {
    List<AnnotationLayer> layersToRender = new ArrayList<>();
    for (AnnotationLayer layer : aBratAnnotatorModel.getAnnotationLayers()) {
        boolean isSegmentationLayer = layer.getName().equals(Token.class.getName()) || layer.getName().equals(Sentence.class.getName());
        boolean isUnsupportedLayer = layer.getType().equals(CHAIN_TYPE);
        if (layer.isEnabled() && !isSegmentationLayer && !isUnsupportedLayer) {
            layersToRender.add(layer);
        }
    }
    VDocument vdoc = new VDocument();
    preRenderer.render(vdoc, aBratAnnotatorModel, aJcas, layersToRender);
    GetDocumentResponse response = new GetDocumentResponse();
    BratRenderer.render(response, aBratAnnotatorModel, vdoc, aJcas, annotationService, aCurationColoringStrategy);
    return JSONUtil.toInterpretableJsonString(response);
}
Also used : GetDocumentResponse(de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) ArrayList(java.util.ArrayList) AnnotationLayer(de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)

Example 2 with GetDocumentResponse

use of de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse in project webanno by webanno.

the class BratAnnotationEditor method actionGetDocument.

private String actionGetDocument(JCas jCas) {
    StopWatch timer = new StopWatch();
    timer.start();
    GetDocumentResponse response = new GetDocumentResponse();
    String json;
    if (getModelObject().getProject() != null) {
        render(response, jCas);
        json = toJson(response);
        lastRenderedJson = json;
    } else {
        json = toJson(response);
    }
    timer.stop();
    metrics.renderComplete(RenderType.FULL, timer.getTime(), json, null);
    return json;
}
Also used : GetDocumentResponse(de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 3 with GetDocumentResponse

use of de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse in project webanno by webanno.

the class BratAnnotationEditor method bratRenderCommand.

private String bratRenderCommand(JCas aJCas) {
    StopWatch timer = new StopWatch();
    timer.start();
    GetDocumentResponse response = new GetDocumentResponse();
    render(response, aJCas);
    String json = toJson(response);
    // By default, we do a full rendering...
    RenderType renderType = RenderType.FULL;
    String cmd = "renderData";
    String data = json;
    // ... try to render diff
    String diff = null;
    JsonNode current = null;
    JsonNode previous = null;
    try {
        ObjectMapper mapper = JSONUtil.getJsonConverter().getObjectMapper();
        current = mapper.readTree(json);
        previous = lastRenderedJson != null ? mapper.readTree(lastRenderedJson) : null;
    } catch (IOException e) {
        LOG.error("Unable to generate diff, falling back to full render.", e);
    // Fall-through
    }
    if (previous != null && current != null) {
        diff = JsonDiff.asJson(previous, current).toString();
        // pages, the patch usually ends up being twice as large as the full data.
        if (diff.length() < json.length()) {
            cmd = "renderDataPatch";
            data = diff;
            renderType = RenderType.DIFFERENTIAL;
        }
    // LOG.info("Diff:  " + diff);
    // LOG.info("Full: {}   Patch: {}   Diff time: {}", json.length(), diff.length(), timer);
    }
    // Storing the last rendered JSON as string because JsonNodes are not serializable.
    lastRenderedJson = json;
    timer.stop();
    metrics.renderComplete(renderType, timer.getTime(), json, diff);
    return "Wicket.$('" + vis.getMarkupId() + "').dispatcher.post('" + cmd + "', [" + data + "]);";
}
Also used : GetDocumentResponse(de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) RenderType(de.tudarmstadt.ukp.clarin.webanno.brat.metrics.BratMetrics.RenderType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 4 with GetDocumentResponse

use of de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse in project webanno by webanno.

the class CasToBratJsonTest method testGenerateBratJsonGetDocument.

/**
 * generate brat JSON data for the document
 */
@Test
public void testGenerateBratJsonGetDocument() throws Exception {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    String jsonFilePath = "target/test-output/output_cas_to_json_document.json";
    String file = "src/test/resources/tcf04-karin-wl.xml";
    CAS cas = JCasFactory.createJCas().getCas();
    CollectionReader reader = CollectionReaderFactory.createReader(TcfReader.class, TcfReader.PARAM_SOURCE_LOCATION, file);
    reader.getNext(cas);
    JCas jCas = cas.getJCas();
    AnnotatorState state = new AnnotatorStateImpl(Mode.ANNOTATION);
    state.getPreferences().setWindowSize(10);
    state.setFirstVisibleUnit(WebAnnoCasUtil.getFirstSentence(jCas));
    state.setProject(project);
    VDocument vdoc = new VDocument();
    preRenderer.render(vdoc, state, jCas, annotationSchemaService.listAnnotationLayer(project));
    GetDocumentResponse response = new GetDocumentResponse();
    BratRenderer.render(response, state, vdoc, jCas, annotationSchemaService);
    JSONUtil.generatePrettyJson(jsonConverter, response, new File(jsonFilePath));
    assertThat(linesOf(new File("src/test/resources/output_cas_to_json_document_expected.json"), "UTF-8")).isEqualTo(linesOf(new File(jsonFilePath), "UTF-8"));
}
Also used : MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) CollectionReader(org.apache.uima.collection.CollectionReader) GetDocumentResponse(de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse) CAS(org.apache.uima.cas.CAS) VDocument(de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument) AnnotatorStateImpl(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorStateImpl) AnnotatorState(de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState) JCas(org.apache.uima.jcas.JCas) File(java.io.File) Test(org.junit.Test)

Aggregations

GetDocumentResponse (de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse)4 VDocument (de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.model.VDocument)2 StopWatch (org.apache.commons.lang3.time.StopWatch)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AnnotatorState (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState)1 AnnotatorStateImpl (de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorStateImpl)1 RenderType (de.tudarmstadt.ukp.clarin.webanno.brat.metrics.BratMetrics.RenderType)1 AnnotationLayer (de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CAS (org.apache.uima.cas.CAS)1 CollectionReader (org.apache.uima.collection.CollectionReader)1 JCas (org.apache.uima.jcas.JCas)1 Test (org.junit.Test)1 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)1