예제 #1
0
        /// <summary>
        /// FreeSql
        /// </summary>
        /// <param name="services"></param>
        public static void AddContext(this IServiceCollection services)
        {
            var configuration = services.BuildServiceProvider().GetRequiredService <IConfiguration>();
            IConfigurationSection configurationSection = configuration.GetSection("ConnectionStrings:MySql");
            IFreeSql fsql = new FreeSqlBuilder()
                            .UseConnectionString(DataType.MySql, configurationSection.Value)
                            .UseEntityPropertyNameConvert(StringConvertType.PascalCaseToUnderscoreWithLower) //全局转换实体属性名方法 https://github.com/2881099/FreeSql/pull/60
                            .UseAutoSyncStructure(true)                                                      //自动迁移实体的结构到数据库
                            .UseMonitorCommand(cmd =>
            {
                Trace.WriteLine(cmd.CommandText + ";");
            }
                                               )
                            .UseSyncStructureToLower(true)                                         // 转小写同步结构
                            .Build()
                            .SetDbContextOptions(opt => opt.EnableAddOrUpdateNavigateList = true); //联级保存功能开启(默认为关闭)



            fsql.Aop.CurdBefore += (s, e) =>
            {
            };

            fsql.Aop.CurdAfter += (s, e) =>
            {
                if (e.ElapsedMilliseconds > 200)
                {
                    //记录日志
                    //发送短信给负责人
                }
            };

            //敏感词处理
            if (configuration["AuditValue:Enable"].ToBoolean())
            {
                IllegalWordsSearch illegalWords = ToolGoodUtils.GetIllegalWordsSearch();

                fsql.Aop.AuditValue += (s, e) =>
                {
                    if (e.Column.CsType == typeof(string) && e.Value != null)
                    {
                        string oldVal = (string)e.Value;
                        string newVal = illegalWords.Replace(oldVal);
                        //第二种处理敏感词的方式
                        //string newVal = oldVal.ReplaceStopWords();
                        if (newVal != oldVal)
                        {
                            e.Value = newVal;
                        }
                    }
                };
            }
            services.AddSingleton(fsql);
            services.AddScoped <IUnitOfWork>(sp => sp.GetService <IFreeSql>().CreateUnitOfWork());

            services.AddFreeRepository(filter =>
            {
                filter.Apply <IDeleteAduitEntity>("IsDeleted", a => a.IsDeleted == false);
            }, typeof(AuditBaseRepository <>).Assembly);
        }
        /// <summary>
        /// FreeSql
        /// </summary>
        /// <param name="services"></param>
        public static void AddContext(this IServiceCollection services, IConfiguration configuration)
        {
            IConfigurationSection configurationSection = configuration.GetSection("ConnectionStrings:MySql");


            IFreeSql fsql = new FreeSqlBuilder()
                            .UseConnectionString(DataType.MySql, configurationSection.Value)
                            .UseNameConvert(NameConvertType.PascalCaseToUnderscoreWithLower)
                            .UseAutoSyncStructure(true)
                            .UseNoneCommandParameter(true)
                            .UseMonitorCommand(cmd =>
            {
                Trace.WriteLine(cmd.CommandText + ";");
            }
                                               )
                            .Build()
                            .SetDbContextOptions(opt => opt.EnableAddOrUpdateNavigateList = true);//联级保存功能开启(默认为关闭)



            fsql.Aop.CurdAfter += (s, e) =>
            {
                Log.Debug($"ManagedThreadId:{Thread.CurrentThread.ManagedThreadId}: FullName:{e.EntityType.FullName}" +
                          $" ElapsedMilliseconds:{e.ElapsedMilliseconds}ms, {e.Sql}");

                if (e.ElapsedMilliseconds > 200)
                {
                    //记录日志
                    //发送短信给负责人
                }
            };

            //敏感词处理
            if (configuration["AuditValue:Enable"].ToBoolean())
            {
                IllegalWordsSearch illegalWords = ToolGoodUtils.GetIllegalWordsSearch();

                fsql.Aop.AuditValue += (s, e) =>
                {
                    if (e.Column.CsType == typeof(string) && e.Value != null)
                    {
                        string oldVal = (string)e.Value;
                        string newVal = illegalWords.Replace(oldVal);
                        //第二种处理敏感词的方式
                        //string newVal = oldVal.ReplaceStopWords();
                        if (newVal != oldVal)
                        {
                            e.Value = newVal;
                        }
                    }
                };
            }

            services.AddSingleton(fsql);
            services.AddScoped <UnitOfWorkManager>();
            fsql.GlobalFilter.Apply <IDeleteAduitEntity>("IsDeleted", a => a.IsDeleted == false);
            //在运行时直接生成表结构
            fsql.CodeFirst.SyncStructure(ReflexHelper.GetEntityTypes(typeof(IEntity)));
            services.AddFreeRepository();
        }
