Search in sources :

Example 1 with GraphTraverseHandler

use of org.corpus_tools.salt.core.GraphTraverseHandler in project ANNIS by korpling.

the class CommonHelper method getCorpusPath.

public static List<String> getCorpusPath(SCorpusGraph corpusGraph, SDocument doc) {
    final List<String> result = new LinkedList<String>();
    result.add(doc.getName());
    SCorpus c = corpusGraph.getCorpus(doc);
    List<SNode> cAsList = new ArrayList<>();
    cAsList.add(c);
    corpusGraph.traverse(cAsList, GRAPH_TRAVERSE_TYPE.BOTTOM_UP_DEPTH_FIRST, "getRootCorpora", new GraphTraverseHandler() {

        @Override
        public void nodeReached(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation edge, SNode fromNode, long order) {
            result.add(currNode.getName());
        }

        @Override
        public void nodeLeft(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation edge, SNode fromNode, long order) {
        }

        @Override
        public boolean checkConstraint(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SRelation edge, SNode currNode, long order) {
            return true;
        }
    });
    return result;
}
Also used : SCorpus(org.corpus_tools.salt.common.SCorpus) SRelation(org.corpus_tools.salt.core.SRelation) SNode(org.corpus_tools.salt.core.SNode) GRAPH_TRAVERSE_TYPE(org.corpus_tools.salt.core.SGraph.GRAPH_TRAVERSE_TYPE) GraphTraverseHandler(org.corpus_tools.salt.core.GraphTraverseHandler) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 2 with GraphTraverseHandler

use of org.corpus_tools.salt.core.GraphTraverseHandler in project ANNIS by korpling.

the class RSTImpl method transformSaltToJSON.

private String transformSaltToJSON(VisualizerInput visInput) {
    graph = visInput.getSResult().getDocumentGraph();
    List<SNode> rootSNodes = graph.getRoots();
    List<SNode> rstRoots = new ArrayList<SNode>();
    for (SNode sNode : rootSNodes) {
        if (CommonHelper.checkSLayer(namespace, sNode)) {
            rstRoots.add(sNode);
        }
    }
    if (rootSNodes.size() > 0) {
        // collect all sentence and sort them.
        graph.traverse(rstRoots, GRAPH_TRAVERSE_TYPE.TOP_DOWN_DEPTH_FIRST, "getSentences", new GraphTraverseHandler() {

            @Override
            public void nodeReached(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation sRelation, SNode fromNode, long order) {
                if (currNode instanceof SStructure && isSegment(currNode)) {
                    sentences.add((SStructure) currNode);
                }
            }

            @Override
            public void nodeLeft(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation edge, SNode fromNode, long order) {
            }

            @Override
            public boolean checkConstraint(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SRelation edge, SNode currNode, long order) {
                // token are not needed
                if (currNode instanceof SToken) {
                    return false;
                }
                return true;
            }
        });
        // decorate segments with sentence number
        int i = 1;
        for (SStructure sentence : sentences) {
            sentence.createProcessingAnnotation(SENTENCE_INDEX, SENTENCE_INDEX, Integer.toString(i));
            i++;
        }
        graph.traverse(rstRoots, GRAPH_TRAVERSE_TYPE.TOP_DOWN_DEPTH_FIRST, "jsonBuild", this);
    } else {
        log.debug("does not find an annotation which matched {}", ANNOTATION_KEY);
        graph.traverse(rstRoots, GRAPH_TRAVERSE_TYPE.TOP_DOWN_DEPTH_FIRST, "jsonBuild", this);
    }
    return result.toString();
}
Also used : SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) SNode(org.corpus_tools.salt.core.SNode) GRAPH_TRAVERSE_TYPE(org.corpus_tools.salt.core.SGraph.GRAPH_TRAVERSE_TYPE) GraphTraverseHandler(org.corpus_tools.salt.core.GraphTraverseHandler) ArrayList(java.util.ArrayList) SStructure(org.corpus_tools.salt.common.SStructure)

Example 3 with GraphTraverseHandler

use of org.corpus_tools.salt.core.GraphTraverseHandler in project ANNIS by korpling.

the class TimelineReconstructor method createTokenFromSOrder.

