-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
64 lines (54 loc) · 1.61 KB
/
plugin.js
File metadata and controls
64 lines (54 loc) · 1.61 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* We.js plugin file, main project file
*/
var watson = require('watson-developer-cloud'),
fs = require('fs'),
path = require('path');
module.exports = function loadPlugin (projectPath, Plugin) {
var plugin = new Plugin(__dirname);
plugin.nlc = watson.natural_language_classifier({
"url": "https://gateway.watsonplatform.net/natural-language-classifier/api",
"password": "zyRRvZSIFf7T",
"username": "445ffcd9-85be-43f6-a000-7248278c87f3",
version: 'v1'
});
/**
* Create one classifier abd save in configurations
*/
plugin.createClassifier = function createClassifier (we, done) {
var params = {
language: 'pt-br',
name: 'roupas',
training_data: fs.createReadStream(path.resolve('./data/roupas_train.cvs'))
};
plugin.nlc.create(params, function (err, response) {
if (err) return done(err);
we.log.info('Watson response:', response);
var data = 'module.exports.watsonClassifier = ' + JSON.stringify(response);
fs.writeFile( path.resolve('./config/watsonClassifier.js'), data , done)
});
}
/**
* Preload one classifier
*/
plugin.preloadClassifier = function preloadClassifier (we, next) {
we.db.models.classifier.findOne({
where: { name: 'roupas' }
})
.then(function (r) {
plugin.watsonClassifier = r;
next();
})
.catch(next)
}
/**
* Execute one question in watson natural language
*/
plugin.question = function question (we, text, done) {
plugin.nlc.classify({
text: text,
classifier_id: we.config.watsonClassifier.classifier_id
}, done);
}
return plugin;
};