예제 #3
0
        public void IssuesTest_17()
        {
            var    illegalWordsSearch = new IllegalWordsSearch();
            string s = "中国|zg人|abc";

            illegalWordsSearch.SetKeywords(s.Split('|'));
            var str = illegalWordsSearch.Replace("我是中美国人厉害中国完美abcddb好的", '*');

            Assert.Equal("我是中美国人厉害**完美***ddb好的", str);
        }
예제 #4
0
        public void IssuesTest_17()
        {
            var illegalWordsSearch = new IllegalWordsSearch();
            string s = "中国|zg人|abc";
            illegalWordsSearch.SetKeywords(s.Split('|'));
            var str = illegalWordsSearch.Replace("我是中美国人厉害中国完美abcddb好的", '*');

            //Assert.AreEqual("我是中美国人厉害**完美***ddb好的", str);
            //注,abc先转abc,再判断abc左右是否为英文或数字,因为后面为d是英文,所以不能过滤
            Assert.AreEqual("我是中美国人厉害**完美abcddb好的", str);
        }
예제 #5
0
        public void IssuesTest_65()
        {
            var           search   = new IllegalWordsSearch();
            List <string> keywords = new List <string>();

            keywords.Add("f**k");
            keywords.Add("ffx");
            search.SetKeywords(keywords);
            var result = search.Replace("fFuck");

            Assert.AreEqual("*****", result);
        }
예제 #6
0
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
            IConfigurationSection configurationSection = Configuration.GetSection("ConnectionStrings:Default");

            Fsql = new FreeSqlBuilder()
                   .UseConnectionString(DataType.MySql, configurationSection.Value)
                   .UseEntityPropertyNameConvert(StringConvertType.PascalCaseToUnderscoreWithLower) //全局转换实体属性名方法 https://github.com/2881099/FreeSql/pull/60
                   .UseAutoSyncStructure(true)                                                      //自动迁移实体的结构到数据库
                   .UseMonitorCommand(cmd =>
            {
                Trace.WriteLine(cmd.CommandText);
            }
                                      )
                   .UseSyncStructureToLower(true) // 转小写同步结构
                   .Build();

            Fsql.Aop.CurdBefore = (s, e) =>
            {
            };

            Fsql.Aop.CurdAfter = (s, e) =>
            {
                if (e.ElapsedMilliseconds > 200)
                {
                    //记录日志
                    //发送短信给负责人
                }
            };

            //敏感词处理
            if (Configuration["AuditValue:Enable"].ToBoolean())
            {
                IllegalWordsSearch illegalWords = ToolGoodUtils.GetIllegalWordsSearch();

                Fsql.Aop.AuditValue += (s, e) =>
                {
                    if (e.Column.CsType == typeof(string) && e.Value != null)
                    {
                        string oldVal = (string)e.Value;
                        string newVal = illegalWords.Replace(oldVal);
                        //第二种处理敏感词的方式
                        //string newVal = oldVal.ReplaceStopWords();
                        if (newVal != oldVal)
                        {
                            e.Value = newVal;
                        }
                    }
                };
            }
        }
