前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序语音聊天智能对话(demo)

微信小程序语音聊天智能对话(demo)

作者头像
honey缘木鱼
发布2018-07-04 14:39:04
3.3K0
发布2018-07-04 14:39:04
举报
文章被收录于专栏:娱乐心理测试娱乐心理测试

项目中用到了 olami sdk把录音或者文字转化为用户可以理解的json字符串。

效果图

重要jS代码:

代码语言:javascript
复制
 //手指按下时 语音转文字
  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 技术的识别语音代码:

代码语言:javascript
复制
// 发送语料到语义平台
  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;
  },

源码: 百度云:https://pan.baidu.com/s/1jHJj1HG

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.06.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
语音识别
腾讯云语音识别(Automatic Speech Recognition,ASR)是将语音转化成文字的PaaS产品,为企业提供精准而极具性价比的识别服务。被微信、王者荣耀、腾讯视频等大量业务使用,适用于录音质检、会议实时转写、语音输入法等多个场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档