public virtual void TestNamedRelation() { SemanticGraph graph = SemanticGraph.ValueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]"); SemgrexPattern pattern = SemgrexPattern.Compile("{idx:0}=gov >>=foo {idx:3}=dep"); SemgrexMatcher matcher = pattern.Matcher(graph); NUnit.Framework.Assert.IsTrue(matcher.Find()); NUnit.Framework.Assert.AreEqual("ate", matcher.GetNode("gov").ToString()); NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("dep").ToString()); NUnit.Framework.Assert.AreEqual("compound", matcher.GetRelnString("foo")); NUnit.Framework.Assert.IsFalse(matcher.Find()); pattern = SemgrexPattern.Compile("{idx:3}=dep <<=foo {idx:0}=gov"); matcher = pattern.Matcher(graph); NUnit.Framework.Assert.IsTrue(matcher.Find()); NUnit.Framework.Assert.AreEqual("ate", matcher.GetNode("gov").ToString()); NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("dep").ToString()); NUnit.Framework.Assert.AreEqual("dobj", matcher.GetRelnString("foo")); NUnit.Framework.Assert.IsFalse(matcher.Find()); pattern = SemgrexPattern.Compile("{idx:3}=dep <=foo {idx:2}=gov"); matcher = pattern.Matcher(graph); NUnit.Framework.Assert.IsTrue(matcher.Find()); NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("gov").ToString()); NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("dep").ToString()); NUnit.Framework.Assert.AreEqual("compound", matcher.GetRelnString("foo")); NUnit.Framework.Assert.IsFalse(matcher.Find()); pattern = SemgrexPattern.Compile("{idx:2}=gov >=foo {idx:3}=dep"); matcher = pattern.Matcher(graph); NUnit.Framework.Assert.IsTrue(matcher.Find()); NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("gov").ToString()); NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("dep").ToString()); NUnit.Framework.Assert.AreEqual("compound", matcher.GetRelnString("foo")); NUnit.Framework.Assert.IsFalse(matcher.Find()); }
public static void OutputResults(SemgrexPattern pattern, SemanticGraph graph, params string[] ignored) { System.Console.Out.WriteLine("Matching pattern " + pattern + " to\n" + graph + " :" + (pattern.Matcher(graph).Matches() ? "matches" : "doesn't match")); System.Console.Out.WriteLine(); pattern.PrettyPrint(); System.Console.Out.WriteLine(); SemgrexMatcher matcher = pattern.Matcher(graph); while (matcher.Find()) { System.Console.Out.WriteLine(" " + matcher.GetMatch()); ICollection <string> nodeNames = matcher.GetNodeNames(); if (nodeNames != null && nodeNames.Count > 0) { foreach (string name in nodeNames) { System.Console.Out.WriteLine(" " + name + ": " + matcher.GetNode(name)); } } ICollection <string> relNames = matcher.GetRelationNames(); if (relNames != null) { foreach (string name in relNames) { System.Console.Out.WriteLine(" " + name + ": " + matcher.GetRelnString(name)); } } } }