예제 #7
0
        protected override void Load(ContainerBuilder builder)
        {
            IFreeSql fsql = new FreeSqlBuilder()
                            .UseConnectionString(_configuration)
                            .UseNameConvert(NameConvertType.PascalCaseToUnderscoreWithLower)
                            .UseAutoSyncStructure(true)
                            .UseNoneCommandParameter(true)
                            .UseMonitorCommand(cmd =>
            {
                Trace.WriteLine(cmd.CommandText + ";");
            }
                                               )
                            .Build()
                            .SetDbContextOptions(opt => opt.EnableAddOrUpdateNavigateList = true);//联级保存功能开启(默认为关闭)


            builder.RegisterInstance(fsql).SingleInstance();


            fsql.Aop.CurdAfter += (s, e) =>
            {
                Log.Debug($"ManagedThreadId:{Thread.CurrentThread.ManagedThreadId}: FullName:{e.EntityType.FullName}" +
                          $" ElapsedMilliseconds:{e.ElapsedMilliseconds}ms, {e.Sql}");


                if (e.ElapsedMilliseconds > 200)
                {
                    //记录日志
                    //发送短信给负责人
                }
            };

            //敏感词处理
            if (_configuration["AuditValue:Enable"].ToBoolean())
            {
                IllegalWordsSearch illegalWords = ToolGoodUtils.GetIllegalWordsSearch();

                fsql.Aop.AuditValue += (s, e) =>
                {
                    if (e.Column.CsType == typeof(string) && e.Value != null)
                    {
                        string oldVal = (string)e.Value;
                        string newVal = illegalWords.Replace(oldVal);
                        //第二种处理敏感词的方式
                        //string newVal = oldVal.ReplaceStopWords();
                        if (newVal != oldVal)
                        {
                            e.Value = newVal;
                        }
                    }
                };
            }

            //services.AddFreeRepository();


            builder.RegisterType(typeof(UnitOfWorkManager)).InstancePerLifetimeScope();


            fsql.GlobalFilter.Apply <IDeleteAduitEntity>("IsDeleted", a => a.IsDeleted == false);
            try
            {
                using var objPool = fsql.Ado.MasterPool.Get();
            }
            catch (Exception e)
            {
                Log.Logger.Error(e + e.StackTrace + e.Message + e.InnerException);
                return;
            }
            //在运行时直接生成表结构
            try
            {
                fsql.CodeFirst
                .SeedData()
                .SyncStructure(ReflexHelper.GetTypesByTableAttribute());
            }
            catch (Exception e)
            {
                Log.Logger.Error(e + e.StackTrace + e.Message + e.InnerException);
            }
        }
예제 #8
0
        public void IllegalWordsSearchTest()
        {
            string s = "中国|国人|zg人|f**k|all|as|19|http://|ToolGood|assert|zgasser|共产党";

            int[]  bl   = new int[] { 7, 4, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 };
            string test = "我是中国人";


            var iwords = new IllegalWordsSearch();

            iwords.SetKeywords(s.Split('|'));


            var b = iwords.ContainsAny(test);

            Assert.AreEqual(true, b);


            var f = iwords.FindFirst(test);

            Assert.AreEqual(true, f.Success);
            Assert.AreEqual("中国", f.Keyword);
            Assert.AreEqual(2, f.Start);
            Assert.AreEqual(3, f.End);



            var all = iwords.FindAll(test);

            Assert.AreEqual("中国", all[0].SrcString);
            Assert.AreEqual("国人", all[1].SrcString);

            test = "共产党";
            all  = iwords.FindAll(test);
            Assert.AreEqual("共产党", all[0].SrcString);


            test = "我是中国zg人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("中国", all[0].SrcString);
            Assert.AreEqual("zg人", all[1].SrcString);

            test = "中间国zg人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("zg人", all[0].SrcString);

            test = "f**k al[]l"; //未启用跳词
            all  = iwords.FindAll(test);
            Assert.AreEqual("f**k", all[0].SrcString);
            Assert.AreEqual(1, all.Count);


            test = "f**k al[]l";
            iwords.UseSkipWordFilter = true; //启用跳词
            all = iwords.FindAll(test);
            Assert.AreEqual("f**k", all[0].SrcString);
            Assert.AreEqual("al[]l", all[1].SrcString);
            Assert.AreEqual(2, all.Count);

            test = "http://ToolGood.com";
            all  = iwords.FindAll(test);
            Assert.AreEqual("toolgood", all[0].Keyword); //关键字ToolGood默认转小写
            Assert.AreEqual("ToolGood", all[0].SrcString);
            Assert.AreEqual(1, all.Count);

            test = "asssert all";
            all  = iwords.FindAll(test); //未启用重复词
            Assert.AreEqual("all", all[0].SrcString);
            Assert.AreEqual(1, all.Count);

            test = "asssert all";
            iwords.UseDuplicateWordFilter = true; //启用重复词
            all = iwords.FindAll(test);
            Assert.AreEqual("asssert", all[0].SrcString);
            Assert.AreEqual("assert", all[0].Keyword);
            Assert.AreEqual("all", all[1].SrcString);
            Assert.AreEqual(2, all.Count);

            test = "asssert allll"; //重复词匹配到末尾
            all  = iwords.FindAll(test);
            Assert.AreEqual("asssert", all[0].SrcString);
            Assert.AreEqual("assert", all[0].Keyword);
            Assert.AreEqual("allll", all[1].SrcString);
            Assert.AreEqual(2, all.Count);

            test = "zgasssert aallll"; //不会匹配zgasser 或 assert
            all  = iwords.FindAll(test);
            Assert.AreEqual("aallll", all[0].SrcString);
            Assert.AreEqual("all", all[0].Keyword);
            Assert.AreEqual(1, all.Count);

            test = "我是【中]国【人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("中]国", all[0].SrcString);
            Assert.AreEqual("国【人", all[1].SrcString);

            test = "我是【中国【人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("中国", all[0].SrcString);
            Assert.AreEqual("国【人", all[1].SrcString);
            Assert.AreEqual(2, all.Count);


            var ss = iwords.Replace(test, '*');

            Assert.AreEqual("我是【****", ss);

            test = "我是中国人"; //使用黑名单
            iwords.SetBlacklist(bl);
            iwords.UseBlacklistFilter = true;
            all = iwords.FindAll(test, 1);
            Assert.AreEqual("中国", all[0].SrcString);
            Assert.AreEqual(1, all.Count);
        }
