Search in sources :

Example 1 with SOrderRelation

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

the class CommonHelper method getSortedSegmentationNodes.

/**
 * Calculates a {@link SOrderRelation} node chain of a {@link SDocumentGraph}.
 *
 * <p>
 * If no segmentation name is set, a list of sorted {@link SToken} will be
 * returned.<p>
 *
 * @param segName The segmentation name, for which the chain is computed.
 * @param graph The salt document graph, which is traversed for the
 * segmentation.
 *
 * @return Returns a List of {@link SNode}, which is sorted by the
 * {@link SOrderRelation}.
 */
public static List<SNode> getSortedSegmentationNodes(String segName, SDocumentGraph graph) {
    List<SNode> token = new ArrayList<SNode>();
    if (segName == null) {
        // if no segmentation is given just return the sorted token list
        List<SToken> unsortedToken = graph.getSortedTokenByText();
        if (unsortedToken != null) {
            token.addAll(unsortedToken);
        }
    } else {
        // get the very first node of the order relation chain
        Set<SNode> startNodes = new LinkedHashSet<SNode>();
        for (SNode n : graph.getNodes()) {
            SFeature feat = n.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_FIRST_NODE_SEGMENTATION_CHAIN);
            if (feat != null && segName.equalsIgnoreCase(feat.getValue_STEXT())) {
                startNodes.add(n);
            }
        }
        Set<String> alreadyAdded = new HashSet<String>();
        // add all nodes on the order relation chain beginning from the start node
        for (SNode s : startNodes) {
            SNode current = s;
            while (current != null) {
                token.add(current);
                List<SRelation<SNode, SNode>> out = graph.getOutRelations(current.getId());
                current = null;
                if (out != null) {
                    for (SRelation<? extends SNode, ? extends SNode> e : out) {
                        if (e instanceof SOrderRelation) {
                            current = ((SOrderRelation) e).getTarget();
                            if (alreadyAdded.contains(current.getId())) {
                                // abort if cycle detected
                                current = null;
                            } else {
                                alreadyAdded.add(current.getId());
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
    return token;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SNode(org.corpus_tools.salt.core.SNode) ArrayList(java.util.ArrayList) SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) SOrderRelation(org.corpus_tools.salt.common.SOrderRelation) SFeature(org.corpus_tools.salt.core.SFeature) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with SOrderRelation

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

the class SaltAnnotateExtractor method addOrderingRelations.

private void addOrderingRelations(SDocumentGraph graph, TreeMap<String, TreeMap<Long, String>> nodeBySegmentationPath) {
    AtomicInteger numberOfSOrderRels = new AtomicInteger();
    for (Map.Entry<String, TreeMap<Long, String>> e : nodeBySegmentationPath.entrySet()) {
        String segName = e.getKey();
        TreeMap<Long, String> nodeBySegIndex = e.getValue();
        // mark the first node in the chain
        if (!nodeBySegIndex.isEmpty()) {
            String idOfFirstNode = nodeBySegIndex.firstEntry().getValue();
            SNode firstNodeInSegChain = graph.getNode(idOfFirstNode);
            if (firstNodeInSegChain != null) {
                SFeature featFistSegInChain = SaltFactory.createSFeature();
                featFistSegInChain.setNamespace(ANNIS_NS);
                featFistSegInChain.setName(FEAT_FIRST_NODE_SEGMENTATION_CHAIN);
                featFistSegInChain.setValue(segName);
                firstNodeInSegChain.addFeature(featFistSegInChain);
            }
        }
        SStructuredNode lastNode = null;
        for (String nodeID : nodeBySegIndex.values()) {
            SNode nodeById = graph.getNode(nodeID);
            if (nodeById instanceof SStructuredNode) {
                SStructuredNode n = (SStructuredNode) nodeById;
                if (lastNode != null) {
                    SOrderRelation orderRel = SaltFactory.createSOrderRelation();
                    orderRel.setSource(lastNode);
                    orderRel.setTarget(n);
                    orderRel.setType(segName);
                    orderRel.setName("sOrderRel" + numberOfSOrderRels.getAndIncrement());
                    graph.addRelation(orderRel);
                }
                lastNode = n;
            }
        }
    }
}
Also used : SStructuredNode(org.corpus_tools.salt.common.SStructuredNode) SNode(org.corpus_tools.salt.core.SNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) SOrderRelation(org.corpus_tools.salt.common.SOrderRelation) SFeature(org.corpus_tools.salt.core.SFeature)

Example 3 with SOrderRelation

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

SOrderRelation (org.corpus_tools.salt.common.SOrderRelation)3 SFeature (org.corpus_tools.salt.core.SFeature)3 SNode (org.corpus_tools.salt.core.SNode)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SRelation (org.corpus_tools.salt.core.SRelation)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 TreeMap (java.util.TreeMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SSpan (org.corpus_tools.salt.common.SSpan)1 SStructuredNode (org.corpus_tools.salt.common.SStructuredNode)1 STextualDS (org.corpus_tools.salt.common.STextualDS)1 SToken (org.corpus_tools.salt.common.SToken)1 GraphTraverseHandler (org.corpus_tools.salt.core.GraphTraverseHandler)1 GRAPH_TRAVERSE_TYPE (org.corpus_tools.salt.core.SGraph.GRAPH_TRAVERSE_TYPE)1