Search in sources :

Example 41 with ObjectNode

use of org.codehaus.jackson.node.ObjectNode in project stanbol by apache.

the class AnalyzedTextParser method parseSpan.

private void parseSpan(AnalysedText at, JsonNode node) throws IOException {
    if (node.isObject()) {
        ObjectNode jSpan = (ObjectNode) node;
        int[] spanPos = new int[] { -1, -1 };
        Collection<Entry<String, JsonNode>> jAnnotations = new ArrayList<Entry<String, JsonNode>>(4);
        SpanTypeEnum spanType = parseSpanData(jSpan, spanPos, jAnnotations);
        if (spanType == null || spanPos[0] < 0 || spanPos[1] < 0) {
            log.warn("Illegal or missing span type, start and/or end position (ignored, json: " + jSpan);
            return;
        }
        // now create the Span
        Span span;
        switch(spanType) {
            case Text:
                log.warn("Encounterd 'Text' span that is not the first span in the " + "'spans' array (ignored, json: " + node + ")");
                return;
            case TextSection:
                log.warn("Encountered 'TextSection' span. This SpanTypeEnum entry " + "is currently unused. If this is no longer the case please " + "update this implementation (ignored, json: " + node + ")");
                return;
            case Sentence:
                span = at.addSentence(spanPos[0], spanPos[1]);
                break;
            case Chunk:
                span = at.addChunk(spanPos[0], spanPos[1]);
                break;
            case Token:
                span = at.addToken(spanPos[0], spanPos[1]);
                break;
            default:
                log.warn("Unsupported SpanTypeEnum  '" + spanType + "'!. Please " + "update this implementation (ignored, json: " + node + ")");
                return;
        }
        if (!jAnnotations.isEmpty()) {
            parseAnnotations(span, jAnnotations);
        }
    } else {
        log.warn("Unable to parse Span form JsonNode " + node + " (expected JSON object)!");
    }
}
Also used : Entry(java.util.Map.Entry) SpanTypeEnum(org.apache.stanbol.enhancer.nlp.model.SpanTypeEnum) ObjectNode(org.codehaus.jackson.node.ObjectNode) ArrayList(java.util.ArrayList) JsonNode(org.codehaus.jackson.JsonNode) SerializedString(org.codehaus.jackson.io.SerializedString) Span(org.apache.stanbol.enhancer.nlp.model.Span)

Example 42 with ObjectNode

use of org.codehaus.jackson.node.ObjectNode in project stanbol by apache.

the class DependencyRelationSupport method serialize.

@Override
public ObjectNode serialize(ObjectMapper mapper, DependencyRelation relation) {
    ObjectNode jDependencyRelation = mapper.createObjectNode();
    GrammaticalRelationTag gramRelTag = relation.getGrammaticalRelationTag();
    jDependencyRelation.put(RELATION_TYPE_TAG, gramRelTag.getTag());
    jDependencyRelation.put(RELATION_STANBOL_TYPE_TAG, gramRelTag.getGrammaticalRelation().ordinal());
    jDependencyRelation.put(RELATION_IS_DEPENDENT_TAG, (relation.isDependent()));
    Span partner = relation.getPartner();
    if (partner != null) {
        jDependencyRelation.put(RELATION_PARTNER_TYPE_TAG, partner.getType().toString());
        jDependencyRelation.put(RELATION_PARTNER_START_TAG, partner.getStart());
        jDependencyRelation.put(RELATION_PARTNER_END_TAG, partner.getEnd());
    } else {
        jDependencyRelation.put(RELATION_PARTNER_TYPE_TAG, ROOT_TAG);
        jDependencyRelation.put(RELATION_PARTNER_START_TAG, 0);
        jDependencyRelation.put(RELATION_PARTNER_END_TAG, 0);
    }
    return jDependencyRelation;
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) GrammaticalRelationTag(org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelationTag) Span(org.apache.stanbol.enhancer.nlp.model.Span)

Example 43 with ObjectNode

use of org.codehaus.jackson.node.ObjectNode in project stanbol by apache.

the class PosTagSupport method serialize.

