Search in sources :

Example 26 with IndexedWord

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

the class SemanticGraph method toRecoveredSentenceString.

public String toRecoveredSentenceString() {
    StringBuilder sb = new StringBuilder();
    boolean pastFirst = false;
    for (IndexedWord word : vertexListSorted()) {
        if (pastFirst) {
            sb.append(' ');
        }
        pastFirst = true;
        sb.append(word.word());
    }
    return sb.toString();
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 27 with IndexedWord

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

the class SemanticGraph method getAllNodesByWordPattern.

/**
   * Returns all nodes of type {@link edu.stanford.nlp.ling.IndexedWord
   * IndexedWord} in this <code>SemanticGraph</code> having the given word or
   * regex, or returns empty list if no such found.
   */
public List<IndexedWord> getAllNodesByWordPattern(String pattern) {
    Pattern p = Pattern.compile(pattern);
    List<IndexedWord> nodes = new ArrayList<>();
    for (IndexedWord vertex : vertexSet()) {
        String w = vertex.word();
        if ((w == null && pattern == null) || w != null && p.matcher(w).matches()) {
            nodes.add(vertex);
        }
    }
    return nodes;
}
Also used : Pattern(java.util.regex.Pattern) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 28 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 29 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 30 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)

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