脏字处理思路


 http://www.360coding.com/html/ASP.NET/24071.html

ExpandedBlockStart.gif 代码
  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(); 
   } 
   } 
  } 

 

 

ExpandedBlockStart.gif 代码
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; } 
   } 
   
   } 
  } 

 

 

http://www.cnblogs.com/SkyD/archive/2009/03/16/updateTextVali.html

 

http://www.cnblogs.com/xingd/archive/2008/01/23/1050443.html

http://www.cnblogs.com/goody9807/archive/2006/09/12/502094.html

 

 

最后小结:

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Collections;

namespace  Pixysoft
{
    
class  BadWordHelper
    {
        
private   const   char  default_split_string  =   ' | ' ;
        
private   const   string  default_replace_string  =   " * " ;

        
private   int  maxWordLength  =   0 ;
        
private   int  minWordLength  =   int .MaxValue;

        
private  List < string >  hash  =   new  List < string > (); // 多字检查
         private   byte [] fastCheck  =   new   byte [ char .MaxValue]; // 多字检查
         private  BitArray charCheck  =   new  BitArray( char .MaxValue); // 单字检查


        
public  BadWordHelper( string  dictionary)
        {
            
if  ( string .IsNullOrEmpty(dictionary))
                
throw   new  Exception( " no dictionary founded. " );

            
string [] badwords  =  dictionary.Split(default_split_string);

            
foreach  ( string  word  in  badwords)
            {
                maxWordLength 
=  Math.Max(maxWordLength, word.Length);

                minWordLength 
=  Math.Min(minWordLength, word.Length);

                
for  ( int  i  =   0 ; i  <   7   &&  i  <  word.Length; i ++ )
                {
                    fastCheck[word[i]] 
|=  ( byte )( 1   <<  i);
                }

                
for  ( int  i  =   7 ; i  <  word.Length; i ++ )
                {
                    fastCheck[word[i]] 
|=   0x80 ;
                }

                
if  (word.Length  ==   1 )
                {
                    charCheck[word[
0 ]]  =   true ;
                }
                
else
                {
                    hash.Add(word);
                }
            }
        }

        
public   bool  HasBadWord( string  text)
        {
            
if  ( string .IsNullOrEmpty(text))
                
return   false ;

            
int  index  =   0 ;

            
while  (index  <  text.Length)
            {

                
if  ((fastCheck[text[index]]  &   1 ==   0 )
                {
                    
while  (index  <  text.Length  -   1   &&  (fastCheck[text[ ++ index]]  &   1 ==   0 ) ;
                }

                
// 单字节检测
                 if  (minWordLength  ==   1   &&  charCheck[text[index]])
                {
                    
return   true ;
                }

                
// 多字节检测
                 for  ( int  j  =   1 ; j  <=  Math.Min(maxWordLength, text.Length  -  index  -   1 ); j ++ )
                {
                    
// 快速排除
                     if  ((fastCheck[text[index  +  j]]  &  ( 1   <<  Math.Min(j,  7 )))  ==   0 )
                    {
                        
break ;
                    }
                    
if  (j  +   1   >=  minWordLength)
                    {
                        
string  sub  =  text.Substring(index, j  +   1 );

                        
if  (hash.Contains(sub))
                        {
                            
return   true ;
                        }
                    }
                }
                index
++ ;
            }
            
return   false ;
        }

        
public   string  ReplaceBadWord( string  text)
        {
            
int  index  =   0 ;

            
for  (index  =   0 ; index  <  text.Length; index ++ )
            {
                
if  ((fastCheck[text[index]]  &   1 ==   0 )
                {
                    
while  (index  <  text.Length  -   1   &&  (fastCheck[text[ ++ index]]  &   1 ==   0 ) ;
                }

                
// 单字节检测
                 if  (minWordLength  ==   1   &&  charCheck[text[index]])
                {
                    text 
=  text.Replace(text[index], default_replace_string[ 0 ]);

                    
continue ;
                }

                
// 多字节检测
                 for  ( int  j  =   1 ; j  <=  Math.Min(maxWordLength, text.Length  -  index  -   1 ); j ++ )
                {
                    
// 快速排除
                     if  ((fastCheck[text[index  +  j]]  &  ( 1   <<  Math.Min(j,  7 )))  ==   0 )
                    {
                        
break ;
                    }
                    
if  (j  +   1   >=  minWordLength)
                    {
                        
string  sub  =  text.Substring(index, j  +   1 );

                        
if  (hash.Contains(sub))
                        {
                            
// 替换字符操作
                             char  cc  =  default_replace_string[ 0 ];
                            
string  rp  =  default_replace_string.PadRight((j  +   1 ), cc);
                            text 
=  text.Replace(sub, rp);
                            
// 记录新位置
                            index  +=  j;
                            
break ;
                        }
                    }
                }
            }

            
return  text;
        }
    }
}

 

 

 

 

转载于:https://www.cnblogs.com/zc22/archive/2010/08/19/1803589.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值