
de.citec.scie.web.exporter.json.JsonExporter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webservice Show documentation
Show all versions of webservice Show documentation
Module providing the webservice interface based on the Jetty
embedded webserver and the FreeMarker template engine. Defines a simple
format for providing textual annotations and produced output in HTML or
JSON. This module has no dependencies to the other SCIE modules (except
for the PDF text extractor) or the UIMA framework and thus can be used
in any context, where text is annotated by an algorithm and should be
presented to an end user.
The newest version!
/*
* SCIE -- Spinal Cord Injury Information Extraction
* Copyright (C) 2013, 2014
* Raphael Dickfelder, Jan Göpfert, Benjamin Paaßen, Andreas Stöckel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package de.citec.scie.web.exporter.json;
import de.citec.scie.web.analysis.AbstractAnalysisResult;
import de.citec.scie.web.analysis.AnnotationDataEntry;
import de.citec.scie.web.exporter.AbstractExporter;
import java.util.Set;
import java.util.TreeSet;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Class used to export an AbstractAnalysisResult as JSON.
*
* @author Andreas Stöckel -- [email protected]
*/
public class JsonExporter implements AbstractExporter {
/**
* Indentation used for formatting the JSON result.
*/
private static final int INDENDATION = 4;
/**
* If true, the text should be included in the JSON output.
*/
private final boolean includeText;
/**
* Constructor of the JsonExporter class.
*
* @param includeText if true, the analysis text is included in the result
* which may lead to large files.
*/
public JsonExporter(boolean includeText) {
this.includeText = includeText;
}
@Override
public void export(StringBuilder sb, AbstractAnalysisResult result) {
final JSONObject res = new JSONObject();
// Add the text to the result
if (includeText) {
res.put("text", result.getText());
}
// Add the annotations to the annotation array
final Set groupIds = new TreeSet<>();
final JSONArray annotations = new JSONArray();
for (int i = 0; i < result.getAnnotationCount(); i++) {
final int gid = result.getAnnotationGroupId(i);
groupIds.add(gid);
final JSONObject annotation = new JSONObject();
annotation.put("name", result.getAnnotationName(i));
annotation.put("begin", result.getAnnotationBegin(i));
annotation.put("end", result.getAnnotationEnd(i));
annotation.put("group", gid);
// Assemble the generic data
final JSONArray data = new JSONArray();
for (AnnotationDataEntry e : result.getAnnotationData(i).getEntries()) {
final JSONObject entry = new JSONObject();
entry.put("key", e.getKey());
entry.put("value", e.getValue());
data.put(entry);
}
annotation.put("data", data);
annotations.put(annotation);
}
res.put("annotations", annotations);
// Add the groups to the group object
final JSONObject groups = new JSONObject();
for (int gid : groupIds) {
final JSONObject group = new JSONObject();
group.put("name", result.getGroupName(gid));
group.put("preselected", result.getGroupPreselected(gid));
group.put("category", result.getGroupCategory(gid));
groups.put(Integer.toString(gid), group);
}
res.put("groups", groups);
sb.append(res.toString(INDENDATION));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy