sentiment-parser
Version:
The sentiment-parser package is used to easily parse sentiments from sentences, using a simple one-liner.
108 lines (105 loc) • 4.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var watson = require("watson-developer-cloud");
var confidencethreshold = 0.55;
var Parser = /** @class */ (function () {
function Parser() {
this.watsonCredentials = {};
}
Parser.prototype.setCredentials = function (credentials) {
this.watsonCredentials = credentials;
if (credentials) {
if (credentials.nlAnalyzer) {
this.naturalLanguageAnalyzer = new watson.NaturalLanguageUnderstandingV1({
username: credentials.nlAnalyzer.username,
password: credentials.nlAnalyzer.password,
version: '2018-04-05',
url: 'https://gateway.watsonplatform.net/natural-language-understanding/api/',
});
}
if (credentials.toneAnalyzer) {
this.toneAnalyzer = new watson.ToneAnalyzerV3({
username: credentials.toneAnalyzer.username,
password: credentials.toneAnalyzer.password,
version: 'v3',
version_date: '2016-05-19',
});
}
}
};
Parser.prototype.parseSentiment = function (sentence) {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.watsonCredentials.nlAnalyzer) {
_this.naturalLanguageAnalyzer.analyze({
text: sentence,
features: {
concepts: {},
keywords: {},
sentiment: { document: true },
},
language: 'en',
}, function (err, response) {
if (err) {
reject(err);
}
else {
resolve(response.sentiment.document);
}
});
}
else {
reject(new Error('You must set credentials in order to use the Watson Natural Language Credentials'));
}
});
};
Parser.prototype.parseEmotion = function (sentence) {
var _this = this;
return new Promise(function (resolve, reject) {
if (_this.watsonCredentials.toneAnalyzer) {
var parsedSentiment_1 = {};
_this.toneAnalyzer.tone({ text: sentence }, function (err, tone) {
if (err) {
reject(err);
}
else {
var resolved_1 = false;
tone.document_tone.tone_categories.forEach(function (tonecategory) {
if (tonecategory.category_id === 'emotion_tone') {
var maxConfidence_1 = Math.max.apply(Math, tonecategory.tones.map(function (examinedTone) {
return examinedTone.score;
}));
var strongestTone = tonecategory.tones.find(function (examinedTone) {
return examinedTone.score === maxConfidence_1;
});
// tslint:disable-next-line:no-console
parsedSentiment_1.watsonTone = {
allTones: tonecategory.tones,
strongestTone: strongestTone,
};
resolved_1 = true;
resolve(parsedSentiment_1);
}
});
if (!resolved_1) {
reject(new Error('Unknown problem. Possible no tone categories.'));
}
}
});
}
else {
reject(new Error('You must set credentials in order to use the Watson Natural Language Credentials'));
}
});
};
return Parser;
}());
exports.Parser = Parser;
/*const inspect = require('unist-util-inspect');
const unified = require('unified');
const english = require('retext-english');
const sentiment = require('retext-sentiment');
const processor = unified()
.use(english)
.use(sentiment);
*/