Keyword Filtering Algorithm

Source: Internet
Author: User

 

  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
}

 

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.