// just static 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 ITreebankLangParserParams @params = new EnglishTreebankParserParams(); IGrammaticalStructureFactory 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); // This will produce two results on the given tree: "likes" is an // ancestor of both "dog" and "my" via the nsubj relation while (matcher.Find()) { log.Info(matcher.GetNode("A") + " <<nsubj " + matcher.GetNode("B")); } }
// static methods /// <summary> /// Put the tree in the CoreMap for the sentence, also add any /// dependency graphs to the sentence, and fill in missing tag annotations. /// </summary> /// <remarks> /// Put the tree in the CoreMap for the sentence, also add any /// dependency graphs to the sentence, and fill in missing tag annotations. /// Thread safety note: nothing special is done to ensure the thread /// safety of the GrammaticalStructureFactory. However, both the /// EnglishGrammaticalStructureFactory and the /// ChineseGrammaticalStructureFactory are thread safe. /// </remarks> public static void FillInParseAnnotations(bool verbose, bool buildGraphs, IGrammaticalStructureFactory gsf, ICoreMap sentence, IList <Tree> trees, GrammaticalStructure.Extras extras) { bool first = true; foreach (Tree tree in trees) { // make sure all tree nodes are CoreLabels // TODO: why isn't this always true? something fishy is going on Edu.Stanford.Nlp.Trees.Trees.ConvertToCoreLabels(tree); // index nodes, i.e., add start and end token positions to all nodes // this is needed by other annotators down stream, e.g., the NFLAnnotator tree.IndexSpans(0); if (first) { sentence.Set(typeof(TreeCoreAnnotations.TreeAnnotation), tree); if (verbose) { log.Info("Tree is:"); tree.PennPrint(System.Console.Error); } SetMissingTags(sentence, tree); if (buildGraphs) { // generate the dependency graph // unfortunately, it is necessary to make the // GrammaticalStructure three times, as the dependency // conversion changes the given data structure SemanticGraph deps = SemanticGraphFactory.GenerateCollapsedDependencies(gsf.NewGrammaticalStructure(tree), extras); SemanticGraph uncollapsedDeps = SemanticGraphFactory.GenerateUncollapsedDependencies(gsf.NewGrammaticalStructure(tree), extras); SemanticGraph ccDeps = SemanticGraphFactory.GenerateCCProcessedDependencies(gsf.NewGrammaticalStructure(tree), extras); SemanticGraph enhancedDeps = SemanticGraphFactory.GenerateEnhancedDependencies(gsf.NewGrammaticalStructure(tree)); SemanticGraph enhancedPlusPlusDeps = SemanticGraphFactory.GenerateEnhancedPlusPlusDependencies(gsf.NewGrammaticalStructure(tree)); if (verbose) { log.Info("SDs:"); log.Info(deps.ToString(SemanticGraph.OutputFormat.List)); } sentence.Set(typeof(SemanticGraphCoreAnnotations.CollapsedDependenciesAnnotation), deps); sentence.Set(typeof(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation), uncollapsedDeps); sentence.Set(typeof(SemanticGraphCoreAnnotations.CollapsedCCProcessedDependenciesAnnotation), ccDeps); sentence.Set(typeof(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation), enhancedDeps); sentence.Set(typeof(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation), enhancedPlusPlusDeps); } first = false; } } if (trees.Count > 1) { sentence.Set(typeof(TreeCoreAnnotations.KBestTreesAnnotation), trees); } }