Efficient. NET dirty word filtering algorithm and application example

Source: Internet
Author: User
Examples of this article describe efficiency. NET dirty word filtering algorithm. Share to everyone for your reference, as follows:

BadWordsFilter.cs class

Using system;using system.collections.generic;using system.linq;using system.web;using System.Collections;using System.data;namespace wnf{public class Badwordsfilter {private hashset<string> hash = new HASHSET&LT;STRING&G t; (); Keyword Private byte[] Fastcheck = new Byte[char.    MaxValue]; Private byte[] Fastlength = new Byte[char.    MaxValue]; Private BitArray Charcheck = new BitArray (char.    MaxValue); Private BitArray Endcheck = new BitArray (char.    MaxValue);    private int maxwordlength = 0; private int minwordlength = Int.    MaxValue; Public Badwordsfilter () {}//Initialize the keyword public void Init (DataTable badwords) {for (int j = 0; J < Bad Words. Rows.Count; J + +) {string word = badwords. Rows[j][0].        ToString (); 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 {fastlength[word[0]] |= (byte) (1 << (Math.min (7, Word.          (Length-2))); Endcheck[word[word.          Length-1]] = true; Hash.        ADD (word);    }}} public string Filter (string text, string mask) {throw new NotImplementedException ();      }//Check if there is a keyword public bool Hasbadword (string text) {int index = 0; while (Index < text.        Length) {int count = 1; if (Index > 0 | | | (Fastcheck[text[index]] & 1) = = 0) {while (Index < text.        Length-1 && (Fastcheck[text[++index]] & 1) = = 0);        } char begin = Text[index];        if (minwordlength = = 1 && charcheck[begin]) {return true; } for (int j = 1; J <=Math.min (maxwordlength, text. LENGTH-INDEX-1);          J + +) {Char current = Text[index + j];          if ((Fastcheck[current] & 1) = = 0) {++count;          } if ((Fastcheck[current] & (1 << math.min (J, 7)) = = 0) {break; } if (j + 1 >= minwordlength) {if ((Fastlength[begin] & (1 << math.min (j-1, 7 ))) > 0 && endcheck[current]) {string sub = text.              Substring (Index, J + 1); if (hash.              Contains (sub)) {return true;      }}}} index + = count;    } return false; }  }}

Reference:

String sql = "Select keywords from Tb_keyword"; Badwordsfilter badwordfilter = new Badwordsfilter ();//Initialize the keyword badwordfilter. Init (Oetb.getdataset (SQL). Tables[0]);//Check to see if there is a keyword bool a = Badwordfilter. Hasbadword (TextBox1.Text), if (a = = True) {    page.registerclientscriptblock ("A", "<script>alert" the comment contains illegal text! ') </script> ");} else{    Pinglun ();//write Comment form}

It is hoped that this article will help you with ASP.

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.