Search in sources :

Example 11 with SNode

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

the class CSVMultiTokExporter method createAdjacencyMatrix.

/**
 * Takes a match and stores annotation names to construct the header in
 * {@link #outputText(de.hu_berlin.german.korpling.saltnpepper.salt.saltCommon.sDocumentStructure.SDocumentGraph, boolean, int, java.io.Writer) }
 *
 * @param graph
 * @param args
 * @param matchNumber
 * @param nodeCount
 *
 * @throws java.io.IOException
 */
@Override
public void createAdjacencyMatrix(SDocumentGraph graph, Map<String, String> args, int matchNumber, int nodeCount) throws IOException, IllegalArgumentException {
    // first match
    if (matchNumber == 0) {
        // get list of metakeys to export
        metakeys = new HashSet<>();
        if (args.containsKey("metakeys")) {
            metakeys.addAll(Arrays.asList(args.get("metakeys").split(",")));
        }
        // initialize list of annotations for the matched nodes
        annotationsForMatchedNodes = new TreeMap<>();
    }
    for (SNode node : this.getMatchedNodes(graph)) {
        int node_id = node.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_MATCHEDNODE).getValue_SNUMERIC().intValue();
        if (!annotationsForMatchedNodes.containsKey(node_id))
            annotationsForMatchedNodes.put(node_id, new TreeSet<String>());
        List<SAnnotation> annots = new ArrayList<>(node.getAnnotations());
        Set<String> annoNames = annotationsForMatchedNodes.get(node_id);
        for (SAnnotation annot : annots) {
            annoNames.add(annot.getNamespace() + "::" + annot.getName());
        }
    }
}
Also used : SNode(org.corpus_tools.salt.core.SNode) SAnnotation(org.corpus_tools.salt.core.SAnnotation) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList)

Example 12 with SNode

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

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

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

the class TimelineReconstructor method cleanup.

private void cleanup() {
    for (SNode node : nodesToDelete) {
        graph.removeNode(node);
    }
    // update the feature matchedIDs
    SFeature matchids = graph.getFeature(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_MATCHEDIDS);
    if (matchids != null) {
        String[] ids = matchids.getValue_STEXT().split(",");
        for (int i = 0; i < ids.length; i++) {
            if (this.oldID2newID.containsKey(ids[i]))
                ids[i] = this.oldID2newID.get(ids[i]);
        }
        matchids.setValue(Joiner.on(',').join(ids));
    }
}
Also used : SNode(org.corpus_tools.salt.core.SNode) SFeature(org.corpus_tools.salt.core.SFeature)

Example 15 with SNode

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

the class SingleResultPanel method initVisualizer.

private void initVisualizer() {
    try {
        ResolverEntry[] entries = resolverProvider == null ? new ResolverEntry[0] : resolverProvider.getResolverEntries(result);
        visualizers = new LinkedList<>();
        resolverEntries = new LinkedList<>();
        List<VisualizerPanel> openVisualizers = new LinkedList<>();
        List<SNode> segNodes = CommonHelper.getSortedSegmentationNodes(segmentationName, result.getDocumentGraph());
        Map<String, Long> markedAndCovered = Helper.calculateMarkedAndCoveredIDs(result, segNodes, segmentationName);
        Helper.calulcateColorsForMarkedAndCovered(result, markedAndCovered, markedCoveredMap);
        String resultID = "" + new Random().nextInt(Integer.MAX_VALUE);
        for (int i = 0; i < entries.length; i++) {
            String htmlID = "resolver-" + resultNumber + "_" + i;
            VisualizerPanel p = new VisualizerPanel(entries[i], result, match, visibleTokenAnnos, markedAndCovered, markedCoveredMap, markedExactMap, htmlID, resultID, this, segmentationName, ps, instanceConfig);
            visualizers.add(p);
            resolverEntries.add(entries[i]);
            Properties mappings = entries[i].getMappings();
            // resovler entry is used, for determine the visibility status
            if (visualizerState.containsKey(entries[i].getId())) {
                if (visualizerState.get(entries[i].getId())) {
                    openVisualizers.add(p);
                }
            } else {
                if (Boolean.parseBoolean(mappings.getProperty(INITIAL_OPEN, "false"))) {
                    openVisualizers.add(p);
                }
            }
        }
        // attach visualizer
        for (VisualizerPanel p : visualizers) {
            addComponent(p);
        }
        for (VisualizerPanel p : openVisualizers) {
            p.toggleVisualizer(true, null);
        }
    } catch (RuntimeException ex) {
        log.error("problems with initializing Visualizer Panel", ex);
    } catch (Exception ex) {
        log.error("problems with initializing Visualizer Panel", ex);
    }
}
Also used : SNode(org.corpus_tools.salt.core.SNode) ResolverEntry(annis.resolver.ResolverEntry) Properties(java.util.Properties) LinkedList(java.util.LinkedList) Random(java.util.Random)

Aggregations

SNode (org.corpus_tools.salt.core.SNode)37 SToken (org.corpus_tools.salt.common.SToken)15 SRelation (org.corpus_tools.salt.core.SRelation)14 ArrayList (java.util.ArrayList)13 LinkedList (java.util.LinkedList)12 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)12 SFeature (org.corpus_tools.salt.core.SFeature)12 RelannisNodeFeature (annis.model.RelannisNodeFeature)11 HashMap (java.util.HashMap)11 SAnnotation (org.corpus_tools.salt.core.SAnnotation)8 Map (java.util.Map)6 SDocument (org.corpus_tools.salt.common.SDocument)6 HashSet (java.util.HashSet)5 SCorpusGraph (org.corpus_tools.salt.common.SCorpusGraph)5 Annotation (annis.model.Annotation)4 TreeMap (java.util.TreeMap)4 SCorpus (org.corpus_tools.salt.common.SCorpus)4 SaltProject (org.corpus_tools.salt.common.SaltProject)4 SLayer (org.corpus_tools.salt.core.SLayer)4 JSONException (org.json.JSONException)4