Method One: Use regular expressions
Copy Code code as follows:
Dirty dictionary data store file path
private static string File_name= "Zang.txt";
Dirty data dictionary table, such as: Dirty data One | Dirty data two | dirty data three
public static string Dirtystr= "";
Public Validdirty ()
{
if (httpruntime.cache["Regex"]==null)
{
Dirtystr=readdic ();
Regular expression used to detect dirty dictionaries
Regex validatereg= The new Regex ("^ (?!") +dirtystr+ "). (? <! " +dirtystr+ ")) *$", regexoptions.compiled|regexoptions.explicitcapture);
HttpRuntime.Cache.Insert ("Regex", Validatereg,null,datetime.now.addminutes (), TimeSpan.Zero);
}
}
private String Readdic ()
{
file_name=environment.currentdirectory+ "\" +file_name;
if (! File.exists (file_name))
{
Console.WriteLine ("{0} does not exist.", file_name);
Return "";
}
StreamReader sr = File.OpenText (file_name);
String input= "";
while (Sr. Peek () >-1)
{
input = Sr. ReadLine ();
}
Sr. Close ();
return input;
}
public bool Validbyreg (string str)
{
Regex reg= (regex) httpruntime.cache["regex"];
Return Reg. IsMatch (str);
}
Feel this method of execution is not very high, simple test 1000 words of the article, Dirty Dictionary has more than 800 keywords
The formula is 1.238 seconds, we have no better way, please do not hesitate to enlighten!
Method Two: General circulation search method
Copy Code code as follows:
public bool Validgeneral (string str)
{
if (! File.exists (file_name))
{
Console.WriteLine ("File path or file path does not have error message");
return false;
}
Else
{
StreamReader objreader = new StreamReader (file_name,system.text.encoding.getencoding ("gb2312"));
String Sline= "";
ArrayList arrtext = new ArrayList ();
while (sline!= null)
{
sline = objReader.ReadLine ();
if (sline!= null)
Arrtext.add (sline);
}
Objreader.close ();
foreach (String soutput in Arrtext)
{
String[] Strarr=soutput.split (' | ');
for (int i = 0; i < strarr.length; i++)
{
if (str. IndexOf (Strarr[i])!=-1)
{
return false;
}
}
}
return true;
}
}
The following is the test method, what is the problem please point out!
Copy Code code as follows:
DateTime T1 =datetime.now;
String str= "213";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
str+= "Cherish crystal Love cherish crystal Love cherish crystal Love";
Validdirty vd=new Validdirty ();
Console.WriteLine (VD. Validbyreg (str));
DateTime T2 =datetime.now;
TimeSpan ts=t2-t1;
Console.WriteLine (TS. TotalMilliseconds);
Console.read ();
Algorithm |
Retrieving text file length/time consuming (MS) |
Regular algorithm |
10 Chinese Characters/980 |
100 Chinese Characters/999 |
1000 Chinese Characters/1234 |
Common algorithm |
10 Chinese characters/234 |
100 Chinese Characters/234 |
1000 Chinese Characters/265 |
Dirty Dictionary Download
However, this article may have some problems, suggest to look at the following 2 articles before deciding to use.