Search in sources :

Example 21 with IndexedWord

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

the class SemanticGraphUtils method setSentIndex.

/**
   * GIven a graph, returns a new graph with the the new sentence index enforced.
   * NOTE: new vertices are inserted.
   * TODO: is this ok?  rewrite this?
   */
public static SemanticGraph setSentIndex(SemanticGraph sg, int newSentIndex) {
    SemanticGraph newGraph = new SemanticGraph(sg);
    List<IndexedWord> prevRoots = new ArrayList<>(newGraph.getRoots());
    List<IndexedWord> newRoots = new ArrayList<>();
    // vertices while iterating.  Perhaps there is a better way to do it.
    for (IndexedWord node : newGraph.vertexListSorted()) {
        IndexedWord newWord = new IndexedWord(node);
        newWord.setSentIndex(newSentIndex);
        SemanticGraphUtils.replaceNode(newWord, node, newGraph);
        if (prevRoots.contains(node))
            newRoots.add(newWord);
    }
    newGraph.setRoots(newRoots);
    return newGraph;
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 22 with IndexedWord

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

the class SemanticGraphUtils method killNonRooted.

/**
   * Deletes all nodes that are not rooted (such as dangling vertices after a series of
   * edges have been chopped).
   */
public static void killNonRooted(SemanticGraph sg) {
    List<IndexedWord> nodes = new ArrayList<>(sg.vertexSet());
    // Hack: store all of the nodes we know are in the rootset
    Set<IndexedWord> guaranteed = Generics.newHashSet();
    for (IndexedWord root : sg.getRoots()) {
        guaranteed.add(root);
        guaranteed.addAll(sg.descendants(root));
    }
    for (IndexedWord node : nodes) {
        if (!guaranteed.contains(node)) {
            sg.removeVertex(node);
        }
    }
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 23 with IndexedWord

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

the class SemanticGraphUtils method semgrexFromGraph.

/**
   * nodeValuesTranformation is a function that converts a vertex (IndexedWord) to the value.
   * For an example, see {@code semgrexFromGraph}
   * function implementations (if useWord and useTag is true, the value is "{word: vertex.word; tag: vertex.tag}").
   * @throws Exception
   */
public static String semgrexFromGraph(SemanticGraph sg, Collection<IndexedWord> wildcardNodes, Map<IndexedWord, String> nodeNameMap, Function<IndexedWord, String> wordTransformation) throws Exception {
    IndexedWord patternRoot = sg.getFirstRoot();
    StringWriter buf = new StringWriter();
    Set<IndexedWord> tabu = Generics.newHashSet();
    Set<SemanticGraphEdge> seenEdges = Generics.newHashSet();
    buf.append(semgrexFromGraphHelper(patternRoot, sg, tabu, seenEdges, true, true, wildcardNodes, nodeNameMap, false, wordTransformation));
    String patternString = buf.toString();
    return patternString;
}
Also used : StringWriter(java.io.StringWriter) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 24 with IndexedWord

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

the class SemanticGraph method getVerticesWithoutParents.

/**
   * Initially looks for nodes which have no incoming arcs. If there are any, it
   * returns a list of them. If not, it looks for nodes from which every other
   * node is reachable. If there are any, it returns a list of them. Otherwise,
   * it returns an empty list.
   *
   * @return A list of root nodes or an empty list.
   */
private List<IndexedWord> getVerticesWithoutParents() {
    List<IndexedWord> result = new ArrayList<>();
    for (IndexedWord v : vertexSet()) {
        int inDegree = inDegree(v);
        if (inDegree == 0) {
            result.add(v);
        }
    }
    Collections.sort(result);
    return result;
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Example 25 with IndexedWord

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

the class SemanticGraph method toEnUncollapsedSentenceString.

/**
   * Similar to <code>toRecoveredString</code>, but will fill in words that were
   * collapsed into relations (i.e. prep_for --> 'for'). Mostly to deal with
   * collapsed dependency trees.
   *
   * TODO: consider merging with toRecoveredString() NOTE: assumptions currently
   * are for English. NOTE: currently takes immediate successors to current word
   * and expands them. This assumption may not be valid for other conditions or
   * languages?
   */
public String toEnUncollapsedSentenceString() {
    List<IndexedWord> uncompressedList = Generics.newLinkedList(vertexSet());
    List<Pair<String, IndexedWord>> specifics = Generics.newArrayList();
    // to avoid concurrent modification exceptions.
    for (IndexedWord word : vertexSet()) {
        for (SemanticGraphEdge edge : getIncomingEdgesSorted(word)) {
            GrammaticalRelation relation = edge.getRelation();
            // Extract the specific: need to account for possibility that relation
            // can
            // be a String or GrammaticalRelation (how did it happen this way?)
            String specific = relation.getSpecific();
            if (specific == null) {
                if (edge.getRelation().equals(EnglishGrammaticalRelations.AGENT)) {
                    specific = "by";
                }
            }
            // this node.
            if (specific != null) {
                Pair<String, IndexedWord> specPair = new Pair<>(specific, word);
                specifics.add(specPair);
            }
        }
    }
    for (Pair<String, IndexedWord> tuple : specifics) {
        insertSpecificIntoList(tuple.first(), tuple.second(), uncompressedList);
    }
    return StringUtils.join(uncompressedList, " ");
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord) Pair(edu.stanford.nlp.util.Pair)

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