NET脏字过滤算法(转)
感谢sumtech的回复和讨论,原本的效率已经足够网站实用了,虽然也想到一些改进方法,但是一直懒得去做。sumtech通过邮件跟我讨论,我也终于抽了时间做了改进,改进后的算法效率比原先的算法提高了400%,也就是仅需要原来的1/5时间。
算法关键是将两个BitArray合并成了byte[char.MaxValue],其中7个bit用来判断前7个字符,另一个bit判断其他字符。并且增加了minWordLength和charCheck,用来过滤过短的判断,以及仅有一个字符时的快速判断。
public class BadWordsFilter
{
private HashSet<string> hash = new HashSet<string>();
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()
{
}
public void Init(string[] badwords)
{
foreach (string word in badwords)
{
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();
}
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;
}
}
算法关键是将两个BitArray合并成了byte[char.MaxValue],其中7个bit用来判断前7个字符,另一个bit判断其他字符。并且增加了minWordLength和charCheck,用来过滤过短的判断,以及仅有一个字符时的快速判断。
public class BadWordsFilter
{
private HashSet<string> hash = new HashSet<string>();
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()
{
}
public void Init(string[] badwords)
{
foreach (string word in badwords)
{
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();
}
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;
}
}