-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathtrainer.js
More file actions
executable file
·41 lines (31 loc) · 1.43 KB
/
trainer.js
File metadata and controls
executable file
·41 lines (31 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"use strict";
const config = require('./config/database');
const Promise = require("bluebird");
const mongoose = require('mongoose');
mongoose.Promise = Promise;
mongoose.connect(config.db.uri);
const path = require('path');
const CronJob = require("cron").CronJob;
const Torrent = require('./models/Torrent.js');
const Classifier = require('./models/Classifier.js');
const natural = require("natural");
let classifier = new natural.BayesClassifier();
const bunyan = require("bunyan");
const logger = bunyan.createLogger({name: "trainer"});
const filter = { $nor: [ { 'category' : /Unknown/ }, { 'category' : /Other/ } ] };
const job = new CronJob("* 30 * * * *", function() { // run each 30 min
const cursor = Torrent.find(filter).sort({'imported': -1}).cursor();
cursor.eachAsync(torrent => {
logger.info(`Adding ${torrent._id} training`);
if(!torrent.files) return Promise.reject(`Torrent ${torrent._id} has no files!`);
const exts = torrent.files
.map(file => path.extname(file).toLowerCase())
.filter(ext => ext.length > 0) // no empty
.filter(ext => ext.length < config.limitExt) // with min length
return Promise.resolve(classifier.addDocument(exts, torrent.category));
})
.then(() => Promise.resolve(classifier.train()))
.then(() => Classifier.findOneAndUpdate( {}, { $set : { 'raw' : JSON.stringify(classifier) } }, { upsert : true }))
.catch(err => Promise.reject(logger.error(err)))
});
job.start();