Skip to content

Commit 64aaf6e

Browse files
committed
Add the ability to get the raw scores
1 parent 637607d commit 64aaf6e

4 files changed

Lines changed: 23 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ A quick way to get "good enough" sentiment analysis into your applications, this
1919

2020
var dracula = require('dracula-sentiment');
2121
var text = "xoxo cant wait";
22+
// Output a 'positive', 'negative', or 'neutral' label
2223
console.log(text, dracula.analyze(text));
24+
// To output a [negative, neutral, positive] float array
25+
console.log(text, dracula.score(text));
2326

2427
For best performance and accuracy, remove any non-ascii characters by converting them to their closest equivalents via [`unidecode`](https://www.npmjs.com/package/unidecode) or something similar, and feed it sentence-sized chunks of text.
2528

dracula.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This is the core function, used by everything
2-
function dracula(content) {
2+
function draculaScores(content) {
33
// Tokenize
44
var tokens = draculaTokenize(content);
55
// Create embeddings
@@ -66,6 +66,11 @@ function dracula(content) {
6666

6767
// Output
6868
var probs = draculaSoftmax([finalPool]);
69-
output = determineLabels(probs);
69+
return probs;
70+
}
71+
72+
function dracula(content) {
73+
var probs = draculaScores(content);
74+
var output = determineLabels(probs);
7075
return output.join(', ');
7176
}

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ eval(fs.readFileSync(path.join(__dirname, 'dracula.js'))+'');
2525
module.exports = {
2626
analyze: function(string) {
2727
return dracula(string);
28+
},
29+
score: function(string) {
30+
return draculaScores(string)[0];
2831
}
2932
}

test/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var should = require('chai').should(),
22
dracula = require('../index'),
3-
analyze = dracula.analyze;
3+
analyze = dracula.analyze,
4+
score = dracula.score
45

56
describe('#analyze', function() {
67
it('Should think "terror" is bad news', function() {
@@ -15,3 +16,11 @@ describe('#analyze', function() {
1516
analyze('he\'s been saying really negative things about me').should.equal("negative");
1617
});
1718
});
19+
20+
describe('#score', function() {
21+
it('Should think "terrorism" is generally negative', function() {
22+
var scores = score("terrorism");
23+
scores[0].should.above(scores[1]);
24+
scores[0].should.above(scores[2]);
25+
});
26+
});

0 commit comments

Comments
 (0)