All files / lib/nlg nlg-manager.js

100% Statements 40/40
100% Branches 31/31
100% Functions 8/8
100% Lines 40/40
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149                                              30x                   164x 164x                     58x                           313x 284x   29x 29x 6x   23x 23x 55x 48x     23x                     308x 308x 288x   20x 10x   10x       375x 49x   326x 63x   263x 499x 499x 3x     260x                     372x 1x   371x 49x   371x 112x   371x                           3x 3x 2x         30x  
/*
 * Copyright (c) AXA Shared Services Spain S.A.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
const Evaluator = require('../util/evaluator');
 
/**
 * Natural Language Generator Manger
 */
class NlgManager {
  /**
   * Constructor of the class
   */
  constructor() {
    this.evaluator = new Evaluator();
    this.responses = {};
  }
 
  /**
   * Evaluate the condition using the context, and return true
   * if the condition is passed, false otherwise.
   * @param {String} condition Condition to be evaluated.
   * @param {Object} context Context to use to validate the condition.
   * @returns {boolean} True if the condition is passed, false otherwise.
   */
  isValid(condition, context) {
    return (
      !condition ||
      condition === '' ||
      this.evaluator.evaluate(condition, context) === true
    );
  }
 
  /**
   * Find all valid answers for the given locale and intent.
   * @param {String} locale Locale of the intent.
   * @param {String} intent Intent name.
   * @param {Object} context Context to evaluate conditions.
   */
  findAllAnswers(locale, intent, context) {
    if (!this.responses[locale]) {
      return [];
    }
    const answers = this.responses[locale][intent];
    if (!answers || answers.length === 0) {
      return [];
    }
    const result = [];
    answers.forEach(answer => {
      if (this.isValid(answer.condition, context)) {
        result.push(answer);
      }
    });
    return result;
  }
 
  /**
   * Find one valid answers for the given locale and intent. If there are
   * more than one valid answer, then return one of them at random.
   * @param {String} locale Locale of the intent.
   * @param {String} intent Intent name.
   * @param {Object} context Context to evaluate conditions.
   */
  findAnswer(locale, intent, context) {
    const result = this.findAllAnswers(locale, intent, context);
    if (result.length === 0) {
      return undefined;
    }
    if (result.length === 1) {
      return result[0];
    }
    return result[Math.floor(Math.random() * result.length)];
  }
 
  indexOfAnswer(locale, intent, answer, condition) {
    if (!this.responses[locale]) {
      return -1;
    }
    if (!this.responses[locale][intent]) {
      return -1;
    }
    for (let i = 0; i < this.responses[locale][intent].length; i += 1) {
      const response = this.responses[locale][intent][i];
      if (response.response === answer && response.condition === condition) {
        return i;
      }
    }
    return -1;
  }
 
  /**
   * Adds an answer for a locale and intent.
   * @param {String} locale Locale of the intent.
   * @param {String} intent Intent name.
   * @param {String} answer Text of the answer.
   * @param {String} condition Condition to be evaluated.
   */
  addAnswer(locale, intent, answer, condition) {
    if (this.indexOfAnswer(locale, intent, answer, condition) !== -1) {
      return;
    }
    if (!this.responses[locale]) {
      this.responses[locale] = {};
    }
    if (!this.responses[locale][intent]) {
      this.responses[locale][intent] = [];
    }
    this.responses[locale][intent].push({
      condition,
      response: answer,
    });
  }
 
  /**
   * Remove and answer from a locale and intent.
   * @param {String} locale Locale of the intent.
   * @param {String} intent Intent name.
   * @param {String} answer Text of the answer.
   * @param {String} condition Condition to be evaluated.
   */
  removeAnswer(locale, intent, answer, condition) {
    const index = this.indexOfAnswer(locale, intent, answer, condition);
    if (index !== -1) {
      this.responses[locale][intent].splice(index, 1);
    }
  }
}
 
module.exports = NlgManager;