コード例 #1
0
        /// <summary>
        /// 初始化 使用前必须调用一次
        /// </summary>
        /// <param name="keyWords">敏感词列表</param>
        public static void Init(List <string> keyWords)
        {
            //if (_root != null) return;
            if (keyWords.Count == 0)
            {
                return;
            }
            lock (LockObj)
            {
                _root = new FilterKeyWordsNode {
                    CharArray = new BitArray(char.MaxValue)
                };

                /*初始化特殊符号*/
                MapChar(SkipList);
                foreach (var c in SkipList)
                {
                    SkipBitArray[c] = true;
                }


                foreach (var key in keyWords)
                {
                    MapChar(key);
                }

                var list = keyWords.Distinct().ToArray();

                for (int i = 1; i <= list.Max(p => p.Length); i++)
                {
                    int i1        = i;
                    var startList = list.Where(p => p.Length >= i1).Select(p => p.Substring(0, i1)).Distinct().ToArray();
                    foreach (var startWord in startList)
                    {
                        var tmp = _root;
                        for (int j = 0; j < startWord.Length; j++)
                        {
                            var t = startWord[j];
                            tmp.CharArray[t] = true;

                            if (tmp.Child == null)
                            {
                                tmp.Child = new Dictionary <char, FilterKeyWordsNode>();
                            }

                            if (!tmp.Child.ContainsKey(t))
                            {
                                var thisCode = startWord.Substring(0, j + 1);
                                var node     = new FilterKeyWordsNode {
                                    CharArray = new BitArray(char.MaxValue), IsEnd = list.Contains(thisCode), Value = thisCode
                                };
                                tmp.Child.Add(t, node);
                            }

                            tmp = tmp.Child[t];
                        }
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 初始化 使用前必须调用一次
        /// </summary>
        /// <param name="data">敏感词列表</param>
        public static void Initialize(string data)
        {
            if (string.IsNullOrEmpty(data.Trim()))
            {
                return;
            }
            if (m_Root != null)
            {
                return;
            }
            var keyWords = data.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

            lock (m_LockObj)
            {
                m_Root = new FilterKeyWordsNode();
                var list = keyWords.Select(p => new string(Translation(p))).Distinct().OrderBy(p => p).ThenBy(p => p.Length).ToList();
                for (int i = list.Min(p => p.Length); i <= list.Max(p => p.Length); i++)
                {
                    int i1        = i;
                    var startList = list.Where(p => p.Length >= i1).Select(p => p.Substring(0, i1)).Distinct();
                    foreach (var startWord in startList)
                    {
                        var tmp = m_Root;
                        for (int j = 0; j < startWord.Length; j++)
                        {
                            var t = startWord[j];
                            if (tmp.Child == null)
                            {
                                tmp.Child = new Dictionary <char, FilterKeyWordsNode>();
                            }

                            if (!tmp.Child.ContainsKey(t))
                            {
                                tmp.Child.Add(t, new FilterKeyWordsNode {
                                    IsEnd = list.Contains(startWord.Substring(0, 1 + j))
                                });
                            }

                            tmp = tmp.Child[t];
                        }
                    }
                }
            }
        }
コード例 #3
0
ファイル: BadWordFilter.cs プロジェクト: kuxingseng/Hospital
        /// <summary>
        /// 初始化 使用前必须调用一次
        /// </summary>
        /// <param name="keyWords">敏感词列表</param>
        public static void Init(string[] keyWords)
        {
            if (mRoot != null)
            {
                return;
            }
            mRoot = new FilterKeyWordsNode();
            var list = keyWords.Distinct().OrderBy(p => p).ThenBy(p => p.Length).ToList();

            for (var i = list.Min(p => p.Length); i <= list.Max(p => p.Length); i++)
            {
                var i1        = i;
                var startList = list.Where(p => p.Length >= i1).Select(p => p.Substring(0, i1)).Distinct();
                foreach (var startWord in startList)
                {
                    var tmp = mRoot;
                    for (var j = 0; j < startWord.Length; j++)
                    {
                        var t = startWord[j];
                        if (tmp.Child == null)
                        {
                            tmp.Child = new Dictionary <char, FilterKeyWordsNode>();
                        }

                        if (!tmp.Child.ContainsKey(t))
                        {
                            tmp.Child.Add(t, new FilterKeyWordsNode {
                                IsEnd = list.Contains(startWord.Substring(0, 1 + j))
                            });
                        }

                        tmp = tmp.Child[t];
                    }
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// 初始化 使用前必须调用一次
        /// </summary>
        /// <param name="keyWords">敏感词列表</param>
        static void Init(string[] keyWords)
        {
            if (_root != null) return;
            lock (LockObj)
            {
                _root = new FilterKeyWordsNode();
                var list = keyWords.Select(p => new string(Translation(p))).Distinct().OrderBy(p => p).ThenBy(p => p.Length).ToList();
                for (int i = list.Min(p => p.Length); i <= list.Max(p => p.Length); i++)
                {
                    int i1 = i;
                    var startList = list.Where(p => p.Length >= i1).Select(p => p.Substring(0, i1)).Distinct();
                    foreach (var startWord in startList)
                    {
                        var tmp = _root;
                        for (int j = 0; j < startWord.Length; j++)
                        {
                            var t = startWord[j];
                            if (tmp.Child == null)
                                tmp.Child = new Dictionary<char, FilterKeyWordsNode>();

                            if (!tmp.Child.ContainsKey(t))
                            {
                                tmp.Child.Add(t, new FilterKeyWordsNode { IsEnd = list.Contains(startWord.Substring(0, 1 + j)) });
                            }

                            tmp = tmp.Child[t];
                        }
                    }
                }

            }
        }
コード例 #5
0
 public static void ReadKeyword(string[] keyWords)
 {
     _root = null;
     Init(keyWords.Distinct().ToArray());
 }