Skip to content

Commit

Permalink
BREAKING CHANGE: Create an abstract class for NLU modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Blancard committed Mar 1, 2018
1 parent 0d4a645 commit 821a863
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 26 deletions.
4 changes: 3 additions & 1 deletion packages/botfuel-dialog/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const FileCorpus = require('./corpora/file-corpus');
const Brain = require('./brains/brain');
const MemoryBrain = require('./brains/memory-brain');
const MongoBrain = require('./brains/mongo-brain');
const Nlu = require('./nlu');
const Nlu = require('./nlus/nlu');
const BotfuelNlu = require('./nlus/botfuel-nlu');
const PromptDialog = require('./dialogs/prompt-dialog');
const PromptView = require('./views/prompt-view');
const TextDialog = require('./dialogs/text-dialog');
Expand All @@ -55,6 +56,7 @@ const QnasView = require('./views/qnas-view');
module.exports = {
Logger: Logger.getLogger,
Bot,
BotfuelNlu,
Brain,
Classifier,
Adapter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,26 @@ const fs = require('fs');
const fsExtra = require('fs-extra');
const dir = require('node-dir');
const Qna = require('botfuel-qna-sdk');
const logger = require('logtown')('Nlu');
const AuthenticationError = require('./errors/authentication-error');
const Classifier = require('./classifier');
const BooleanExtractor = require('./extractors/boolean-extractor');
const CompositeExtractor = require('./extractors/composite-extractor');
const logger = require('logtown')('BotfuelNlu');
const AuthenticationError = require('../errors/authentication-error');
const Classifier = require('../classifier');
const BooleanExtractor = require('../extractors/boolean-extractor');
const CompositeExtractor = require('../extractors/composite-extractor');
const Nlu = require('./nlu');

/**
* A nlu module (could be replaced by an external one).
*/
class Nlu {
/**
* @constructor
* @param {Object} config - the bot config
*/
constructor(config) {
logger.debug('constructor', config);
this.config = config;
class BotfuelNlu extends Nlu {
/** @inheritdoc */
constructor(nluConfig) {
logger.debug('constructor', nluConfig);
super(nluConfig);
this.extractor = null;
this.qna = null;
this.classifier = null;
this.intentFilter = async intents =>
intents.filter(intent => intent.value > config.intentThreshold).map(intent => intent.name);
intents.filter(intent => intent.value > nluConfig.intentThreshold).map(intent => intent.name);
const intentFilterPath = `${this.config.path}/src/intent-filter.js`;
if (fsExtra.pathExistsSync(intentFilterPath)) {
this.intentFilter = require(intentFilterPath);
Expand Down Expand Up @@ -75,12 +73,11 @@ class Nlu {
return extractors;
}

/**
* Initializes the Nlu module.
* @returns {Promise.<void>}
*/
/** @inheritdoc */
async init() {
logger.debug('init');
super.init();

// Extractors
this.extractor = new CompositeExtractor({
extractors: this.getExtractors(`${this.config.path}/src/extractors`),
Expand All @@ -100,12 +97,7 @@ class Nlu {
}
}

/**
* Computes intents and entities.
* @param {String} sentence - the sentence
* @param {Object} [context] - an optional context (brain and userMessage)
* @returns {Promise} a promise with entities and intents
*/
/** @inheritdoc */
async compute(sentence, context) {
logger.debug('compute', sentence);
if (this.config.qna) {
Expand Down Expand Up @@ -189,4 +181,4 @@ class Nlu {
}
}

module.exports = Nlu;
module.exports = BotfuelNlu;
52 changes: 52 additions & 0 deletions packages/botfuel-dialog/src/nlus/nlu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright (c) 2017 - present, Botfuel (https://www.botfuel.io).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const logger = require('logtown')('Nlu');
const MissingImplementationError = require('../errors/missing-implementation-error');

/**
* A nlu module (could be replaced by an external one).
*/
class Nlu {
/**
* @constructor
* @param {Object} nluConfig - the NLU configuration (config.nlu)
*/
constructor(nluConfig) {
logger.debug('constructor', nluConfig);
this.nluConfig = nluConfig;
}

/**
* Initializes the Nlu module.
* @returns {Promise.<void>}
*/
async init() {
logger.debug('init');
}

/**
* Computes intents and entities.
* @param {String} sentence - the sentence
* @param {Object} [context] - an optional context (brain and userMessage)
* @returns {Promise} a promise with entities and intents
*/
async compute() {
throw new MissingImplementationError();
}
}

module.exports = Nlu;

0 comments on commit 821a863

Please sign in to comment.