Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added vararg on TrainingDocument #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
21 changes: 18 additions & 3 deletions lib/BotTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
58 changes: 58 additions & 0 deletions test/BotTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

Expand Down
10 changes: 10 additions & 0 deletions test/BotTypesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down