当前位置: 首页>>代码示例>>C#>>正文


C# Matcher.IsMatch方法代码示例

本文整理汇总了C#中Matcher.IsMatch方法的典型用法代码示例。如果您正苦于以下问题:C# Matcher.IsMatch方法的具体用法?C# Matcher.IsMatch怎么用?C# Matcher.IsMatch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Matcher的用法示例。


在下文中一共展示了Matcher.IsMatch方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Subpattern

        public void Subpattern()
        {
            AnnotatedStringData sentence = CreateStringData("the old, angry man slept well.");

            Pattern<AnnotatedStringData, int> pattern = Pattern<AnnotatedStringData, int>.New()
                .Subpattern("unvoiceInitial", unvoiceInitial => unvoiceInitial
                    .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("voice-").Symbol("son-").Value)
                    .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl-").Value).ZeroOrMore
                    .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl+").Value))
                .Subpattern("word", word => word
                    .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl-").Value).ZeroOrMore
                    .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl+").Value)
                    .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl-").Value).ZeroOrMore).Value;

            var matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern);
            Assert.IsTrue(matcher.IsMatch(sentence));
            Assert.IsTrue(matcher.IsMatch(sentence, 19));
            Assert.IsFalse(matcher.IsMatch(sentence, 29));

            Match<AnnotatedStringData, int> match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(0, 3), match.Span);
            Assert.AreEqual("unvoiceInitial", match.PatternPath[0]);
            match = matcher.Match(sentence, 19);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(19, 22), match.Span);
            Assert.AreEqual("unvoiceInitial", match.PatternPath[0]);
            match = matcher.Match(sentence, 29);
            Assert.IsFalse(match.Success);

            Match<AnnotatedStringData, int>[] matches = matcher.Matches(sentence).ToArray();
            Assert.AreEqual(6, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 3), matches[0].Span);
            Assert.AreEqual("unvoiceInitial", matches[0].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(4, 7), matches[1].Span);
            Assert.AreEqual("word", matches[1].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(25, 29), matches[5].Span);
            Assert.AreEqual("word", matches[5].PatternPath[0]);
            matches = matcher.Matches(sentence, 19).ToArray();
            Assert.AreEqual(2, matches.Length);
            Assert.AreEqual(SpanFactory.Create(19, 22), matches[0].Span);
            Assert.AreEqual("unvoiceInitial", matches[0].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(25, 29), matches[1].Span);
            Assert.AreEqual("word", matches[1].PatternPath[0]);
            matches = matcher.Matches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            matches = matcher.AllMatches(sentence).ToArray();
            Assert.AreEqual(32, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 3), matches[0].Span);
            Assert.AreEqual("unvoiceInitial", matches[0].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(0, 3), matches[1].Span);
            Assert.AreEqual("word", matches[1].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(1, 3), matches[2].Span);
            Assert.AreEqual("word", matches[2].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(16, 17), matches[15].Span);
            Assert.AreEqual("word", matches[15].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(26, 27), matches[31].Span);
            Assert.AreEqual("word", matches[31].PatternPath[0]);
            matches = matcher.AllMatches(sentence, 19).ToArray();
            Assert.AreEqual(16, matches.Length);
            Assert.AreEqual(SpanFactory.Create(19, 22), matches[0].Span);
            Assert.AreEqual("unvoiceInitial", matches[0].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(19, 24), matches[1].Span);
            Assert.AreEqual("word", matches[1].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(19, 23), matches[2].Span);
            Assert.AreEqual("word", matches[2].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(21, 24), matches[7].Span);
            Assert.AreEqual("word", matches[7].PatternPath[0]);
            Assert.AreEqual(SpanFactory.Create(26, 27), matches[15].Span);
            Assert.AreEqual("word", matches[15].PatternPath[0]);
            matches = matcher.AllMatches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);
        }
开发者ID:bbriggs,项目名称:machine,代码行数:74,代码来源:MatcherTests.cs

