Search in sources :

Example 1 with DependencyRelation

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

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

DependencyRelation (org.apache.stanbol.enhancer.nlp.dependency.DependencyRelation)2 GrammaticalRelationTag (org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelationTag)2 GrammaticalRelation (org.apache.stanbol.enhancer.nlp.dependency.GrammaticalRelation)1 Sentence (org.apache.stanbol.enhancer.nlp.model.Sentence)1 Span (org.apache.stanbol.enhancer.nlp.model.Span)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