Search in sources :

Example 1 with Pos

use of org.apache.stanbol.enhancer.nlp.pos.Pos in project stanbol by apache.

the class Nif20Helper method writePos.

/**
     * Writes the {@link NlpAnnotations#POS_ANNOTATION} as NIF 1.0 to the parsed
     * RDF graph by using the parsed segmentUri as subject
     * @param graph the graph
     * @param annotated the annotated element (e.g. a {@link Token})
     * @param segmentUri the URI of the resource representing the parsed 
     * annotated element in the graph
     */
public static void writePos(Graph graph, Annotated annotated, IRI segmentUri) {
    Value<PosTag> posTag = annotated.getAnnotation(NlpAnnotations.POS_ANNOTATION);
    if (posTag != null) {
        if (posTag.value().isMapped()) {
            for (Pos pos : posTag.value().getPos()) {
                graph.add(new TripleImpl(segmentUri, Nif20.oliaCategory.getUri(), pos.getUri()));
            }
            for (LexicalCategory cat : posTag.value().getCategories()) {
                graph.add(new TripleImpl(segmentUri, Nif20.oliaCategory.getUri(), cat.getUri()));
            }
        }
        graph.add(new TripleImpl(segmentUri, Nif20.posTag.getUri(), lf.createTypedLiteral(posTag.value().getTag())));
        //set the oliaConf
        //remove existing conf values (e.g. for a single word phrase)
        setOliaConf(graph, segmentUri, posTag);
    }
}
Also used : PosTag(org.apache.stanbol.enhancer.nlp.pos.PosTag) Pos(org.apache.stanbol.enhancer.nlp.pos.Pos) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) LexicalCategory(org.apache.stanbol.enhancer.nlp.pos.LexicalCategory)

Example 2 with Pos

use of org.apache.stanbol.enhancer.nlp.pos.Pos 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 3 with Pos

use of org.apache.stanbol.enhancer.nlp.pos.Pos in project stanbol by apache.

the class PosTagSupport method parse.

@Override
public PosTag parse(ObjectNode jValue, AnalysedText at) {
    PosTagInfo tagInfo = new PosTagInfo();
    JsonNode tag = jValue.path("tag");
    if (!tag.isTextual()) {
        throw new IllegalStateException("Unable to parse PosTag. The value of the " + "'tag' field MUST have a textual value (json: " + jValue + ")");
    }
    tagInfo.tag = tag.getTextValue();
    if (jValue.has("lc")) {
        tagInfo.categories = JsonUtils.parseEnum(jValue, "lc", LexicalCategory.class);
    } else {
        tagInfo.categories = EnumSet.noneOf(LexicalCategory.class);
    }
    if (jValue.has("pos")) {
        tagInfo.pos = JsonUtils.parseEnum(jValue, "pos", Pos.class);
    } else {
        tagInfo.pos = EnumSet.noneOf(Pos.class);
    }
    PosTag posTag = posTagCache.get(tagInfo);
    if (posTag == null) {
        posTag = new PosTag(tagInfo.tag, tagInfo.categories, tagInfo.pos);
        posTagCache.put(tagInfo, posTag);
    }
    return posTag;
}
Also used : PosTag(org.apache.stanbol.enhancer.nlp.pos.PosTag) Pos(org.apache.stanbol.enhancer.nlp.pos.Pos) JsonNode(org.codehaus.jackson.JsonNode) LexicalCategory(org.apache.stanbol.enhancer.nlp.pos.LexicalCategory)

Example 4 with Pos

use of org.apache.stanbol.enhancer.nlp.pos.Pos in project stanbol by apache.

the class NIFHelper method writePos.

/**
     * Writes the {@link NlpAnnotations#POS_ANNOTATION} as NIF 1.0 to the parsed
     * RDF graph by using the parsed segmentUri as subject
     * @param graph the graph
     * @param annotated the annotated element (e.g. a {@link Token})
     * @param segmentUri the URI of the resource representing the parsed 
     * annotated element in the graph
     */
public static void writePos(Graph graph, Annotated annotated, IRI segmentUri) {
    Value<PosTag> posTag = annotated.getAnnotation(NlpAnnotations.POS_ANNOTATION);
    if (posTag != null) {
        if (posTag.value().isMapped()) {
            for (Pos pos : posTag.value().getPos()) {
                graph.add(new TripleImpl(segmentUri, SsoOntology.oliaLink.getUri(), pos.getUri()));
            }
            for (LexicalCategory cat : posTag.value().getCategories()) {
                graph.add(new TripleImpl(segmentUri, SsoOntology.oliaLink.getUri(), cat.getUri()));
            }
        }
        graph.add(new TripleImpl(segmentUri, SsoOntology.posTag.getUri(), lf.createTypedLiteral(posTag.value().getTag())));
        graph.add(new TripleImpl(segmentUri, ENHANCER_CONFIDENCE, lf.createTypedLiteral(posTag.probability())));
    }
}
Also used : PosTag(org.apache.stanbol.enhancer.nlp.pos.PosTag) Pos(org.apache.stanbol.enhancer.nlp.pos.Pos) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) LexicalCategory(org.apache.stanbol.enhancer.nlp.pos.LexicalCategory)

Aggregations

LexicalCategory (org.apache.stanbol.enhancer.nlp.pos.LexicalCategory)4 Pos (org.apache.stanbol.enhancer.nlp.pos.Pos)4 PosTag (org.apache.stanbol.enhancer.nlp.pos.PosTag)3 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)2 JsonNode (org.codehaus.jackson.JsonNode)1 ArrayNode (org.codehaus.jackson.node.ArrayNode)1 ObjectNode (org.codehaus.jackson.node.ObjectNode)1