Search in sources :

Example 6 with SRelation

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

the class SaltAnnotateExtractor method findExistingRelation.

private SRelation findExistingRelation(SDocumentGraph graph, SNode sourceNode, SNode targetNode, String relationName, SLayer layer) {
    SRelation rel = null;
    List<SRelation<SNode, SNode>> existingRelations = graph.getRelations(sourceNode.getId(), targetNode.getId());
    if (existingRelations != null) {
        for (Relation e : existingRelations) {
            // the same layer "edge_namespace")
            if (e instanceof SRelation) {
                SRelation existingRel = (SRelation) e;
                boolean noType = existingRel.getType() == null;
                if (((noType && relationName == null) || (!noType && existingRel.getType().equals(relationName))) && existingRel.getLayers().contains(layer)) {
                    rel = existingRel;
                    break;
                }
            }
        }
    }
    return rel;
}
Also used : SRelation(org.corpus_tools.salt.core.SRelation) SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation) STextualRelation(org.corpus_tools.salt.common.STextualRelation) SRelation(org.corpus_tools.salt.core.SRelation) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) SOrderRelation(org.corpus_tools.salt.common.SOrderRelation) Relation(org.corpus_tools.salt.graph.Relation)

Example 7 with SRelation

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

the class SaltAnnotateExtractor method createNewRelation.

private SRelation createNewRelation(SDocumentGraph graph, SStructuredNode sourceNode, SNode targetNode, String relationName, String type, long componentID, SLayer layer, long pre, FastInverseMap<Long, SNode> nodeByRankID, AtomicInteger numberOfRelations) {
    SRelation rel = null;
    if (null != type) // create new relation
    {
        switch(type) {
            case "d":
                SDominanceRelation domrel = SaltFactory.createSDominanceRelation();
                // always set a name by ourself since the SDocumentGraph#basicAddRelation()
                // functions otherwise real slow
                domrel.setName("sDomRel" + numberOfRelations.incrementAndGet());
                rel = domrel;
                if (sourceNode != null && !(sourceNode instanceof SStructure)) {
                    log.debug("Mismatched source type: should be SStructure");
                    SNode oldNode = sourceNode;
                    sourceNode = recreateNode(SStructure.class, sourceNode);
                    updateMapAfterRecreatingNode(oldNode, sourceNode, nodeByRankID);
                }
                if (relationName == null || relationName.isEmpty()) {
                    // layer but has a non-empty relation name
                    if (handleArtificialDominanceRelation(graph, sourceNode, targetNode, rel, layer, componentID, pre)) {
                        // don't include this relation
                        rel = null;
                    }
                }
                break;
            case "c":
                SSpanningRelation spanrel = SaltFactory.createSSpanningRelation();
                // always set a name by ourself since the SDocumentGraph#basicAddRelation()
                // functions is real slow otherwise
                spanrel.setName("sSpanRel" + numberOfRelations.incrementAndGet());
                rel = spanrel;
                sourceNode = testAndFixNonSpan(sourceNode, nodeByRankID);
                break;
            case "p":
                SPointingRelation pointingrel = SaltFactory.createSPointingRelation();
                pointingrel.setName("sPointingRel" + numberOfRelations.incrementAndGet());
                rel = pointingrel;
                break;
            default:
                throw new IllegalArgumentException("Invalid type " + type + " for new Relation");
        }
        try {
            if (rel != null) {
                rel.setType(relationName);
                RelannisEdgeFeature featRelation = new RelannisEdgeFeature();
                featRelation.setPre(pre);
                featRelation.setComponentID(componentID);
                SFeature sfeatRelation = SaltFactory.createSFeature();
                sfeatRelation.setNamespace(ANNIS_NS);
                sfeatRelation.setName(FEAT_RELANNIS_EDGE);
                sfeatRelation.setValue(featRelation);
                rel.addFeature(sfeatRelation);
                rel.setSource(sourceNode);
                if ("c".equals(type) && !(targetNode instanceof SToken)) {
                    log.warn("invalid relation detected: target node ({}) " + "of a coverage relation (from: {}, internal id {}) was not a token", new Object[] { targetNode.getName(), sourceNode == null ? "null" : sourceNode.getName(), "" + pre });
                } else {
                    rel.setTarget(targetNode);
                    graph.addRelation(rel);
                    layer.addRelation(rel);
                }
            }
        } catch (SaltException ex) {
            log.warn("invalid relation detected", ex);
        }
    }
    return rel;
}
Also used : SPointingRelation(org.corpus_tools.salt.common.SPointingRelation) SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) SNode(org.corpus_tools.salt.core.SNode) SaltException(org.corpus_tools.salt.exceptions.SaltException) SSpanningRelation(org.corpus_tools.salt.common.SSpanningRelation) RelannisEdgeFeature(annis.model.RelannisEdgeFeature) SDominanceRelation(org.corpus_tools.salt.common.SDominanceRelation) SStructure(org.corpus_tools.salt.common.SStructure) SFeature(org.corpus_tools.salt.core.SFeature)

Example 8 with SRelation

use of org.corpus_tools.salt.core.SRelation 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 9 with SRelation

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

the class CommonHelper method getSpannedText.

/**
 * Gets the spannend/covered text for a token. This will get all
 * {@link STextualRelation} edges for a {@link SToken} from the
 * {@link SDocumentGraph} and calculates the appropiate substring from the
 * {@link STextualDS}.
 *
 * @param tok The {@link SToken} which is overlapping the text sequence.
 * @return An empty {@link String} object, if there is no
 * {@link STextualRelation}
 */
public static String getSpannedText(SToken tok) {
    SGraph graph = tok.getGraph();
    List<SRelation<SNode, SNode>> edges = graph.getOutRelations(tok.getId());
    for (SRelation<? extends SNode, ? extends SNode> e : edges) {
        if (e instanceof STextualRelation) {
            STextualRelation textRel = (STextualRelation) e;
            return textRel.getTarget().getText().substring(textRel.getStart(), textRel.getEnd());
        }
    }
    return "";
}
Also used : SRelation(org.corpus_tools.salt.core.SRelation) STextualRelation(org.corpus_tools.salt.common.STextualRelation) SGraph(org.corpus_tools.salt.core.SGraph)

Example 10 with SRelation

use of org.corpus_tools.salt.core.SRelation 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)

Aggregations

SRelation (org.corpus_tools.salt.core.SRelation)24 SNode (org.corpus_tools.salt.core.SNode)14 SToken (org.corpus_tools.salt.common.SToken)12 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)9 LinkedList (java.util.LinkedList)8 SAnnotation (org.corpus_tools.salt.core.SAnnotation)8 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)7 HashMap (java.util.HashMap)6 SDominanceRelation (org.corpus_tools.salt.common.SDominanceRelation)6 SLayer (org.corpus_tools.salt.core.SLayer)6 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)5 SOrderRelation (org.corpus_tools.salt.common.SOrderRelation)5 SFeature (org.corpus_tools.salt.core.SFeature)5 ArrayList (java.util.ArrayList)4 STextualRelation (org.corpus_tools.salt.common.STextualRelation)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 RelannisEdgeFeature (annis.model.RelannisEdgeFeature)3 RelannisNodeFeature (annis.model.RelannisNodeFeature)3 LinkedHashSet (java.util.LinkedHashSet)3