Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function reduce(numerator,denominator){
- var gcd = function gcd(a,b){
- return b ? gcd(b, a%b) : a;
- };
- gcd = gcd(numerator,denominator);
- return [numerator/gcd, denominator/gcd];
- }
- function spamDetection(messages, spamSignals) {
- let res = ["passed","","passed","passed"], message, user, msgByUsr = {}, fewerThan5 = 0, contain = 0, contWords = [];
- Array.prototype.unique = function() {
- var a = this.concat();
- for(var i=0; i<a.length; ++i) {
- for(var j=i+1; j<a.length; ++j) {
- if(a[i] === a[j])
- a.splice(j--, 1);
- }
- }
- return a;
- };
- for(let i = 0; i<messages.length; i++){
- message = messages[i][0];
- user = messages[i][1];
- // Check less than 5 words
- if(message.split(" ").length<5) fewerThan5++;
- // Check 50% has the same content from the same user
- if(msgByUsr[user] !== undefined){
- if(msgByUsr[user][message] !== undefined) msgByUsr[user][message]++;
- else msgByUsr[user][message] = 1;
- }
- else{
- msgByUsr[user] = {};
- msgByUsr[user][message] = 1;
- }
- // Contain suspicious word
- for(let j = 0; j<spamSignals.length; j++){
- if(message.replace(/[^a-z^\ ]/gi, ' ').toLowerCase().split(" ").indexOf(spamSignals[j]) != -1 ){
- contain++;
- // if(contWords[spamSignals[j]] === undefined) contWords[spamSignals] = 0
- contWords.push(spamSignals[j]);
- }
- }
- }
- console.log(msgByUsr);
- let frac = reduce(fewerThan5, messages.length)
- if(fewerThan5/messages.length > 0.9) res[0] = "failed: "+frac[0]+"/"+frac[1];
- let amount = 0, max = -1, maxAll = -1, amountAll = 0, maxMsg, maxMsgAll;
- Object.keys(msgByUsr).forEach(function(key1){
- amount = 0; max = -1;
- Object.keys(msgByUsr[key1]).forEach(function(key2){
- amount += msgByUsr[key1][key2];
- if(max < msgByUsr[key1][key2]){
- max = msgByUsr[key1][key2];
- maxMsg = key2;
- if(!maxMsgAll){
- maxMsgAll = maxMsg;
- maxAll = max;
- }
- }
- });
- amountAll += amount;
- if(maxMsgAll === maxMsg)
- maxAll += max;
- else{
- if(max < maxAll){
- maxAll = max;
- maxMsgAll = maxMsg;
- }
- }
- if(amount >= 2 && max/amount > 0.5) {
- res[1] += " "+key1;
- }
- });
- if(res[1] !== "") res[1] = "failed:"+res[1];
- else res[1] = "passed";
- if(amountAll >= 2 && maxAll/amountAll > 0.5)
- res[2] = "failed: "+maxMsgAll;
- if(messages.length >= 2 && contain>messages.length/2){
- res[3] = "failed: "+contWords.unique().sort().join(" ");
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement