/** * Created by WebStorm. * User: martin * Date: 28/07/2020 * Time: 11:08 */ const Jobs = require('../../lib/mongoManager'); const { Utils } = require('@rakh/utils'); const fs = require('fs'); var bayes = require('bayes'); var classifier = bayes({ 'tokenizer': function (text) { return text.split(','); } }); function load() { const file = fs.readFileSync('brain.json'); classifier = bayes.fromJson(file); } function save() { var stateJson = classifier.toJson(); console.log(stateJson); fs.writeFileSync('brain.json', stateJson); } load(); exports.upvote = (req, res) => { console.log('>upvote req', req.params); if(!req.params.id) return res.status(500).send({ 'message': 'Job id missing' }); const id = req.params.id; Jobs.findById(id).then(async (doc) => { if (doc) { const words = doc._doc.data.autoclass.words.join(','); await classifier.learn(words, 'good'); save(); res.status(200).end(); } }).catch((err) => { console.error(err.message); res.status(500).send({ 'message': err.message || 'Some error occurred while querying the database.' }); }); }; exports.downvote = (req, res) => { console.log('>upvote req', req.params); if(!req.params.id) return res.status(500).send({ 'message': 'Job id missing' }); const id = req.params.id; Jobs.findById(id).then(async (doc) => { if (doc) { const words = doc._doc.data.autoclass.words.join(','); await classifier.learn(words, 'bad'); save(); res.status(200).end(); } }).catch((err) => { console.error(err.message); res.status(500).send({ 'message': err.message || 'Some error occurred while querying the database.' }); }); };