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; }
@Override public int compareTo(Span o) { if(context != null && o.getContext() != null && !context.equals(o.getContext())){ log.warn("Comparing Spans with different Context. This is not an " + "intended usage of this class as start|end|type parameters " + "do not have a natural oder over different texts."); log.info("This will sort Spans based on start|end|type parameters "+ "regardless that the might be over different texts!"); //TODO consider throwing an IllegalStateExcetion! } //Compare Integers ASC (used here three times) // (x < y) ? -1 : ((x == y) ? 0 : 1); int start = (span[0] < o.getStart()) ? -1 : ((span[0] == o.getStart()) ? 0 : 1); if(start == 0){ //sort end in DESC order int end = (span[1] < o.getEnd()) ? 1 : ((span[1] == o.getEnd()) ? 0 : -1); //if start AND end is the same compare based on the span type //Natural order of span types is defined by the Enum.ordinal() int o1 = getType().ordinal(); int o2 = o.getType().ordinal(); return end != 0 ? end : (o1 < o2) ? -1 : ((o1 == o2) ? 0 : 1); } else { return start; } }
if("type".equals(field.getKey())){ if(field.getValue().isTextual()){ spanType = SpanTypeEnum.valueOf(field.getValue().getTextValue()); } else if(field.getValue().isInt()){ spanType = SpanTypeEnum.values()[field.getValue().getIntValue()]; } else { log.warn("Unable to parse SpanType form JSON field "+field +" (ignored, json: "+jSpan+")");
@Override public ObjectNode serialize(ObjectMapper mapper, CorefFeature coref) { ObjectNode jCoref = mapper.createObjectNode(); jCoref.put(IS_REPRESENTATIVE_TAG, coref.isRepresentative()); Set<Span> mentions = coref.getMentions(); ArrayNode jMentions = mapper.createArrayNode(); for(Span mention : mentions) { ObjectNode jMention = mapper.createObjectNode(); jMention.put(MENTION_TYPE_TAG, mention.getType().toString()); jMention.put(MENTION_START_TAG, mention.getStart()); jMention.put(MENTION_END_TAG, mention.getEnd()); jMentions.add(jMention); } jCoref.put(MENTIONS_TAG, jMentions); return jCoref; }
SpanTypeEnum spanType = SpanTypeEnum.valueOf(jMention.path(MENTION_TYPE_TAG).getTextValue()); int spanStart = jMention.path(MENTION_START_TAG).asInt(); int spanEnd = jMention.path(MENTION_END_TAG).asInt();
@Override public ObjectNode serialize(ObjectMapper mapper, CorefFeature coref) { ObjectNode jCoref = mapper.createObjectNode(); jCoref.put(IS_REPRESENTATIVE_TAG, coref.isRepresentative()); Set<Span> mentions = coref.getMentions(); ArrayNode jMentions = mapper.createArrayNode(); for(Span mention : mentions) { ObjectNode jMention = mapper.createObjectNode(); jMention.put(MENTION_TYPE_TAG, mention.getType().toString()); jMention.put(MENTION_START_TAG, mention.getStart()); jMention.put(MENTION_END_TAG, mention.getEnd()); jMentions.add(jMention); } jCoref.put(MENTIONS_TAG, jMentions); return jCoref; }
SpanTypeEnum spanType = SpanTypeEnum.valueOf(jDependencyRelation.path(RELATION_PARTNER_TYPE_TAG) .getTextValue()); int spanStart = jDependencyRelation.path(RELATION_PARTNER_START_TAG).asInt();
if("type".equals(field.getKey())){ if(field.getValue().isTextual()){ spanType = SpanTypeEnum.valueOf(field.getValue().getTextValue()); } else if(field.getValue().isInt()){ spanType = SpanTypeEnum.values()[field.getValue().getIntValue()]; } else { log.warn("Unable to parse SpanType form JSON field "+field +" (ignored, json: "+jSpan+")");
@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; }
SpanTypeEnum spanType = SpanTypeEnum.valueOf(jMention.path(MENTION_TYPE_TAG).getTextValue()); int spanStart = jMention.path(MENTION_START_TAG).asInt(); int spanEnd = jMention.path(MENTION_END_TAG).asInt();
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; }
@Override public int compareTo(Span o) { if(context != null && o.getContext() != null && !context.equals(o.getContext())){ log.warn("Comparing Spans with different Context. This is not an " + "intended usage of this class as start|end|type parameters " + "do not have a natural oder over different texts."); log.info("This will sort Spans based on start|end|type parameters "+ "regardless that the might be over different texts!"); //TODO consider throwing an IllegalStateExcetion! } //Compare Integers ASC (used here three times) // (x < y) ? -1 : ((x == y) ? 0 : 1); int start = (span[0] < o.getStart()) ? -1 : ((span[0] == o.getStart()) ? 0 : 1); if(start == 0){ //sort end in DESC order int end = (span[1] < o.getEnd()) ? 1 : ((span[1] == o.getEnd()) ? 0 : -1); //if start AND end is the same compare based on the span type //Natural order of span types is defined by the Enum.ordinal() int o1 = getType().ordinal(); int o2 = o.getType().ordinal(); return end != 0 ? end : (o1 < o2) ? -1 : ((o1 == o2) ? 0 : 1); } else { return start; } }
@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; }
SpanTypeEnum spanType = SpanTypeEnum.valueOf(jDependencyRelation.path(RELATION_PARTNER_TYPE_TAG) .getTextValue()); int spanStart = jDependencyRelation.path(RELATION_PARTNER_START_TAG).asInt();