Search in sources :

Example 26 with SToken

use of org.corpus_tools.salt.common.SToken in project ANNIS by korpling.

the class AutoTokQuery method analyzingQuery.

@Override
public void analyzingQuery(SaltProject saltProject) {
    List<SToken> tokens = new ArrayList<>();
    for (SCorpusGraph g : saltProject.getCorpusGraphs()) {
        if (g != null) {
            for (SDocument doc : g.getDocuments()) {
                SDocumentGraph docGraph = doc.getDocumentGraph();
                List<SNode> sNodes = docGraph.getNodes();
                if (sNodes != null) {
                    for (SNode n : sNodes) {
                        if (n instanceof SToken) {
                            tokens.add((SToken) n);
                        }
                    }
                }
            }
        }
    }
    // select one random token from the result
    if (!tokens.isEmpty()) {
        int tries = 10;
        int r = new Random().nextInt(tokens.size() - 1);
        String text = CommonHelper.getSpannedText(tokens.get(r));
        while ("".equals(text) && tries > 0) {
            r = new Random().nextInt(tokens.size() - 1);
            text = CommonHelper.getSpannedText(tokens.get(r));
            tries--;
        }
        if ("".equals(text)) {
            finalAql = null;
        } else {
            finalAql = "\"" + text + "\"";
        }
    }
}
Also used : SToken(org.corpus_tools.salt.common.SToken) SNode(org.corpus_tools.salt.core.SNode) Random(java.util.Random) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) ArrayList(java.util.ArrayList) SDocument(org.corpus_tools.salt.common.SDocument) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph)

Example 27 with SToken

use of org.corpus_tools.salt.common.SToken in project ANNIS by korpling.

the class SingleResultPanel method getIds.

private MinMax getIds(SDocumentGraph graph) {
    List<SToken> sTokens = graph.getTokens();
    MinMax minMax = new MinMax();
    minMax.min = Long.MAX_VALUE;
    minMax.max = Long.MIN_VALUE;
    if (segmentationName == null) {
        minMax.segName = "tokens";
        if (sTokens != null) {
            for (SToken t : sTokens) {
                SFeature feature = t.getFeature(ANNIS_NS, FEAT_RELANNIS_NODE);
                if (feature != null && feature.getValue() instanceof RelannisNodeFeature) {
                    RelannisNodeFeature f = (RelannisNodeFeature) feature.getValue();
                    if (minMax.min > f.getTokenIndex()) {
                        minMax.min = f.getTokenIndex();
                    }
                    if (minMax.max < f.getTokenIndex()) {
                        minMax.max = f.getTokenIndex();
                    }
                }
            }
        }
    } else {
        minMax.segName = segmentationName;
        List<SNode> nodes = CommonHelper.getSortedSegmentationNodes(segmentationName, graph);
        for (SNode n : nodes) {
            RelannisNodeFeature f = RelannisNodeFeature.extract(n);
            if (minMax.min > f.getSegIndex()) {
                minMax.min = f.getSegIndex();
            }
            if (minMax.max < f.getSegIndex()) {
                minMax.max = f.getSegIndex();
            }
        }
    }
    minMax.min++;
    minMax.max++;
    return minMax;
}
Also used : SToken(org.corpus_tools.salt.common.SToken) RelannisNodeFeature(annis.model.RelannisNodeFeature) SNode(org.corpus_tools.salt.core.SNode) SFeature(org.corpus_tools.salt.core.SFeature)

Example 28 with SToken

use of org.corpus_tools.salt.common.SToken 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 29 with SToken

use of org.corpus_tools.salt.common.SToken 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);
    }
}
Also used : SToken(org.corpus_tools.salt.common.SToken) SSpan(org.corpus_tools.salt.common.SSpan) STimeline(org.corpus_tools.salt.common.STimeline) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation)

Example 30 with SToken

use of org.corpus_tools.salt.common.SToken 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

SToken (org.corpus_tools.salt.common.SToken)30 SNode (org.corpus_tools.salt.core.SNode)16 SRelation (org.corpus_tools.salt.core.SRelation)12 HashMap (java.util.HashMap)9 SAnnotation (org.corpus_tools.salt.core.SAnnotation)9 ArrayList (java.util.ArrayList)8 LinkedList (java.util.LinkedList)8 SSpan (org.corpus_tools.salt.common.SSpan)8 SFeature (org.corpus_tools.salt.core.SFeature)8 RelannisNodeFeature (annis.model.RelannisNodeFeature)7 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)7 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)6 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)5 Map (java.util.Map)4 TreeMap (java.util.TreeMap)4 SDocument (org.corpus_tools.salt.common.SDocument)4 Annotation (annis.model.Annotation)3 HashSet (java.util.HashSet)3 SDominanceRelation (org.corpus_tools.salt.common.SDominanceRelation)3 STextualDS (org.corpus_tools.salt.common.STextualDS)3