示例2: SimplePattern

        public void SimplePattern()
        {
            AnnotatedStringData sentence = CreateStringData("the old, angry man slept well.");

            Pattern<AnnotatedStringData, int> pattern = Pattern<AnnotatedStringData, int>.New()
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl-").Value).Value;

            var matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern);
            Assert.IsTrue(matcher.IsMatch(sentence));
            Assert.IsTrue(matcher.IsMatch(sentence, 7));
            Assert.IsFalse(matcher.IsMatch(sentence, 29));

            Match<AnnotatedStringData, int> match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(0, 1), match.Span);
            match = matcher.Match(sentence, 7);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(10, 11), match.Span);
            match = matcher.Match(sentence, 29);
            Assert.IsFalse(match.Success);

            Match<AnnotatedStringData, int>[] matches = matcher.Matches(sentence).ToArray();
            Assert.AreEqual(17, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 1), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(13, 14), matches[7].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[16].Span);
            matches = matcher.Matches(sentence, 7).ToArray();
            Assert.AreEqual(13, matches.Length);
            Assert.AreEqual(SpanFactory.Create(10, 11), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(17, 18), matches[5].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[12].Span);
            matches = matcher.Matches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            matches = matcher.AllMatches(sentence).ToArray();
            Assert.AreEqual(17, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 1), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(13, 14), matches[7].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[16].Span);
            matches = matcher.AllMatches(sentence, 7).ToArray();
            Assert.AreEqual(13, matches.Length);
            Assert.AreEqual(SpanFactory.Create(10, 11), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(17, 18), matches[5].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[12].Span);
            matches = matcher.AllMatches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            sentence.Annotations.Add(0, 3, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("det").Value);
            sentence.Annotations.Add(4, 7, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value);
            sentence.Annotations.Add(9, 14, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value);
            sentence.Annotations.Add(15, 18, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("noun").Value);
            sentence.Annotations.Add(19, 24, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("verb").Value);
            sentence.Annotations.Add(25, 29, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adv").Value);
            sentence.Annotations.Add(0, 18, FeatureStruct.New().Symbol(NP).Value);
            sentence.Annotations.Add(19, 29, FeatureStruct.New().Symbol(VP).Value);

            pattern = Pattern<AnnotatedStringData, int>.New()
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl-").Value)
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl+").Value).Value;

            matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern);
            Assert.IsTrue(matcher.IsMatch(sentence));
            Assert.IsTrue(matcher.IsMatch(sentence, 7));
            Assert.IsFalse(matcher.IsMatch(sentence, 29));

            match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(1, 3), match.Span);
            match = matcher.Match(sentence, 7);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(15, 17), match.Span);
            match = matcher.Match(sentence, 29);
            Assert.IsFalse(match.Success);

            matches = matcher.Matches(sentence).ToArray();
            Assert.AreEqual(4, matches.Length);
            Assert.AreEqual(SpanFactory.Create(1, 3), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(15, 17), matches[1].Span);
            Assert.AreEqual(SpanFactory.Create(25, 27), matches[3].Span);
            matches = matcher.Matches(sentence, 7).ToArray();
            Assert.AreEqual(3, matches.Length);
            Assert.AreEqual(SpanFactory.Create(15, 17), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(20, 22), matches[1].Span);
            Assert.AreEqual(SpanFactory.Create(25, 27), matches[2].Span);
            matches = matcher.Matches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            matches = matcher.AllMatches(sentence).ToArray();
            Assert.AreEqual(4, matches.Length);
            Assert.AreEqual(SpanFactory.Create(1, 3), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(15, 17), matches[1].Span);
            Assert.AreEqual(SpanFactory.Create(25, 27), matches[3].Span);
            matches = matcher.AllMatches(sentence, 7).ToArray();
            Assert.AreEqual(3, matches.Length);
            Assert.AreEqual(SpanFactory.Create(15, 17), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(20, 22), matches[1].Span);
            Assert.AreEqual(SpanFactory.Create(25, 27), matches[2].Span);
            matches = matcher.AllMatches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);
        }
开发者ID:bbriggs,项目名称:machine,代码行数:100,代码来源:MatcherTests.cs

