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;
}
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);
}
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)));
}
Aggregations