예제 #9
0
        static void Main(string[] args)
        {
            ReadBadWord();
            var text = File.ReadAllText("Talk.txt");

            //var l = text.Length;
            //illegalWordsSearch.Save("test.ini");
            //var tt = illegalWordsSearch.FindAll(text);

            //word2.Load("test.ini");
            //Console.Write("-------------------- SetKeywords Test --------------------\r\n");

            //Run(1, "StringSearch.SetKeywords  ", () => {
            //    List<string> list = new List<string>();
            //    using (StreamReader sw = new StreamReader(File.OpenRead("BadWord.txt"))) {
            //        string key = sw.ReadLine();
            //        while (key != null) {
            //            if (key != string.Empty) {
            //                list.Add(key);
            //            }
            //            key = sw.ReadLine();
            //        }
            //    }
            //    StringSearch s = new StringSearch();
            //    s.SetKeywords(list);
            //});
            //Run(1, "StringSearchEx.SetKeywords  ", () => {
            //    List<string> list = new List<string>();
            //    using (StreamReader sw = new StreamReader(File.OpenRead("BadWord.txt"))) {
            //        string key = sw.ReadLine();
            //        while (key != null) {
            //            if (key != string.Empty) {
            //                list.Add(key);
            //            }
            //            key = sw.ReadLine();
            //        }
            //    }
            //    StringSearchEx s = new StringSearchEx();
            //    s.SetKeywords(list);
            //});
            //Run(1, "StringSearchEx.Load  ", () => {
            //    StringSearchEx s = new StringSearchEx();
            //    s.Load("test.ini");
            //});



            //var ts1 = word.FindAll(text);
            //var ts = word2.FindAll(text);
            //Console.Write("-------------------- ToSenseWord Test --------------------\r\n");

            //Run("ToSenseWord1  ", () => { WordTest.ToSenseWord1(text); });
            //Run("ToSenseWord2  ", () => { WordTest.ToSenseWord2(text); });
            //Run("ToSenseWord3  ", () => { WordTest.ToSenseWord3(text); });
            //Run("ToSenseWord4  ", () => { WordTest.ToSenseWord4(text); });
            //Run("ToSenseWord5  ", () => { WordTest.ToSenseWord5(text); });
            //Run("ToSenseWord6  ", () => { WordTest.ToSenseWord6(text); });
            //Run("ToSenseWord7  ", () => { WordTest.ToSenseWord7(text); });
            //Run("ToSenseWord8  ", () => { WordTest.ToSenseWord8(text); });
            //Run("ToSenseWord9  ", () => { WordTest.ToSenseWord9(text); });
            //Run("ToSenseWord10  ", () => { WordTest.ToSenseWord10(text); });

            //Run("GetDisablePostion1  ", () => { WordTest.GetDisablePostion1(text); });
            //Run("GetDisablePostion2  ", () => { WordTest.GetDisablePostion2(text); });
            //Run("GetDisablePostion3  ", () => { WordTest.GetDisablePostion3(text); });
            //Run("GetDisablePostion4  ", () => { WordTest.GetDisablePostion4(text); });
            //Run("GetDisablePostion5  ", () => { WordTest.GetDisablePostion5(text); });
            //Run("GetDisablePostion6  ", () => { WordTest.GetDisablePostion6(text); });
            //Run("GetDisablePostion7  ", () => { WordTest.GetDisablePostion7(text); });
            //Run("GetDisablePostion9  ", () => { WordTest.GetDisablePostion9(text); });
            //Run("GetDisablePostion8  ", () => { WordTest.GetDisablePostion8(text); });

            //Console.Write("-------------------- ToSenseIllegalWords --------------------\r\n");

            //Run("ToSenseIllegalWords", () => { WordsHelper.ToSenseIllegalWords(text); });



            Console.Write("-------------------- FindFirst OR ContainsAny --------------------\r\n");
            Run("TrieFilter", () => { tf1.HasBadWord(text); });
            Run("FastFilter", () => { ff.HasBadWord(text); });
            Run("StringSearch(ContainsAny)", () => { stringSearch.ContainsAny(text); });
            Run("StringSearch(FindFirst)", () => { stringSearch.FindFirst(text); });
            Run("StringSearchEx(ContainsAny)", () => { stringSearchEx.ContainsAny(text); });
            Run("StringSearchEx(FindFirst)", () => { stringSearchEx.FindFirst(text); });
            Run("WordsSearch(ContainsAny)", () => { wordsSearch.ContainsAny(text); });
            Run("WordsSearch(FindFirst)", () => { wordsSearch.FindFirst(text); });
            Run("WordsSearchEx(ContainsAny)", () => { wordsSearchEx.ContainsAny(text); });
            Run("WordsSearchEx(FindFirst)", () => { wordsSearchEx.FindFirst(text); });

            Run("IllegalWordsSearch(FindFirst)", () => { illegalWordsSearch.FindFirst(text); });
            Run("IllegalWordsSearch(ContainsAny)", () => { illegalWordsSearch.ContainsAny(text); });

            Console.Write("-------------------- Find All --------------------\r\n");
            Run("TrieFilter(FindAll)", () => { tf1.FindAll(text); });
            Run("FastFilter(FindAll)", () => { ff.FindAll(text); });
            Run("StringSearch(FindAll)", () => { stringSearch.FindAll(text); });
            Run("StringSearchEx(FindAll)", () => { stringSearchEx.FindAll(text); });
            Run("WordsSearch(FindAll)", () => { wordsSearch.FindAll(text); });
            Run("WordsSearchEx(FindAll)", () => { wordsSearchEx.FindAll(text); });
            Run("IllegalWordsSearch(FindAll)", () => { illegalWordsSearch.FindAll(text); });
            Console.Write("-------------------- Replace --------------------\r\n");
            Run("TrieFilter(Replace)", () => { tf1.Replace(text); });
            Run("FastFilter(Replace)", () => { ff.Replace(text); });
            Run("StringSearch(Replace)", () => { stringSearch.Replace(text); });
            Run("StringSearchEx(Replace)", () => { stringSearchEx.Replace(text); });

            Run("WordsSearch(Replace)", () => { wordsSearch.Replace(text); });
            Run("WordsSearchEx(Replace)", () => { wordsSearchEx.Replace(text); });
            Run("IllegalWordsSearch(Replace)", () => { illegalWordsSearch.Replace(text); });

            Console.Write("-------------------- Regex --------------------\r\n");
            Run("Regex.IsMatch", () => { re.IsMatch(text); });
            Run("Regex.Match", () => { re.Match(text); });
            Run("Regex.Matches", () => { re.Matches(text); });

            Console.Write("-------------------- Regex used Trie tree --------------------\r\n");
            Run("Regex.IsMatch", () => { re2.IsMatch(text); });
            Run("Regex.Match", () => { re2.Match(text); });
            Run("Regex.Matches", () => { re2.Matches(text); });


            Console.ReadKey();
        }
