Search in sources :

Example 6 with GrammaticalStructure

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

the class SemgrexDemo method main.

public static void main(String[] args) {
    String treeString = "(ROOT  (S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ likes) (S (VP (VBG eating) (NP (NN sausage))))) (. .)))";
    // Typically the tree is constructed by parsing or reading a
    // treebank.  This is just for example purposes
    Tree tree = Tree.valueOf(treeString);
    // This creates English uncollapsed dependencies as a
    // SemanticGraph.  If you are creating many SemanticGraphs, you
    // should use a GrammaticalStructureFactory and use it to generate
    // the intermediate GrammaticalStructure instead
    SemanticGraph graph = SemanticGraphFactory.generateUncollapsedDependencies(tree);
    // Alternatively, this could have been the Chinese params or any
    // other language supported.  As of 2014, only English and Chinese
    TreebankLangParserParams params = new EnglishTreebankParserParams();
    GrammaticalStructureFactory gsf = params.treebankLanguagePack().grammaticalStructureFactory(params.treebankLanguagePack().punctuationWordRejectFilter(), params.typedDependencyHeadFinder());
    GrammaticalStructure gs = gsf.newGrammaticalStructure(tree);
    log.info(graph);
    SemgrexPattern semgrex = SemgrexPattern.compile("{}=A <<nsubj {}=B");
    SemgrexMatcher matcher = semgrex.matcher(graph);
    // ancestor of both "dog" and "my" via the nsubj relation
    while (matcher.find()) {
        log.info(matcher.getNode("A") + " <<nsubj " + matcher.getNode("B"));
    }
}
Also used : SemgrexMatcher(edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher) SemgrexPattern(edu.stanford.nlp.semgraph.semgrex.SemgrexPattern) GrammaticalStructureFactory(edu.stanford.nlp.trees.GrammaticalStructureFactory) GrammaticalStructure(edu.stanford.nlp.trees.GrammaticalStructure) Tree(edu.stanford.nlp.trees.Tree) SemanticGraph(edu.stanford.nlp.semgraph.SemanticGraph) TreebankLangParserParams(edu.stanford.nlp.parser.lexparser.TreebankLangParserParams) EnglishTreebankParserParams(edu.stanford.nlp.parser.lexparser.EnglishTreebankParserParams)

Aggregations

GrammaticalStructure (edu.stanford.nlp.trees.GrammaticalStructure)6 Tree (edu.stanford.nlp.trees.Tree)3 TypedDependency (edu.stanford.nlp.trees.TypedDependency)3 HasWord (edu.stanford.nlp.ling.HasWord)2 TaggedWord (edu.stanford.nlp.ling.TaggedWord)2 DocumentPreprocessor (edu.stanford.nlp.process.DocumentPreprocessor)2 SemanticGraph (edu.stanford.nlp.semgraph.SemanticGraph)2 MaxentTagger (edu.stanford.nlp.tagger.maxent.MaxentTagger)2 GrammaticalStructureFactory (edu.stanford.nlp.trees.GrammaticalStructureFactory)2 CoreAnnotations (edu.stanford.nlp.ling.CoreAnnotations)1 CoreLabel (edu.stanford.nlp.ling.CoreLabel)1 IndexedWord (edu.stanford.nlp.ling.IndexedWord)1 EnglishTreebankParserParams (edu.stanford.nlp.parser.lexparser.EnglishTreebankParserParams)1 TreebankLangParserParams (edu.stanford.nlp.parser.lexparser.TreebankLangParserParams)1 DependencyParser (edu.stanford.nlp.parser.nndep.DependencyParser)1 SemanticGraphCoreAnnotations (edu.stanford.nlp.semgraph.SemanticGraphCoreAnnotations)1 SemgrexMatcher (edu.stanford.nlp.semgraph.semgrex.SemgrexMatcher)1 SemgrexPattern (edu.stanford.nlp.semgraph.semgrex.SemgrexPattern)1 EnglishGrammaticalStructure (edu.stanford.nlp.trees.EnglishGrammaticalStructure)1 PennTreebankLanguagePack (edu.stanford.nlp.trees.PennTreebankLanguagePack)1