Skip to content

Commit

Permalink
fix: None feature was affecting too much when no so many intents.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesus Seijas committed Aug 20, 2019
1 parent 1cdf6b6 commit 344885c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
18 changes: 18 additions & 0 deletions lib/classifiers/neural-network.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ class NeuralNetwork {
}

train(srcData) {
const useNoneFeature =
srcData[srcData.length - 1].input.nonefeature !== undefined;
if (useNoneFeature) {
const intents = {};
for (let i = 0; i < srcData.length - 1; i += 1) {
const tokens = Object.keys(srcData[i].output);
for (let j = 0; j < tokens.length; j += 1) {
if (!intents[tokens[j]]) {
intents[tokens[j]] = 1;
}
}
}
const current = srcData[srcData.length - 1];
const keys = Object.keys(intents);
for (let i = 0; i < keys.length; i += 1) {
current.output[keys[i]] = 0.0000001;
}
}
const data = this.formatData(srcData);
if (!this.status) {
this.status = { error: Infinity, deltaError: Infinity, iterations: 0 };
Expand Down
10 changes: 8 additions & 2 deletions lib/nlp/nlp-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,21 @@ class NlpManager {
*/
addDocument(locale, srcUtterance, intent) {
const utterance = this.translateUtterance(srcUtterance);
this.nluManager.addDocument(locale, utterance, intent);
const entities = this.nerManager.getEntitiesFromUtterance(utterance);
this.slotManager.addBatch(intent, entities);
const optionalUtterance = this.nerManager.generateNamedEntityUtterance(
utterance,
locale
);
if (optionalUtterance) {
this.nluManager.addDocument(locale, optionalUtterance, intent);
this.nluManager.addDocument(locale, `${optionalUtterance}`, intent);
this.nluManager.addDocument(
locale,
`${optionalUtterance} ${utterance}`,
intent
);
} else {
this.nluManager.addDocument(locale, utterance, intent);
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/nlu/base-nlu.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,13 @@ class BaseNLU {
}

getWhitelist(tokens) {
const result = [];
const result = {};
for (let i = 0; i < this.docs.length; i += 1) {
if (this.someSimilar(tokens, this.docs[i].tokens)) {
result.push(this.docs[i].intent);
result[this.docs[i].intent] = 1;
}
}
return result;
return Object.keys(result);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/nlu/brain-nlu.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class BrainNLU extends BaseNLU {
getClassifications(utterance) {
const tokens = this.textToFeatures(utterance);
const whitelist = this.getWhitelist(tokens);
if (!whitelist.includes.None) {
if (!whitelist.includes('None')) {
whitelist.push('None');
}
const classifications = this.classifier.getClassifications(tokens);
Expand Down

0 comments on commit 344885c

Please sign in to comment.