2019-03-15 11:03:05 +01:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var should = require("should");
|
|
|
|
var sentimentNode = require("../../../analysis/mlsentiment/mlsentiment.js");
|
|
|
|
var helper = require("node-red-node-test-helper");
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
describe('mlsentiment Node', function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
before(function (done) {
|
2019-03-15 11:03:05 +01:00
|
|
|
helper.startServer(done);
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
after(function (done) {
|
2019-03-15 11:03:05 +01:00
|
|
|
helper.stopServer(done);
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
afterEach(function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
helper.unload();
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
it('should be loaded', function (done) {
|
|
|
|
var flow = [{ id: "sentimentNode1", type: "mlsentiment", name: "sentimentNode" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
var sentimentNode1 = helper.getNode("sentimentNode1");
|
|
|
|
sentimentNode1.should.have.property('name', 'sentimentNode');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
it('should pass on msg if no payload', function (done) {
|
|
|
|
var flow = [{ id: "jn1", type: "mlsentiment", wires: [["jn2"]] },
|
|
|
|
{ id: "jn2", type: "helper" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
var jn1 = helper.getNode("jn1");
|
|
|
|
var jn2 = helper.getNode("jn2");
|
2023-08-31 22:25:00 +02:00
|
|
|
jn2.on("input", function (msg) {
|
2019-03-15 11:03:05 +01:00
|
|
|
msg.should.not.have.property('sentiment');
|
|
|
|
msg.topic.should.equal("pass on");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
var testString = 'good, great, best, brilliant';
|
2023-08-31 22:25:00 +02:00
|
|
|
jn1.receive({ topic: "pass on" });
|
2019-03-15 11:03:05 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
it('should add a positive score for good words', function (done) {
|
|
|
|
var flow = [{ id: "jn1", type: "mlsentiment", wires: [["jn2"]] },
|
|
|
|
{ id: "jn2", type: "helper" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
var jn1 = helper.getNode("jn1");
|
|
|
|
var jn2 = helper.getNode("jn2");
|
2023-08-31 22:25:00 +02:00
|
|
|
jn2.on("input", function (msg) {
|
2019-03-15 11:03:05 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property('sentiment');
|
|
|
|
msg.sentiment.should.have.property('score');
|
|
|
|
msg.sentiment.score.should.be.a.Number();
|
|
|
|
msg.sentiment.score.should.be.above(10);
|
|
|
|
done();
|
2023-08-31 22:25:00 +02:00
|
|
|
} catch (err) {
|
2019-03-15 11:03:05 +01:00
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var testString = 'good, great, best, brilliant';
|
2023-08-31 22:25:00 +02:00
|
|
|
jn1.receive({ payload: testString });
|
2019-03-15 11:03:05 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
it('should add a positive score for good words (in French)', function (done) {
|
|
|
|
var flow = [{ id: "jn1", type: "mlsentiment", wires: [["jn2"]], lang: "fr" },
|
|
|
|
{ id: "jn2", type: "helper" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
var jn1 = helper.getNode("jn1");
|
|
|
|
var jn2 = helper.getNode("jn2");
|
2023-08-31 22:25:00 +02:00
|
|
|
jn2.on("input", function (msg) {
|
2019-03-15 11:03:05 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property('sentiment');
|
|
|
|
msg.sentiment.should.have.property('score');
|
|
|
|
msg.sentiment.score.should.be.a.Number();
|
|
|
|
msg.sentiment.score.should.be.above(5);
|
|
|
|
done();
|
2023-08-31 22:25:00 +02:00
|
|
|
} catch (err) {
|
2019-03-15 11:03:05 +01:00
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var testString = 'bon, belle, don du ciel, brillant';
|
2023-08-31 22:25:00 +02:00
|
|
|
jn1.receive({ payload: testString });
|
2019-03-15 11:03:05 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
it('should add a positive score for good words - alternative property', function (done) {
|
|
|
|
var flow = [{ id: "jn1", type: "mlsentiment", property: "foo", wires: [["jn2"]] },
|
|
|
|
{ id: "jn2", type: "helper" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
var jn1 = helper.getNode("jn1");
|
|
|
|
var jn2 = helper.getNode("jn2");
|
2023-08-31 22:25:00 +02:00
|
|
|
jn2.on("input", function (msg) {
|
2019-03-15 11:03:05 +01:00
|
|
|
try {
|
|
|
|
msg.should.have.property('sentiment');
|
|
|
|
msg.sentiment.should.have.property('score');
|
|
|
|
msg.sentiment.score.should.be.a.Number();
|
|
|
|
msg.sentiment.score.should.be.above(10);
|
|
|
|
done();
|
2023-08-31 22:25:00 +02:00
|
|
|
} catch (err) {
|
2019-03-15 11:03:05 +01:00
|
|
|
done(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var testString = 'good, great, best, brilliant';
|
2023-08-31 22:25:00 +02:00
|
|
|
jn1.receive({ foo: testString });
|
2019-03-15 11:03:05 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
it('should add a negative score for bad words', function (done) {
|
|
|
|
var flow = [{ id: "jn1", type: "mlsentiment", wires: [["jn2"]] },
|
|
|
|
{ id: "jn2", type: "helper" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
var jn1 = helper.getNode("jn1");
|
|
|
|
var jn2 = helper.getNode("jn2");
|
2023-08-31 22:25:00 +02:00
|
|
|
jn2.on("input", function (msg) {
|
2019-03-15 11:03:05 +01:00
|
|
|
msg.should.have.property('sentiment');
|
|
|
|
msg.sentiment.should.have.property('score');
|
|
|
|
msg.sentiment.score.should.be.a.Number();
|
|
|
|
msg.sentiment.score.should.be.below(-10);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
var testString = 'bad, horrible, negative, awful';
|
2023-08-31 22:25:00 +02:00
|
|
|
jn1.receive({ payload: testString });
|
2019-03-15 11:03:05 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
it('should add a negative score for bad words - alternative property', function (done) {
|
|
|
|
var flow = [{ id: "jn1", type: "mlsentiment", property: "foo", wires: [["jn2"]] },
|
|
|
|
{ id: "jn2", type: "helper" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
var jn1 = helper.getNode("jn1");
|
|
|
|
var jn2 = helper.getNode("jn2");
|
2023-08-31 22:25:00 +02:00
|
|
|
jn2.on("input", function (msg) {
|
2019-03-15 11:03:05 +01:00
|
|
|
msg.should.have.property('sentiment');
|
|
|
|
msg.sentiment.should.have.property('score');
|
|
|
|
msg.sentiment.score.should.be.a.Number();
|
|
|
|
msg.sentiment.score.should.be.below(-10);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
var testString = 'bad, horrible, negative, awful';
|
2023-08-31 22:25:00 +02:00
|
|
|
jn1.receive({ foo: testString });
|
2019-03-15 11:03:05 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
it('should allow you to override word scoring', function (done) {
|
|
|
|
var flow = [{ id: "jn1", type: "mlsentiment", wires: [["jn2"]] },
|
|
|
|
{ id: "jn2", type: "helper" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
var jn1 = helper.getNode("jn1");
|
|
|
|
var jn2 = helper.getNode("jn2");
|
2023-08-31 22:25:00 +02:00
|
|
|
jn2.on("input", function (msg) {
|
2019-03-15 11:03:05 +01:00
|
|
|
msg.should.have.property('sentiment');
|
|
|
|
msg.sentiment.should.have.property('score');
|
|
|
|
msg.sentiment.score.should.be.a.Number();
|
|
|
|
msg.sentiment.score.should.equal(20);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
var testString = 'sick, wicked';
|
2023-08-31 22:25:00 +02:00
|
|
|
var wordOverrides = { 'sick': 10, 'wicked': 10 };
|
|
|
|
jn1.receive({ payload: testString, words: wordOverrides });
|
2019-03-15 11:03:05 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-08-31 22:25:00 +02:00
|
|
|
it('should allow you to override word scoring - alternative property', function (done) {
|
|
|
|
var flow = [{ id: "jn1", type: "mlsentiment", property: "foo", wires: [["jn2"]] },
|
|
|
|
{ id: "jn2", type: "helper" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
2019-03-15 11:03:05 +01:00
|
|
|
var jn1 = helper.getNode("jn1");
|
|
|
|
var jn2 = helper.getNode("jn2");
|
2023-08-31 22:25:00 +02:00
|
|
|
jn2.on("input", function (msg) {
|
2019-03-15 11:03:05 +01:00
|
|
|
msg.should.have.property('sentiment');
|
|
|
|
msg.sentiment.should.have.property('score');
|
|
|
|
msg.sentiment.score.should.be.a.Number();
|
|
|
|
msg.sentiment.score.should.equal(20);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
var testString = 'sick, wicked';
|
2023-08-31 22:25:00 +02:00
|
|
|
var wordOverrides = { 'sick': 10, 'wicked': 10 };
|
|
|
|
jn1.receive({ foo: testString, words: wordOverrides });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow you to use custom tokens', function (done) {
|
|
|
|
var flow = [{ id: "jn1", type: "mlsentiment", wires: [["jn2"]] },
|
|
|
|
{ id: "jn2", type: "helper" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
|
|
|
var jn1 = helper.getNode("jn1");
|
|
|
|
var jn2 = helper.getNode("jn2");
|
|
|
|
jn2.on("input", function (msg) {
|
|
|
|
msg.should.have.property('sentiment');
|
|
|
|
msg.sentiment.should.have.property('score');
|
|
|
|
msg.sentiment.score.should.be.a.Number();
|
|
|
|
msg.sentiment.score.should.equal(-3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
var testString = '世界就是一个疯子的囚笼';
|
|
|
|
var tokenOverrides = ['世界', '就', '是', '一个', '疯子', '的', '囚笼'];
|
|
|
|
jn1.receive({ payload: testString, tokens: tokenOverrides });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow you to use custom tokens - alternative property', function (done) {
|
|
|
|
var flow = [{ id: "jn1", type: "mlsentiment", property: "foo", wires: [["jn2"]] },
|
|
|
|
{ id: "jn2", type: "helper" }];
|
|
|
|
helper.load(sentimentNode, flow, function () {
|
|
|
|
var jn1 = helper.getNode("jn1");
|
|
|
|
var jn2 = helper.getNode("jn2");
|
|
|
|
jn2.on("input", function (msg) {
|
|
|
|
msg.should.have.property('sentiment');
|
|
|
|
msg.sentiment.should.have.property('score');
|
|
|
|
msg.sentiment.score.should.be.a.Number();
|
|
|
|
msg.sentiment.score.should.equal(-3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
var testString = '世界就是一个疯子的囚笼';
|
|
|
|
var tokenOverrides = ['世界', '就', '是', '一个', '疯子', '的', '囚笼'];
|
|
|
|
jn1.receive({ foo: testString, tokens: tokenOverrides });
|
2019-03-15 11:03:05 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|