Search in sources :

Example 71 with IndexedWord

use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.

the class SemanticGraph method recToString.

// helper for toString()
private void recToString(IndexedWord curr, CoreLabel.OutputFormat wordFormat, StringBuilder sb, int offset, Set<IndexedWord> used) {
    used.add(curr);
    List<SemanticGraphEdge> edges = outgoingEdgeList(curr);
    Collections.sort(edges);
    for (SemanticGraphEdge edge : edges) {
        IndexedWord target = edge.getTarget();
        sb.append(space(2 * offset)).append("-> ").append(target.toString(wordFormat)).append(" (").append(edge.getRelation()).append(")\n");
        if (!used.contains(target)) {
            // recurse
            recToString(target, wordFormat, sb, offset + 1, used);
        }
    }
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 72 with IndexedWord

use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.

the class SemanticGraph method isDag.

/**
   *
   * @param root root node of the subgraph.
   * @return true if the subgraph rooted at <code>root</code> contains no cycles.
   */
public boolean isDag(IndexedWord root) {
    Set<IndexedWord> unused = wordMapFactory.newSet();
    unused.addAll(this.getSubgraphVertices(root));
    while (!unused.isEmpty()) {
        IndexedWord arbitrary = unused.iterator().next();
        boolean result = isDagHelper(arbitrary, unused, wordMapFactory.newSet());
        if (result) {
            return false;
        }
    }
    return true;
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 73 with IndexedWord

use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.

the class SemanticGraphFactory method deepCopyFromGraphs.

/**
   * Like makeFromGraphs, but it makes a deep copy of the graphs and
   * renumbers the index words.
   * <br>
   * {@code lengths} must be a vector containing the number of
   * tokens in each sentence.  This is used to reindex the tokens.
   */
public static SemanticGraph deepCopyFromGraphs(List<SemanticGraph> graphs, List<Integer> lengths) {
    SemanticGraph newGraph = new SemanticGraph();
    Map<Integer, IndexedWord> newWords = Generics.newHashMap();
    List<IndexedWord> newRoots = new ArrayList<>();
    int vertexOffset = 0;
    for (int i = 0; i < graphs.size(); ++i) {
        SemanticGraph graph = graphs.get(i);
        for (IndexedWord vertex : graph.vertexSet()) {
            IndexedWord newVertex = new IndexedWord(vertex);
            newVertex.setIndex(vertex.index() + vertexOffset);
            newGraph.addVertex(newVertex);
            newWords.put(newVertex.index(), newVertex);
        }
        for (SemanticGraphEdge edge : graph.edgeIterable()) {
            IndexedWord gov = newWords.get(edge.getGovernor().index() + vertexOffset);
            IndexedWord dep = newWords.get(edge.getDependent().index() + vertexOffset);
            if (gov == null || dep == null) {
                throw new AssertionError("Counting problem (or broken edge)");
            }
            newGraph.addEdge(gov, dep, edge.getRelation(), edge.getWeight(), edge.isExtra());
        }
        for (IndexedWord root : graph.getRoots()) {
            newRoots.add(newWords.get(root.index() + vertexOffset));
        }
        vertexOffset += lengths.get(i);
    }
    newGraph.setRoots(newRoots);
    return newGraph;
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 74 with IndexedWord

use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.

the class SemanticGraphFactory method makeFromEdges.

/**
   * Given a list of edges, attempts to create and return a rooted SemanticGraph.
   * <p>
   * TODO: throw Exceptions, or flag warnings on conditions for concern (no root, etc)
   */
public static SemanticGraph makeFromEdges(Iterable<SemanticGraphEdge> edges) {
    // Identify the root(s) of this graph
    SemanticGraph sg = new SemanticGraph();
    Collection<IndexedWord> vertices = getVerticesFromEdgeSet(edges);
    for (IndexedWord vertex : vertices) {
        sg.addVertex(vertex);
    }
    for (SemanticGraphEdge edge : edges) {
        sg.addEdge(edge.getSource(), edge.getTarget(), edge.getRelation(), edge.getWeight(), edge.isExtra());
    }
    sg.resetRoots();
    return sg;
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 75 with IndexedWord

use of edu.stanford.nlp.ling.IndexedWord in project CoreNLP by stanfordnlp.

the class SemanticGraphFactory method duplicateKeepNodes.

/**
   * This creates a new graph based off the given, but uses the existing nodes objects.
   */
public static SemanticGraph duplicateKeepNodes(SemanticGraph sg) {
    SemanticGraph retSg = new SemanticGraph();
    for (IndexedWord node : sg.vertexSet()) {
        retSg.addVertex(node);
    }
    retSg.setRoots(sg.getRoots());
    for (SemanticGraphEdge edge : sg.edgeIterable()) {
        retSg.addEdge(edge.getGovernor(), edge.getDependent(), edge.getRelation(), edge.getWeight(), edge.isExtra());
    }
    return retSg;
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Aggregations

IndexedWord (edu.stanford.nlp.ling.IndexedWord)204 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)55 SemanticGraphEdge (edu.stanford.nlp.semgraph.SemanticGraphEdge)53 GrammaticalRelation (edu.stanford.nlp.trees.GrammaticalRelation)41 CoreLabel (edu.stanford.nlp.ling.CoreLabel)38 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)36 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)24 SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)21 ArrayList (java.util.ArrayList)16 SemgrexPattern (edu.stanford.nlp.semgraph.semgrex.SemgrexPattern)10 Tree (edu.stanford.nlp.trees.Tree)10 Pair (edu.stanford.nlp.util.Pair)10 CoreMap (edu.stanford.nlp.util.CoreMap)8 IntPair (edu.stanford.nlp.util.IntPair)8 java.util (java.util)8 Collectors (java.util.stream.Collectors)8 Span (edu.stanford.nlp.ie.machinereading.structure.Span)7 Annotation (edu.stanford.nlp.pipeline.Annotation)6 edu.stanford.nlp.util (edu.stanford.nlp.util)6 Mention (edu.stanford.nlp.coref.data.Mention)5