C # Dirty word Filtering Algorithm

Source: Internet
Author: User

Public class DirtyWordOper
{
Private static Dictionary <string, object> hash = new Dictionary <string, object> ();
Private static BitArray firstCharCheck = new BitArray (char. MaxValue); // record the first character of a dirty word
Private static BitArray allCharCheck = new BitArray (char. MaxValue); // records all the characters of each dirty word
Private static int maxLength = 0 ;//
Private static bool onlyOne = true;

# Region
/// <Summary>
/// Return the length of the replaced string.
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Public string Replace (string text)
{
If (onlyOne)
{
Init (); // initialization data will not be executed once
OnlyOne = false;
}
If (! IsDirtyword (text ))
{
Return text;
}
// Obtain the replacement operation table
List <DetailRepModel> drlist = GetList (text );
// Perform the Replacement Operation
Return Replace2 (text, drlist );
}

/// <Summary>
/// Perform initialization only once
/// </Summary>
/// <Param name = "text"> </param>
Private static void Init ()
{
String [] badwords = DirtyWordData. DirtyKeyword. Split (| );
Foreach (string bw in badwords)
{
String [] strarrtemp = bw. Split (&);
String word = strarrtemp [0];
Word = word. Trim (); // remove spaces and format symbols from the data.
Word = word. Replace ("/r ","");
Word = word. Replace ("/n ","");
If (word = "")
{
Break;
}
If (! Hash. ContainsKey (word ))
{
Hash. Add (word, null );
MaxLength = Math. Max (maxLength, word. Length );
FirstCharCheck [word [0] = true;

Foreach (char c in word)
{
AllCharCheck [c] = true;
}
}
}
}
/// <Summary>
/// Whether dirty words are contained
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Private static bool isDirtyword (string text)
{
Int index = 0;
// Int offset = 0;
While (index <text. Length)
{
// If the first character does not match
If (! FirstCharCheck [text [index])
{// Locate the same as the first character of a dirty word
While (index <text. Length-1 &&! FirstCharCheck [text [++ index]);
}
For (int j = 1; j <= Math. Min (maxLength, text. Length-index); j ++)
{
& Nb

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.