Skip to content

Commit

Permalink
Merge pull request #2017 from botpress/fl_error_when_no_utterances
Browse files Browse the repository at this point in the history
fix(nlu): skip intent prediction after exact match if not enough utterances
  • Loading branch information
franklevasseur committed Jun 28, 2019
2 parents c6f9395 + 38217d8 commit 0fbf5c6
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions modules/nlu/src/backend/engine.ts
Expand Up @@ -252,8 +252,14 @@ export default class ScopedEngine implements Engine {
protected async loadModels(intents: sdk.NLU.IntentDefinition[], modelHash: string) {
this.logger.debug(`Restoring models '${modelHash}' from storage`)
const trainableLangs = _.intersection(this.getTrainingLanguages(intents), this.languages)
for (const lang of this.languages) {
const trainingSet = await this.getTrainingSets(intents, lang)
this._exactIntentMatchers[lang] = new ExactMatcher(trainingSet)

if (!trainableLangs.includes(lang)) {
return
}

for (const lang of trainableLangs) {
const models = await this.storage.getModelsFromHash(modelHash, lang)

const intentModels = _.chain(models)
Expand All @@ -277,9 +283,6 @@ export default class ScopedEngine implements Engine {
throw new Error(`Could not find intent models. Hash = "${modelHash}"`)
}

const trainingSet = await this.getTrainingSets(intents, lang)
this._exactIntentMatchers[lang] = new ExactMatcher(trainingSet)

await this.intentClassifiers[lang].load(intentModels)
await this.slotExtractors[lang].load(trainingSet, skipgramModel.model, crfModel.model)
}
Expand Down Expand Up @@ -390,18 +393,20 @@ export default class ScopedEngine implements Engine {
private _extractIntents = async (ds: NLUStructure): Promise<NLUStructure> => {
const exactMatcher = this._exactIntentMatchers[ds.language]
const exactIntent = exactMatcher && exactMatcher.exactMatch(ds.sanitizedText, ds.includedContexts)

const skipIntentExtraction = !((await this.getIntents()) || []).length
if (skipIntentExtraction) {
return ds
}

if (exactIntent) {
ds.intent = exactIntent
ds.intents = [exactIntent]
return ds
}

const allIntents = (await this.getIntents()) || []
const shouldPredict =
allIntents.length && allIntents.some(i => i.utterances[ds.language].length >= MIN_NB_UTTERANCES)

if (!shouldPredict) {
return ds
}

const intents = await this.intentClassifiers[ds.language].predict(ds.tokens, ds.includedContexts)

// alter ctx with the given predictions in case where no ctx were provided
Expand Down Expand Up @@ -436,6 +441,10 @@ export default class ScopedEngine implements Engine {

const intentDef = await this.storage.getIntent(ds.intent.name)

if (!(intentDef.slots && intentDef.slots.length)) {
return ds
}

ds.slots = await this.slotExtractors[ds.language].extract(
ds.lowerText,
ds.language,
Expand Down

0 comments on commit 0fbf5c6

Please sign in to comment.