示例3: RangePattern

        public void RangePattern()
        {
            AnnotatedStringData sentence = CreateStringData("the old, angry man slept well.");

            Pattern<AnnotatedStringData, int> pattern = Pattern<AnnotatedStringData, int>.New()
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Value)
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Value).Range(0, 2).Value;

            var matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern);
            Assert.IsTrue(matcher.IsMatch(sentence));
            Assert.IsTrue(matcher.IsMatch(sentence, 7));
            Assert.IsFalse(matcher.IsMatch(sentence, 29));

            Match<AnnotatedStringData, int> match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(0, 3), match.Span);
            match = matcher.Match(sentence, 7);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(9, 12), match.Span);
            match = matcher.Match(sentence, 29);
            Assert.IsFalse(match.Success);

            Match<AnnotatedStringData, int>[] matches = matcher.Matches(sentence).ToArray();
            Assert.AreEqual(9, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 3), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(15, 18), matches[4].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[8].Span);
            matches = matcher.Matches(sentence, 7).ToArray();
            Assert.AreEqual(7, matches.Length);
            Assert.AreEqual(SpanFactory.Create(9, 12), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(19, 22), matches[3].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[6].Span);
            matches = matcher.Matches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            matches = matcher.AllMatches(sentence).ToArray();
            Assert.AreEqual(51, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 3), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(0, 2), matches[1].Span);
            Assert.AreEqual(SpanFactory.Create(0, 1), matches[2].Span);
            Assert.AreEqual(SpanFactory.Create(1, 3), matches[3].Span);
            Assert.AreEqual(SpanFactory.Create(1, 2), matches[4].Span);
            Assert.AreEqual(SpanFactory.Create(2, 3), matches[5].Span);
            Assert.AreEqual(SpanFactory.Create(9, 10), matches[14].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[50].Span);
            matches = matcher.AllMatches(sentence, 7).ToArray();
            Assert.AreEqual(39, matches.Length);
            Assert.AreEqual(SpanFactory.Create(9, 12), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(9, 11), matches[1].Span);
            Assert.AreEqual(SpanFactory.Create(9, 10), matches[2].Span);
            Assert.AreEqual(SpanFactory.Create(13, 14), matches[11].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[38].Span);
            matches = matcher.AllMatches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            sentence.Annotations.Add(0, 3, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("det").Value);
            sentence.Annotations.Add(4, 7, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value);
            sentence.Annotations.Add(9, 14, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value);
            sentence.Annotations.Add(15, 18, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("noun").Value);
            sentence.Annotations.Add(19, 24, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("verb").Value);
            sentence.Annotations.Add(25, 29, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adv").Value);
            sentence.Annotations.Add(0, 18, FeatureStruct.New().Symbol(NP).Value);
            sentence.Annotations.Add(19, 29, FeatureStruct.New().Symbol(VP).Value);

            pattern = Pattern<AnnotatedStringData, int>.New()
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Value)
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Value).Range(1, 3).Value;

            matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern);
            Assert.IsTrue(matcher.IsMatch(sentence));
            Assert.IsTrue(matcher.IsMatch(sentence, 7));
            Assert.IsFalse(matcher.IsMatch(sentence, 29));

            match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(0, 3), match.Span);
            match = matcher.Match(sentence, 7);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(9, 13), match.Span);
            match = matcher.Match(sentence, 29);
            Assert.IsFalse(match.Success);

            matches = matcher.Matches(sentence).ToArray();
            Assert.AreEqual(6, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 3), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(15, 18), matches[3].Span);
            Assert.AreEqual(SpanFactory.Create(25, 29), matches[5].Span);
            matches = matcher.Matches(sentence, 7).ToArray();
            Assert.AreEqual(4, matches.Length);
            Assert.AreEqual(SpanFactory.Create(9, 13), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(19, 23), matches[2].Span);
            Assert.AreEqual(SpanFactory.Create(25, 29), matches[3].Span);
            matches = matcher.Matches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            matches = matcher.AllMatches(sentence).ToArray();
            Assert.AreEqual(33, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 3), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(0, 2), matches[1].Span);
            Assert.AreEqual(SpanFactory.Create(1, 3), matches[2].Span);
//.........这里部分代码省略.........
开发者ID:bbriggs,项目名称:machine,代码行数:101,代码来源:MatcherTests.cs

