Search in sources :

Example 21 with SAnnotation

use of org.corpus_tools.salt.core.SAnnotation 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);
                    }
                }
            }
        }
    }
}
Also used : SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) SSpan(org.corpus_tools.salt.common.SSpan) SAnnotation(org.corpus_tools.salt.core.SAnnotation) LinkedList(java.util.LinkedList) SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation)

Example 22 with SAnnotation

use of org.corpus_tools.salt.core.SAnnotation 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];
}
Also used : SAnnotation(org.corpus_tools.salt.core.SAnnotation) LinkedList(java.util.LinkedList) SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) SGraph(org.corpus_tools.salt.core.SGraph)

Aggregations

SAnnotation (org.corpus_tools.salt.core.SAnnotation)22 SToken (org.corpus_tools.salt.common.SToken)9 SNode (org.corpus_tools.salt.core.SNode)8 SRelation (org.corpus_tools.salt.core.SRelation)8 LinkedList (java.util.LinkedList)6 RelannisNodeFeature (annis.model.RelannisNodeFeature)5 HashMap (java.util.HashMap)4 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)4 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)4 SFeature (org.corpus_tools.salt.core.SFeature)4 SLayer (org.corpus_tools.salt.core.SLayer)4 Annotation (annis.model.Annotation)3 ArrayList (java.util.ArrayList)3 TreeSet (java.util.TreeSet)3 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)3 SSpan (org.corpus_tools.salt.common.SSpan)3 STextualDS (org.corpus_tools.salt.common.STextualDS)3 DataSourceSequence (org.corpus_tools.salt.util.DataSourceSequence)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2