예제 #10
0
 /// <summary>
 /// a function iterate through
 /// all string or string array property of a class
 /// and replace all bad words in it
 /// this method is using reflection
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="t"></param>
 /// <param name="type"></param>
 /// <param name="fuckAll">是否检查所有可能字段,为true的时候无视属性的<see cref="BadWordIgnoreAttribute"/></param>
 /// <returns></returns>
 public T BadwordsFucker <T>(T t, Type type = null, bool fuckAll = false)
 {
     type = type == null ? typeof(T) : type;
     if (t == null)
     {
         return(default(T));
     }
     foreach (var item in type.GetProperties())
     {
         object val = new object();
         try
         {
             val = item.GetValue(t);
         }
         catch (Exception)
         {
             continue;
         }
         if (val == null)
         {
             continue;
         }
         if (!fuckAll)
         {
             var attr = item.GetCustomAttribute <BadWordIgnoreAttribute>();
             if (attr != null)
             {
                 continue;
             }
         }
         if (val is string)
         {
             val = StringSearch.Replace(val as string);
             try
             {
                 item.SetValue(t, val);
             }
             catch (Exception)
             {
                 continue;
             }
         }
         else if (val is string[])
         {
             var value = val as string[];
             for (int i = 0; i < value.Length; i++)
             {
                 value[i] = StringSearch.Replace(value[i]);
             }
         }
         else if (val is IList <string> )
         {
             var strings = val as IList <string>;
             for (int i = 0; i < strings.Count; i++)
             {
                 strings[i] = StringSearch.Replace(strings[i]);
             }
         }
     }
     return(t);
 }
