网友真实露脸自拍10p,成人国产精品秘?久久久按摩,国产精品久久久久久无码不卡,成人免费区一区二区三区

小程序模板網

微信小程序語音聊天智能對話(demo)

發布時間:2018-09-07 08:48 所屬欄目:小程序開發教程

項目中用到了 olami sdk把錄音或者文字轉化為用戶可以理解的json字符串。

效果圖

重要jS代碼:

 

  1. //手指按下時 語音轉文字
    voiceToChar:function(){
    var urls = app.globalData.slikToCharUrl;
    var voiceFilePath = that.data.filePath;
    if(voiceFilePath == null){
    console.log("[Console log]:File path do not exist!");
    wx.showModal({
    title: '錄音文件不存在',
    content: '我也不知道哪錯了,反正你就再試一次吧!',
    showCancel: false,
    confirmText: '確定',
    confirmColor: '#09BB07',
    })
    return;
    }
    var appkey = app.globalData.NLPAppkey;
    var appsecret = app.globalData.NLPAppSecret;
    var NLPCusid = app.globalData.NLPCusid;
    wx.showLoading({
    title: '語音識別中...',
    })
    wx.uploadFile({
    url: urls,
    filePath: voiceFilePath,
    name: 'file',
    formData: { "appKey": appkey, "appSecret": appsecret, "userId": NLPCusid },
    header: { 'content-type': 'multipart/form-data' },
    success: function (res) {
    wx.hideLoading();
    var data = JSON.parse(res.data);
    var seg = JSON.parse(data.result).seg;
    console.log("[Console log]:Voice to char:" + seg);
    if(seg == null || seg.length == 0){
    wx.showModal({
    title: '錄音識別失敗',
    content: "我什么都沒聽到,你再說一遍!",
    showCancel: false,
    success: function (res) {
    }
    });
    return;
    }
    that.addChat(seg, 'r');
    console.log("[Console log]:Add user voice input to chat list");
    that.sendRequest(seg);
    return;
    },
    fail: function (res) {
    console.log("[Console log]:Voice upload failed:" + res.errMsg);
    wx.hideLoading();
    wx.showModal({
    title: '錄音識別失敗',
    content: "請你離WIFI近一點再試一次!",
    showCancel: false,
    success: function (res) {
    }
    });
    }
    });
    },

olami 技術的識別語音代碼:

 

  1. // 發送語料到語義平臺
    sendChat: function (e) {
    let word = e.detail.value.ask_word ? e.detail.value.ask_word : e.detail.value;
    console.log("[Console log]:User input:" + word);
    that.addChat(word, 'r');
    console.log("[Console log]:Add user input to chat list");
    that.setData({
    askWord: '',
    sendButtDisable: true,
    });
    that.sendRequest(word);
    },
    // 發送請求到語義平臺
    sendRequest(corpus){
    app.NLIRequest(corpus, {
    'success': function (res) {
    if (res.status == "error") {
    wx.showToast({
    title: '返回數據有誤!',
    })
    return;
    }
    var resjson = JSON.parse(res);
    var data = JSON.stringify(resjson.data);
    that.NLIProcess(data);
    },
    'fail': function (res) {
    wx.showToast({
    title: '請求失??!',
    })
    return;
    }
    });
    },
    // 處理語義
    NLIProcess: function(res){
    var nlires = JSON.parse(res);
    var nliArray = nlires.nli;
    if(nliArray == null || nliArray.length == 0){
    wx.showToast({
    title: '返回數據有誤!',
    })
    return;
    }
    var answer = nliArray[0].desc_obj.result;
    if(answer == null){
    wx.showToast({
    title: '返回數據有誤!',
    })
    return;
    }
    console.log("[Console log]:Add answer to chat list...");
    that.addChat(answer, 'l');
    var dataArray = nliArray[0].data_obj;
    if(dataArray != null && dataArray.length > 0){
    var objType = nliArray[0].type;
    if(objType == 'selection' && dataArray.length > 1){
    that.newsProcess(dataArray);
    return;
    }
    if (objType == 'news' && dataArray.length == 1) {
    console.log("[Console log]:Add news to chat list...");
    var title = dataArray[0].title;
    var detail = dataArray[0].detail;
    var news = title + "\n" + detail;
    that.addChat(news, 'l');
    return;
    }
    var content = dataArray[0].content;
    if (content != null && content != answer){
    console.log("[Console log]:Add content to chat list...");
    that.addChat(content, 'l');
    }
    }
    return;
    },
    


本文地址:http://www.xiuhaier.com/wxmini/doc/course/24778.html 復制鏈接 如需定制請聯系易優客服咨詢:800182392 點擊咨詢
QQ在線咨詢
AI智能客服 ×