示例4: MatcherSettings

        public void MatcherSettings()
        {
            AnnotatedStringData sentence = CreateStringData("the old, angry man slept well.");
            sentence.Annotations.Add(0, 3, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("det").Value);
            sentence.Annotations.Add(4, 7, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value);
            sentence.Annotations.Add(9, 14, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value);
            sentence.Annotations.Add(15, 18, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("noun").Value);
            sentence.Annotations.Add(19, 24, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("verb").Value);
            sentence.Annotations.Add(25, 29, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adv").Value);

            Pattern<AnnotatedStringData, int> pattern = Pattern<AnnotatedStringData, int>.New()
                .Group("NP", np => np
                    .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("det").Value).Optional
                    .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value).ZeroOrMore
                    .Group("headNoun", headNoun => headNoun.Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("noun").Value)))
                .Group("VP", vp => vp
                    .Group("headVerb", headVerb => headVerb.Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("verb").Value))
                    .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adv").Value).ZeroOrMore).Value;

            var matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern);
            Assert.IsFalse(matcher.IsMatch(sentence));

            matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern,
                new MatcherSettings<int> {Filter = ann => ((FeatureSymbol) ann.FeatureStruct.GetValue(Type)) == Word});
            Match<AnnotatedStringData, int> match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(0, 29), match.Span);
            Assert.IsTrue(match.GroupCaptures["NP"].Success);
            Assert.AreEqual(SpanFactory.Create(0, 18), match.GroupCaptures["NP"].Span);
            Assert.IsTrue(match.GroupCaptures["headNoun"].Success);
            Assert.AreEqual(SpanFactory.Create(15, 18), match.GroupCaptures["headNoun"].Span);
            Assert.IsTrue(match.GroupCaptures["VP"].Success);
            Assert.AreEqual(SpanFactory.Create(19, 29), match.GroupCaptures["VP"].Span);
            Assert.IsTrue(match.GroupCaptures["headVerb"].Success);
            Assert.AreEqual(SpanFactory.Create(19, 24), match.GroupCaptures["headVerb"].Span);

            pattern = Pattern<AnnotatedStringData, int>.New()
                .Group("NP", np => np
                    .Group(det => det
                        .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("det").Value)
                        .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Value).OneOrMore).Optional
                    .Group(adj => adj
                        .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value)
                        .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Value).OneOrMore).ZeroOrMore
                    .Group("headNoun", headNoun => headNoun.Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("noun").Value)))
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Value).OneOrMore
                .Group("VP", vp => vp
                    .Group("headVerb", headVerb => headVerb.Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("verb").Value))
                    .Group(adv => adv
                        .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Value).OneOrMore
                        .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adv").Value)).ZeroOrMore).Value;

            matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern, new MatcherSettings<int> { Nondeterministic = true });
            match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(0, 29), match.Span);
            Assert.IsTrue(match.GroupCaptures["NP"].Success);
            Assert.AreEqual(SpanFactory.Create(0, 18), match.GroupCaptures["NP"].Span);
            Assert.IsTrue(match.GroupCaptures["headNoun"].Success);
            Assert.AreEqual(SpanFactory.Create(15, 18), match.GroupCaptures["headNoun"].Span);
            Assert.IsTrue(match.GroupCaptures["VP"].Success);
            Assert.AreEqual(SpanFactory.Create(19, 29), match.GroupCaptures["VP"].Span);
            Assert.IsTrue(match.GroupCaptures["headVerb"].Success);
            Assert.AreEqual(SpanFactory.Create(19, 24), match.GroupCaptures["headVerb"].Span);

            pattern = Pattern<AnnotatedStringData, int>.New()
                .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("det").Value)
                .Or
                .Group(g => g
                    .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value)
                    .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("noun").Value)
                    .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("verb").Value)
                    .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adv").Value)).Value;

            matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern,
                new MatcherSettings<int> {Filter = ann => ((FeatureSymbol) ann.FeatureStruct.GetValue(Type)) == Word});
            match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(0, 3), match.Span);

            matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern,
                new MatcherSettings<int> {Filter = ann => ((FeatureSymbol) ann.FeatureStruct.GetValue(Type)) == Word, Direction = Direction.RightToLeft});
            match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(9, 29), match.Span);

            pattern = Pattern<AnnotatedStringData, int>.New()
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Feature("strRep").EqualTo(" ").Value)
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("nas+").Value).Value;

            matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern,
                new MatcherSettings<int> {Filter = ann => ((FeatureSymbol) ann.FeatureStruct.GetValue(Type)) != Word, MatchingMethod = MatchingMethod.Unification});
            Match<AnnotatedStringData, int>[] matches = matcher.Matches(sentence).ToArray();
            Assert.AreEqual(4, matches.Length);

            matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern,
                new MatcherSettings<int> {Filter = ann => ((FeatureSymbol) ann.FeatureStruct.GetValue(Type)) != Word, UseDefaults = true, MatchingMethod = MatchingMethod.Unification});
            matches = matcher.Matches(sentence).ToArray();
            Assert.AreEqual(1, matches.Length);
        }
