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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | 42x 42x 42x 42x 205x 205x 205x 205x 205x 205x 205x 205x 14x 205x 2x 178x 236x 236x 232x 236x 232x 324x 293x 31x 31x 96x 96x 96x 96x 8x 8x 8x 96x 88x 88x 22x 88x 8x 10x 6x 10x 748x 748x 431x 317x 2x 2x 3x 3x 11x 11x 7x 11x 2x 733x 733x 5x 733x 2x 731x 731x 2x 729x 729x 729x 10x 10x 5x 10x 2x 8x 8x 2x 6x 6x 2x 3x 46x 46x 1x 46x 71x 70x 46x 625x 625x 625x 320x 320x 320x 305x 305x 305x 305x 305x 1x 305x 305x 622x 541x 81x 81x 1x 80x 622x 622x 622x 622x 622x 622x 2x 2x 2x 2x 2x 620x 620x 620x 620x 620x 620x 620x 8x 8x 8x 8x 8x 8x 13x 8x 8x 15x 15x 15x 15x 15x 15x 15x 17x 17x 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 DomainManager = require('./domain-manager'); const { Language } = require('../language'); const NlpUtil = require('../nlp/nlp-util'); const PuncTokenizer = require('../nlp/tokenizers/punct-tokenizer'); /** * Class for NLU Manager */ class NluManager { /** * Constructor of the class. * @param {Object} settings Settings for this instance. */ constructor(settings) { this.settings = settings || {}; this.guesser = new Language(); this.languages = []; this.languageNames = {}; this.domainManagers = {}; this.tokenizer = new PuncTokenizer(); this.wordDictionary = {}; if (this.settings.languages) { this.addLanguage(this.settings.languages); } this.intentDomains = {}; } describeLanguage(locale, name) { this.languageNames[locale] = { locale, name }; } /** * Adds a language or several languages to the NLU Manager. * @param {String[]} srcLocales Locales to be added. */ addLanguage(locales) { (Array.isArray(locales) ? locales : [locales]).forEach(locale => { const truncated = NlpUtil.getTruncatedLocale(locale); if (!this.languages.includes(truncated)) { this.languages.push(truncated); } if (!this.domainManagers[truncated]) { this.domainManagers[truncated] = new DomainManager({ language: truncated, ...this.settings, }); } }); } /** * Given a text, try to guess the language, over the languages used for the NLP. * @param {String} utterance Text to be guessed. * @returns {String} ISO2 locale of the language, or undefined if not found. */ guessLanguage(utterance) { if (this.languages.length === 1) { return this.languages[0]; } const guess = this.guesser.guess(utterance, this.languages, 1); return guess && guess.length > 0 ? guess[0].alpha2 : undefined; } /** * Assign an intent to a domain. * The same intent can be in different domains, based on locale. * @param {String} locale Locale of the intent. * @param {String} intent Intent to be assigned. * @param {String} domain Domain to include the intent. */ assignDomain(srcLocale, srcIntent, srcDomain) { let locale = srcLocale; let intent = srcIntent; let domain = srcDomain; if (!domain) { domain = intent; intent = locale; locale = undefined; } if (locale) { const truncated = NlpUtil.getTruncatedLocale(locale); if (!this.intentDomains[truncated]) { this.intentDomains[truncated] = {}; } this.intentDomains[truncated][intent] = domain; } else { this.languages.forEach(truncated => { if (!this.intentDomains[truncated]) { this.intentDomains[truncated] = {}; } this.intentDomains[truncated][intent] = domain; }); } } /** * Returns the domain of a given intent. * @param {String} locale Locale of the intent. * @param {String} intent Intent name. * @returns {String} Domain of the intent. */ getIntentDomain(locale, intent) { const truncated = NlpUtil.getTruncatedLocale(locale); if (!this.intentDomains[truncated]) { return 'default'; } return this.intentDomains[truncated][intent] || 'default'; } /** * Get an object with the intents of each domain. */ getDomains() { const result = {}; Object.keys(this.intentDomains).forEach(locale => { result[locale] = {}; Object.keys(this.intentDomains[locale]).forEach(intent => { const domain = this.intentDomains[locale][intent]; if (!result[locale][domain]) { result[locale][domain] = []; } result[locale][domain].push(intent); }); }); return result; } /** * Adds a new utterance associated to an intent for the given locale. * @param {String} srcLocale Locale of the language. * @param {String} utterance Text of the utterance. * @param {String} intent Intent name. */ addDocument(srcLocale, utterance, intent) { let locale = NlpUtil.getTruncatedLocale(srcLocale); if (!locale) { locale = this.guessLanguage(utterance); } if (!locale) { throw new Error('Locale must be defined'); } const manager = this.domainManagers[locale]; if (!manager) { throw new Error(`Domain Manager not found for locale ${locale}`); } const domain = this.getIntentDomain(locale, intent); this.guesser.addExtraSentence(locale, utterance); manager.add(domain, utterance, intent); } /** * Removes an utterance associated to an intent for the given locale. * @param {String} srcLocale Locale of the language. * @param {String} utterance Text of the utterance. * @param {String} intent Intent name. */ removeDocument(srcLocale, utterance, intent) { let locale = NlpUtil.getTruncatedLocale(srcLocale); if (!locale) { locale = this.guessLanguage(utterance); } if (!locale) { throw new Error('Locale must be defined'); } const manager = this.domainManagers[locale]; if (!manager) { throw new Error(`Domain Manager not found for locale ${locale}`); } const domain = this.getIntentDomain(locale, intent); manager.remove(domain, utterance, intent); } /** * Begin Edit Mode. */ beginEdit() { Object.keys(this.domainManagers).forEach(locale => { this.domainManagers[locale].beginEdit(); }); } /** * Train the domains of the locales. */ async train(languages) { let locales = languages || this.languages; if (!Array.isArray(locales)) { locales = [locales]; } const trainPromises = locales .filter(locale => this.domainManagers[locale]) .map(locale => this.domainManagers[locale].train()); return Promise.all(trainPromises); } fillLanguage(obj) { const result = obj; result.languageGuessed = false; if (result.locale) { result.localeIso2 = NlpUtil.getTruncatedLocale(result.locale); result.language = ( this.languageNames[result.localeIso2] || this.guesser.languagesAlpha2[result.localeIso2] || {} ).name; return result; } Eif (!result.locale) { result.locale = this.guessLanguage(result.utterance); result.localeIso2 = NlpUtil.getTruncatedLocale(result.locale); result.languageGuessed = true; } if (!this.languages.includes(result.localeIso2)) { [result.localeIso2] = this.languages; } result.language = ( this.languageNames[result.localeIso2] || this.guesser.languagesAlpha2[result.localeIso2] || {} ).name; return result; } /** * Indicates if all the classifications are equal to 0.5 * @param {Object[]} classifications Array of classifications */ isEqualClassification(classifications) { if (classifications.length === 1) { return false; } Iif (classifications.length === 0 || classifications[0].value === 0) { return true; } if (classifications[0].value === classifications[1].value) { return true; } return false; } /** * Get all the labels and score for each label from this utterance. * @param {String} srcLocale Locale of the utterance, optional * @param {String} utterance Utterance to be classified. * @param {String} domainName Name of the domain, optional. * @returns {Object[]} Sorted array of classifications, with label and score. */ getClassifications(locale, utterance, domainName) { const result = {}; result.utterance = utterance === undefined ? locale : utterance; result.locale = utterance === undefined ? undefined : locale; this.fillLanguage(result); const domain = this.domainManagers[result.localeIso2]; if (!domain) { result.classifications = []; result.domain = undefined; result.intent = undefined; result.score = undefined; return result; } const classifications = domain.getClassifications( result.utterance, domainName ); result.domain = classifications.domain; result.classifications = classifications.classifications; Iif ( this.isEqualClassification(result.classifications) || result.classifications.length === 0 ) { result.intent = 'None'; result.score = 1; } else { result.intent = result.classifications[0].label; result.score = result.classifications[0].value; } return result; } toObj() { const result = {}; result.settings = this.settings; result.languages = this.languages; result.intentDomains = this.intentDomains; result.domainManagers = {}; Object.keys(this.domainManagers).forEach(locale => { result.domainManagers[locale] = this.domainManagers[locale].toObj(); }); result.extraSentences = this.guesser.extraSentences; return result; } fromObj(obj) { this.guesser.extraSentences = obj.extraSentences || []; this.guesser.processExtraSentences(); this.settings = obj.settings; this.languages = obj.languages; this.intentDomains = obj.intentDomains; this.domainManagers = {}; Object.keys(obj.domainManagers).forEach(locale => { this.domainManagers[locale] = new DomainManager({ language: locale, ...this.settings, }); this.domainManagers[locale].fromObj(obj.domainManagers[locale]); }); } } module.exports = NluManager; |