Ejemplo n.º 1
0
        public void test3()
        {
            string s    = "中国|国人|zg人";
            string test = "我是中国人";

            WordsSearchEx2 wordsSearch = new WordsSearchEx2();

            wordsSearch.SetKeywords(s.Split('|').ToList());

            var b = wordsSearch.ContainsAny(test);

            Assert.AreEqual(true, b);


            var f = wordsSearch.FindFirst(test);

            Assert.AreEqual("中国", f.Keyword);

            var alls = wordsSearch.FindAll(test);

            Assert.AreEqual("中国", alls[0].Keyword);
            Assert.AreEqual(2, alls[0].Start);
            Assert.AreEqual(3, alls[0].End);
            Assert.AreEqual(0, alls[0].Index);//返回索引Index,默认从0开始
            Assert.AreEqual("国人", alls[1].Keyword);
            Assert.AreEqual(2, alls.Count);

            var t = wordsSearch.Replace(test, '*');

            Assert.AreEqual("我是***", t);
        }
Ejemplo n.º 2
0
        static List <string> ReadBadWord()
        {
            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)
                    {
                        tf1.AddKey(key);

                        ff.AddKey(key);

                        list.Add(key);
                    }
                    key = sw.ReadLine();
                }
            }
            stringSearch.SetKeywords(list);
            stringSearchEx.SetKeywords(list);
            stringSearchEx2.SetKeywords(list);
            stringSearchEx3.SetKeywords(list);
            wordsSearch.SetKeywords(list);
            wordsSearchEx.SetKeywords(list);
            wordsSearchEx2.SetKeywords(list);
            wordsSearchEx3.SetKeywords(list);
            //illegalWordsSearch.SetKeywords(list);

            list = list.OrderBy(q => q).ToList();
            var str = string.Join("|", list);

            str = Regex.Replace(str, @"([\\\.\+\*\-\(\)\[\]\{\}!])", @"\$1");

            re = new Regex(str, RegexOptions.IgnoreCase);


            var str2 = tf1.ToString();

            //str2 = Regex.Replace(str2, @"([\.\+\*\-\[\]\{\}!])", @"\$1");
            re2 = new Regex(str2);

            return(list);
        }