Overview

Namespaces

  • Baidu
    • Duer
      • Botsdk
        • Card
        • Directive
          • AppLauncher
          • AudioPlayer
          • WebBrowser
        • Plugins

Classes

  • Baidu\Duer\Botsdk\Bot
  • Baidu\Duer\Botsdk\Card\BaseCard
  • Baidu\Duer\Botsdk\Card\ImageCard
  • Baidu\Duer\Botsdk\Card\ListCard
  • Baidu\Duer\Botsdk\Card\ListCardItem
  • Baidu\Duer\Botsdk\Card\StandardCard
  • Baidu\Duer\Botsdk\Card\TextCard
  • Baidu\Duer\Botsdk\Certificate
  • Baidu\Duer\Botsdk\Directive\AppLauncher\LaunchApp
  • Baidu\Duer\Botsdk\Directive\AudioPlayer\Play
  • Baidu\Duer\Botsdk\Directive\AudioPlayer\Stop
  • Baidu\Duer\Botsdk\Directive\BaseDirective
  • Baidu\Duer\Botsdk\Directive\WebBrowser\LaunchBrowser
  • Baidu\Duer\Botsdk\Intercept
  • Baidu\Duer\Botsdk\Log
  • Baidu\Duer\Botsdk\Nlu
  • Baidu\Duer\Botsdk\Plugins\DuerSessionIntercept
  • Baidu\Duer\Botsdk\Request
  • Baidu\Duer\Botsdk\Response
  • Baidu\Duer\Botsdk\Session

Traits

  • Baidu\Duer\Botsdk\DataObject
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 
<?php
/**
 * Copyright (c) 2017 Baidu, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @desc NLU解析query,分析的结果
 * @author ******@baidu.com
 **/
namespace Baidu\Duer\Botsdk;

class Nlu{
    /**
     * @desc 一般处于多轮的服务,会对一个slot进行询问。
     * 但是,会出现答非所问的情况,或者解析覆盖不到的地方
     * 如果出现上述情况,解析会填这个字段, value为int,计数出现了多少次没理解用户说的query
     **/
    const SLOT_NOT_UNDERSTAND = "da_system_not_understand";

    /**
     * 数据
     **/
    private $data = [];

    /**
     * 记录返回的指令
     **/
    private $directive = [];


    /**
     * @param array $data
     * @return null
     **/
    public function __construct($data) {
        $this->data = $data; 
    }

    /**
     * 设置槽位
     * @desc 设置slot。如果不存在,新增一个slot
     * @param string $field 槽位名
     * @param string $value 槽位值
     * @param string $index 第几组slot
     * @return null
     **/
    public function setSlot($field, $value, $index=0){
        if(empty($field)){
            return;
        }
    
        $slots = &$this->data[$index]['slots'];
        if($slots[$field]) {
            $slots[$field]['value'] = $value;
            return;
        }
        
        $slots[$field] = [
            'name' => $field,
            'value' => $value,
        ];
    }

    /**
     * 获取槽位
     * @desc 获取一个slot对应的值
     * @param string $field 槽位名
     * @return string 槽位值
     **/
    public function getSlot($field, $index=0) {
        if(empty($field)){
            return;
        }

        $slots = $this->data[$index]['slots'];
        return $slots[$field]['value'];
    }


    /**
     * @desc 获取当前的intent 名
     * @param null
     * @return string
     **/
    public function getIntentName(){
        return $this->data[0]['name'];
    }

    /**
     * 是否询问过用户
     * @desc 是否有询问用户
     *
     * @param null
     * @return boolean
     **/
    public function hasAsked(){
        return !!$this->directive; 
    }

    /**
     * 询问一个特定的槽位
     * @desc 询问某些槽位。如果有询问一些槽位,表明多轮进行中
     * @param string|array $slot 槽位名
     * @return null
     **/
    public function ask($slot){
        if(!$slot) {
            return;
        }

        $this->askSlot = $slot;
        $this->directive = [
            'type' => 'Dialog.ElicitSlot',
            'slotToElicit' => $slot,
            'updatedIntent' => $this->getUpdateIntent(),
        ];
    }

    /**
     * @desc 打包NLU交互协议,返回DuerOS,为第二轮用户回答提供上下文
     *       在Response 中被调用
     * @param null
     * @return array
     **/
    public function toDirective(){
        $intents=[];

        return $this->directive;
    }

    /**
     * @desc 私有。构造返回的update intent 数据结构
     * @param null
     * @return array
     **/
    private function getUpdateIntent(){
        return [
                'name' => $this->getIntentName(),
                'slots' => $this->data[0]['slots'],
            ];
    }

    /**
     * @desc bot可以修改intent中slot对应的值,返回给DuerOS更新
     *       在Response 中被调用
     * @param null
     * @return array
     **/
    public function toUpdateIntent(){
        return [
            'intent' => $this->data[0]
        ]; 
    }

    /**
     * @desc 设置delegate某个槽位或确认意图。
     * @param null
     * @return null
     **/
    public function setDelegate(){
        $this->directive = [
            'type' => 'Dialog.Delegate',
            'updatedIntent' => $this->getUpdateIntent(),
        ];
    }

    /**
     * @desc 设置对一个槽位的确认
     * @param string $field 槽位名
     * @return null
     **/
    public function setConfirmSlot($field){
        $slots = $this->data[0]['slots'];

        if(array_key_exists($field, $slots)) {
            $this->directive = [
                'type' => 'Dialog.ConfirmSlot',
                'slotToConfirm' => $field,
                'updatedIntent' => $this->getUpdateIntent(),
            ];
        }
    }

    /**
     * @desc 设置confirm 意图。询问用户是否对意图确认,设置后需要自行返回outputSpeech
     * @param null
     * @return
     **/
    public function setConfirmIntent() {
        $this->directive = [
            'type' => 'Dialog.ConfirmIntent',
            'updatedIntent' => $this->getUpdateIntent(),
        ]; 
    }
}
 
API documentation generated by ApiGen