public virtual void TestEnglishDependenciesByTree() { // the trees to test string[] testTrees = new string[] { "((S (NP (NNP Sam)) (VP (VBD died) (NP-TMP (NN today)))))", "(ROOT (S (NP (PRP I)) (VP (VBD saw) (NP (NP (DT the) (NN book)) (SBAR (WHNP (WDT which)) (S (NP (PRP you)) (VP (VBD bought)))))) (. .)))" }; // the expected dependency answers (basic) string[] testAnswers = new string[] { "root(ROOT-0, died-2) nsubj(died-2, Sam-1) tmod(died-2, today-3)", "nsubj(saw-2, I-1) root(ROOT-0, saw-2) det(book-4, the-3) dobj(saw-2, book-4) dobj(bought-7, which-5) ref(book-4, which-5) dobj(bought-7, which-5) nsubj(bought-7, you-6) rcmod(book-4, bought-7)" }; // the expected dependency answers (collapsed dependencies) string[] testAnswersCollapsed = new string[] { "root(ROOT-0, died-2) nsubj(died-2, Sam-1) tmod(died-2, today-3)", "nsubj(saw-2, I-1) root(ROOT-0, saw-2) det(book-4, the-3) dobj(saw-2, book-4) dobj(bought-7, book-4) nsubj(bought-7, you-6) rcmod(book-4, bought-7)" }; // the expected dependency answers (conjunctions processed) string[] testAnswersCCProcessed = new string[] { "root(ROOT-0, died-2) nsubj(died-2, Sam-1) tmod(died-2, today-3)", "nsubj(saw-2, I-1) root(ROOT-0, saw-2) det(book-4, the-3) dobj(saw-2, book-4) dobj(bought-7, book-4) nsubj(bought-7, you-6) rcmod(book-4, bought-7)" }; for (int i = 0; i < testTrees.Length; i++) { string testTree = testTrees[i]; string testAnswer = testAnswers[i]; string testAnswerCollapsed = testAnswersCollapsed[i]; string testAnswerCCProcessed = testAnswersCCProcessed[i]; HashSet <string> testAnswerTokens = TokenSet(testAnswer); HashSet <string> testAnswerCollapsedTokens = TokenSet(testAnswerCollapsed); HashSet <string> testAnswerCCProcessedTokens = TokenSet(testAnswerCCProcessed); Tree tree; try { tree = new PennTreeReader(new StringReader(testTree), new LabeledScoredTreeFactory()).ReadTree(); } catch (IOException e) { // these trees should all parse correctly throw new Exception(e); } GrammaticalStructure gs = new EnglishGrammaticalStructure(tree); NUnit.Framework.Assert.AreEqual("Unexpected basic dependencies for tree " + testTree, testAnswerTokens, TokenSet(gs.TypedDependencies(GrammaticalStructure.Extras.Maximal))); NUnit.Framework.Assert.AreEqual("Unexpected collapsed dependencies for tree " + testTree, testAnswerCollapsedTokens, TokenSet(gs.TypedDependenciesCollapsed(GrammaticalStructure.Extras.Maximal))); NUnit.Framework.Assert.AreEqual("Unexpected cc-processed dependencies for tree " + testTree, testAnswerCCProcessedTokens, TokenSet(gs.TypedDependenciesCCprocessed(GrammaticalStructure.Extras.Maximal))); } }
/// <summary> /// Returns a new /// <c>SemanticGraph</c> /// constructed from a given /// <see cref="Edu.Stanford.Nlp.Trees.Tree"/> /// with given options. /// This factory method is intended to replace a profusion of highly similar /// factory methods, such as /// <c>typedDependencies()</c> /// , /// <c>typedDependenciesCollapsed()</c> /// , /// <c>allTypedDependencies()</c> /// , /// <c>allTypedDependenciesCollapsed()</c> /// , etc. /// For a fuller explanation of the meaning of the boolean arguments, see /// <see cref="Edu.Stanford.Nlp.Trees.GrammaticalStructure"/> /// . /// </summary> /// <param name="tree">A tree representing a phrase structure parse</param> /// <param name="includeExtras"> /// Whether to include extra dependencies, which may /// result in a non-tree /// </param> /// <param name="filter">A filter to exclude certain dependencies; ignored if null</param> /// <param name="originalDependencies"> /// generate original Stanford dependencies instead of new /// Universal Dependencies /// </param> /// <returns>A SemanticGraph</returns> public static SemanticGraph MakeFromTree(Tree tree, SemanticGraphFactory.Mode mode, GrammaticalStructure.Extras includeExtras, IPredicate <TypedDependency> filter, bool originalDependencies, bool includePunctuationDependencies) { GrammaticalStructure gs; if (originalDependencies) { IPredicate <string> wordFilt; if (includePunctuationDependencies) { wordFilt = Filters.AcceptFilter(); } else { wordFilt = new PennTreebankLanguagePack().PunctuationWordRejectFilter(); } gs = new EnglishGrammaticalStructure(tree, wordFilt, new SemanticHeadFinder(true)); } else { IPredicate <string> tagFilt; if (includePunctuationDependencies) { tagFilt = Filters.AcceptFilter(); } else { tagFilt = new PennTreebankLanguagePack().PunctuationTagRejectFilter(); } gs = new UniversalEnglishGrammaticalStructure(tree, tagFilt, new UniversalSemanticHeadFinder(true)); } return(MakeFromTree(gs, mode, includeExtras, filter)); }
/// <summary>Just for testing.</summary> public static void Main(string[] args) { try { ITreeReader tr = new PennTreeReader(new StringReader("(S (NP (NNP Sam)) (VP (VBD died) (NP (NN today))))"), new LabeledScoredTreeFactory()); Tree t = tr.ReadTree(); System.Console.Out.WriteLine(t); TreeGraphNode tgn = new TreeGraphNode(t, (TreeGraphNode)null); System.Console.Out.WriteLine(tgn.ToPrettyString(0)); EnglishGrammaticalStructure gs = new EnglishGrammaticalStructure(tgn); System.Console.Out.WriteLine(tgn.ToPrettyString(0)); tgn.PercolateHeads(new SemanticHeadFinder()); System.Console.Out.WriteLine(tgn.ToPrettyString(0)); } catch (Exception e) { log.Error("Horrible error: " + e); log.Error(e); } }