예제 #11
0
        public void IllegalWordsSearchTest()
        {
            string s    = "中国|国人|zg人|f**k|all|as|19|http://|ToolGood";
            string test = "我是中国人";


            IllegalWordsSearch iwords = new IllegalWordsSearch(2);

            iwords.SetKeywords(s.Split('|'));


            var b = iwords.ContainsAny(test);

            Assert.AreEqual(true, b);


            var f = iwords.FindFirst(test);

            Assert.AreEqual(true, f.Success);
            Assert.AreEqual("中国", f.Keyword);
            Assert.AreEqual(2, f.Start);
            Assert.AreEqual(3, f.End);



            var all = iwords.FindAll(test);

            Assert.AreEqual("中国", all[0].SrcString);
            Assert.AreEqual("国人", all[1].SrcString);

            test = "我是中国zg人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("中国", all[0].SrcString);
            Assert.AreEqual("zg人", all[1].SrcString);
            Assert.AreEqual("国zg人", all[2].SrcString);

            test = "中间国zg人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("zg人", all[0].SrcString);
            Assert.AreEqual("国zg人", all[1].SrcString);

            test = "f**k al.l";
            all  = iwords.FindAll(test);
            Assert.AreEqual("f**k", all[0].SrcString);
            Assert.AreEqual("al.l", all[1].SrcString);
            Assert.AreEqual(2, all.Count);

            test = "ht@tp://ToolGood.com";
            all  = iwords.FindAll(test);
            Assert.AreEqual("ht@tp://", all[0].SrcString);
            Assert.AreEqual("http://", all[0].Keyword);
            Assert.AreEqual("toolgood", all[1].Keyword);
            Assert.AreEqual("ToolGood", all[1].SrcString);
            Assert.AreEqual(2, all.Count);


            test = "asssert all";
            all  = iwords.FindAll(test);
            Assert.AreEqual("all", all[0].SrcString);
            Assert.AreEqual(1, all.Count);

            test = "19w 1919 all";
            all  = iwords.FindAll(test);
            Assert.AreEqual("19", all[0].SrcString);
            Assert.AreEqual("all", all[1].SrcString);

            test = "我是【中]国【人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("中]国", all[0].SrcString);
            Assert.AreEqual("国【人", all[1].SrcString);

            test = "我是【中国【人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("中国", all[0].SrcString);
            Assert.AreEqual("国【人", all[1].SrcString);
            Assert.AreEqual(2, all.Count);


            var ss = iwords.Replace(test, '*');

            Assert.AreEqual("我是【****", ss);
        }
