您当前的位置: 首页 > 解决方案

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

  • 作者: admin
  • 发布于 2018-09-06 13:46:00
  • 来源:  
  • 栏目:解决方案

导语: 项目中用到了 olami sdk把录音或者文字转化为用户可以理解的json字符串。效果图重要jS代码: //手指按下时 语音转文字 voiceToChar:function(){ var urls = app.glob

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

效果图

09.jpg

重要jS代码:

 
  1. //手指按下时 语音转文字
  2. voiceToChar:function(){
  3. var urls = app.globalData.slikToCharUrl;
  4. var voiceFilePath = that.data.filePath;
  5. if(voiceFilePath == null){
  6. console.log("[Console log]:File path do not exist!");
  7. wx.showModal({
  8. title: '录音文件不存在',
  9. content: '我也不知道哪错了,反正你就再试一次吧!',
  10. showCancel: false,
  11. confirmText: '确定',
  12. confirmColor: '#09BB07',
  13. })
  14. return;
  15. }
  16. var appkey = app.globalData.NLPAppkey;
  17. var appsecret = app.globalData.NLPAppSecret;
  18. var NLPCusid = app.globalData.NLPCusid;
  19. wx.showLoading({
  20. title: '语音识别中...',
  21. })
  22. wx.uploadFile({
  23. url: urls,
  24. filePath: voiceFilePath,
  25. name: 'file',
  26. formData: { "appKey": appkey, "appSecret": appsecret, "userId": NLPCusid },
  27. header: { 'content-type': 'multipart/form-data' },
  28. success: function (res) {
  29. wx.hideLoading();
  30. var data = JSON.parse(res.data);
  31. var seg = JSON.parse(data.result).seg;
  32. console.log("[Console log]:Voice to char:" + seg);
  33. if(seg == null || seg.length == 0){
  34. wx.showModal({
  35. title: '录音识别失败',
  36. content: "我什么都没听到,你再说一遍!",
  37. showCancel: false,
  38. success: function (res) {
  39. }
  40. });
  41. return;
  42. }
  43. that.addChat(seg, 'r');
  44. console.log("[Console log]:Add user voice input to chat list");
  45. that.sendRequest(seg);
  46. return;
  47. },
  48. fail: function (res) {
  49. console.log("[Console log]:Voice upload failed:" + res.errMsg);
  50. wx.hideLoading();
  51. wx.showModal({
  52. title: '录音识别失败',
  53. content: "请你离WIFI近一点再试一次!",
  54. showCancel: false,
  55. success: function (res) {
  56. }
  57. });
  58. }
  59. });
  60. },

olami 技术的识别语音代码:

 
  1. // 发送语料到语义平台
  2. sendChat: function (e) {
  3. let word = e.detail.value.ask_word ? e.detail.value.ask_word : e.detail.value;
  4. console.log("[Console log]:User input:" + word);
  5. that.addChat(word, 'r');
  6. console.log("[Console log]:Add user input to chat list");
  7. that.setData({
  8. askWord: '',
  9. sendButtDisable: true,
  10. });
  11. that.sendRequest(word);
  12. },
  13. // 发送请求到语义平台
  14. sendRequest(corpus){
  15. app.NLIRequest(corpus, {
  16. 'success': function (res) {
  17. if (res.status == "error") {
  18. wx.showToast({
  19. title: '返回数据有误!',
  20. })
  21. return;
  22. }
  23. var resjson = JSON.parse(res);
  24. var data = JSON.stringify(resjson.data);
  25. that.NLIProcess(data);
  26. },
  27. 'fail': function (res) {
  28. wx.showToast({
  29. title: '请求失败!',
  30. })
  31. return;
  32. }
  33. });
  34. },
  35. // 处理语义
  36. NLIProcess: function(res){
  37. var nlires = JSON.parse(res);
  38. var nliArray = nlires.nli;
  39. if(nliArray == null || nliArray.length == 0){
  40. wx.showToast({
  41. title: '返回数据有误!',
  42. })
  43. return;
  44. }
  45. var answer = nliArray[0].desc_obj.result;
  46. if(answer == null){
  47. wx.showToast({
  48. title: '返回数据有误!',
  49. })
  50. return;
  51. }
  52. console.log("[Console log]:Add answer to chat list...");
  53. that.addChat(answer, 'l');
  54. var dataArray = nliArray[0].data_obj;
  55. if(dataArray != null && dataArray.length > 0){
  56. var objType = nliArray[0].type;
  57. if(objType == 'selection' && dataArray.length > 1){
  58. that.newsProcess(dataArray);
  59. return;
  60. }
  61. if (objType == 'news' && dataArray.length == 1) {
  62. console.log("[Console log]:Add news to chat list...");
  63. var title = dataArray[0].title;
  64. var detail = dataArray[0].detail;
  65. var news = title + "\n" + detail;
  66. that.addChat(news, 'l');
  67. return;
  68. }
  69. var content = dataArray[0].content;
  70. if (content != null && content != answer){
  71. console.log("[Console log]:Add content to chat list...");
  72. that.addChat(content, 'l');
  73. }
  74. }
  75. return;
  76. },

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

小程序社区博主:honey缘木鱼  



温馨提示:这篇文章没有解决您的问题?欢迎添加微信:18948083295,有微信小程序专业人员,保证有问必答。转载本站文章请注明转自http://www.okeydown.com/(微信小程序网)。

  • 微信扫描二维码关注官方微信
  • ▲长按图片识别二维码
关注我们

微信小程序官方微信

栏目最新
栏目推荐
返回顶部