Search in sources :

Example 66 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 67 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 68 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)

Example 69 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 70 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)

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