. Net dirty word filtering algorithm)

Source: Internet
Author: User
Source: xingd.net-blog
But in my tests, RegEx is about twice faster. However, I am still not satisfied. I should use a lot of dirty words for filtering on our website, which has some impact on efficiency. After some consideration, I made Algorithm . I tested it on my machine and used the dirty Character Library in the original text, the string length of 0x19c, 1000 loops, 1933.47 ms for text search, and 1216.719 ms for RegEx, my algorithm only uses 244.125 Ms.

The main algorithms are as follows:CodeAs shown in
Private Static dictionary DIC = New Dictionary ();
Private Static bitarray fastcheck = new bitarray (char. maxvalue );
Static void prepare ()
{
String [] badwords = // read from File
Foreach (string word in badwords)
{
If (! Dic. containskey (Word ))
{
Dic. Add (word, null );
Maxlength = math. Max (maxlength, word. Length );
Int value = word [0];
Fastcheck [word [0] = true;
}
}
}

when using
int Index = 0;
while (index {< br> If (! Fastcheck [target [Index])
{< br> while (index }< br> for (Int J = 0; j {< br> string sub = target. substring (index, J);
If (DIC. containskey (sub)
{< br> Sb. replace (sub, "***", index, J);
index + = J;
break;
}< BR >}< br> index ++;
}

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.