Search in sources :

Example 1 with GrammaticalRelationTag

use of org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelationTag 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 2 with GrammaticalRelationTag

use of org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelationTag in project stanbol by apache.

the class DependencyRelationSupport method parse.

@Override
public DependencyRelation parse(ObjectNode jDependencyRelation, AnalysedText at) {
    JsonNode tag = jDependencyRelation.path(RELATION_TYPE_TAG);
    if (!tag.isTextual()) {
        throw new IllegalStateException("Unable to parse GrammaticalRelationTag. The value of the " + "'tag' field MUST have a textual value (json: " + jDependencyRelation + ")");
    }
    GrammaticalRelation grammaticalRelation = GrammaticalRelation.class.getEnumConstants()[jDependencyRelation.path(RELATION_STANBOL_TYPE_TAG).asInt()];
    GrammaticalRelationTag gramRelTag = new GrammaticalRelationTag(tag.getTextValue(), grammaticalRelation);
    JsonNode isDependent = jDependencyRelation.path(RELATION_IS_DEPENDENT_TAG);
    if (!isDependent.isBoolean()) {
        throw new IllegalStateException("Field 'isDependent' must have a true/false format");
    }
    Span partnerSpan = null;
    String typeString = jDependencyRelation.path(RELATION_PARTNER_TYPE_TAG).getTextValue();
    if (!typeString.equals(ROOT_TAG)) {
        SpanTypeEnum spanType = SpanTypeEnum.valueOf(jDependencyRelation.path(RELATION_PARTNER_TYPE_TAG).getTextValue());
        int spanStart = jDependencyRelation.path(RELATION_PARTNER_START_TAG).asInt();
        int spanEnd = jDependencyRelation.path(RELATION_PARTNER_END_TAG).asInt();
        switch(spanType) {
            case Chunk:
                partnerSpan = at.addChunk(spanStart, spanEnd);
                break;
            // break;
            case Token:
                partnerSpan = at.addToken(spanStart, spanEnd);
                break;
        }
    }
    return new DependencyRelation(gramRelTag, isDependent.asBoolean(), partnerSpan);
}
Also used : SpanTypeEnum(org.apache.stanbol.enhancer.nlp.model.SpanTypeEnum) GrammaticalRelation(org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelation) JsonNode(org.codehaus.jackson.JsonNode) GrammaticalRelationTag(org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelationTag) Span(org.apache.stanbol.enhancer.nlp.model.Span) DependencyRelation(org.apache.stanbol.enhancer.nlp.dependency.DependencyRelation)

Example 3 with GrammaticalRelationTag

use of org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelationTag in project stanbol by apache.

the class DependencyRelationSupportTest method initDepTreeAnnotations.

private static void initDepTreeAnnotations() {
    Sentence sentence = at.addSentence(0, text.indexOf(".") + 1);
    Token obama = sentence.addToken(0, "Obama".length());
    int visitedStartIdx = sentence.getSpan().indexOf("visited");
    Token visited = sentence.addToken(visitedStartIdx, visitedStartIdx + "visited".length());
    int chinaStartIdx = sentence.getSpan().indexOf("China");
    Token china = sentence.addToken(chinaStartIdx, chinaStartIdx + "China".length());
    GrammaticalRelationTag nSubjGrammRelTag = new GrammaticalRelationTag("nsubj", GrammaticalRelation.NominalSubject);
    obama.addAnnotation(NlpAnnotations.DEPENDENCY_ANNOTATION, Value.value(new DependencyRelation(nSubjGrammRelTag, true, visited)));
    GrammaticalRelationTag rootGrammRelTag = new GrammaticalRelationTag("root", GrammaticalRelation.Root);
    GrammaticalRelationTag dobjGrammRelTag = new GrammaticalRelationTag("dobj", GrammaticalRelation.DirectObject);
    visited.addAnnotation(NlpAnnotations.DEPENDENCY_ANNOTATION, Value.value(new DependencyRelation(rootGrammRelTag, true, null)));
    visited.addAnnotation(NlpAnnotations.DEPENDENCY_ANNOTATION, Value.value(new DependencyRelation(nSubjGrammRelTag, false, obama)));
    visited.addAnnotation(NlpAnnotations.DEPENDENCY_ANNOTATION, Value.value(new DependencyRelation(dobjGrammRelTag, false, china)));
    china.addAnnotation(NlpAnnotations.DEPENDENCY_ANNOTATION, Value.value(new DependencyRelation(dobjGrammRelTag, true, visited)));
}
Also used : Token(org.apache.stanbol.enhancer.nlp.model.Token) GrammaticalRelationTag(org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelationTag) Sentence(org.apache.stanbol.enhancer.nlp.model.Sentence) DependencyRelation(org.apache.stanbol.enhancer.nlp.dependency.DependencyRelation)

Aggregations

GrammaticalRelationTag (org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelationTag)3 DependencyRelation (org.apache.stanbol.enhancer.nlp.dependency.DependencyRelation)2 Span (org.apache.stanbol.enhancer.nlp.model.Span)2 GrammaticalRelation (org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelation)1 Sentence (org.apache.stanbol.enhancer.nlp.model.Sentence)1 SpanTypeEnum (org.apache.stanbol.enhancer.nlp.model.SpanTypeEnum)1 Token (org.apache.stanbol.enhancer.nlp.model.Token)1 JsonNode (org.codehaus.jackson.JsonNode)1 ObjectNode (org.codehaus.jackson.node.ObjectNode)1