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)); } } } }
public virtual void TestPrettyPrint() { // SemgrexPattern pat = SemgrexPattern.compile("{} >sub {} & ?>ss {w:w}"); // SemgrexPattern pat = // SemgrexPattern.compile("({} </nsubj|agent/ {pos:/VB.*/}=hypVerb) @ ({#} </nsubj|agent/ {pos:/VB.*/}=txtVerb)"); // SemgrexPattern pat = // SemgrexPattern.compile("({sentIndex:4} <=hypReln {sentIndex:2}=hypGov) @ ({} <=hypReln ({} >conj_and ({} @ {}=hypGov)))"); SemgrexPattern pat = SemgrexPattern.Compile("({}=partnerOne [[<prep_to ({word:/married/} >nsubjpass {}=partnerTwo)] | [<nsubjpass ({word:married} >prep_to {}=partnerTwo)]]) @ ({} [[>/nn|appos/ {lemma:/wife|husband/} >poss ({}=txtPartner @ {}=partnerTwo)] | [<poss (({}=txtPartner @ {}=partnerTwo) >/appos|nn/ {lemma:/wife|husband/})]])" ); // SemgrexPattern pat = // SemgrexPattern.compile("({pos:/VB.*/}=hVerb @ {pos:/VB.*/}=tVerb) >/nsubj|nsubjpass|dobj|iobj|prep.*/=hReln ({}=hWord @ ({}=tWord [ [ >/nsubj|nsubjpass|dobj|iobj|prep.*/=tReln {}=tVerb] | [ >appos ({} >/nsubj|nsubjpass|dobj|iobj|prep.*/=tReln {}=tVerb) ] | [ <appos ({} >/nsubj|nsubjpass|dobj|iobj|prep.*/=tReln {}=tVerb)] | ![> {}=tVerb]]))"); // SemgrexPattern pat = // SemgrexPattern.compile("({}=partnerOne [[<prep_to ({word:married} >nsubjpass {}=partnerTwo)] | [<nsubjpass ({word:married} >prep_to {}=partnerTwo)]]) @ ({} [[>nn {lemma:/wife|husband/} >poss {}=txtPartner] | [<poss ({}=txtPartner >nn {lemma:/wife|husband/})]])"); // @ ({} </nsubj|agent/ {pos:/VB.*/}=txtVerb) pat.PrettyPrint(); }
public virtual void TestFind() { SemanticGraph h = SemanticGraph.ValueOf("[married/VBN nsubjpass>Hughes/NNP auxpass>was/VBD prep_to>Gracia/NNP]"); SemanticGraph t = SemanticGraph.ValueOf("[loved/VBD\nnsubj>Hughes/NNP\ndobj>[wife/NN poss>his/PRP$ appos>Gracia/NNP]\nconj_and>[obsessed/JJ\ncop>was/VBD\nadvmod>absolutely/RB\nprep_with>[Elicia/NN poss>his/PRP$ amod>little/JJ compound>daughter/NN]]]" ); string s = "(ROOT\n(S\n(NP (DT The) (NN chimney) (NNS sweeps))\n(VP (VBP do) (RB not)\n(VP (VB like)\n(S\n(VP (VBG working)\n(PP (IN on)\n(NP (DT an) (JJ empty) (NN stomach)))))))\n(. .)))"; Tree tree = Tree.ValueOf(s); SemanticGraph sg = SemanticGraphFactory.MakeFromTree(tree, SemanticGraphFactory.Mode.Collapsed, GrammaticalStructure.Extras.Maximal, null); SemgrexPattern pat = SemgrexPattern.Compile("{}=gov ![>det {}] & > {word:/^(?!not).*$/}=dep"); sg.PrettyPrint(); // SemgrexPattern pat = // SemgrexPattern.compile("{} [[<prep_to ({word:married} >nsubjpass {})] | [<nsubjpass ({word:married} >prep_to {})]]"); pat.PrettyPrint(); SemgrexMatcher mat = pat.Matcher(sg); while (mat.Find()) { // String match = mat.getMatch().word(); string gov = mat.GetNode("gov").Word(); // String reln = mat.getRelnString("reln"); string dep = mat.GetNode("dep").Word(); // System.out.println(match); System.Console.Out.WriteLine(dep + ' ' + gov); } SemgrexPattern pat2 = SemgrexPattern.Compile("{} [[>/nn|appos/ ({lemma:/wife|husband|partner/} >/poss/ {}=txtPartner)] | [<poss ({}=txtPartner >/nn|appos/ {lemma:/wife|husband|partner/})]" + "| [<nsubj ({$} >> ({word:/wife|husband|partner/} >poss {word:/his|her/} >/nn|appos/ {}))]]" ); SemgrexMatcher mat2 = pat2.Matcher(t); while (mat2.Find()) { string match = mat2.GetMatch().Word(); // String gov = mat.getNode("gov").word(); // String reln = mat.getRelnString("reln"); // String dep = mat.getNode("dep").word(); System.Console.Out.WriteLine(match); } // System.out.println(dep + " " + gov); Dictionary <IndexedWord, IndexedWord> map = new Dictionary <IndexedWord, IndexedWord>(); map[h.GetNodeByWordPattern("Hughes")] = t.GetNodeByWordPattern("Hughes"); map[h.GetNodeByWordPattern("Gracia")] = t.GetNodeByWordPattern("Gracia"); Alignment alignment = new Alignment(map, 0, string.Empty); SemgrexPattern fullPat = SemgrexPattern.Compile("({}=partnerOne [[<prep_to ({word:married} >nsubjpass {}=partnerTwo)] | [<nsubjpass ({word:married} >prep_to {}=partnerTwo)]]) @ ({} [[>/nn|appos/ ({lemma:/wife|husband|partner/} >/poss/ {}=txtPartner)] | [<poss ({}=txtPartner >/nn|appos/ {lemma:/wife|husband|partner/})]" + "| [<nsubj ({$} >> ({word:/wife|husband|partner/} >poss {word:/his|her/} >/nn|appos/ {}=txtPartner))]])"); fullPat.PrettyPrint(); SemgrexMatcher fullMat = fullPat.Matcher(h, alignment, t); if (fullMat.Find()) { System.Console.Out.WriteLine("woo: " + fullMat.GetMatch().Word()); System.Console.Out.WriteLine(fullMat.GetNode("txtPartner")); System.Console.Out.WriteLine(fullMat.GetNode("partnerOne")); System.Console.Out.WriteLine(fullMat.GetNode("partnerTwo")); } else { System.Console.Out.WriteLine("boo"); } SemgrexPattern pat3 = SemgrexPattern.Compile("({word:LIKE}=parent >>/aux.*/ {word:/do/}=node)"); System.Console.Out.WriteLine("pattern is "); pat3.PrettyPrint(); System.Console.Out.WriteLine("tree is "); sg.PrettyPrint(); //checking if ignoring case or not SemgrexMatcher mat3 = pat3.Matcher(sg, true); if (mat3.Find()) { string parent = mat3.GetNode("parent").Word(); string node = mat3.GetNode("node").Word(); System.Console.Out.WriteLine("Result: parent is " + parent + " and node is " + node); NUnit.Framework.Assert.AreEqual(parent, "like"); NUnit.Framework.Assert.AreEqual(node, "do"); } else { NUnit.Framework.Assert.Fail(); } }