-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifier.js
More file actions
executable file
·66 lines (63 loc) · 1.86 KB
/
classifier.js
File metadata and controls
executable file
·66 lines (63 loc) · 1.86 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
65
const mongoose = require('mongoose');
const Twit = require('twit');
const config = require('./config');
let T = new Twit(config);
var NaturalLanguageUnderstandingV1 = require('watson-developer-cloud/natural-language-understanding/v1.js');
var natural_language_understanding = new NaturalLanguageUnderstandingV1({
username: process.env.USERNAME,
password: process.env.PASSWORD,
version_date: process.env.VERSION_DATE,
});
module.exports = params => {
return new Promise((resolve, reject) => {
var result;
T.get('search/tweets', params)
.then(info => {
var twitString = '';
let tweets = info.data.statuses;
for (let i = 0; i < tweets.length; i++) {
twitString =
twitString +
' ' +
tweets[i].text.replace(/[&\/\\#,;.+()$~%.'":*?<>{}]/g, '');
}
return twitString;
})
.then(data => {
var parameters = {
text: data,
features: {
entities: {
emotion: true,
sentiment: true,
limit: 1,
},
},
};
natural_language_understanding.analyze(parameters, function(
err,
response
) {
if (err) console.log(err);
else
var a = 0,
b;
for (var i = 0; i < 5; i++) {
if (a < Object.values(response.entities[0].emotion)[i])
a = Object.values(response.entities[0].emotion)[i];
b = Object.getOwnPropertyNames(response.entities[0].emotion)[i];
console.log(b)
}
var result = {
name: Object.values(response.entities[0].text)
.join()
.toString()
.replace(/,/g, ''),
emotion: b,
value: a,
};
resolve(result);
});
});
});
};