Beispiel #1
0
        /// <summary>
        /// Given a path to a file containing a list of SsurgeonPatterns, returns
        /// TODO: deal with resources
        /// </summary>
        /// <exception cref="System.Exception"/>
        public virtual IList <SsurgeonPattern> ReadFromFile(File file)
        {
            IList <SsurgeonPattern> retList = new List <SsurgeonPattern>();
            IDocument doc          = DocumentBuilderFactory.NewInstance().NewDocumentBuilder().Parse(file);
            INodeList patternNodes = doc.GetElementsByTagName(SsurgeonPattern.SsurgeonElemTag);

            for (int i = 0; i < patternNodes.GetLength(); i++)
            {
                INode node = patternNodes.Item(i);
                if (node.GetNodeType() == NodeConstants.ElementNode)
                {
                    IElement        elt     = (IElement)node;
                    SsurgeonPattern pattern = SsurgeonPatternFromXML(elt);
                    retList.Add(pattern);
                }
            }
            INodeList resourceNodes = doc.GetElementsByTagName(SsurgeonPattern.ResourceTag);

            for (int i_1 = 0; i_1 < resourceNodes.GetLength(); i_1++)
            {
                INode node = patternNodes.Item(i_1);
                if (node.GetNodeType() == NodeConstants.ElementNode)
                {
                    IElement         resourceElt = (IElement)node;
                    SsurgeonWordlist wlRsrc      = new SsurgeonWordlist(resourceElt);
                    AddResource(wlRsrc);
                }
            }
            return(retList);
        }
Beispiel #2
0
        /// <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);
        }
Beispiel #3
0
 public static string WriteToString(SsurgeonPattern pattern)
 {
     try
     {
         IList <SsurgeonPattern> patterns = new LinkedList <SsurgeonPattern>();
         patterns.Add(pattern);
         IDocument domDoc = CreatePatternXMLDoc(patterns);
         if (domDoc != null)
         {
             Transformer tformer = TransformerFactory.NewInstance().NewTransformer();
             tformer.SetOutputProperty(OutputKeys.Indent, "yes");
             StringWriter sw = new StringWriter();
             tformer.Transform(new DOMSource(domDoc), new StreamResult(sw));
             return(sw.ToString());
         }
         else
         {
             log.Warning("Was not able to create XML document for pattern list.");
         }
     }
     catch (Exception e)
     {
         log.Info("Error in writeToString, could not process pattern=" + pattern);
         log.Info(e);
         return(null);
     }
     return(string.Empty);
 }
        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]");
        }
 public virtual void SetOwningPattern(SsurgeonPattern owningPattern)
 {
     this.owningPattern = owningPattern;
 }