@Override
public ObjectNode serialize(ObjectMapper mapper, PosTag value) {
    ObjectNode jPosTag = mapper.createObjectNode();
    jPosTag.put("tag", value.getTag());
    if (value.getPos().size() == 1) {
        jPosTag.put("pos", value.getPos().iterator().next().ordinal());
    } else if (!value.getPos().isEmpty()) {
        ArrayNode jPos = mapper.createArrayNode();
        for (Pos pos : value.getPos()) {
            jPos.add(pos.ordinal());
        }
        jPosTag.put("pos", jPos);
    }
    if (!value.getCategories().isEmpty()) {
        // we need only the categories not covered by Pos elements
        EnumSet<LexicalCategory> categories = EnumSet.noneOf(LexicalCategory.class);
        categories.addAll(value.getCategories());
        for (Pos pos : value.getPos()) {
            categories.removeAll(pos.categories());
        }
        if (categories.size() == 1) {
            jPosTag.put("lc", categories.iterator().next().ordinal());
        } else if (!categories.isEmpty()) {
            ArrayNode jCategory = mapper.createArrayNode();
            for (LexicalCategory lc : categories) {
                jCategory.add(lc.ordinal());
            }
            jPosTag.put("lc", jCategory);
        }
    }
    return jPosTag;
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) Pos(org.apache.stanbol.enhancer.nlp.pos.Pos) ArrayNode(org.codehaus.jackson.node.ArrayNode) LexicalCategory(org.apache.stanbol.enhancer.nlp.pos.LexicalCategory)

Example 44 with ObjectNode

use of org.codehaus.jackson.node.ObjectNode in project stanbol by apache.

the class AnalyzedTextSerializer method writeSpan.

private ObjectNode writeSpan(Span span) throws IOException {
    log.trace("wirte {}", span);
    ObjectNode jSpan = mapper.createObjectNode();
    jSpan.put("type", span.getType().name());
    jSpan.put("start", span.getStart());
    jSpan.put("end", span.getEnd());
    for (String key : span.getKeys()) {
        List<Value<?>> values = span.getValues(key);
        if (values.size() == 1) {
            jSpan.put(key, writeValue(values.get(0)));
        } else {
            ArrayNode jValues = jSpan.putArray(key);
            for (Value<?> value : values) {
                jValues.add(writeValue(value));
            }
            jSpan.put(key, jValues);
        }
    }
    log.trace(" ... {}", jSpan);
    return jSpan;
}
Also used : ObjectNode(org.codehaus.jackson.node.ObjectNode) Value(org.apache.stanbol.enhancer.nlp.model.annotation.Value) ArrayNode(org.codehaus.jackson.node.ArrayNode)

Example 45 with ObjectNode

use of org.codehaus.jackson.node.ObjectNode in project stanbol by apache.

the class AnalyzedTextSerializer method writeValue.

@SuppressWarnings({ "rawtypes", "unchecked" })
private ObjectNode writeValue(Value<?> value) {
    ObjectNode jValue;
    Class<?> valueType = value.value().getClass();
    ValueTypeSerializer vts = valueTypeSerializerRegistry.getSerializer(valueType);
    if (vts != null) {
        jValue = vts.serialize(mapper, value.value());
    // TODO assert that jValue does not define "class" and "prob"!
    } else {
        // use the default binding and the "data" field
        jValue = mapper.createObjectNode();
        jValue.put("value", mapper.valueToTree(value.value()));
    }
    jValue.put("class", valueType.getName());
    if (value.probability() != Value.UNKNOWN_PROBABILITY) {
        jValue.put("prob", value.probability());
    }
    return jValue;
}
Also used : ValueTypeSerializer(org.apache.stanbol.enhancer.nlp.json.valuetype.ValueTypeSerializer) ObjectNode(org.codehaus.jackson.node.ObjectNode)

Aggregations

ObjectNode (org.codehaus.jackson.node.ObjectNode)97 ArrayNode (org.codehaus.jackson.node.ArrayNode)29 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)28 JsonNode (org.codehaus.jackson.JsonNode)22 GET (javax.ws.rs.GET)21 Path (javax.ws.rs.Path)18 Test (org.junit.Test)17 Produces (javax.ws.rs.Produces)12 Map (java.util.Map)11 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)9 IOException (java.io.IOException)7 StringWriter (java.io.StringWriter)5 JsonFactory (org.codehaus.jackson.JsonFactory)5 HelixDataAccessor (org.apache.helix.HelixDataAccessor)4 Span (org.apache.stanbol.enhancer.nlp.model.Span)4 DatasetImpl (org.eol.globi.service.DatasetImpl)4 Date (java.util.Date)3 TaskDriver (org.apache.helix.task.TaskDriver)3 WorkflowConfig (org.apache.helix.task.WorkflowConfig)3