Search in sources :

Example 1 with SProcessingAnnotation

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

Example 2 with SProcessingAnnotation

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

the class SaltAnnotateExtractor method moveNodeProperties.

private void moveNodeProperties(SStructuredNode oldNode, SStructuredNode newNode, SGraph graph) {
    Validate.notNull(oldNode);
    Validate.notNull(newNode);
    // step 1: collect every information that is need in a separate variable
    String id = oldNode.getId();
    String name = oldNode.getName();
    Set<SAnnotation> annotations = new LinkedHashSet<>(oldNode.getAnnotations());
    Set<SFeature> features = new LinkedHashSet<>(oldNode.getFeatures());
    Set<SProcessingAnnotation> processingAnnotations = new LinkedHashSet<>(oldNode.getProcessingAnnotations());
    Set<SMetaAnnotation> metaAnnotations = new LinkedHashSet<>(oldNode.getMetaAnnotations());
    Set<SLayer> nodeLayers = new LinkedHashSet<>(oldNode.getLayers());
    Multimap<SRelation, SLayer> layerOfRelation = ArrayListMultimap.create();
    List<SRelation<SNode, SNode>> inRelations = new LinkedList<>(graph.getInRelations(oldNode.getId()));
    List<SRelation<SNode, SNode>> outRelations = new LinkedList<>(graph.getOutRelations(oldNode.getId()));
    // step 2: remove the old node from everything it is connected to
    for (SRelation<SNode, SNode> rel : inRelations) {
        if (rel.getLayers() != null) {
            layerOfRelation.putAll(rel, rel.getLayers());
        }
        graph.removeRelation(rel);
    }
    for (SRelation<SNode, SNode> rel : outRelations) {
        if (rel.getLayers() != null) {
            layerOfRelation.putAll(rel, rel.getLayers());
        }
        graph.removeRelation(rel);
    }
    graph.removeNode(oldNode);
    // step 3: add the new node to everything it should be connected to
    newNode.setName(name);
    newNode.setId(id);
    graph.addNode(newNode);
    for (SAnnotation anno : annotations) {
        newNode.addAnnotation(anno);
    }
    for (SFeature feat : features) {
        // filter the features, do not include salt::SNAME
        if (!(SaltUtil.SALT_NAMESPACE.equals(feat.getNamespace()) && SaltUtil.FEAT_NAME.equals(feat.getName()))) {
            newNode.addFeature(feat);
        }
    }
    for (SProcessingAnnotation proc : processingAnnotations) {
        newNode.addProcessingAnnotation(proc);
    }
    for (SMetaAnnotation meta : metaAnnotations) {
        newNode.addMetaAnnotation(meta);
    }
    for (SLayer l : nodeLayers) {
        l.addNode(newNode);
    }
    for (SRelation rel : inRelations) {
        rel.setTarget(newNode);
        graph.addRelation(rel);
        if (layerOfRelation.containsKey(rel)) {
            for (SLayer l : layerOfRelation.get(rel)) {
                l.addRelation(rel);
            }
        }
    }
    for (SRelation rel : outRelations) {
        rel.setSource(newNode);
        graph.addRelation(rel);
        if (layerOfRelation.containsKey(rel)) {
            for (SLayer l : layerOfRelation.get(rel)) {
                l.addRelation(rel);
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SLayer(org.corpus_tools.salt.core.SLayer) SNode(org.corpus_tools.salt.core.SNode) SAnnotation(org.corpus_tools.salt.core.SAnnotation) SProcessingAnnotation(org.corpus_tools.salt.core.SProcessingAnnotation) LinkedList(java.util.LinkedList) SRelation(org.corpus_tools.salt.core.SRelation) SMetaAnnotation(org.corpus_tools.salt.core.SMetaAnnotation) SFeature(org.corpus_tools.salt.core.SFeature)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)2 SNode (org.corpus_tools.salt.core.SNode)2 SProcessingAnnotation (org.corpus_tools.salt.core.SProcessingAnnotation)2 SRelation (org.corpus_tools.salt.core.SRelation)2 LinkedList (java.util.LinkedList)1 SStructure (org.corpus_tools.salt.common.SStructure)1 SToken (org.corpus_tools.salt.common.SToken)1 SAnnotation (org.corpus_tools.salt.core.SAnnotation)1 SFeature (org.corpus_tools.salt.core.SFeature)1 SLayer (org.corpus_tools.salt.core.SLayer)1 SMetaAnnotation (org.corpus_tools.salt.core.SMetaAnnotation)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1