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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值