• 百度语音识别api使用(Java版本)


    系列文章目录


    一、pom依赖引入

      
            <dependency>
                <groupId>com.baidu.aipgroupId>
                <artifactId>java-sdkartifactId>
                <version>4.1.1version>
                <exclusions>
                    <exclusion>
                        <groupId>log4jgroupId>
                        <artifactId>log4jartifactId>
                    exclusion>
                exclusions>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    二、百度语音转文字

    具体参考百度API文档

     	/**
         * 百度语音转文字
         *
         * @param voiceBytes pcm格式的文件字节数组
         * @param accessToken 百度AI的token
         * @param cuid  参考百度API文档,调试阶段入参随机值
         * @return 识别出的结果值
         */
        public String voiceIdentifyToString(byte[] voiceBytes, String accessToken, String cuid) {
            if (voiceBytes == null || voiceBytes.length == 0) {
                return StringUtils.EMPTY;
            }
            String speech = Base64.getEncoder().encodeToString(voiceBytes);
            BaiduReqDto baiduReqDto = new BaiduReqDto().setFormat("pcm").setRate(16000).setChannel(1)
                    .setCuid(cuid).setSpeech(speech).setLen(voiceBytes.length).setToken(accessToken);
            String voiceResp = HttpUtil.simplePostJson(appConfig.getBaiduVopUrl(), JSON.toJSONString(baiduReqDto));
            BaiduRespDto baiduRespDto = JSONObject.parseObject(voiceResp, BaiduRespDto.class);
            System.out.println(baiduRespDto.getResult());
            if (baiduRespDto == null || baiduRespDto.getResult() == null || baiduRespDto.getResult().size() == 0) {
                return StringUtils.EMPTY;
            }
            return String.join("", baiduRespDto.getResult()).replaceAll("。", "");
        }
        
         /**
         * 获取百度Al的token
         *
         * @return
         */
        public String baiduAccessToken() {
            String cacheKey = "baidu:AlAccessToken";
            Object valueByKey = redisUtil.getValueByKey(cacheKey);
            if (valueByKey == null || StringUtils.isEmpty(valueByKey.toString())) {
                String url = String.format("https://aip.baidubce.com/oauth/2.0/token?client_id=%s&client_secret=%s&grant_type=client_credentials", "xxxxx", "xxxxxxx");
                String s = HttpUtil.simplePostJson(url, new JSONObject().toString());
                TokenRespDto tokenRespDto = JSONObject.parseObject(s, TokenRespDto.class);
                valueByKey = tokenRespDto.getAccess_token();
                redisUtil.setKeyValueWithExpire(cacheKey, valueByKey, 60 * 60 * 24 * 7l);
            }
            return valueByKey.toString();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    三、两个字符串相似度比较方法(补充)

    /**
     * 匹配相似度
     *
     * @param var1 用户输入
     * @param var2 标准话术
     * @return
     */
    public BigDecimal voiceSimilarity(String var1, String var2) {
        BigDecimal bd = new BigDecimal(0);
        // 1、用户输入为空
        if (StringUtils.isEmpty(var1)) {
            if (StringUtils.isBlank(var2)) {
                bd = bd.add(new BigDecimal(100));
            }
            return bd;
        }
        int v1Len = var1.length();
        int v2Len = var2.length();
        // 2、输入字符数大于标准
        String splitVar = v1Len > v2Len ? var2 : var1;
        String containVar = v1Len > v2Len ? var1 : var2;
        List<String> strings = this.splitSubstrings(splitVar);
        for (String st : strings) {
            if (containVar.contains(st)) {
                bd = new BigDecimal(st.length()).divide(new BigDecimal(v2Len), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP);
                System.out.println(bd);
                break;
            }
        }
        return bd;
    }
    
      /**
       * 字符串拆解所有子集并按长度大小排序
       * @param s
       * @return
       */
      public List<String> splitSubstrings(String s) {
          int i, j;
          int stringLength = s.length();
          ArrayList<String> subStringList = new ArrayList<String>();
          // first for loop
          for (i = 0; i < stringLength; i++) {
              for (j = i + 1; j <= stringLength; j++) {
                  subStringList.add(s.substring(i, j));
              }
    
          }
          return subStringList.stream().sorted(Comparator.comparing(String::length).reversed()).collect(Collectors.toList());
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
  • 相关阅读:
    【学习笔记】ARC147/ARC141/ARC145
    MySQL知识总结
    前端面试题JS篇(3)
    如何快速创建SQL Server数据库服务并远程连接?
    CSS进阶
    C#时间使用整理,DateTime 使用整理
    PTA-L2-004 这是二叉搜索树吗?
    Shiro安全(一):Shiro-550反序列化
    大数据产业酝酿巨变 元宇宙新格局呼之欲出
    实时云渲染技术,元宇宙应用的核心之一
  • 原文地址:https://blog.csdn.net/CEVERY/article/details/128117874