Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function reduce(numerator,denominator){
  2.   var gcd = function gcd(a,b){
  3.     return b ? gcd(b, a%b) : a;
  4.   };
  5.   gcd = gcd(numerator,denominator);
  6.   return [numerator/gcd, denominator/gcd];
  7. }
  8.  
  9. function spamDetection(messages, spamSignals) {
  10.     let res = ["passed","","passed","passed"], message, user, msgByUsr = {}, fewerThan5 = 0, contain = 0, contWords = [];
  11.     Array.prototype.unique = function() {
  12.         var a = this.concat();
  13.         for(var i=0; i<a.length; ++i) {
  14.             for(var j=i+1; j<a.length; ++j) {
  15.                 if(a[i] === a[j])
  16.                     a.splice(j--, 1);
  17.             }
  18.         }
  19.  
  20.         return a;
  21.     };
  22.     for(let i = 0; i<messages.length; i++){
  23.         message = messages[i][0];
  24.         user = messages[i][1];
  25.        
  26.         // Check less than 5 words
  27.         if(message.split(" ").length<5) fewerThan5++;
  28.        
  29.         // Check 50% has the same content from the same user
  30.         if(msgByUsr[user] !== undefined){
  31.             if(msgByUsr[user][message] !== undefined) msgByUsr[user][message]++;
  32.             else msgByUsr[user][message] = 1;
  33.         }
  34.         else{
  35.             msgByUsr[user] = {};
  36.             msgByUsr[user][message] = 1;
  37.         }
  38.            
  39.            
  40.            
  41.         // Contain suspicious word
  42.         for(let j = 0; j<spamSignals.length; j++){
  43.             if(message.replace(/[^a-z^\ ]/gi, ' ').toLowerCase().split(" ").indexOf(spamSignals[j]) != -1 ){
  44.                 contain++;
  45.                 // if(contWords[spamSignals[j]] === undefined) contWords[spamSignals] = 0
  46.                 contWords.push(spamSignals[j]);
  47.             }
  48.         }
  49.        
  50.        
  51.     }
  52.     console.log(msgByUsr);
  53.     let frac = reduce(fewerThan5, messages.length)
  54.     if(fewerThan5/messages.length > 0.9) res[0] = "failed: "+frac[0]+"/"+frac[1];
  55.    
  56.    
  57.     let amount = 0, max = -1, maxAll = -1, amountAll = 0, maxMsg, maxMsgAll;
  58.     Object.keys(msgByUsr).forEach(function(key1){
  59.         amount = 0; max = -1;
  60.         Object.keys(msgByUsr[key1]).forEach(function(key2){
  61.             amount += msgByUsr[key1][key2];
  62.             if(max < msgByUsr[key1][key2]){
  63.                 max = msgByUsr[key1][key2];
  64.                 maxMsg = key2;
  65.                 if(!maxMsgAll){
  66.                     maxMsgAll = maxMsg;
  67.                     maxAll = max;
  68.                 }
  69.             }
  70.            
  71.         });
  72.         amountAll += amount;
  73.         if(maxMsgAll === maxMsg)
  74.                 maxAll += max;
  75.         else{
  76.             if(max < maxAll){
  77.                 maxAll = max;
  78.                 maxMsgAll = maxMsg;
  79.             }
  80.         }
  81.         if(amount >= 2 && max/amount > 0.5) {
  82.             res[1] += " "+key1;
  83.         }
  84.     });
  85.     if(res[1] !== "") res[1] = "failed:"+res[1];
  86.     else res[1] = "passed";
  87.     if(amountAll >= 2 && maxAll/amountAll > 0.5)
  88.         res[2] = "failed: "+maxMsgAll;
  89.    
  90.     if(messages.length >= 2 && contain>messages.length/2){
  91.         res[3] = "failed: "+contWords.unique().sort().join(" ");
  92.     }
  93.     return res;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement