<commit_before>var nconf = require('nconf').file('config.json');
var wit = require('node-wit');
var path = require('path');
var fs = require('fs');
var timer;

var ACCESS_TOKEN = nconf.get('witai-access-token');

commands = [];
intents = path.join(__dirname, 'intents');
require('fs').readdirSync(intents).forEach(function(file) {
  commands.push.apply(commands, require(path.join(intents, file)).commands);
});

module.exports.getTextIntent = function(query, callback) {
  timer = process.hrtime();
  wit.captureTextIntent(ACCESS_TOKEN, query, function (err, data) {
    getIntent(err, data, callback);
  });
}

module.exports.getSpeechIntent = function(stream, callback) {
  timer = process.hrtime();
  wit.captureSpeechIntent(ACCESS_TOKEN, stream, "audio/wav", function (err, data) {
    getIntent(err, data, callback);
  });
}


getIntent = function(err, data, callback) {
  if (err) {
    console.log("Error: ", err);
    console.log("Data:" + data)
  } else {
    processIntent(data, function(err, response) {
      var result = {raw: data, res: response, time: process.hrtime(timer)};
      if (err) {
        result.res = 'I was unable to complete the task because: ' + err;
      }
      callback(null, result);
    });
  }
}

processIntent = function(data, callback) {
  var outcome = data.outcomes[0];
  var intent = outcome.intent;
  var entities = outcome.entities;

  commands.some(function(command) {
    try {
      if (command.valid(intent, entities)) {
        console.log(command.text);
        command.execute(entities, callback);
        return true;
      }
    } catch (e) {}
  });
}<commit_msg>Added default response in case we don't know how to handle a request<commit_after>var nconf = require('nconf').file('config.json');
var wit = require('node-wit');
var path = require('path');
var fs = require('fs');
var timer;

var ACCESS_TOKEN = nconf.get('witai-access-token');

commands = [];
intents = path.join(__dirname, 'intents');
require('fs').readdirSync(intents).forEach(function(file) {
  commands.push.apply(commands, require(path.join(intents, file)).commands);
});

module.exports.getTextIntent = function(query, callback) {
  timer = process.hrtime();
  wit.captureTextIntent(ACCESS_TOKEN, query, function (err, data) {
    getIntent(err, data, callback);
  });
}

module.exports.getSpeechIntent = function(stream, callback) {
  timer = process.hrtime();
  wit.captureSpeechIntent(ACCESS_TOKEN, stream, "audio/wav", function (err, data) {
    getIntent(err, data, callback);
  });
}


getIntent = function(err, data, callback) {
  if (err) {
    console.log("Error: ", err);
    console.log("Data:" + data)
  } else {
    processIntent(data, function(err, response) {
      var result = {raw: data, res: response, time: process.hrtime(timer)};
      if (err) {
        result.res = 'I was unable to complete the task because: ' + err;
      }
      callback(null, result);
    });
  }
}

processIntent = function(data, callback) {
  var outcome = data.outcomes[0];
  var intent = outcome.intent;
  var entities = outcome.entities;

  var success = commands.some(function(command) {
    try {
      if (command.valid(intent, entities)) {
        console.log(command.text);
        command.execute(entities, callback);
        return true;
      }
    } catch (e) {}
  });
  if (!success) {
    callback(null, "I don't know what you mean.")
  }
}
{'fraction_non_alphanumeric': 0.15812851643470535, 'fraction_numerical': 0.0005922416345869114, 'mean_word_length': 3.784702549575071, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1618763', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1088, 'n_words': 247}