Пример #1
0
        public virtual void TestMatchAll()
        {
            SemanticGraph             graph   = SemanticGraph.ValueOf("[ate subj>Bill dobj>[muffins compound>blueberry]]");
            ICollection <IndexedWord> words   = graph.VertexSet();
            SemgrexPattern            pattern = SemgrexPattern.Compile("{}");
            SemgrexMatcher            matcher = pattern.Matcher(graph);

            string[] expectedMatches = new string[] { "ate", "Bill", "muffins", "blueberry" };
            for (int i = 0; i < expectedMatches.Length; ++i)
            {
                NUnit.Framework.Assert.IsTrue(matcher.FindNextMatchingNode());
            }
            NUnit.Framework.Assert.IsFalse(matcher.FindNextMatchingNode());
        }
Пример #2
0
        public static void RunTest(SemgrexPattern pattern, SemanticGraph graph, params string[] expectedMatches)
        {
            // results are not in the order I would expect.  Using a counter
            // allows them to be in any order
            IntCounter <string> counts = new IntCounter <string>();

            for (int i = 0; i < expectedMatches.Length; ++i)
            {
                counts.IncrementCount(expectedMatches[i]);
            }
            IntCounter <string> originalCounts = new IntCounter <string>(counts);
            SemgrexMatcher      matcher        = pattern.Matcher(graph);

            for (int i_1 = 0; i_1 < expectedMatches.Length; ++i_1)
            {
                if (!matcher.Find())
                {
                    throw new AssertionFailedError("Expected " + expectedMatches.Length + " matches for pattern " + pattern + " on " + graph + ", only got " + i_1);
                }
                string match = matcher.GetMatch().ToString();
                if (!counts.ContainsKey(match))
                {
                    throw new AssertionFailedError("Unexpected match " + match + " for pattern " + pattern + " on " + graph);
                }
                counts.DecrementCount(match);
                if (counts.GetCount(match) < 0)
                {
                    throw new AssertionFailedError("Found too many matches for " + match + " for pattern " + pattern + " on " + graph);
                }
            }
            if (matcher.FindNextMatchingNode())
            {
                throw new AssertionFailedError("Found more than " + expectedMatches.Length + " matches for pattern " + pattern + " on " + graph + "... extra match is " + matcher.GetMatch());
            }
        }