仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 815|回复: 11
打印 上一主题 下一主题

[学习教程] ASP.NET网站制作之.NET脏字过滤算法,晋级版仓酷云

[复制链接]
小妖女 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:19:59 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
对于new隐藏成员的作用,往往是出于使用了一个第三方类库,而你又无法获得这个类库的源代码,当你继承这个类库的某个类时,你需要重新实现其中的一个方法,而又需要与父类中的函数使用同样的函数,这是就需要在自定义的子类中把那个同名函数(或成员)加上new标记,从而隐藏父类中同名的成员。再度改善,在脏字大概存在的情形下,比方呈现了多个脏字前Length-1部分时,功能比拟/dotnet/20111127/315436.html中形貌的又提拔了300%~400%。

间接贴出全体代码了,经由过程新增的一个byte[char.MaxValue]和BitArray(char.MaxValue),削减了大批的Substring和GetHashCode的挪用。耗的内存也不算多,除HashSet外,仅必要144k内存。

援用此文大概利用此代码请申明出处,感谢,以便于我未来的更新。

2008-02-02订正:if(index>0||(fastCheck[text[index]]&1)==0)应往失落index>0的判别,这个优化思索的不敷成熟。感激sumtec和灵感之源指堕落误。制止最短婚配时,能够在if(hash.Contains(sub))以后,能够到场判别if((fastLength[begin]>>Math.Min(j,7))==0),然后再returntrue。

2008-02-03订正:for轮回外部的if((fastCheck[current]&1)==0)应为if((fastCheck[current]&1)==0&&count==j)。修改bug并到场巨细写敏感后,效力下降1倍。
publicclassBadWordsFilter
{
privateHashSet<string>hash=newHashSet<string>();
privatebyte[]fastCheck=newbyte[char.MaxValue];
privatebyte[]fastLength=newbyte[char.MaxValue];
privateBitArraycharCheck=newBitArray(char.MaxValue);
privateBitArrayendCheck=newBitArray(char.MaxValue);
privateintmaxWordLength=0;
privateintminWordLength=int.MaxValue;

publicBadWordsFilter()
{

}

publicvoidInit(string[]badwords)
{
foreach(stringwordinbadwords)
{
maxWordLength=Math.Max(maxWordLength,word.Length);
minWordLength=Math.Min(minWordLength,word.Length);

for(inti=0;i<7&&i<word.Length;i++)
{
fastCheck[word]|=(byte)(1<<i);
}

for(inti=7;i<word.Length;i++)
{
fastCheck[word]|=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);
}
}
}

publicstringFilter(stringtext,stringmask)
{
thrownewNotImplementedException();
}

publicboolHasBadWord(stringtext)
{
intindex=0;

while(index<text.Length)
{
intcount=1;

if(index>0||(fastCheck[text[index]]&1)==0)
{
while(index<text.Length-1&&(fastCheck[text[++index]]&1)==0);
}

charbegin=text[index];

if(minWordLength==1&&charCheck[begin])
{
returntrue;
}

for(intj=1;j<=Math.Min(maxWordLength,text.Length-index-1);j++)
{
charcurrent=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])
{
stringsub=text.Substring(index,j+1);

if(hash.Contains(sub))
{
returntrue;
}
}
}
}

index+=count;
}

returnfalse;
}
}前几天同学问我学习方向的问题。有点想法,不知道对不对,怕误导同学,现在“开源一下”。注:括号内是我现在整理的时填加上的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-6-18 21:23

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表