Bläddra i källkod

阿里云解析接口调整 解析返回的结果逻辑调整

王苗苗 1 vecka sedan
förälder
incheckning
190ca164e5

+ 2 - 1
slibra-admin/src/main/java/com/slibra/web/controller/business/FileTransJavaDemo.java

@@ -70,6 +70,7 @@ public class FileTransJavaDemo {
         taskObject.put(KEY_VERSION, "4.0");
         // 设置是否输出词信息,默认为false,开启时需要设置version为4.0及以上。
         taskObject.put(KEY_ENABLE_WORDS, true);
+        taskObject.put("auto_split", true);
         String task = taskObject.toJSONString();
         System.out.println(task);
         // 设置以上JSON字符串为Body参数。
@@ -156,7 +157,7 @@ public class FileTransJavaDemo {
         final String accessKeyId = "LTAI5tQ2HmiHCygZkt5BYrYR";
         final String accessKeySecret = "KhmxTd14SUcXafpFk5yofA43FoeM99";
         final String appKey = "OKt6jogp6fRjHQVp";
-        String fileLink = "https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav";
+        String fileLink = "https://static.fuxicarbon.com/test/234527-I-0168-15765398611-S.wav";
 //        String fileLink = "https://www.w3school.com.cn/i/audio/song.mp3";
         FileTransJavaDemo demo = new FileTransJavaDemo(accessKeyId, accessKeySecret);
         // 第一步:提交录音文件识别请求,获取任务ID用于后续的识别结果轮询。

+ 7 - 1
slibra-system/src/main/java/com/slibra/business/service/impl/File2Text.java

@@ -1,5 +1,6 @@
 package com.slibra.business.service.impl;
 
+import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONObject;
 import com.aliyuncs.CommonRequest;
 import com.aliyuncs.CommonResponse;
@@ -24,6 +25,7 @@ public class File2Text {
     public static final String KEY_FILE_LINK = "file_link";
     public static final String KEY_VERSION = "version";
     public static final String KEY_ENABLE_WORDS = "enable_words";
+    public static final String AUTO_SPLIT = "auto_split";
     // 响应参数
     public static final String KEY_TASK = "Task";
     public static final String KEY_TASK_ID = "TaskId";
@@ -71,6 +73,7 @@ public class File2Text {
         taskObject.put(KEY_VERSION, "4.0");
         // 设置是否输出词信息,默认为false,开启时需要设置version为4.0及以上。
         taskObject.put(KEY_ENABLE_WORDS, true);
+        taskObject.put(AUTO_SPLIT, true);
         String task = taskObject.toJSONString();
         System.out.println(task);
         // 设置以上JSON字符串为Body参数。
@@ -157,7 +160,8 @@ public class File2Text {
         final String accessKeyId = "LTAI5tQ2HmiHCygZkt5BYrYR";
         final String accessKeySecret = "KhmxTd14SUcXafpFk5yofA43FoeM99";
         final String appKey = "OKt6jogp6fRjHQVp";
-        String fileLink = "https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav";
+//        String fileLink = "https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav";
+        String fileLink = "https://static.fuxicarbon.com/test/234527-I-0168-15765398611-S.wav";
 //        String fileLink = "https://www.w3school.com.cn/i/audio/song.mp3";
         File2Text demo = new File2Text(accessKeyId, accessKeySecret);
         // 第一步:提交录音文件识别请求,获取任务ID用于后续的识别结果轮询。
@@ -173,6 +177,8 @@ public class File2Text {
         String result = demo.getFileTransResult(taskId);
         if (result != null) {
             System.out.println("录音文件识别结果查询成功:" + result);
+            String sentences = JSON.parseObject(result).get("Sentences").toString();
+            System.out.println(sentences);
         }
         else {
             System.out.println("录音文件识别结果查询失败!");

+ 13 - 5
slibra-system/src/main/java/com/slibra/business/service/impl/TCallRecordServiceImpl.java

@@ -210,15 +210,23 @@ public class TCallRecordServiceImpl implements ITCallRecordService
         // 第二步:根据任务ID轮询识别结果。
         String result = file2Text.getFileTransResult(taskId);
         if (result != null) {
-            log.info("录音文件识别结果查询成功:" + result);
-            Ele ele = JSON.parseObject(JSON.parseArray(JSON.parseObject(result).get("Sentences").toString()).get(0).toString(), Ele.class);
+//            log.info("录音文件识别结果查询成功:" + result);
+//            Ele ele = JSON.parseObject(JSON.parseArray(JSON.parseObject(result).get("Sentences").toString()).get(0).toString(), Ele.class);
 //            log.info(JSON.toJSONString(ele));
-            String text = ele.getText();
+//            String text = ele.getText();
+            //2024年11月19日11:09:33  不用再解析Sentences下面的某个值,把返回的Sentences全部返回给前端
+            String sentences = null;
+            try {
+                sentences = JSON.parseObject(result).get("Sentences").toString();
+            } catch (Exception e) {
+                log.error("解析返回的语音结果异常,返回结果为{}\n异常信息为{}", result, e.getMessage());
+                throw new ServiceException("解析返回的语音结果异常");
+            }
             //同时更新到数据库中去
             tCallRecord.setHasParsed(LONG_1);
-            tCallRecord.setParsedVoiceContent(text);
+            tCallRecord.setParsedVoiceContent(sentences);
             this.tCallRecordMapper.updateTCallRecord(tCallRecord);
-            return text;
+            return sentences;
         }
         else {
 //            log.error("录音文件识别结果查询失败!");