Search in sources :

Example 1 with SStructure

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

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

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

the class RSTImpl method createJsonEntry.

private JSONObject createJsonEntry(SNode currNode) {
    JSONObject jsonData = new JSONObject();
    StringBuilder sb = new StringBuilder();
    // use a hash set so we don't get any duplicate entries
    LinkedHashSet<SToken> token = new LinkedHashSet<>();
    List<SRelation<SNode, SNode>> edges;
    if (currNode instanceof SStructure) {
        edges = currNode.getGraph().getOutRelations(currNode.getId());
        // get all tokens directly dominated tokens and build a string
        for (SRelation<SNode, SNode> sedge : edges) {
            if (sedge.getTarget() instanceof SToken) {
                token.add((SToken) sedge.getTarget());
            }
        }
        // build strings
        Iterator<SToken> tokIterator = token.iterator();
        while (tokIterator.hasNext()) {
            SToken tok = tokIterator.next();
            String text = getText(tok);
            String color = getHTMLColor(tok);
            if (color != null) {
                sb.append("<span style=\"color : ").append(color).append(";\">");
            } else {
                sb.append("<span>");
            }
            if (tokIterator.hasNext()) {
                sb.append(text).append(" ");
            } else {
                sb.append(text);
            }
            sb.append("</span>");
        }
    }
    try {
        // build unique id, cause is used for an unique html element id.
        jsonData.put("id", getUniStrId(currNode));
        jsonData.put("name", currNode.getName());
        /**
         * additional data oject for edge labels and rendering sentences
         */
        JSONObject data = new JSONObject();
        JSONArray edgesJSON = getOutGoingEdgeTypeAnnotation(currNode);
        // since we have found some tokens, it must be a sentence in RST.
        if (token.size() > 0) {
            data.put("sentence", sb.toString());
        }
        if (edgesJSON != null) {
            data.put("edges", edgesJSON);
        }
        if (currNode instanceof SStructure && isSegment(currNode)) {
            SProcessingAnnotation sentence_idx = currNode.getProcessingAnnotation(SENTENCE_INDEX + "::" + SENTENCE_INDEX);
            int index = sentence_idx == null ? -1 : Integer.parseInt(sentence_idx.getValue_STEXT());
            data.put(SENTENCE_LEFT, index);
            data.put(SENTENCE_RIGHT, index);
        }
        jsonData.put("data", data);
    } catch (JSONException ex) {
        log.error("problems create entry for {}", currNode, ex);
    }
    return jsonData;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SNode(org.corpus_tools.salt.core.SNode) SProcessingAnnotation(org.corpus_tools.salt.core.SProcessingAnnotation) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) SToken(org.corpus_tools.salt.common.SToken) SRelation(org.corpus_tools.salt.core.SRelation) JSONObject(org.json.JSONObject) SStructure(org.corpus_tools.salt.common.SStructure)

Aggregations

SStructure (org.corpus_tools.salt.common.SStructure)3 SToken (org.corpus_tools.salt.common.SToken)3 SNode (org.corpus_tools.salt.core.SNode)3 SRelation (org.corpus_tools.salt.core.SRelation)3 RelannisEdgeFeature (annis.model.RelannisEdgeFeature)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1 SDominanceRelation (org.corpus_tools.salt.common.SDominanceRelation)1 SPointingRelation (org.corpus_tools.salt.common.SPointingRelation)1 SSpanningRelation (org.corpus_tools.salt.common.SSpanningRelation)1 GraphTraverseHandler (org.corpus_tools.salt.core.GraphTraverseHandler)1 SFeature (org.corpus_tools.salt.core.SFeature)1 GRAPH_TRAVERSE_TYPE (org.corpus_tools.salt.core.SGraph.GRAPH_TRAVERSE_TYPE)1 SProcessingAnnotation (org.corpus_tools.salt.core.SProcessingAnnotation)1 SaltException (org.corpus_tools.salt.exceptions.SaltException)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1