开发者ID:bbriggs,项目名称:machine,代码行数:100,代码来源:MatcherTests.cs

示例5: AlternationPattern

        public void AlternationPattern()
        {
            AnnotatedStringData sentence = CreateStringData("the old, angry man slept well.");

            Pattern<AnnotatedStringData, int> pattern = Pattern<AnnotatedStringData, int>.New()
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Feature("strRep").EqualTo(" ").Value)
                .Or
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("son+").Symbol(Seg).Symbol("syl-").Value).Value;

            var matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern);
            Assert.IsTrue(matcher.IsMatch(sentence));
            Assert.IsTrue(matcher.IsMatch(sentence, 7));
            Assert.IsFalse(matcher.IsMatch(sentence, 29));

            Match<AnnotatedStringData, int> match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(1, 2), match.Span);
            match = matcher.Match(sentence, 7);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(8, 9), match.Span);
            match = matcher.Match(sentence, 29);
            Assert.IsFalse(match.Success);

            Match<AnnotatedStringData, int>[] matches = matcher.Matches(sentence).ToArray();
            Assert.AreEqual(16, matches.Length);
            Assert.AreEqual(SpanFactory.Create(1, 2), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(13, 14), matches[6].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[15].Span);
            matches = matcher.Matches(sentence, 7).ToArray();
            Assert.AreEqual(13, matches.Length);
            Assert.AreEqual(SpanFactory.Create(8, 9), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(15, 16), matches[5].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[12].Span);
            matches = matcher.Matches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            matches = matcher.AllMatches(sentence).ToArray();
            Assert.AreEqual(16, matches.Length);
            Assert.AreEqual(SpanFactory.Create(1, 2), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(13, 14), matches[6].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[15].Span);
            matches = matcher.AllMatches(sentence, 7).ToArray();
            Assert.AreEqual(13, matches.Length);
            Assert.AreEqual(SpanFactory.Create(8, 9), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(15, 16), matches[5].Span);
            Assert.AreEqual(SpanFactory.Create(28, 29), matches[12].Span);
            matches = matcher.AllMatches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            sentence.Annotations.Add(0, 3, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("det").Value);
            sentence.Annotations.Add(4, 7, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value);
            sentence.Annotations.Add(9, 14, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value);
            sentence.Annotations.Add(15, 18, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("noun").Value);
            sentence.Annotations.Add(19, 24, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("verb").Value);
            sentence.Annotations.Add(25, 29, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adv").Value);
            sentence.Annotations.Add(0, 18, FeatureStruct.New().Symbol(NP).Value);
            sentence.Annotations.Add(19, 29, FeatureStruct.New().Symbol(VP).Value);

            pattern = Pattern<AnnotatedStringData, int>.New()
                .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("det").Value)
                .Or
                .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value)
                .Or
                .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("noun").Value)
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Bdry).Feature("strRep").EqualTo(" ").Value)
                .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("det").Value)
                .Or
                .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value)
                .Or
                .Annotation(FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("noun").Value).Value;

            matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern);
            Assert.IsTrue(matcher.IsMatch(sentence));
            Assert.IsTrue(matcher.IsMatch(sentence, 7));
            Assert.IsFalse(matcher.IsMatch(sentence, 10));

            match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(0, 7), match.Span);
            match = matcher.Match(sentence, 7);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(9, 18), match.Span);
            match = matcher.Match(sentence, 10);
            Assert.IsFalse(match.Success);

            matches = matcher.Matches(sentence).ToArray();
            Assert.AreEqual(2, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 7), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(9, 18), matches[1].Span);
            matches = matcher.Matches(sentence, 7).ToArray();
            Assert.AreEqual(1, matches.Length);
            Assert.AreEqual(SpanFactory.Create(9, 18), matches[0].Span);
            matches = matcher.Matches(sentence, 10).ToArray();
            Assert.AreEqual(0, matches.Length);

            matches = matcher.AllMatches(sentence).ToArray();
            Assert.AreEqual(2, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 7), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(9, 18), matches[1].Span);
            matches = matcher.AllMatches(sentence, 7).ToArray();
