发个简单的脏字过滤代码

算法采用的是一棵N叉树,用Hashtable实现
使用方法:
Filter filter = new Filter();
filter.Inital("filter.txt");
Console.WriteLine(filter.Pass("去台,要不要你好不好"));
Console.WriteLine(filter.Pass("我好开心"));
Console.WriteLine(filter.Pass("我好吧"));

filter.txt中的脏字可以用空格,TAB或者回车分开
如:
你好
我好吧
我好开心
我好开啊
好开心啊

关键代码一:Filter.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;

namespace filter
{
    
class Filter
    
{
        
private Hashtable h = new Hashtable();

        
public Filter()
        

            
        }


        
public void Inital(string filename)
        
{
            
string[] strs = GetBadWords(filename);
            
foreach (string str in strs)
            
{
                AddBadWord(str);
            }

        }


        
private void AddBadWord(string str)
        
{
            
char c = str[0];
            
if (h.ContainsKey(c))
            
{
                TreeNode node 
= (TreeNode)h[c];
                AddNode(node, str, 
1);
            }

            
else
            
{
                TreeNode node 
= new TreeNode();
                node.Text 
= c;
                
if (str.Length == 1)
                    node.Leaf 
= true;
                
else
                    node.Leaf 
= false;
                
                h.Add(c, node);
                AddNode(node, str, 
1);
            }

        }


        
private void AddNode(TreeNode node, string str, int p)
        
{
            
if (str.Length > p)
            
{
                
if (node.ChildNodes == null)
                    node.ChildNodes 
= new Hashtable();
                
                TreeNode n;
                
if (!node.ChildNodes.ContainsKey(str[p]))
                
{
                    n 
= new TreeNode();
                    n.Text 
= str[p];
                    
if (str.Length == p + 1)
                        n.Leaf 
= true;
                    
else
                        n.Leaf 
= false;

                    node.ChildNodes.Add(str[p], n);
                }

                
else
                    n 
= (TreeNode)node.ChildNodes[str[p]];

                AddNode(n, str, p 
+ 1);
            }


        }

        
public bool Pass(string str)
        
{
            
int total, current;
            total 
= str.Length;
            
for (current = 0; current < total; current++)
            
{
                
if (h.ContainsKey(str[current]))
                
{
                    TreeNode node 
= (TreeNode)h[str[current]];
                    
if (containChar(node, str, current + 1,total))
                        
return false;
                }

            }

            
return true;
        }


        
private bool containChar(TreeNode node, string str, int p,int total)
        
{
            
            
if (node.Leaf == true)
                
return true;

            
if (p >= total)
                
return false;

            
if (node.ChildNodes.ContainsKey(str[p]))
            
{
                TreeNode n 
= (TreeNode)node.ChildNodes[str[p]];
                
return containChar(n, str, p + 1, total);
            }

            
else
                
return false;

        }


        
private string[] GetBadWords(string filename)
        
{
            List
<string> list = new List<string>();
            
using (StreamReader sr = new StreamReader(filename, Encoding.Default))
            
{
                
string str = sr.ReadToEnd();
                MatchCollection mc 
= Regex.Matches(str, @"\S+");
                
foreach (Match m in mc)
                
{
                    Console.WriteLine(m.Value);
                    list.Add(m.Value);
                }

            }

            
return list.ToArray();
        }

    }

}


关键代码二:TreeNode.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace filter
{
    
public class TreeNode
    
{
        
private Hashtable childNodes;

        
public Hashtable ChildNodes
        
{
            
get return childNodes; }
            
set { childNodes = value; }
        }
     
   

        
private bool leaf;

        
public bool Leaf
        
{
            
get return leaf; }
            
set { leaf = value; }
        }

        
private char text;

        
public char Text
        
{
            
get return text; }
            
set { text = value; }
        }

       
    }

}


算法总结:
算法本身很简单,实现起来也很容易。时间复杂度较接近0(n)
改进的一些想法:如果能把N叉树内的结点做类似KMP的处理就好了。
发现BUG请联系我nid007 at 163.com

posted on 2008-02-02 11:30  niddle  阅读(1829)  评论(3)    收藏  举报

导航