Permalink
Browse files

Adding answer method to the bot.

  • Loading branch information...
1 parent 3115645 commit 836bbb794d7e4a7a5d96b512891d9541b6b2b6ce @vesln committed Jan 21, 2012
Showing with 69 additions and 24 deletions.
  1. +12 −0 db/defaults.json
  2. +33 −10 lib/bot.js
  3. +3 −3 lib/db.js
  4. +21 −11 test/bot.test.js
View
@@ -0,0 +1,12 @@
+[
+ "No idea man.",
+ "LOL!",
+ "That is cool.",
+ "That is sweet.",
+ "Well yeah, dunno.",
+ "Strange things happens sometimes.",
+ "Cool.",
+ "Btw what is the purpose of that anyway?",
+ "Whatever.",
+ "Yeah, cool."
+]
View
@@ -6,32 +6,55 @@
*/
/**
- * Bot constructor.
+ * Text class.
*
- * @param {Object} Database.
+ * @type {Function}
*/
-function Bot(db) {
- this._db = db;
-};
+var Text = require('./text');
/**
- * Version.
+ * Bot constructor.
*
- * @type {String}
+ * @param {Db} Database.
+ * @param {Array} Default answers used when nothing is found.
*/
-Bot.prototype.version = require('../package.json').version;
+function Bot(db, defaults) {
+ this._db = db;
+ this._defaults = defaults;
+};
/**
* Gives an anwser to supplied sentence.
*
* @param {String} Sentence.
* @param {Function} Callback.
+ * @api public
*/
Bot.prototype.answer = function(sentence, cb) {
- cb('Hello.');
+ var text = new Text(sentence);
+ var results = null;
+ if (!text.words().length) return null;
+ results = this._db.findAll(text);
+ if (!results.length) results = this._defaults;
+ return this.random(results);
+};
+
+/**
+ * Returns random array key.
+ *
+ * @param {Array} Array to search in.
+ * @returns {Mixed} Array key.
+ */
+Bot.prototype.random = function(arr) {
+ return arr[Math.floor(Math.random() * arr.length)];
};
/**
* Exposing `Bot`.
*/
-module.exports = Bot;
+module.exports = Bot;
+
+/**
+ * Exposing module version.
+ */
+module.exports.version = require('../package.json').version;
View
@@ -27,6 +27,7 @@ function Db(content) {
*
* @param {Text} Text object.
* @returns {String} Answer.
+ * @api public
*/
Db.prototype.findOne = function(text) {
var match = null;
@@ -52,16 +53,15 @@ Db.prototype.findOne = function(text) {
*
* @param {Text} Text object.
* @returns {Array} answers.
+ * @api public
*/
Db.prototype.findAll = function(text) {
var matches = [];
var words = text.words();
this._content.forEach(function(obj) {
for (var i = -1, len = words.length; ++i < len;) {
- if (~obj.words.indexOf(words[i])) {
- return matches.push(obj.answer);
- }
+ if (~obj.words.indexOf(words[i])) return matches.push(obj.answer);
}
});
View
@@ -12,28 +12,38 @@
*/
var Bot = require('../lib/bot');
+/**
+ * Test dependencies.
+ */
+var Db = require('../lib/db');
+var defaults = require('../lib/db');
+
describe('Bot', function() {
describe('version', function() {
- it('should be sane', function() {
- var bot = new Bot;
- bot.version.should.be.ok;
+ it('should be exposed', function() {
+ Bot.version.should.be.ok;
});
});
describe('constructor', function() {
- it('should load db', function() {
- var bot = new Bot({foo: 'bar'});
+ it('should load db and defaults', function() {
+ var bot = new Bot({foo: 'bar'}, ['foo', 'bar']);
bot._db.should.eql({foo: 'bar'});
+ bot._defaults.should.eql(['foo', 'bar']);
});
});
describe('.answer()', function() {
- it('should answer simple questions', function(done) {
- var bot = new Bot;
- bot.answer('Hello!', function(answer) {
- answer.should.match(/(Hi.|Hello.|Greetings.|How can I help you today?)/);
- done();
- });
+ it('should answer questions', function() {
+ var db = new Db;
+ var bot = new Bot(db);
+ bot.answer('Hello.').should.eql('"Hello," he lied.');
+ });
+
+ it('should answer even if he does not know what to say', function() {
+ var db = new Db;
+ var bot = new Bot(db, ['foo']);
+ bot.answer('dasjdisahdia').should.eql('foo');
});
});
});

0 comments on commit 836bbb7

Please sign in to comment.