예제 #12
0
        static void Main(string[] args)
        {
            ReadBadWord();
            var text = File.ReadAllText("Talk.txt");


            Console.Write("-------------------- FindFirst OR ContainsAny 100000次 --------------------\r\n");
            Run("TrieFilter", () => { tf1.HasBadWord(text); });
            Run("FastFilter", () => { ff.HasBadWord(text); });
            Run("StringSearch(ContainsAny)", () => { stringSearch.ContainsAny(text); });
            Run("StringSearchEx(ContainsAny)--- WordsSearchEx(ContainsAny)代码相同", () => { stringSearchEx.ContainsAny(text); });
            Run("StringSearchEx2(ContainsAny)--- WordsSearchEx2(ContainsAny)代码相同", () => { stringSearchEx2.ContainsAny(text); });
            Run("StringSearchEx3(ContainsAny)--- WordsSearchEx3(ContainsAny)代码相同", () => { stringSearchEx3.ContainsAny(text); });
            Run("IllegalWordsSearch(ContainsAny)", () => { illegalWordsSearch.ContainsAny(text); });

            Run("StringSearch(FindFirst)", () => { stringSearch.FindFirst(text); });
            Run("StringSearchEx(FindFirst)", () => { stringSearchEx.FindFirst(text); });
            Run("StringSearchEx2(FindFirst)", () => { stringSearchEx2.FindFirst(text); });
            Run("StringSearchEx3(FindFirst)", () => { stringSearchEx3.FindFirst(text); });
            Run("WordsSearch(FindFirst)", () => { wordsSearch.FindFirst(text); });
            Run("WordsSearchEx(FindFirst)", () => { wordsSearchEx.FindFirst(text); });
            Run("WordsSearchEx2(FindFirst)", () => { wordsSearchEx2.FindFirst(text); });
            Run("WordsSearchEx3(FindFirst)", () => { wordsSearchEx3.FindFirst(text); });
            Run("IllegalWordsSearch(FindFirst)", () => { illegalWordsSearch.FindFirst(text); });


            Console.Write("-------------------- Find All 100000次 --------------------\r\n");
            Run("TrieFilter(FindAll)", () => { tf1.FindAll(text); });
            Run("FastFilter(FindAll)", () => { ff.FindAll(text); });
            Run("StringSearch(FindAll)", () => { stringSearch.FindAll(text); });
            Run("StringSearchEx(FindAll)", () => { stringSearchEx.FindAll(text); });
            Run("StringSearchEx2(FindAll)", () => { stringSearchEx2.FindAll(text); });
            Run("StringSearchEx3(FindAll)", () => { stringSearchEx3.FindAll(text); });

            Run("WordsSearch(FindAll)", () => { wordsSearch.FindAll(text); });
            Run("WordsSearchEx(FindAll)", () => { wordsSearchEx.FindAll(text); });
            Run("WordsSearchEx2(FindAll)", () => { wordsSearchEx2.FindAll(text); });
            Run("WordsSearchEx3(FindAll)", () => { wordsSearchEx3.FindAll(text); });
            Run("IllegalWordsSearch(FindAll)", () => { illegalWordsSearch.FindAll(text); });

            Console.Write("-------------------- Replace  100000次 --------------------\r\n");
            Run("TrieFilter(Replace)", () => { tf1.Replace(text); });
            Run("FastFilter(Replace)", () => { ff.Replace(text); });
            Run("StringSearch(Replace)", () => { stringSearch.Replace(text); });
            Run("WordsSearch(Replace)", () => { wordsSearch.Replace(text); });
            Run("StringSearchEx(Replace)--- WordsSearchEx(Replace)代码相同", () => { stringSearchEx.Replace(text); });
            Run("StringSearchEx2(Replace)--- WordsSearchEx2(Replace)代码相同", () => { stringSearchEx2.Replace(text); });
            Run("StringSearchEx3(Replace)--- WordsSearchEx3(Replace)代码相同", () => { stringSearchEx3.Replace(text); });
            Run("IllegalWordsSearch(Replace)", () => { illegalWordsSearch.Replace(text); });

            Console.Write("-------------------- Regex  100次 --------------------\r\n");
            Run(100, "Regex.IsMatch", () => { re.IsMatch(text); });
            Run(100, "Regex.Match", () => { re.Match(text); });
            Run(100, "Regex.Matches", () => { re.Matches(text); });

            Console.Write("-------------------- Regex used Trie tree  100次 --------------------\r\n");
            Run(100, "Regex.IsMatch", () => { re2.IsMatch(text); });
            Run(100, "Regex.Match", () => { re2.Match(text); });
            Run(100, "Regex.Matches", () => { re2.Matches(text); });

            Console.ReadKey();
        }
