use of org.corpus_tools.salt.common.SSpanningRelation in project ANNIS by korpling.
the class TimelineReconstructor method moveRelations.
private void moveRelations(SStructuredNode oldSpan, SToken newToken, Set<String> validSpanAnnos, String orderName) {
final List<SRelation> inRels = new LinkedList<>(oldSpan.getInRelations());
final List<SRelation> outRels = new LinkedList<>(oldSpan.getOutRelations());
final List<SToken> coveredByOldSpan = new LinkedList<>();
for (SRelation rel : outRels) {
if (rel instanceof SPointingRelation || rel instanceof SDominanceRelation) {
rel.setSource(newToken);
} else if (rel instanceof SSpanningRelation) {
coveredByOldSpan.add(((SSpanningRelation) rel).getTarget());
}
}
for (SRelation rel : inRels) {
if (rel instanceof SPointingRelation || rel instanceof SDominanceRelation) {
rel.setTarget(newToken);
}
}
// find the connected spans and connect them with the new token instead
for (SToken tok : coveredByOldSpan) {
if (tok.getInRelations() != null) {
for (SRelation<?, ?> rel : tok.getInRelations()) {
if (rel instanceof SSpanningRelation) {
boolean valid = false;
SSpan spanToMap = ((SSpanningRelation) rel).getSource();
if (virtualTokenizationFromNamespace) {
for (SAnnotation anno : spanToMap.getAnnotations()) {
if (anno.getNamespace() != null && anno.getNamespace().equals(orderName)) {
valid = true;
break;
}
}
} else {
for (String validAnno : validSpanAnnos) {
if (spanToMap.getAnnotation(validAnno) != null) {
valid = true;
break;
}
}
}
if (valid) {
graph.createRelation(spanToMap, newToken, SALT_TYPE.SSPANNING_RELATION, null);
}
}
}
}
}
}
use of org.corpus_tools.salt.common.SSpanningRelation in project ANNIS by korpling.
the class TimelineReconstructor method addTimeline.
private void addTimeline() {
STimeline timeline = graph.createTimeline();
for (SToken virtualTok : graph.getSortedTokenByText()) {
timeline.increasePointOfTime();
// find all spans that are connected to this token and are part of an SOrderRelation
for (SRelation<?, ?> inRel : virtualTok.getInRelations()) {
if (inRel instanceof SSpanningRelation) {
SSpanningRelation spanRel = (SSpanningRelation) inRel;
SSpan overlappingSpan = spanRel.getSource();
if (overlappingSpan != null) {
spans2TimelinePos.put(overlappingSpan, timeline.getEnd());
}
}
}
nodesToDelete.add(virtualTok);
}
}
use of org.corpus_tools.salt.common.SSpanningRelation in project ANNIS by korpling.
the class TimeHelper method getOverlappedTime.
/**
* Get start and end time from the overlapped {@link SToken}. The minimum
* start time and maximum end times are choosen.
* @param node The span to start from.
* @return A double array, will have length of 0 if no time annotations where found,
* one element of only start elements where found, 2 elements if both
* start and end time are found.
*/
public static double[] getOverlappedTime(SNode node) {
SGraph graph = node.getGraph();
final List<Double> startTimes = new LinkedList<>();
final List<Double> endTimes = new LinkedList<>();
List<SToken> token = new LinkedList<>();
if (node instanceof SToken) {
token.add((SToken) node);
} else {
List<SRelation<SNode, SNode>> outRelations = graph.getOutRelations(node.getId());
if (outRelations != null) {
for (SRelation<? extends SNode, ? extends SNode> e : outRelations) {
if (e instanceof SSpanningRelation) {
SToken tok = ((SSpanningRelation) e).getTarget();
token.add(tok);
}
}
}
// end for each out relations
}
for (SToken tok : token) {
SAnnotation anno = tok.getAnnotation(SaltUtil.createQName("annis", "time"));
if (anno != null && anno.getValue_STEXT() != null && !anno.getValue_STEXT().isEmpty() && !anno.getValue_STEXT().matches("\\-[0-9]*(\\.[0-9]*)?")) {
try {
String[] split = anno.getValue_STEXT().split("-");
if (split.length == 1) {
startTimes.add(Double.parseDouble(split[0]));
}
if (split.length == 2) {
startTimes.add(Double.parseDouble(split[0]));
endTimes.add(Double.parseDouble(split[1]));
}
} catch (NumberFormatException ex) {
log.debug("Invalid time annotation", ex);
}
}
}
if (startTimes.size() > 0 && endTimes.size() > 0) {
return new double[] { Collections.min(startTimes), Collections.max(endTimes) };
} else if (startTimes.size() > 0) {
return new double[] { Collections.min(startTimes) };
}
return new double[0];
}
Aggregations