//.........这里部分代码省略.........
开发者ID:bbriggs,项目名称:machine,代码行数:101,代码来源:MatcherTests.cs

示例6: CapturingGroupPattern

        public void CapturingGroupPattern()
        {
            AnnotatedStringData sentence = CreateStringData("the old, angry man slept well.");

            Pattern<AnnotatedStringData, int> pattern = Pattern<AnnotatedStringData, int>.New()
                .Group("onset", onset => onset
                    .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl-").Value).ZeroOrMore)
                .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl+").Value)
                .Group("coda", coda => coda
                    .Annotation(FeatureStruct.New(PhoneticFeatSys).Symbol(Seg).Symbol("syl-").Value).ZeroOrMore).Value;

            var matcher = new Matcher<AnnotatedStringData, int>(SpanFactory, pattern);
            Assert.IsTrue(matcher.IsMatch(sentence));
            Assert.IsTrue(matcher.IsMatch(sentence, 7));
            Assert.IsFalse(matcher.IsMatch(sentence, 29));

            Match<AnnotatedStringData, int> match = matcher.Match(sentence);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(0, 3), match.Span);
            Assert.IsTrue(match.GroupCaptures["onset"].Success);
            Assert.AreEqual(SpanFactory.Create(0, 2), match.GroupCaptures["onset"].Span);
            Assert.IsFalse(match.GroupCaptures["coda"].Success);
            match = matcher.Match(sentence, 7);
            Assert.IsTrue(match.Success);
            Assert.AreEqual(SpanFactory.Create(9, 14), match.Span);
            Assert.IsFalse(match.GroupCaptures["onset"].Success);
            Assert.IsTrue(match.GroupCaptures["coda"].Success);
            Assert.AreEqual(SpanFactory.Create(10, 14), match.GroupCaptures["coda"].Span);
            match = matcher.Match(sentence, 29);
            Assert.IsFalse(match.Success);

            Match<AnnotatedStringData, int>[] matches = matcher.Matches(sentence).ToArray();
            Assert.AreEqual(6, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 3), matches[0].Span);
            Assert.AreEqual(SpanFactory.Create(4, 7), matches[1].Span);
            Assert.IsFalse(matches[1].GroupCaptures["onset"].Success);
            Assert.IsTrue(matches[1].GroupCaptures["coda"].Success);
            Assert.AreEqual(SpanFactory.Create(5, 7), matches[1].GroupCaptures["coda"].Span);
            Assert.AreEqual(SpanFactory.Create(25, 29), matches[5].Span);
            Assert.IsTrue(matches[5].GroupCaptures["onset"].Success);
            Assert.AreEqual(SpanFactory.Create(25, 26), matches[5].GroupCaptures["onset"].Span);
            Assert.IsTrue(matches[5].GroupCaptures["coda"].Success);
            Assert.AreEqual(SpanFactory.Create(27, 29), matches[5].GroupCaptures["coda"].Span);
            matches = matcher.Matches(sentence, 7).ToArray();
            Assert.AreEqual(4, matches.Length);
            Assert.AreEqual(SpanFactory.Create(9, 14), matches[0].Span);
            Assert.IsFalse(matches[0].GroupCaptures["onset"].Success);
            Assert.IsTrue(matches[0].GroupCaptures["coda"].Success);
            Assert.AreEqual(SpanFactory.Create(10, 14), matches[0].GroupCaptures["coda"].Span);
            Assert.AreEqual(SpanFactory.Create(15, 18), matches[1].Span);
            Assert.IsTrue(matches[1].GroupCaptures["onset"].Success);
            Assert.AreEqual(SpanFactory.Create(15, 16), matches[1].GroupCaptures["onset"].Span);
            Assert.IsTrue(matches[1].GroupCaptures["coda"].Success);
            Assert.AreEqual(SpanFactory.Create(17, 18), matches[1].GroupCaptures["coda"].Span);
            Assert.AreEqual(SpanFactory.Create(25, 29), matches[3].Span);
            matches = matcher.Matches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            matches = matcher.AllMatches(sentence).ToArray();
            Assert.AreEqual(30, matches.Length);
            Assert.AreEqual(SpanFactory.Create(0, 3), matches[0].Span);
            Assert.IsTrue(matches[0].GroupCaptures["onset"].Success);
            Assert.AreEqual(SpanFactory.Create(0, 2), matches[0].GroupCaptures["onset"].Span);
            Assert.IsFalse(matches[0].GroupCaptures["coda"].Success);
            Assert.AreEqual(SpanFactory.Create(1, 3), matches[1].Span);
            Assert.IsTrue(matches[1].GroupCaptures["onset"].Success);
            Assert.AreEqual(SpanFactory.Create(1, 2), matches[1].GroupCaptures["onset"].Span);
            Assert.IsFalse(matches[1].GroupCaptures["coda"].Success);
            Assert.AreEqual(SpanFactory.Create(2, 3), matches[2].Span);
            Assert.AreEqual(SpanFactory.Create(4, 7), matches[3].Span);
            Assert.AreEqual(SpanFactory.Create(4, 6), matches[4].Span);
            Assert.AreEqual(SpanFactory.Create(4, 5), matches[5].Span);
            Assert.AreEqual(SpanFactory.Create(16, 17), matches[14].Span);
            Assert.AreEqual(SpanFactory.Create(26, 27), matches[29].Span);
            matches = matcher.AllMatches(sentence, 7).ToArray();
            Assert.AreEqual(24, matches.Length);
            Assert.AreEqual(SpanFactory.Create(9, 14), matches[0].Span);
            Assert.IsFalse(matches[0].GroupCaptures["onset"].Success);
            Assert.IsTrue(matches[0].GroupCaptures["coda"].Success);
            Assert.AreEqual(SpanFactory.Create(10, 14), matches[0].GroupCaptures["coda"].Span);
            Assert.AreEqual(SpanFactory.Create(9, 13), matches[1].Span);
            Assert.IsFalse(matches[1].GroupCaptures["onset"].Success);
            Assert.IsTrue(matches[1].GroupCaptures["coda"].Success);
            Assert.AreEqual(SpanFactory.Create(10, 13), matches[1].GroupCaptures["coda"].Span);
            Assert.AreEqual(SpanFactory.Create(9, 12), matches[2].Span);
            Assert.AreEqual(SpanFactory.Create(19, 22), matches[11].Span);
            Assert.AreEqual(SpanFactory.Create(26, 27), matches[23].Span);
            matches = matcher.AllMatches(sentence, 29).ToArray();
            Assert.AreEqual(0, matches.Length);

            sentence.Annotations.Add(0, 3, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("det").Value);
            sentence.Annotations.Add(4, 7, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value);
            sentence.Annotations.Add(9, 14, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adj").Value);
            sentence.Annotations.Add(15, 18, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("noun").Value);
            sentence.Annotations.Add(19, 24, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("verb").Value);
            sentence.Annotations.Add(25, 29, FeatureStruct.New(WordFeatSys).Symbol(Word).Symbol("adv").Value);

            pattern = Pattern<AnnotatedStringData, int>.New()
                .Group("NP", np => np
                    .Group(det => det
//.........这里部分代码省略.........
开发者ID:bbriggs,项目名称:machine,代码行数:101,代码来源:MatcherTests.cs


注:本文中的Matcher.IsMatch方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。