Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 150 151 152 153 154 | 42x 189x 189x 76x 328x 290x 38x 38x 9x 73x 323x 323x 297x 26x 13x 13x 468x 62x 406x 81x 325x 618x 618x 3x 322x 465x 1x 464x 62x 464x 143x 464x 3x 3x 2x 42x | /* * Copyright (c) AXA Group Operations 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 Manager */ 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 []; } return answers.filter(answer => this.isValid(answer.condition, context)); } /** * 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)]; } /** * Finds the index of an answer. * @param {String} locale Locale of the answer. * @param {String} intent Intent of the answer. * @param {String} answer Text of the answer. * @param {String} condition Condition of the answer. * @returns {number} Index of the answer, -1 if not found. */ 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. * @param {String} media url to use. */ addAnswer(locale, intent, answer, condition, url) { 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, media: url, }); } /** * 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. * @param {String} media url to use. */ removeAnswer(locale, intent, answer, condition, url) { const index = this.indexOfAnswer(locale, intent, answer, condition, url); if (index !== -1) { this.responses[locale][intent].splice(index, 1); } } } module.exports = NlgManager; |