diff --git a/lib/Bot.js b/lib/Bot.js index ab2b7a9..04f43fe 100644 --- a/lib/Bot.js +++ b/lib/Bot.js @@ -29,7 +29,8 @@ function Bot(config) { this.train = function TrainBotFn(topic, text, callback) { var classifier = botObject.getClassifier(); if(!topic || (typeof topic !== 'string' && !(topic instanceof String))) throw new TypeError('topic must be a String and cannot be undefined'); - if(!text || (typeof text !== 'string' && !(text instanceof String))) throw new TypeError('text must be a String and cannot be undefined'); + + if(!text || (!(text instanceof Array) && typeof text !== 'string' && !(text instanceof String))) throw new TypeError('text must be a String and cannot be undefined'); classifier.trainDocument({topic: topic, text: text}, function(err) { if(err) return callback(err); diff --git a/lib/BotTypes.js b/lib/BotTypes.js index 0fdbb23..24fd99a 100644 --- a/lib/BotTypes.js +++ b/lib/BotTypes.js @@ -22,11 +22,26 @@ function Correspondence(id, message) { this.message = message; } -function TrainingDocument(topic, text) { +function TrainingDocument(topic, textArg) { if(!topic || (typeof topic !== 'string' && !(topic instanceof String))) throw new TypeError('topic parameter is of invalid type. Must exist and be a String.'); - if(!text || (typeof text !== 'string' && !(text instanceof String))) throw new TypeError('text parameter is of invalid type. Must exist and be a String.'); + + for(var i = 1; i < arguments.length; i++) { + var text = arguments[i]; + if(!text || (typeof text !== 'string' && !(text instanceof String))) throw new TypeError('text parameter is of invalid type. Must exist and be a String.'); + if(!this.text) { + this.text = text; + continue; + } + + if(this.text && !(this.text instanceof Array)) { + this.text = [this.text, text]; + continue; + } + + this.text.push(text); + } + this.topic = topic; - this.text = text; } function Context(id) { diff --git a/test/BotTest.js b/test/BotTest.js index 9d5ce48..4ac5875 100644 --- a/test/BotTest.js +++ b/test/BotTest.js @@ -87,6 +87,34 @@ describe('Bot', function () { done(); }); + it('trains multiple documents when parameters are valid', function(done) { + const TalkifyClassifier = require('talkify-classifier'); + var mockClassifier = new TalkifyClassifier(); + + expect.spyOn(mockClassifier, 'trainDocument').andCall(function(document, callback) { + return callback(); + }); + + expect.spyOn(mockClassifier, 'initialize').andCall(function(callback) { + return callback(); + }); + + var mockLrClassifier = { + LogisticRegressionClassifier: function () { + return mockClassifier; + } + }; + + mockery.registerMock('talkify-natural-classifier', mockLrClassifier); + + var bot = new Bot(); + bot.train('topic', ['text1', 'text2', 'text3'], function () { + expect(mockClassifier.trainDocument.calls[0].arguments[0]).toEqual({text:['text1', 'text2', 'text3'], topic:'topic'}); + + done(); + }); + }); + it('throws TypeError when topic is undefined', function (done) { var bot = new Bot(); try { @@ -142,6 +170,36 @@ describe('Bot', function () { }); }); + it('trains document with multiple textx when parameters are valid', function(done) { + // var mockClassifier = mockClassifierWithMockClassifierFactory(); + const TalkifyClassifier = require('talkify-classifier'); + var mockClassifier = new TalkifyClassifier(); + mockClassifier.trainDocument = function(document, callback) { + return callback(undefined, true); + }; + mockClassifier.initialize = function(callback) { + return callback(); + }; + expect.spyOn(mockClassifier, 'trainDocument').andCallThrough(); + expect.spyOn(mockClassifier, 'initialize').andCallThrough(); + + var mockLrClassifier = { + LogisticRegressionClassifier: function () { + return mockClassifier; + } + }; + + mockery.registerMock('talkify-natural-classifier', mockLrClassifier); + + var bot = new Bot(); + return bot.trainAll([{text: ['text1', 'text2', 'text3'], topic: 'topic'}], function (err) { + expect(err).toNotExist(); + expect(mockClassifier.trainDocument.calls[0].arguments[0]).toEqual([{text:['text1', 'text2', 'text3'], topic:'topic'}]); + + done(); + }); + }); + it('throws TypeError when documents is not an array', function (done) { var mockClassifier = mockClassifierWithMockClassifierFactory(); diff --git a/test/BotTypesTest.js b/test/BotTypesTest.js index a06598a..4c4519a 100644 --- a/test/BotTypesTest.js +++ b/test/BotTypesTest.js @@ -163,6 +163,16 @@ describe('TrainingDocument', function() { done(); } }); + + it("allows for multiple text input for the same topic", function() { + var trainingDOcument = new TrainingDocument('topic', 'text1', 'text2', 'text3'); + expect(trainingDOcument.topic).toBe('topic'); + expect(trainingDOcument.text).toBeA(Array); + expect(trainingDOcument.text.length).toBe(3); + expect(trainingDOcument.text[0]).toBe('text1'); + expect(trainingDOcument.text[1]).toBe('text2'); + expect(trainingDOcument.text[2]).toBe('text3'); + }); }); describe('Skill', function() {