Search in sources :

Example 6 with TypedDependency

use of edu.stanford.nlp.trees.TypedDependency in project CoreNLP by stanfordnlp.

the class DependencyParser method predict.

/**
 * Determine the dependency parse of the given sentence using the loaded model.
 * You must first load a parser before calling this method.
 *
 * @throws java.lang.IllegalStateException If parser has not yet been loaded and initialized
 *         (see {@link #initialize(boolean)}
 */
public GrammaticalStructure predict(CoreMap sentence) {
    if (system == null)
        throw new IllegalStateException("Parser has not been  " + "loaded and initialized; first load a model.");
    DependencyTree result = predictInner(sentence);
    // The rest of this method is just busy-work to convert the
    // package-local representation into a CoreNLP-standard
    // GrammaticalStructure.
    List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
    List<TypedDependency> dependencies = new ArrayList<>();
    IndexedWord root = new IndexedWord(new Word("ROOT"));
    root.set(CoreAnnotations.IndexAnnotation.class, 0);
    for (int i = 1; i <= result.n; i++) {
        int head = result.getHead(i);
        String label = result.getLabel(i);
        IndexedWord thisWord = new IndexedWord(tokens.get(i - 1));
        IndexedWord headWord = head == 0 ? root : new IndexedWord(tokens.get(head - 1));
        GrammaticalRelation relation = head == 0 ? GrammaticalRelation.ROOT : makeGrammaticalRelation(label);
        dependencies.add(new TypedDependency(relation, headWord, thisWord));
    }
    // Build GrammaticalStructure
    // TODO ideally submodule should just return GrammaticalStructure
    TreeGraphNode rootNode = new TreeGraphNode(root);
    return makeGrammaticalStructure(dependencies, rootNode);
}
Also used : IndexedWord(edu.stanford.nlp.ling.IndexedWord) HasWord(edu.stanford.nlp.ling.HasWord) TaggedWord(edu.stanford.nlp.ling.TaggedWord) Word(edu.stanford.nlp.ling.Word) TypedDependency(edu.stanford.nlp.trees.TypedDependency) CoreLabel(edu.stanford.nlp.ling.CoreLabel) CoreAnnotations(edu.stanford.nlp.ling.CoreAnnotations) GrammaticalRelation(edu.stanford.nlp.trees.GrammaticalRelation) TreeGraphNode(edu.stanford.nlp.trees.TreeGraphNode) IndexedWord(edu.stanford.nlp.ling.IndexedWord)

Aggregations

TypedDependency (edu.stanford.nlp.trees.TypedDependency)6 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)4 GrammaticalStructure (edu.stanford.nlp.trees.GrammaticalStructure)4 HasWord (edu.stanford.nlp.ling.HasWord)3 IndexedWord (edu.stanford.nlp.ling.IndexedWord)3 GrammaticalRelation (edu.stanford.nlp.trees.GrammaticalRelation)3 CoreLabel (edu.stanford.nlp.ling.CoreLabel)2 TaggedWord (edu.stanford.nlp.ling.TaggedWord)2 Word (edu.stanford.nlp.ling.Word)2 Tree (edu.stanford.nlp.trees.Tree)2 TreeGraphNode (edu.stanford.nlp.trees.TreeGraphNode)2 UniversalEnglishGrammaticalStructure (edu.stanford.nlp.trees.UniversalEnglishGrammaticalStructure)2 Annotation (edu.stanford.nlp.pipeline.Annotation)1 StanfordCoreNLP (edu.stanford.nlp.pipeline.StanfordCoreNLP)1 DocumentPreprocessor (edu.stanford.nlp.process.DocumentPreprocessor)1 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)1 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)1 MaxentTagger (edu.stanford.nlp.tagger.maxent.MaxentTagger)1 EnglishGrammaticalStructure (edu.stanford.nlp.trees.EnglishGrammaticalStructure)1 GrammaticalStructureFactory (edu.stanford.nlp.trees.GrammaticalStructureFactory)1