-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
96 lines (72 loc) · 1.63 KB
/
index.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'use strict';
var events = require('events');
var clickhole = require('clickhole-headlines');
var buzzfeed = require('buzzfeed-headlines');
var Feedtitles = require('feedtitles');
var classifier = require('./lib/classifier')();
/**
* Boolean for when we are ready
*/
var ready = false;
/**
* Give this module an event emitter
*/
var exports = module.exports = Object.create(events.EventEmitter.prototype);
/**
* Go get some clickhole and buzzfeed garbage and start training
*/
clickhole(train, 10);
buzzfeed(train);
/**
* Our train function for the sites
*
* @param {Boolean} err
* @parma {Array} titles
*/
function train(err, titles) {
if (err) return exports.emit('error', err);
titles.forEach(function(title){
classifier.addDocument(title, 'spam');
});
done();
};
/**
* We know we are waiting for 3 sources to finish, do that here
*/
var counts = 0;
function done() {
counts++;
if (counts < 2) return;
ready = true;
classifier.train();
exports.emit('ready');
};
/**
* Some links to 'reputable' new sources
*/
var world = 'http://feeds.reuters.com/Reuters/worldNews';
var us = 'http://feeds.reuters.com/Reuters/domesticNews';
/**
* Create our title parser feed
*/
var feed = new Feedtitles(world);
/**
* Feed again
*/
feed.feed(us);
/**
* When we find an individual title, train `not` on it
*/
feed.on('title', function(title){
classifier.addDocument(title, 'not');
});
/**
* Let the users find out if something is click bait or not
*
* @param {String} phrase
* @return {String}
*/
exports.bait = function(phrase) {
if (!ready) return null;
return classifier.classify(phrase);
};