Last active
August 29, 2015 14:08
-
-
Save mi3tek-amb/fc86ee96909930240788 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* jQuery SpamChecker Js | |
*---------------------------------------------------- | |
* The MIT License (MIT) Copyright (c) 2014 | | |
* copyright : Mistuki Suzuki | | |
* | | |
*---------------------------------------------------- | |
*/ | |
;!function(j){ | |
j.fn.spamChecker = function spamChecker(options) | |
{ | |
return this.each(function(){ | |
new spamChecker.init(this, options); | |
}); | |
}, | |
j.fn.spamChecker.init = function(elements, options) | |
{ | |
var self = this, | |
queryElems, fncs=[]; | |
j.extend(self.options, options), | |
queryElems = j(elements).find(self.options.query), | |
j.each(queryElems, function(i){ | |
var element = this, | |
text = j(element).text(); | |
fncs[i] = j.ajax({ | |
url:'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='+encodeURIComponent(text), | |
dataType:'jsonp' | |
}).then( | |
function(res){ | |
self.jury(res, text, element) | |
}, | |
function(){ | |
self.options.error.call(element, arguments) | |
} | |
) | |
}), | |
j.when.apply(j,fncs).done( | |
function(){ | |
self.options.complete.call(elements, arguments) | |
} | |
); | |
}, | |
j.fn.spamChecker.init.prototype = | |
{ | |
jury : function( resultData, text, element ) | |
{ | |
var self = this, | |
i, j, result, similarRes = {} | |
, threshold = self.options.threshold, spamPoint = 0 | |
, resultData = resultData.responseData.results; | |
for( i = 0; (result = resultData[i] ) != null; i++ ) | |
{ | |
for(j = 0; j < self.spamWords.length; j = j++ ) | |
{ | |
if( text.match( RegExp(self.spamWords[j]) ) ) | |
{ | |
threshold = threshold - 2; | |
} | |
}; | |
similarRes = self.similar_text(result.content, text); | |
if( similarRes.similar > threshold ) | |
{ | |
spamPoint = spamPoint + 1; | |
}; | |
}; | |
if( spamPoint > 3 ) | |
{ | |
self.options.correct.call( element, similarRes, spamPoint ); | |
} | |
else | |
{ | |
self.options.incorrect.call( element, similarRes, spamPoint ); | |
}; | |
}, | |
similar_str : function(text1, len1, text2, len2) | |
{ | |
var pos1 = 0, | |
pos2 = 0, | |
max = 0, | |
i, j, l; | |
for (i = 0; i < len1; ++i) | |
{ | |
for (j = 0; j < len2; ++j) | |
{ | |
for (l = 0; | |
(i + l < len1) && (j + l < len2) && | |
(text1.charAt(i + l) == text2.charAt(j + l)); ++l); | |
if (l > max) | |
{ | |
max = l; | |
pos1 = i; | |
pos2 = j; | |
} | |
} | |
} | |
return { | |
'pos1': pos1, | |
'pos2': pos2, | |
'max': max | |
}; | |
}, | |
similar_char : function(text1, len1, text2, len2) | |
{ | |
var r = this.similar_str(text1, len1, text2, len2), | |
sum = r.max; | |
if (sum) | |
{ | |
if (r.pos1 && r.pos2) | |
{ | |
sum += this.similar_char(text1, r.pos1, text2, r.pos2); | |
} | |
if ((r.pos1 + r.max < len1) && (r.pos2 + r.max < len2)) | |
{ | |
sum += this.similar_char( | |
text1.substr(r.pos1 + r.max), | |
len1 - r.pos1 - r.max, | |
text2.substr(r.pos2 + r.max), | |
len2 - r.pos2 - r.max); | |
} | |
} | |
return sum; | |
}, | |
similar_text : function(text1, text2) | |
{ | |
var sim = this.similar_char(text1, text1.length, text2, text2.length); | |
return { | |
'similar': sim, | |
'percent': sim * 200.0 / (text1.length + text2.length) | |
}; | |
}, | |
spamWords : [ | |
'他の人の意見', | |
'ameblo.jp', | |
'芸能プロダクション', | |
'色んなブログ', | |
'いろんな方のブログ', | |
'読者登録', | |
'また遊び', | |
'また来ま', | |
'更新楽し', | |
'ぜひ遊びに', | |
'また覗かせ', | |
'交流できれば', | |
'無料プレゼント' | |
], | |
options : { | |
correct : function(){}, | |
incorrect : function(){}, | |
complete : function(){}, | |
error : this.incorrect, | |
threshold : 20, | |
query : 'li' | |
} | |
} | |
}(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment