Esempio n. 1
0
        /// <summary>
        /// 在文本中查找所有的关键字
        /// </summary>
        /// <param name="text">文本</param>
        /// <returns></returns>
        public virtual List <IllegalWordsSearchResult> FindAll(string text)
        {
            List <IllegalWordsSearchResult> result = new List <IllegalWordsSearchResult>();

            TrieNode ptr   = null;
            char     pChar = (char)0;

            for (int i = 0; i < text.Length; i++)
            {
                char c = ToSenseWord(text[i]);
                if (_skipBitArray[c])
                {
                    continue;
                }
                TrieNode tn;
                if (ptr == null)
                {
                    tn = _first[c];
                }
                else if (ptr.TryGetValue(c, out tn) == false)
                {
                    tn = (c != pChar) ? _first[c] : ptr;
                }
                if (tn != null)
                {
                    if (ptr != tn)
                    {
                        tn.LastMatchLocation = i;
                        tn.Parent            = ptr;
                    }
                    if (tn.End)
                    {
                        foreach (var find in tn.Results)
                        {
                            var p = tn.GetParent(find.Length - 1);
                            var r = new IllegalWordsSearchResult(find, p.LastMatchLocation, i, text);
                            result.Add(r);
                        }
                    }
                }
                ptr   = tn;
                pChar = c;
            }
            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// 从文本中查找到第一个关键字
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public override IllegalWordsSearchResult FindFirst(string text)
        {
            IllegalWordsSearchResult result = null;

            search(text, (keyword, ch, end) => {
                var start = end + 1 - keyword.Length;
                result    = GetIllegalResult(keyword, ch, start, end, text, text);
                return(result != null);
            });
            if (result != null)
            {
                return(result);
            }
            var searchText = WordsHelper.ToSenseIllegalWords(text);

            search(searchText, (keyword, ch, end) => {
                var start = end + 1 - keyword.Length;
                result    = GetIllegalResult(keyword, ch, start, end, text, searchText);
                return(result != null);
            });
            if (result != null)
            {
                return(result);
            }
            searchText = WordsHelper.RemoveNontext(searchText);
            search(searchText, (keyword, ch, end) => {
                var start = end;
                for (int i = 0; i < keyword.Length; i++)
                {
                    var n = searchText[start--];
                    while (n == 1)
                    {
                        n = searchText[start--];
                    }
                }
                start++;
                result = GetIllegalResult(keyword, ch, start, end, text, searchText);
                return(result != null);
            });
            if (result != null)
            {
                return(result);
            }
            return(IllegalWordsSearchResult.Empty);
        }