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<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.