首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用bot框架sdk for node.js提取文本提示中的实体?

如何使用bot框架sdk for node.js提取文本提示中的实体?
EN

Stack Overflow用户
提问于 2018-12-18 02:52:34
回答 1查看 464关注 0票数 0

我正在使用node.js v4的机器人框架开发一个机器人。想象一下下面的场景:

用户:您好

有什么可以帮你的吗?

用户:完成转账的截止日期是多少?

bot:转账的价值是什么?

用户:$ 5,000

此时,我正在执行文本提示以请求转账的价值,并且我需要验证用户实体($ 5,000)是否已被标识为货币实体。

这是对话堆栈:

代码语言:javascript
复制
 this.addDialog(new WaterfallDialog(DUVIDA_NIVEL_APROVACAO_DIALOG, [
        this.initializeStateStep.bind(this),
        this.moneyStep.bind(this),
        this.captureMoney.bind(this),
        this.endConversation.bind(this)
    ]));

this.addDialog(new TextPrompt(MONEY_PROMPT, this.validateMoneyInput));

以及验证方法:

代码语言:javascript
复制
async validateMoneyInput(validatorContext) {
    const value = validatorContext.recognized.value; //how to get entities?
    if (value == 'money') {
        return VALIDATION_SUCCEEDED;
    } else {
        await validatorContext.context.sendActivity(`What is the value of the transfer?`);
        return VALIDATION_FAILED;
    }
}

但是,在验证文本提示符的回调中,我只有用户发送的文本。

如何在textprompt验证方法中获取Luis提取的实体?

EN

回答 1

Stack Overflow用户

发布于 2018-12-20 08:09:33

要将任何LUIS结果放入对话框瀑布中,首先需要在turnContext上捕获结果,如下所示:

代码语言:javascript
复制
if (turnContext.activity.type === ActivityTypes.Message) {

    // Returns LUIS matched results
    const results = await this.luisRecognizer.recognize(turnContext);

    // Results are assigned to the turnContext object and is passed into the dialog stack
    turnContext.topIntent = results.luisResult.topScoringIntent;
    turnContext.topIntent.entities = results.luisResult.entities;
    turnContext.topIntent.value = results.luisResult.query;

    // Create a dialog context object.
    const dc = await this.dialogs.createContext(turnContext);

    const utterance = (turnContext.activity.text || '').trim().toLowerCase();
    if (utterance === 'cancel') {
        if (dc.activeDialog) {
            await dc.cancelAllDialogs();
            await dc.context.sendActivity(`Ok... canceled.`);
        } else {
            await dc.context.sendActivity(`Nothing to cancel.`);
        }
    }

    // If the bot has not yet responded, continue processing the current dialog.
    await dc.continueDialog();

    // Start the sample dialog in response to any other input.
    if (!turnContext.responded) {
        await dc.beginDialog(DUVIDA_NIVEL_APROVACAO_DIALOG);
    }
}

现在已经传入了结果,您可以通过step.context对象访问结果,如下所示:

代码语言:javascript
复制
this.dialogs.add(new TextPrompt(MONEY_PROMPT, this.validateMoneyInput.bind(this)));

async moneyStep(step) {
    await step.prompt(MONEY_PROMPT, `What is the value of the transfer?`,
        {
            retryPrompt: 'Try again. What is the value of the transfer?'
        }
    );
}

async validateMoneyInput(step) {
    // LUIS results passed into turnContext are retrieved
    const intent = step.context.topIntent['intent'];

    const entity = step.context.topIntent.entities;
    console.log(entity);

    // Validation based on matched intent
    if (intent == 'Money') {
        return await step.context.sendActivity('Validation succeeded');
    } else if (intent != 'Money') {
        return await step.context.sendActivity('Validation failed');
    }
}

我还将实体值赋给了一个变量以供访问,因为您正在询问它。

希望能帮上忙!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53821397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档