<repo_name>lavriv92/morning-lohika-demo<file_sep>/src/public/app.js
(function App() {
  let textarea = document.querySelector('[data-input="comment-form"]');
  let comments = document.querySelector('[data-list="comments"]');
  let button = document.querySelector('[data-button="send-button"]');


  const predictValidation = (jsonNetwork, message) => {
    let spamBayes = new Bayes();
    spamBayes.fromJSON(JSON.stringify(jsonNetwork));

    var scores = spamBayes.guess(message);
    var winner = spamBayes.extractWinner(scores);

    console.log(winner);

    if(winner.label === "spam") {
      alert('This message looks like spam, please be polite with another users');
    } else {
      buildNode(message);
    }
  };

  const validateMessage = message => {
    fetch('/categories').then(res => {
      return res.json();
    }).then(jsonNetwork => {
      predictValidation(jsonNetwork, message);
    });
  };


  const buildNode = (message) => {
    let node = document.createElement('div');
    node.className = 'message';
    node.innerHTML = `<div id=${Date.now()}>${textarea.value}</div>`
    comments.appendChild(node);
  };


  button.onclick = e => {
    validateMessage(textarea.value);
  };
})()
<file_sep>/src/train.js
var Bayes = require('ml-bayes')

var classifier = new Bayes()

classifier.train('I want to sell you', 'spam');
classifier.train('I want to sell', 'spam');
classifier.train('Sell', 'spam');
classifier.train('Hello, ', 'non_spam');
classifier.learn('amazing, awesome movie!! Yeah!! Oh boy.', 'non_spam');
classifier.train('Sweet, this is incredibly, amazing, perfect, great!!', 'non_spam');

classifier.train('terrible, shitty thing. Damn. Sucks!!', 'non_spam');

classifier.train('Special proposition', 'spam');

require('fs').writeFileSync('./classifier_state.json', classifier.toJSON());
