-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtweetController.js
More file actions
54 lines (50 loc) 路 1.27 KB
/
Copy pathtweetController.js
File metadata and controls
54 lines (50 loc) 路 1.27 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
// contactController.js
// Import contact model
Tweet = require('./tweetModel');
// Handle index actions
exports.index = function (req, res) {
Tweet.get(function (err, tweets) {
if (err) {
res.json({
status: "error",
message: err,
});
}
res.json({
status: "success",
message: "Tweet retrieved successfully",
data: addSentiment(tweets)
});
});
};
// Handle view contact info
exports.view = function (req, res) {
tweets.findById(req.params.tweets_id, function (err, tweets) {
if (err)
res.send(err);
res.json({
message: 'Contact details loading..',
data: tweets
});
});
};
var ml = require("ml-sentiment")();
function addSentiment(tweets) {
var tweetsWithSetniment = tweets.map((tweet) => {
const sentiment = ml.classify(tweet.body);
let emoji = "馃槙";
if (sentiment >= 5) {
emoji = "馃槂";
} else if (sentiment > 0) {
emoji = "馃檪";
} else if (sentiment == 0) {
emoji = "馃槓";
}
return {
...tweet,
emoji,
sentiment
};
});
return tweetsWithSetniment;
}