示例#1
0
        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));
                    }
                }
            }
        }
示例#2
0
        public virtual void TestNamedNode()
        {
            SemanticGraph graph = MakeComplicatedGraph();

            RunTest("{} >dobj ({} >expl {})", graph, "A");
            SemgrexPattern pattern = SemgrexPattern.Compile("{} >dobj ({} >expl {}=foo)");
            SemgrexMatcher matcher = pattern.Matcher(graph);

            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(1, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("E", matcher.GetNode("foo").ToString());
            NUnit.Framework.Assert.AreEqual("A", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.IsFalse(matcher.Find());
            pattern = SemgrexPattern.Compile("{} >dobj ({} >expl {}=foo) >mod {}");
            matcher = pattern.Matcher(graph);
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(1, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("E", matcher.GetNode("foo").ToString());
            NUnit.Framework.Assert.AreEqual("A", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.IsFalse(matcher.Find());
            pattern = SemgrexPattern.Compile("{} >dobj ({} >expl {}=foo) >mod ({} >mark {})");
            matcher = pattern.Matcher(graph);
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(1, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("E", matcher.GetNode("foo").ToString());
            NUnit.Framework.Assert.AreEqual("A", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.IsFalse(matcher.Find());
            pattern = SemgrexPattern.Compile("{} >dobj ({} >expl {}=foo) >mod ({} > {})");
            matcher = pattern.Matcher(graph);
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(1, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("E", matcher.GetNode("foo").ToString());
            NUnit.Framework.Assert.AreEqual("A", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.IsFalse(matcher.Find());
            pattern = SemgrexPattern.Compile("{} >dobj ({} >expl {}=foo) >mod ({} > {}=foo)");
            matcher = pattern.Matcher(graph);
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(1, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("E", matcher.GetNode("foo").ToString());
            NUnit.Framework.Assert.AreEqual("A", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.IsFalse(matcher.Find());
            pattern = SemgrexPattern.Compile("{} >dobj ({} >expl {}=foo) >mod ({}=foo > {})");
            matcher = pattern.Matcher(graph);
            NUnit.Framework.Assert.IsFalse(matcher.Find());
        }
示例#3
0
        public virtual void TestEqualsRelation()
        {
            SemanticGraph  graph   = SemanticGraph.ValueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
            SemgrexPattern pattern = SemgrexPattern.Compile("{} >> ({}=a == {}=b)");
            SemgrexMatcher matcher = pattern.Matcher(graph);

            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsFalse(matcher.Find());
            // This split pattern should also work
            pattern = SemgrexPattern.Compile("{} >> {}=a >> {}=b : {}=a == {}=b");
            matcher = pattern.Matcher(graph);
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsFalse(matcher.Find());
        }
示例#4
0
        public virtual void TestInitialConditions()
        {
            SemanticGraph  graph   = MakeComplicatedGraph();
            SemgrexPattern pattern = SemgrexPattern.Compile("{}=a >> {}=b : {}=a >> {}=c");
            IDictionary <string, IndexedWord> variables = new Dictionary <string, IndexedWord>();

            variables["b"] = graph.GetNodeByIndex(5);
            variables["c"] = graph.GetNodeByIndex(2);
            SemgrexMatcher matcher = pattern.Matcher(graph, variables);

            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(3, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("A", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("E", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.AreEqual("B", matcher.GetNode("c").ToString());
            NUnit.Framework.Assert.AreEqual("A", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.IsFalse(matcher.Find());
        }
示例#5
0
        public virtual void TestNotEquals()
        {
            SemanticGraph  graph   = SemanticGraph.ValueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
            SemgrexPattern pattern = SemgrexPattern.Compile("{} >> {}=a >> {}=b : {}=a !== {}=b");
            SemgrexMatcher matcher = pattern.Matcher(graph);

            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsFalse(matcher.Find());
            // same as the first test, essentially, but with a more compact expression
            pattern = SemgrexPattern.Compile("{} >> {}=a >> ({}=b !== {}=a)");
            matcher = pattern.Matcher(graph);
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("Bill", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsTrue(matcher.Find());
            NUnit.Framework.Assert.AreEqual(2, matcher.GetNodeNames().Count);
            NUnit.Framework.Assert.AreEqual("ate", matcher.GetMatch().ToString());
            NUnit.Framework.Assert.AreEqual("blueberry", matcher.GetNode("a").ToString());
            NUnit.Framework.Assert.AreEqual("muffins", matcher.GetNode("b").ToString());
            NUnit.Framework.Assert.IsFalse(matcher.Find());
        }
示例#6
0
        /// <summary>Prints out all matches of a semgrex pattern on a file of dependencies.</summary>
        /// <remarks>
        /// Prints out all matches of a semgrex pattern on a file of dependencies.
        /// <p>
        /// Usage:<br />
        /// java edu.stanford.nlp.semgraph.semgrex.SemgrexPattern [args]
        /// <br />
        /// See the help() function for a list of possible arguments to provide.
        /// </remarks>
        /// <exception cref="System.IO.IOException"/>
        public static void Main(string[] args)
        {
            IDictionary <string, int> flagMap = Generics.NewHashMap();

            flagMap[Pattern]            = 1;
            flagMap[TreeFile]           = 1;
            flagMap[Mode]               = 1;
            flagMap[Extras]             = 1;
            flagMap[ConlluFile]         = 1;
            flagMap[OutputFormatOption] = 1;
            IDictionary <string, string[]> argsMap = StringUtils.ArgsToMap(args, flagMap);

            // args = argsMap.get(null);
            // TODO: allow patterns to be extracted from a file
            if (!(argsMap.Contains(Pattern)) || argsMap[Pattern].Length == 0)
            {
                Help();
                System.Environment.Exit(2);
            }
            Edu.Stanford.Nlp.Semgraph.Semgrex.SemgrexPattern semgrex = Edu.Stanford.Nlp.Semgraph.Semgrex.SemgrexPattern.Compile(argsMap[Pattern][0]);
            string modeString = DefaultMode;

            if (argsMap.Contains(Mode) && argsMap[Mode].Length > 0)
            {
                modeString = argsMap[Mode][0].ToUpper();
            }
            SemanticGraphFactory.Mode mode = SemanticGraphFactory.Mode.ValueOf(modeString);
            string outputFormatString      = DefaultOutputFormat;

            if (argsMap.Contains(OutputFormatOption) && argsMap[OutputFormatOption].Length > 0)
            {
                outputFormatString = argsMap[OutputFormatOption][0].ToUpper();
            }
            SemgrexPattern.OutputFormat outputFormat = SemgrexPattern.OutputFormat.ValueOf(outputFormatString);
            bool useExtras = true;

            if (argsMap.Contains(Extras) && argsMap[Extras].Length > 0)
            {
                useExtras = bool.ValueOf(argsMap[Extras][0]);
            }
            IList <SemanticGraph> graphs = Generics.NewArrayList();

            // TODO: allow other sources of graphs, such as dependency files
            if (argsMap.Contains(TreeFile) && argsMap[TreeFile].Length > 0)
            {
                foreach (string treeFile in argsMap[TreeFile])
                {
                    log.Info("Loading file " + treeFile);
                    MemoryTreebank treebank = new MemoryTreebank(new TreeNormalizer());
                    treebank.LoadPath(treeFile);
                    foreach (Tree tree in treebank)
                    {
                        // TODO: allow other languages... this defaults to English
                        SemanticGraph graph = SemanticGraphFactory.MakeFromTree(tree, mode, useExtras ? GrammaticalStructure.Extras.Maximal : GrammaticalStructure.Extras.None);
                        graphs.Add(graph);
                    }
                }
            }
            if (argsMap.Contains(ConlluFile) && argsMap[ConlluFile].Length > 0)
            {
                CoNLLUDocumentReader reader = new CoNLLUDocumentReader();
                foreach (string conlluFile in argsMap[ConlluFile])
                {
                    log.Info("Loading file " + conlluFile);
                    IEnumerator <SemanticGraph> it = reader.GetIterator(IOUtils.ReaderFromString(conlluFile));
                    while (it.MoveNext())
                    {
                        SemanticGraph graph = it.Current;
                        graphs.Add(graph);
                    }
                }
            }
            foreach (SemanticGraph graph_1 in graphs)
            {
                SemgrexMatcher matcher = semgrex.Matcher(graph_1);
                if (!matcher.Find())
                {
                    continue;
                }
                if (outputFormat == SemgrexPattern.OutputFormat.List)
                {
                    log.Info("Matched graph:" + Runtime.LineSeparator() + graph_1.ToString(SemanticGraph.OutputFormat.List));
                    int  i     = 1;
                    bool found = true;
                    while (found)
                    {
                        log.Info("Match " + i + " at: " + matcher.GetMatch().ToString(CoreLabel.OutputFormat.ValueIndex));
                        IList <string> nodeNames = Generics.NewArrayList();
                        Sharpen.Collections.AddAll(nodeNames, matcher.GetNodeNames());
                        nodeNames.Sort();
                        foreach (string name in nodeNames)
                        {
                            log.Info("  " + name + ": " + matcher.GetNode(name).ToString(CoreLabel.OutputFormat.ValueIndex));
                        }
                        log.Info(" ");
                        found = matcher.Find();
                    }
                }
                else
                {
                    if (outputFormat == SemgrexPattern.OutputFormat.Offset)
                    {
                        if (graph_1.VertexListSorted().IsEmpty())
                        {
                            continue;
                        }
                        System.Console.Out.Printf("+%d %s%n", graph_1.VertexListSorted()[0].Get(typeof(CoreAnnotations.LineNumberAnnotation)), argsMap[ConlluFile][0]);
                    }
                }
            }
        }