Skip to content

Commit

Permalink
fix(all): NLP error handling improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Dec 22, 2020
1 parent efed759 commit 6ed7380
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ export class GBConversationalService {
try {
const saved = step.context.activity.text;
step.context.activity.text = text;
nlp = await model.recognize(step.context,{},{},{IncludeAllIntents:false, IncludeInstanceData: false, includeAPIResults: true});
nlp = await model.recognize(step.context, {}, {}, { IncludeAllIntents: false, IncludeInstanceData: false, includeAPIResults: true });
step.context.activity.text = saved;
} catch (error) {
// tslint:disable:no-unsafe-any
Expand Down Expand Up @@ -644,30 +644,24 @@ export class GBConversationalService {
`NLP called: ${intent}, entities: ${nlp.entities.length}, score: ${score} > required (nlpScore): ${instanceScore}`
);

try {
step.activeDialog.state.options.entities = nlp.entities;
step.activeDialog.state.options.entities = nlp.entities;

// FIX MSFT NLP issue.
// FIX MSFT NLP issue.

if (nlp.entities) {
await CollectionUtil.asyncForEach(Object.keys(nlp.entities), async key => {
if (key !== "$instance") {
let entity = nlp.entities[key];
if (Array.isArray(entity[0])) {
nlp.entities[key] = entity.slice(1);
}
if (nlp.entities) {
await CollectionUtil.asyncForEach(Object.keys(nlp.entities), async key => {
if (key !== "$instance") {
let entity = nlp.entities[key];
if (Array.isArray(entity[0])) {
nlp.entities[key] = entity.slice(1);
}
});
}

await step.replaceDialog(`/${intent}`, step.activeDialog.state.options);
}
});
}

return true;
} catch (error) {
const msg = `Error finding dialog associated to NLP event: ${intent}: ${error.message}`;
await step.replaceDialog(`/${intent}`, step.activeDialog.state.options);

throw new Error(msg);
}
return true;
}

GBLog.info(
Expand Down Expand Up @@ -777,22 +771,46 @@ export class GBConversationalService {
}

public async sendText(min: GBMinInstance, step, text) {
await this['sendTextWithOptions'](min, step, text, true);
await this['sendTextWithOptions'](min, step, text, true, null);
}

public async sendTextWithOptions(min: GBMinInstance, step, text, translate) {
public async sendTextWithOptions(min: GBMinInstance, step, text, translate, keepTextList) {
const member = step.context.activity.from;
const user = await min.userProfile.get(step.context, {});
const systemUser = user.systemUser;

if (translate) {
let replacements = [];

if (keepTextList) {
keepTextList = keepTextList.filter(p => p.trim() !== '');
let i = 0;
await CollectionUtil.asyncForEach(keepTextList, item => {
if (text.toLowerCase().indexOf(item.toLowerCase()) != -1) {
const replacementToken = GBAdminService['getNumberIdentifier']();
replacements[i] = { text: item, replacementToken: replacementToken };
i++;
text = text.replace(new RegExp(item.trim(), 'gi'), `${replacementToken}`);
}
});
}

text = await min.conversationalService.translate(
min,
text,
systemUser.locale
? systemUser.locale
: min.core.getParam<string>(min.instance, 'Locale', GBConfigService.get('LOCALE'))
);

if (keepTextList) {
let i = 0;
await CollectionUtil.asyncForEach(replacements, item => {
i++;
text = text.replace(new RegExp(`${item.replacementToken}`, 'gi'), item.text);
});
}

GBLog.info(`Translated text(sendText): ${text}.`);
}

Expand All @@ -804,6 +822,8 @@ export class GBConversationalService {
} else {
await step.context.sendActivity(text);
}


}

public async broadcast(min: GBMinInstance, message: string) {
Expand Down

0 comments on commit 6ed7380

Please sign in to comment.