DFA有限无穷自动机的应用-敏感关键字的过滤

DFA核心代码

public class DFA
    {
        public static Node rootNode = new Node('R');

        /// <summary>
        /// 根据用户输入的关键词列表查找
        /// </summary>
        public List<string> SearchWord(string userStr)
        {
            List<String> words = new List<String>();

            List<String> word = new List<String>();
            int a = 0;

            char[] chars = userStr.ToCharArray();
            Node node = rootNode;
            while (a < chars.Count())
            {
                node = findNode(node, chars[a]);
                if (node == null)
                {
                    node = rootNode;
                    a = a - word.Count;
                    word.Clear();
                }
                else if (node.flag == 1)
                {
                    word.Add(chars[a].ToString());
                    StringBuilder sb = new StringBuilder();
                    foreach (String str in word)
                    {
                        sb.Append(str);
                    }
                    words.Add(sb.ToString());
                    a = a - word.Count + 1;
                    word.Clear();
                    node = rootNode;
                }
                else
                {
                    word.Add(chars[a].ToString());
                }
                a++;
            }

            return words;
        }

        /// <summary>
        /// 创建树
        /// </summary>
        public static Node CreateTree(List<string> slist)
        {
            foreach (string str in slist)
            {
                char[] chars = str.ToCharArray();
                if (chars.Count() > 0)
                {
                    //node.nodes.Add(CreateNode(rootNode, chars, 0));
                    CreateNode(rootNode, chars, 0);
                }
            }
            return rootNode;
        }

        private static Node CreateNode(Node node, char[] cs, int index)
        {
            Node n = findNode(node, cs[index]);
            if (n == null)
            {
                n = new Node(cs[index]);
                node.nodes.Add(n);
            }

            if (index == (cs.Count() - 1))
                n.flag = 1;

            index++;
            if (index < cs.Count())
            {
                CreateNode(n, cs, index);
            }

            return node;

        }

        private static Node findNode(Node node, char c)
        {
            List<Node> nodes = node.nodes;
            Node rn = null;
            foreach (Node n in nodes)
            {
                if (n.c == c)
                {
                    rn = n;
                    break;
                }
            }
            return rn;
        }
    }

    public class Node
    {
        public char c;
        public int flag; //1:表示终结,0:延续   
        public List<Node> nodes = new List<Node>();

        public Node(char c)
        {
            this.c = c;
            this.flag = 0;
        }

        public Node(char c, int flag)
        {
            this.c = c;
            this.flag = flag;
        }
    }

  

搜索查找代码

//过滤脏词
string cacheKeyName = “cacheKeyName”;
Node node = System.Web.HttpRuntime.Cache.Get(cacheKeyName) as Node;
if (null == node)//缓存为空则添加
{
IList<FiltKeywordsVM> listVM = KeywordManage.GetProFiltKeywordList();
List<string> list = new List<string>();
foreach (FiltKeywordsVM item in listVM)
{
list.Add(item.NAME.ToUpper());
}
node = DFA.CreateTree(list);
DateTime time = DateTime.Now.AddHours(ConstClass.FiltKeywordsCacheTime);//Web缓存过期时间
System.Web.HttpRuntime.Cache.Add(cacheRsultKeyName, node, null,
time, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
}

//DFA去过滤脏词
DFA dfa = new DFA();
List<string> c = dfa.SearchWord(title.ToUpper());

  

原文地址:https://www.cnblogs.com/guoxiongfei/p/3372517.html