private static void LoadProviders()
        {
            if (AvailableProviders.ContainsKey(DirectMatchProvider.GetName()) == false)
            {
                AvailableProviders.Add(DirectMatchProvider.GetName(), new DirectMatchProvider(false));
            }
            if (AvailableProviders.ContainsKey(InitialsMatchProvider.GetName()) == false)
            {
                AvailableProviders.Add(InitialsMatchProvider.GetName(), new InitialsMatchProvider(false));
            }
            if (AvailableProviders.ContainsKey(DiscreteMatchProvider.GetName()) == false)
            {
                AvailableProviders.Add(DiscreteMatchProvider.GetName(), new DiscreteMatchProvider(false));
            }
            if (AvailableProviders.ContainsKey(ChineseZhCnPinYinInitialsMatchProvider.GetName()) == false)
            {
                AvailableProviders.Add(ChineseZhCnPinYinInitialsMatchProvider.GetName(), new ChineseZhCnPinYinInitialsMatchProvider(false));
            }
            if (AvailableProviders.ContainsKey(ChineseZhCnPinYinMatchProvider.GetName()) == false)
            {
                AvailableProviders.Add(ChineseZhCnPinYinMatchProvider.GetName(), new ChineseZhCnPinYinMatchProvider(false));
            }

            try
            {
                var files = Directory.GetFiles(Directory.GetParent(typeof(Builder).Assembly.Location).FullName, "*.dll");
                foreach (var dllPath in files)
                {
                    var assembly  = Assembly.LoadFile(dllPath);
                    var types     = assembly.GetTypes();
                    var providers = types.Where(item =>
                                                item.IsSubclassOf(typeof(MatchProviderBase)) && !item.IsAbstract)
                                    .Select(type => CreateProviderInstance(type, false));

                    foreach (var provider in providers.OrderBy(x => x.GetProviderDescription()))
                    {
                        var t = provider.GetProviderName();
                        if (AvailableProviders.ContainsKey(t) == false)
                        {
                            AvailableProviders.Add(t, provider);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Beispiel #2
0
        public void GetProviderDescriptionTest()
        {
            {
                var dsp1 = Builder.GetProviderDescription(DirectMatchProvider.GetName());
                var dsp2 = Builder.GetProviderDescriptionEn(DirectMatchProvider.GetName());
                Assert.IsTrue(!string.IsNullOrEmpty(dsp1));
                Assert.IsTrue(!string.IsNullOrEmpty(dsp2));
            }
            {
                var dsp1 = Builder.GetProviderDescription(InitialsMatchProvider.GetName());
                var dsp2 = Builder.GetProviderDescriptionEn(InitialsMatchProvider.GetName());
                Assert.IsTrue(!string.IsNullOrEmpty(dsp1));
                Assert.IsTrue(!string.IsNullOrEmpty(dsp2));
            }
            {
                var dsp1 = Builder.GetProviderDescription(DiscreteMatchProvider.GetName());
                var dsp2 = Builder.GetProviderDescriptionEn(DiscreteMatchProvider.GetName());
                Assert.IsTrue(!string.IsNullOrEmpty(dsp1));
                Assert.IsTrue(!string.IsNullOrEmpty(dsp2));
            }
            {
                var dsp1 = Builder.GetProviderDescription(ChineseZhCnPinYinMatchProvider.GetName());
                var dsp2 = Builder.GetProviderDescriptionEn(ChineseZhCnPinYinMatchProvider.GetName());
                Assert.IsTrue(!string.IsNullOrEmpty(dsp1));
                Assert.IsTrue(!string.IsNullOrEmpty(dsp2));
            }
            {
                var dsp1 = Builder.GetProviderDescription(ChineseZhCnPinYinInitialsMatchProvider.GetName());
                var dsp2 = Builder.GetProviderDescriptionEn(ChineseZhCnPinYinInitialsMatchProvider.GetName());
                Assert.IsTrue(!string.IsNullOrEmpty(dsp1));
                Assert.IsTrue(!string.IsNullOrEmpty(dsp2));
            }
#if !NET45
            {
                var dsp1 = Builder.GetProviderDescription(JapaneseRomajiProvider.GetName());
                var dsp2 = Builder.GetProviderDescriptionEn(JapaneseRomajiProvider.GetName());
                Assert.IsTrue(!string.IsNullOrEmpty(dsp1));
                Assert.IsTrue(!string.IsNullOrEmpty(dsp2));
            }
#endif
        }
        public void FindMatchesTestCase()
        {
            var matcher = new InitialsMatchProvider(true);
            var dspen   = matcher.GetProviderDescriptionEn();
            var dsp     = matcher.GetProviderDescription();


            {
                string org = "abcdefg";
                var    str = new MatchCache(org);
                matcher.AppendDescriptions(ref str);
            }

            {
                string org = "abcdefg";
                var    kws = new[] { "a" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(ret.Keywords.Count == 1 && ret.Keywords[0] == "a");
                Assert.IsTrue(m.Count == org.Length && m[0] == true && m[1] == false);
            }

            {
                string org = "abcdefg";
                var    kws = new[] { "A" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "abaa a";
                var    kws = new[] { " " };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "abAAa";
                var    kws = new[] { "aa" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "abAA A";
                var    kws = new[] { "Aa" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
            }

            {
                string org = "abAA A";
                var    kws = new[] { "aA" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(m.Count == org.Length &&
                              m[0] == true &&
                              m[1] == false &&
                              m[2] == false &&
                              m[3] == false &&
                              m[4] == false &&
                              m[5] == true);
            }

            {
                string org = "a b c d e";
                var    kws = new[] { "ab", "cd" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(m.Count == org.Length &&
                              m[0] == true &&
                              m[1] == false &&
                              m[2] == true &&
                              m[3] == false &&
                              m[4] == true &&
                              m[5] == false &&
                              m[6] == true &&
                              m[7] == false &&
                              m[8] == false);
            }

            {
                string org = "a BC de";
                var    kws = new[] { "ab", "cd" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "a BC de";
                var    kws = new[] { "ab", "bc" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "a Bc dE ";
                var    kws = new[] { "abd" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "a Bc dE ";
                var    kws = new[] { "aBd" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(m.Count == org.Length &&
                              m[0] == true &&
                              m[1] == false &&
                              m[2] == true &&
                              m[3] == false &&
                              m[4] == false &&
                              m[5] == true &&
                              m[6] == false &&
                              m[7] == false);
            }

            {
                string org = "a Bc dE ";
                var    kws = new[] { "AB", "bd" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "a Bc dE ";
                var    kws = new[] { "aB", "Bd" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(m.Count == org.Length &&
                              m[0] == true &&
                              m[1] == false &&
                              m[2] == true &&
                              m[3] == false &&
                              m[4] == false &&
                              m[5] == true &&
                              m[6] == false &&
                              m[7] == false);
            }



            {
                string org = "a Bc dE ";
                var    kws = new[] { "aB", "Bd", "K" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
                var m = ret.HitFlags;
                Assert.IsTrue(m.Count == org.Length &&
                              m[0] == true &&
                              m[1] == false &&
                              m[2] == true &&
                              m[3] == false &&
                              m[4] == false &&
                              m[5] == true &&
                              m[6] == false &&
                              m[7] == false);
            }
        }
        public void FindMatchesTest()
        {
            var matcher = new InitialsMatchProvider(false);
            var dspen   = matcher.GetProviderDescriptionEn();
            var dsp     = matcher.GetProviderDescription();

            Assert.IsTrue(matcher.GetProviderName() == nameof(InitialsMatchProvider));

            {
                var ret = matcher.DoMatches(new MatchCache(""), new[] { "" });
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "   ";
                var    str = new MatchCache(org);
                matcher.AppendDescriptions(ref str);
                Assert.IsTrue(str.SpellCaches[matcher.GetProviderName()].GetProviderName() == matcher.GetProviderName());
                matcher.DoMatches(str, new[] { " " });
                Assert.IsTrue(
                    str.SpellCaches[nameof(InitialsMatchProvider)] is SpellCache sc &&
                    sc.SkipFlag.All(x => x));
            }

            {
                string org = "abcdefg";
                var    str = new MatchCache(org);
                matcher.AppendDescriptions(ref str);
                matcher.AppendDescriptions(ref str);
            }

            {
                string org = "abcdefg";
                var    kws = new[] { "a" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(ret.Keywords.Count == 1 && ret.Keywords[0] == "a");
                Assert.IsTrue(m.Count == org.Length && m[0] == true && m[1] == false);
            }

            {
                string org = "abcdefg";
                var    kws = new[] { "A" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(ret.Keywords.Count == 1 && ret.Keywords[0] == "A");
                Assert.IsTrue(m.Count == org.Length && m[0] == true && m[1] == false);
            }

            {
                string org = "abaa a";
                var    kws = new[] { " " };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "abAAa";
                var    kws = new[] { "aa" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "abAA A";
                var    kws = new[] { "aa" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(m.Count == org.Length &&
                              m[0] == true &&
                              m[1] == false &&
                              m[2] == false &&
                              m[3] == false &&
                              m[4] == false &&
                              m[5] == true);
            }

            {
                string org = "abAA A";
                var    kws = new[] { "Aa" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(m.Count == org.Length &&
                              m[0] == true &&
                              m[1] == false &&
                              m[2] == false &&
                              m[3] == false &&
                              m[4] == false &&
                              m[5] == true);
            }

            {
                string org = "a b c d e";
                var    kws = new[] { "ab", "cd" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(m.Count == org.Length &&
                              m[0] == true &&
                              m[1] == false &&
                              m[2] == true &&
                              m[3] == false &&
                              m[4] == true &&
                              m[5] == false &&
                              m[6] == true &&
                              m[7] == false &&
                              m[8] == false);
            }

            {
                string org = "a BC de";
                var    kws = new[] { "ab", "cd" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "a BC de";
                var    kws = new[] { "ab", "bc" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == false);
            }

            {
                string org = "a Bc dE ";
                var    kws = new[] { "abd" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(m.Count == org.Length &&
                              m[0] == true &&
                              m[1] == false &&
                              m[2] == true &&
                              m[3] == false &&
                              m[4] == false &&
                              m[5] == true &&
                              m[6] == false &&
                              m[7] == false);
            }


            {
                string org = "a Bc dE ";
                var    kws = new[] { "AB", "bd" };
                var    ret = matcher.DoMatches(new MatchCache(org), kws);
                Assert.IsTrue(ret.IsMatchAllKeywords == true);
                var m = ret.HitFlags;
                Assert.IsTrue(m.Count == org.Length &&
                              m[0] == true &&
                              m[1] == false &&
                              m[2] == true &&
                              m[3] == false &&
                              m[4] == false &&
                              m[5] == true &&
                              m[6] == false &&
                              m[7] == false);
            }
        }