百度智能小程序 詞法分析

2020-09-05 14:15 更新

swan.ai.nlpLexerCustom

基礎(chǔ)庫(kù) 3.20.11 開(kāi)始支持,低版本需做兼容處理。

解釋:詞法分析,提供分詞、詞性標(biāo)注、專名識(shí)別三大功能。

方法參數(shù)

Object object

object 參數(shù)說(shuō)明

屬性名 類型 必填 默認(rèn)值 說(shuō)明

text

String

待分析文本

success

Function

接口調(diào)用成功后的回調(diào)函數(shù)

fail

Function

接口調(diào)用失敗的回調(diào)函數(shù)

complete

Function

接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行)

success 返回參數(shù)說(shuō)明

參數(shù)名 參數(shù)類型 說(shuō)明

log_id

Number

唯一的 log id,用于問(wèn)題定位。

text

String

原始單條請(qǐng)求文本

items

Array

詞匯數(shù)組,每個(gè)元素對(duì)應(yīng)結(jié)果中的一個(gè)詞。

items 參數(shù)說(shuō)明

參數(shù)名 參數(shù)類型 說(shuō)明

item

String

詞匯的字符串

ne

String

命名實(shí)體類型,命名實(shí)體識(shí)別算法使用。詞性標(biāo)注算法中,此項(xiàng)為空串。

pos

String

詞性,詞性標(biāo)注算法使用。命名實(shí)體識(shí)別算法中,此項(xiàng)為空串。

byte_offset

Number

在 text 中的字節(jié)級(jí) offset(使用 GBK 編碼)

byte_length

Number

字節(jié)級(jí) length(使用 GBK 編碼)

uri

String

鏈指到知識(shí)庫(kù)的 URI,只對(duì)命名實(shí)體有效。對(duì)于非命名實(shí)體和鏈接不到知識(shí)庫(kù)的命名實(shí)體,此項(xiàng)為空串。

formal

String

詞匯的標(biāo)準(zhǔn)化表達(dá),主要針對(duì)時(shí)間、數(shù)字單位,沒(méi)有歸一化表達(dá)的,此項(xiàng)為空串。

basic_words

Array

基本詞成分

loc_details

Array

地址成分,非必需,僅對(duì)地址型命名實(shí)體有效,沒(méi)有地址成分的,此項(xiàng)為空數(shù)組。

示例 

在開(kāi)發(fā)者工具中打開(kāi)


圖片示例



代碼示例

<view class="wrap">
    <view class="card-area">
        <view class="top-description border-bottom">識(shí)別內(nèi)容</view>
        <view class="display-area">{{content}}</view>
    </view>
    <view s-if="{{hasResult}}" class="card-area bottom">
        <view class="top-description border-bottom">識(shí)別結(jié)果</view>
        <view s-if="{{sucResult}}">
             <view s-for="item in result" class="list-area border-bottom">
            <view class="list-item">
                <text class="list-item-key-6">詞匯的字符串:</text>
            <text class="list-item-value">{{item.item}}</text>
            </view>
            <view class="list-item">
            <view class="list-item-key-6">命名實(shí)體類型:</view>
            <view class="list-item-value">{{item.ne !== '' ? item.ne : '暫無(wú)'}}</view>
            </view>
            <view class="list-item">
                <view class="list-item-key-6">詞性:</view>
                <view class="list-item-value">{{item.pos !== '' ? item.pos : '暫無(wú)'}}</view>
            </view>
        </view>
        </view>
        <view s-else>
            <view class="result-area-fail">識(shí)別出錯(cuò),請(qǐng)到控制臺(tái)查看信息</view>
        </view>
    </view>
        <view class="swan-security-padding-bottom flex-button">
        <button type="primary" class="" bindtap="nlpLexerCustom">
            nlpLexerCustom
        </button>
    </view>
</view>
Page({
    data: {
        content: '百度是一家高科技公司',
        result: '',
        hasResult: false,
        sucResult: false
    },
    nlpLexerCustom() {
        const text = this.getData('content');
        let that = this;
        // AI系列的api有宿主使用限制,只可在百度App中使用,建議使用時(shí)加一層判斷防止代碼報(bào)未知錯(cuò)誤
        let host = swan.getSystemInfoSync().host;
        if (host === 'baiduboxapp') {
            swan.ai.nlpLexerCustom({
                text,
                success(res) {
                    console.log('ai.nlpLexerCustom success', res);
                    that.setData({
                        'result': res.items,
                        'sucResult': true
                    })
                },
                fail(err) {
                    console.log('ai.nlpLexerCustom fail', err);
                    that.setData({
                        'sucResult': false
                    })
                },
                complete() {
                    that.setData('hasResult', true)
                }
            });
        }
        else {
            swan.showToast({
                title: '此api目前僅可在百度App上使用',
                icon: 'none'
            });
        }
    }
});

返回值示例

{
      "text":"百度是一家高科技公司",
      "items":[
         {
           "byte_length":4,
           "byte_offset":0,
           "formal":"",
           "item":"百度",
           "ne":"ORG",
           "pos":"",
           "uri":"",
           "loc_details":[ ],
           "basic_words":["百度"]
         },
         {
           "byte_length":2,
           "byte_offset":4,
           "formal":"",
           "item":"是",
           "ne":"",
           "pos":"v",
           "uri":"",
           "loc_details":[ ],
           "basic_words":["是"]
         },
         {
           "byte_length":4,
           "byte_offset":6,
           "formal":"",
           "item":"一家",
           "ne":"",
           "pos":"m",
           "uri":"",
           "loc_details":[ ],
           "basic_words":["一","家"]
         },
         {
           "byte_length":6,
           "byte_offset":10,
           "formal":"",
           "item":"高科技",
           "ne":"",
           "pos":"n",
           "uri":"",
           "loc_details":[ ],
           "basic_words":["高","科技"]
         },
         {
           "byte_length":4,
           "byte_offset":16,
           "formal":"",
           "item":"公司",
           "ne":"",
           "pos":"n",
           "uri":"",
           "loc_details":[ ],
           "basic_words":["公司"]
         }
      ]
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)