コード例 #1
0
        public virtual void TestEnv()
        {
            SemanticGraph h = SemanticGraph.ValueOf("[married/VBN nsubjpass>Hughes/NNP auxpass>was/VBD nmod:to>Gracia/NNP]");

            h.GetFirstRoot().Set(typeof(PatternsAnnotations.PatternLabel1), "YES");
            //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 nn:daughter/NN]]]");
            string macro = "macro WORD = married";
            Env    env   = new Env();

            env.Bind("pattern1", typeof(PatternsAnnotations.PatternLabel1));
            string pattern = "({pattern1:YES}=parent >>nsubjpass {}=node)";
            IList <SemgrexPattern> pats = SemgrexBatchParser.CompileStream(new ByteArrayInputStream(Sharpen.Runtime.GetBytesForString((macro + "\n" + pattern), StandardCharsets.Utf8)), env);
            SemgrexPattern         pat3 = pats[0];
            bool           ignoreCase   = true;
            SemgrexMatcher mat3         = pat3.Matcher(h, ignoreCase);

            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, "married");
                NUnit.Framework.Assert.AreEqual(node, "Hughes");
            }
            else
            {
                throw new Exception("failed!");
            }
        }
コード例 #2
0
        public virtual void TestMacro()
        {
            SemanticGraph          h       = SemanticGraph.ValueOf("[married/VBN nsubjpass>Hughes/NNP auxpass>was/VBD nmod:to>Gracia/NNP]");
            string                 macro   = "macro WORD = married";
            string                 pattern = "({word:${WORD}}=parent >>nsubjpass {}=node)";
            IList <SemgrexPattern> pats    = SemgrexBatchParser.CompileStream(new ByteArrayInputStream(Sharpen.Runtime.GetBytesForString((macro + "\n" + pattern), StandardCharsets.Utf8)));
            SemgrexPattern         pat3    = pats[0];
            bool           ignoreCase      = true;
            SemgrexMatcher mat3            = pat3.Matcher(h, ignoreCase);

            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, "married");
                NUnit.Framework.Assert.AreEqual(node, "Hughes");
            }
            else
            {
                throw new Exception("failed!");
            }
        }