예제 #13
0
파일: Program.cs 프로젝트: 333G/NFine_Host
        static void Main(string[] args)
        {
            ReadBadWord();
            var text = File.ReadAllText("Talk.txt");

            Console.Write("-------------------- ToSenseWord Test --------------------\r\n");

            Run("ToSenseWord1  ", () => { WordTest.ToSenseWord1(text); });
            Run("ToSenseWord2  ", () => { WordTest.ToSenseWord2(text); });
            Run("ToSenseWord3  ", () => { WordTest.ToSenseWord3(text); });
            Run("ToSenseWord4  ", () => { WordTest.ToSenseWord4(text); });
            Run("ToSenseWord5  ", () => { WordTest.ToSenseWord5(text); });
            Run("ToSenseWord6  ", () => { WordTest.ToSenseWord6(text); });
            Run("ToSenseWord7  ", () => { WordTest.ToSenseWord7(text); });
            Run("ToSenseWord8  ", () => { WordTest.ToSenseWord8(text); });
            Run("ToSenseWord9  ", () => { WordTest.ToSenseWord9(text); });
            Run("ToSenseWord10  ", () => { WordTest.ToSenseWord10(text); });

            //Run("GetDisablePostion1  ", () => { WordTest.GetDisablePostion1(text); });
            //Run("GetDisablePostion2  ", () => { WordTest.GetDisablePostion2(text); });
            //Run("GetDisablePostion3  ", () => { WordTest.GetDisablePostion3(text); });
            //Run("GetDisablePostion4  ", () => { WordTest.GetDisablePostion4(text); });
            //Run("GetDisablePostion5  ", () => { WordTest.GetDisablePostion5(text); });
            //Run("GetDisablePostion6  ", () => { WordTest.GetDisablePostion6(text); });
            //Run("GetDisablePostion7  ", () => { WordTest.GetDisablePostion7(text); });
            //Run("GetDisablePostion9  ", () => { WordTest.GetDisablePostion9(text); });
            //Run("GetDisablePostion8  ", () => { WordTest.GetDisablePostion8(text); });

            Console.Write("-------------------- ToSenseIllegalWords --------------------\r\n");

            Run("ToSenseIllegalWords", () => { WordsHelper.ToSenseIllegalWords(text); });



            Console.Write("-------------------- FindFirst OR ContainsAny --------------------\r\n");
            Run("TrieFilter", () => { tf1.HasBadWord(text); });
            Run("FastFilter", () => { ff.HasBadWord(text); });
            Run("StringSearch(ContainsAny)", () => { word.ContainsAny(text); });
            Run("StringSearch(FindFirst)", () => { word.FindFirst(text); });
            Run("WordsSearch(ContainsAny)", () => { search.ContainsAny(text); });
            Run("WordsSearch(FindFirst)", () => { search.FindFirst(text); });
            Run("IllegalWordsQuickSearch(FindFirst)", () => { iword1.FindFirst(text); });
            Run("IllegalWordsQuickSearch(ContainsAny)", () => { iword1.ContainsAny(text); });

            Run("IllegalWordsSearch(FindFirst)", () => { iword2.FindFirst(text); });
            Run("IllegalWordsSearch(ContainsAny)", () => { iword2.ContainsAny(text); });

            Console.Write("-------------------- Find All --------------------\r\n");
            Run("TrieFilter(FindAll)", () => { tf1.FindAll(text); });
            Run("FastFilter(FindAll)", () => { ff.FindAll(text); });
            Run("StringSearch(FindAll)", () => { word.FindAll(text); });
            Run("WordsSearch(FindAll)", () => { search.FindAll(text); });
            Run("IllegalWordsQuickSearch(FindAll)", () => { iword1.FindAll(text); });
            Run("IllegalWordsSearch(FindAll)", () => { iword2.FindAll(text); });
            Console.Write("-------------------- Replace --------------------\r\n");
            Run("TrieFilter(Replace)", () => { tf1.Replace(text); });
            Run("FastFilter(Replace)", () => { ff.Replace(text); });
            Run("StringSearch(Replace)", () => { word.Replace(text); });
            Run("WordsSearch(Replace)", () => { search.Replace(text); });
            Run("IllegalWordsQuickSearch(Replace)", () => { iword1.Replace(text); });
            Run("IllegalWordsSearch(Replace)", () => { iword2.Replace(text); });

            Console.Write("-------------------- Regex --------------------\r\n");
            Run("Regex.IsMatch", () => { re.IsMatch(text); });
            Run("Regex.Match", () => { re.Match(text); });
            Run("Regex.Matches", () => { re.Matches(text); });

            Console.Write("-------------------- Regex used Trie tree --------------------\r\n");
            Run("Regex.IsMatch", () => { re2.IsMatch(text); });
            Run("Regex.Match", () => { re2.Match(text); });
            Run("Regex.Matches", () => { re2.Matches(text); });


            Console.ReadKey();
        }