跳到内容 跳到主导航 跳到页脚

程序解析NLP

如何配置语音指令,参考如下:

语音:“打开我的程序”

链接地址:NLP启动OPK示例

解析NLP内容,分2种情况:

  • 语音指令唤醒OPK,入口文件解析NLP

语音:“播放周杰伦的歌”      

文件:当前项目目录/app/demo/DemoScreen.tsx

方法:public render()

变量输出:console.log(‘烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫’,  this.props);

输出的示例(参考,不同NLP配置有差异):

{
    screenProps: {},
    navigation:
    {
        goBack: [Function],
        navigate: [Function],
        setParams: [Function],
        state:
        {
            key: 'system_d398ac9f3933f49ad71a7f581dc9eecc_migumusic',
            routeName: 'system_d398ac9f3933f49ad71a7f581dc9eecc_migumusic',
            params:
            {
                channel: undefined,
                type: 2,
                result:
                {
                    answerTextPlay: false,
                    card: '{}',
                    englishDomain: 'music_1',
                    englishIntent: 'search_music',
                    intent: 'music_1&search_music',
                    nlpData: '{"detail":[{"agent":"task","semantics_flag":1,"query":"播放周杰伦的歌","asrText":"播放周杰伦的歌","source":"OrionRuleBasedGrammar","intent":"search_music","english_domain":"music_1","debug_info":{},"slots":{"singer":[{"ex":{},"dict_name":"nlpmini_migu_singer","slot_id":375609654968321,"slot_type":"NORMAL","text":"周杰伦","value":"周杰伦"}]},"cmd_dispatch_level":"1","domain":"music_1","skill_response":{},"sn":"ceab356f-a7de-4cc8-ac13-e1d1cbd32a5c","skill_nlu":false,"actions":[],"current_time":"2021-08-18 17:16:22","nlpData":{"misc":{"app":[]}}}],"misc":{"app":[]}}',
                              queryType: 1,
                              sid: 'ceab356f-a7de-4cc8-ac13-e1d1cbd32a5c',
                    skillData: '{}',
                    slots: '{"singer":[{"ex":{},"dict_name":"nlpmini_migu_singer","slot_id":375609654968321,"slot_type":"NORMAL","text":"周杰伦","value":"周杰伦"}]}',
                    soundAngle: -1,
                    traceId: 'ceab356fa7de4cc8',
                    userText: '播放周杰伦的歌',
                    id: 6578,
                    spinId: '32b9dfb285634038',
                    chainStartTime: 1629278182418000
                },
                eventId: -1
            }
        },
        router: undefined,
        actions:
        {
            goBack: [Function: goBack],
            navigate: [Function: navigate],
            setParams: [Function: setParams]
        },
        getParam: [Function],
        getChildNavigation: [Function: getChildNavigation],
        isFocused: [Function: isFocused],
        isFirstRouteInParent: [Function: isFirstRouteInParent],
        dispatch: [Function],
        getScreenProps: [Function],
        dangerouslyGetParent: [Function],
        addListener: [Function: addListener],
        emit: [Function: emit]
    }
}

domain: 对应示例中englishDomain的值

intent:  对应示例中englishIntent的值

解析关键字:对应示例中navigation->state->params->result->nlpData->detail->slots->singer->value的值,nlpData需要转换成对象      

方法示例如下:

public render() {
      let obj = this.props.navigation.state.params.result;
      let nlpData_object = JSON.parse(obj.nlpData);    // 此处内容为NLP完整内容
      return(null);
}

继续解析nlpData_object获取想要的内容,传递给服务器或其他需要的地方

  • OPK已启动后,解析NLP数据内容

文件位置:当前项目目录/app/demo/DemoVoice.ts 的  onListenCallback()方法

public onListenCallback(intent: string, result: any, id: number, text: string): boolean {
    console.log(TAG, "ScenePolicy&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&: +  ", id, '     ======', intent, result);
    let nlpData_object = JSON.parse(result.nlpData);      // NLP完整内容
};

继续解析nlpData_object获取想要的内容,传递给服务器或其他需要的地方

这篇文章是否有帮助?

0