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))); } }
public static void Main(string[] args) { Edu.Stanford.Nlp.Trees.CoordinationTransformer transformer = new Edu.Stanford.Nlp.Trees.CoordinationTransformer(null); Treebank tb = new MemoryTreebank(); Properties props = StringUtils.ArgsToProperties(args); string treeFileName = props.GetProperty("treeFile"); if (treeFileName != null) { try { ITreeReader tr = new PennTreeReader(new BufferedReader(new InputStreamReader(new FileInputStream(treeFileName))), new LabeledScoredTreeFactory()); for (Tree t; (t = tr.ReadTree()) != null;) { tb.Add(t); } } catch (IOException e) { throw new Exception("File problem: " + e); } } foreach (Tree t_1 in tb) { System.Console.Out.WriteLine("Original tree"); t_1.PennPrint(); System.Console.Out.WriteLine(); System.Console.Out.WriteLine("Tree transformed"); Tree tree = transformer.TransformTree(t_1); tree.PennPrint(); System.Console.Out.WriteLine(); System.Console.Out.WriteLine("----------------------------"); } }
// todo [cdm 2015]: These next two methods duplicate the Tree.valueOf methods! /// <summary>Simple tree reading utility method.</summary> /// <remarks>Simple tree reading utility method. Given a tree formatted as a PTB string, returns a Tree made by a specific TreeFactory.</remarks> public static Tree ReadTree(string ptbTreeString, ITreeFactory treeFactory) { try { PennTreeReader ptr = new PennTreeReader(new StringReader(ptbTreeString), treeFactory); return(ptr.ReadTree()); } catch (IOException ex) { throw new Exception(ex); } }
public virtual void TestRead() { string treeText = "(1 (2 This)) (3 (4 is) (5 a)) (6 (\\* small) (7 \\/test))"; StringReader reader = new StringReader(treeText); PennTreeReader treeReader = new PennTreeReader(reader); string[] expected = new string[] { "(1 (2 This))", "(3 (4 is) (5 a))", "(6 (* small) (7 /test))" }; for (int i = 0; i < expected.Length; ++i) { Tree tree = treeReader.ReadTree(); NUnit.Framework.Assert.IsTrue(tree != null); NUnit.Framework.Assert.AreEqual(expected[i], tree.ToString()); } Tree tree_1 = treeReader.ReadTree(); NUnit.Framework.Assert.IsFalse(tree_1 != null); }
/// <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); } }