private void createTokenFromSOrder() {
    nodesToDelete.add(graph.getTextualDSs().get(0));
    Map<String, SSpan> rootNodes = new HashMap<>();
    // also add nodes that are are marked as start by ANNIS even if they don't have an outgoing order rel
    for (SSpan n : graph.getSpans()) {
        SFeature feat = n.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_FIRST_NODE_SEGMENTATION_CHAIN);
        if (feat != null && feat.getValue_STEXT() != null) {
            rootNodes.put(feat.getValue_STEXT(), n);
        } else {
            // check if there is no incoming SOrderRelation but an outgoing
            boolean isRoot = true;
            for (SRelation<?, ?> inRel : n.getInRelations()) {
                if (inRel instanceof SOrderRelation) {
                    isRoot = false;
                    break;
                }
            }
            if (isRoot) {
                for (SRelation<?, ?> outRel : n.getOutRelations()) {
                    if (outRel instanceof SOrderRelation) {
                        rootNodes.put(((SOrderRelation) outRel).getType(), n);
                        break;
                    }
                }
            }
        }
    }
    // convert all root nodes to spans
    for (Map.Entry<String, SSpan> rootEntry : rootNodes.entrySet()) {
        SNode root = rootEntry.getValue();
        String orderName = rootEntry.getKey();
        convertSpanToToken((SSpan) root, orderName);
    }
    // traverse through all SOrderRelations in order
    graph.traverse(new LinkedList<SNode>(rootNodes.values()), GRAPH_TRAVERSE_TYPE.TOP_DOWN_DEPTH_FIRST, "TimeReconstructSOrderRelations", new GraphTraverseHandler() {

        @Override
        public void nodeReached(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation relation, SNode fromNode, long order) {
            if (relation instanceof SOrderRelation && currNode instanceof SSpan) {
                String orderName = ((SOrderRelation) relation).getType();
                if (fromNode != null) {
                    // add a space to the text
                    StringBuilder t = textDataByName.get(orderName);
                    if (t != null) {
                        t.append(" ");
                    }
                }
                convertSpanToToken((SSpan) currNode, orderName);
            }
        }

        @Override
        public void nodeLeft(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SNode currNode, SRelation relation, SNode fromNode, long order) {
        }

        @Override
        public boolean checkConstraint(GRAPH_TRAVERSE_TYPE traversalType, String traversalId, SRelation relation, SNode currNode, long order) {
            if (relation == null || relation instanceof SOrderRelation) {
                return true;
            } else {
                return false;
            }
        }
    });
    // update the text of the TextualDSs
    for (Map.Entry<String, StringBuilder> textDataEntry : textDataByName.entrySet()) {
        STextualDS textDS = textsByName.get(textDataEntry.getKey());
        if (textDS != null) {
            textDS.setText(textDataEntry.getValue().toString());
        }
    }
}
Also used : SNode(org.corpus_tools.salt.core.SNode) SSpan(org.corpus_tools.salt.common.SSpan) HashMap(java.util.HashMap) GRAPH_TRAVERSE_TYPE(org.corpus_tools.salt.core.SGraph.GRAPH_TRAVERSE_TYPE) GraphTraverseHandler(org.corpus_tools.salt.core.GraphTraverseHandler) SRelation(org.corpus_tools.salt.core.SRelation) STextualDS(org.corpus_tools.salt.common.STextualDS) SOrderRelation(org.corpus_tools.salt.common.SOrderRelation) HashMap(java.util.HashMap) Map(java.util.Map) SFeature(org.corpus_tools.salt.core.SFeature)

Aggregations

GraphTraverseHandler (org.corpus_tools.salt.core.GraphTraverseHandler)3 GRAPH_TRAVERSE_TYPE (org.corpus_tools.salt.core.SGraph.GRAPH_TRAVERSE_TYPE)3 SNode (org.corpus_tools.salt.core.SNode)3 SRelation (org.corpus_tools.salt.core.SRelation)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 SCorpus (org.corpus_tools.salt.common.SCorpus)1 SOrderRelation (org.corpus_tools.salt.common.SOrderRelation)1 SSpan (org.corpus_tools.salt.common.SSpan)1 SStructure (org.corpus_tools.salt.common.SStructure)1 STextualDS (org.corpus_tools.salt.common.STextualDS)1 SToken (org.corpus_tools.salt.common.SToken)1 SFeature (org.corpus_tools.salt.core.SFeature)1