Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Data;
Using System. collections;
Namespace Bll. Common
{
# Region Operation
Public Class Keywordsfilter
{
# Region Keyword Filtering
/// <Summary>
/// Keyword Filtering
///
/// </Summary>
/// <Param name = "keywords"> </param>
/// <Returns> </returns>
Public Static String Filter ( String Keywords)
{
// Set of keywords to be filtered
List < String > Badwords = New List < String > ();
Keywordsfilterclass KF = New Keywordsfilterclass ();
Keywords = KF. badwordinkeywords (keywords, badwords );
Return Keywords;
}
# Endregion
}
# Endregion
# Region Keyword Filtering
/// <Summary>
/// Keyword Filtering
/// </Summary>
Public Class Keywordsfilterclass
{
Private Dictionary < String , Object > Hash = New Dictionary < String , Object > ();
// Storage of dirty words starting with a dictionary
Private Bitarray firstcharcheck = New Bitarray ( Char . Maxvalue );
// Dirty dictionary single char Storage
Private Bitarray allcharcheck = New Bitarray ( Char . Maxvalue );
Private Int Maxlength = 0 ;
/// <Summary>
/// Initialize the stored filter string
/// </Summary>
/// <Param name = "Words"> </param>
Private Void Inithash (list < String > Badwords)
{
Foreach ( String Word In Badwords)
{
// Save dirty words that do not exist in the dictionary
If ( ! Hash. containskey (Word ))
{
Hash. Add (word, Null );
// Set the length of dirty words
This . Maxlength = Math. Max ( This . Maxlength, word. Length );
Firstcharcheck [word [ 0 ] = True ;
Foreach ( Char C In Word)
{
Allcharcheck [c] = True ;
}
}
}
}
/// <Summary>
/// Replace the dirty characters in the string with the specified characters.
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Public String Badwordinkeywords ( String Text, list < String > Badwords)
{
// Initialize dirty word dictionary
This . Inithash (badwords );
Int Index = 0 ;
While (Index < Text. length)
{
// Determine the dirty characters at the beginning
If ( ! Firstcharcheck [Text [Index])
{
// Index accumulation if no dirty characters at the beginning are found
While (Index < Text. Length - 1 && ! Firstcharcheck [Text [ ++ Index]);
}
For ( Int J = 1 ; J <= Math. Min (maxlength, text. Length - Index); j ++ )
{
If ( ! Allcharcheck [Text [Index + J - 1 ])
{
Break ;
}
String Sub = Text. substring (index, J );
If (Hash. containskey (sub ))
{
Text = Text. Replace (sub, " ** " );
// This. inithash (badwords );
Index + = J;
Break ;
}
}
Index ++ ;
}
Return Text;
}
}
# Endregion
}