/// <summary> /// Given the root Element for a SemgrexPattern (SSURGEON_ELEM_TAG), converts /// it into its corresponding SemgrexPattern object. /// </summary> /// <exception cref="System.Exception"/> public static SsurgeonPattern SsurgeonPatternFromXML(IElement elt) { string uid = GetTagText(elt, SsurgeonPattern.UidElemTag); string notes = GetTagText(elt, SsurgeonPattern.NotesElemTag); string semgrexString = GetTagText(elt, SsurgeonPattern.SemgrexElemTag); SemgrexPattern semgrexPattern = SemgrexPattern.Compile(semgrexString); SsurgeonPattern retPattern = new SsurgeonPattern(uid, semgrexPattern); retPattern.SetNotes(notes); INodeList editNodes = elt.GetElementsByTagName(SsurgeonPattern.EditListElemTag); for (int i = 0; i < editNodes.GetLength(); i++) { INode node = editNodes.Item(i); if (node.GetNodeType() == NodeConstants.ElementNode) { IElement editElt = (IElement)node; string editVal = GetEltText(editElt); retPattern.AddEdit(Edu.Stanford.Nlp.Semgraph.Semgrex.Ssurgeon.Ssurgeon.ParseEditLine(editVal)); } } // If predicate available, parse IElement predElt = GetFirstTag(elt, SsurgeonPattern.PredicateTag); if (predElt != null) { ISsurgPred pred = AssemblePredFromXML(GetFirstChildElement(predElt)); retPattern.SetPredicate(pred); } return(retPattern); }
public virtual void SimpleTest() { SemanticGraph sg = SemanticGraph.ValueOf("[mixed/VBN nsubj>[Joe/NNP appos>[bartender/NN det>the/DT]] dobj>[drink/NN det>a/DT]]"); SemgrexPattern semgrexPattern = SemgrexPattern.Compile("{}=a1 >appos=e1 {}=a2 <nsubj=e2 {}=a3"); SsurgeonPattern pattern = new SsurgeonPattern(semgrexPattern); System.Console.Out.WriteLine("Start = " + sg.ToCompactString()); // Find and snip the appos and root to nsubj links SsurgeonEdit apposSnip = new RemoveNamedEdge("e1", "a1", "a2"); pattern.AddEdit(apposSnip); SsurgeonEdit nsubjSnip = new RemoveNamedEdge("e2", "a3", "a1"); pattern.AddEdit(nsubjSnip); // Attach Joe to be the nsubj of bartender SsurgeonEdit reattachSubj = new AddEdge("a2", "a1", EnglishGrammaticalRelations.NominalSubject); pattern.AddEdit(reattachSubj); // Attach copula IndexedWord isNode = new IndexedWord(); isNode.Set(typeof(CoreAnnotations.TextAnnotation), "is"); isNode.Set(typeof(CoreAnnotations.LemmaAnnotation), "is"); isNode.Set(typeof(CoreAnnotations.OriginalTextAnnotation), "is"); isNode.Set(typeof(CoreAnnotations.PartOfSpeechAnnotation), "VBN"); SsurgeonEdit addCopula = new AddDep("a2", EnglishGrammaticalRelations.Copula, isNode); pattern.AddEdit(addCopula); // Destroy subgraph SsurgeonEdit destroySubgraph = new DeleteGraphFromNode("a3"); pattern.AddEdit(destroySubgraph); // Process and output modified ICollection <SemanticGraph> newSgs = pattern.Execute(sg); foreach (SemanticGraph newSg in newSgs) { System.Console.Out.WriteLine("Modified = " + newSg.ToCompactString()); } string firstGraphString = newSgs.GetEnumerator().Current.ToCompactString().Trim(); NUnit.Framework.Assert.AreEqual(firstGraphString, "[bartender cop>is nsubj>Joe det>the]"); }