因为讯飞官方提供的sdk没有Java的,所以我使用的是WebApi。其中有些注意事项官网有写出,可自行查看。
下面是具体的接入过程:
下载这个demo 然后把里面的三个文件复制到项目里
主要用到的是FileUtil.java这个文件
package com.olive.web.utils.kedaxunfei;
import com.olive.utils.ReturnJson;
import com.olive.web.entity.KrpanoEntity;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
/**
* 讯飞——语音评测 API 接口调用
*
* @author iflytek
*/
public class EnglishEvaluation {
// 合成webapi接口地址
private static final String WEBISE_URL = "用自己的";
// 应用ID
private static final String APPID = "用自己的";
// 接口密钥
private static final String API_KEY = "用自己的";
// 评测文本
// private static final String TEXT = "Firefighters take part in an emergency rescue drill in a forest in Taian city, Shandong province, on Feb 24, 2019. This is the country's largest joint air-ground drill with around 2,000 rescuers, seven helicopters and vehicles, and over 1,200 firefighting equipment taking part in the exercise.";
// 音频编码
private static final String AUE = "raw";
// 采样率
private static final String AUF = "audio/L16;rate=16000";
// 结果级别
private static final String RESULT_LEVEL = "simple";
// 语种
private static final String LANGUAGE = "en_us";
// 评测种类
private static final String CATEGORY = "read_sentence";
//全维度评测:需要开通
private static final String extra_ability = "multi_dimension";
// 音频文件地址
// private static final String AUDIO_PATH = "音频路径";
/**
* 请求讯飞api
*
* @param filePath 音频文件地址
* @return
* @throws IOException
*/
public static String requestXunFei(String filePath,String eText) throws IOException {
Map<String, String> header = buildHttpHeader();
byte[] audioByteArray = FileUtil.read(filePath);
String audioBase64 = new String(Base64.encodeBase64(audioByteArray), "UTF-8");
String result = HttpUtil.doPost1(WEBISE_URL, header, "audio=" + URLEncoder.encode(audioBase64, "UTF-8") + "&text=" + URLEncoder.encode(eText, "UTF-8"));
System.out.println("评测 WebAPI 接口调用结果:" + result);
return result;
}
/**
* 组装http请求头
*/
private static Map<String, String> buildHttpHeader() throws UnsupportedEncodingException {
String curTime = System.currentTimeMillis() / 1000L + "";
String param = "{\"auf\":\"" + AUF + "\",\"aue\":\"" + AUE + "\",\"result_level\":\"" + RESULT_LEVEL + "\",\"language\":\"" + LANGUAGE + "\",\"extra_ability\":\"" + extra_ability + "\",\"category\":\"" + CATEGORY + "\"}";
String paramBase64 = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
String checkSum = DigestUtils.md5Hex(API_KEY + curTime + paramBase64);
Map<String, String> header = new HashMap<String, String>();
header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
header.put("X-Param", paramBase64);
header.put("X-CurTime", curTime);
header.put("X-CheckSum", checkSum);
header.put("X-Appid", APPID);
return header;
}
}
这是我修改过的,有这么几个地方要修改
123这三个属性的值要从控制台获取
5:全维度评测,这个需要开通,和免费数量是区分开的,要单独付费
还有一个就是文本这块(我的项目是前端传值,所以我这里把他原来的注掉了),这个地方需要注意的就是:文本内容要正规,标点符号等都要有,要不然影响评测质量
4:评测级别:这个建议最好用“simple”,如果用”entity”的话返回内容太多了,前端不好解析
这些弄好之后直接调用就可以实现评测,
比较麻烦的地方在语评测的音频处理
官方给的音频是wav格式的,要转换,要不然没法评测
ffempg的使用可以参考这篇文章
ffempg安装使用
命令用官方的就行
ffmpeg -y -i test.wav -acodec pcm_s16le -f s16le -ac 1 -ar 16000 test.pcm
转换好之后就可以用postman测试一下是否调用成功
成功了返回的是一长串结果json
额,公司的全维度评测今天到期了,评测结果我就用官方给的吧
这个是失败的情况,会返回错误编码,这个在官网可以查到对应的原因,方便排错
如果成功了是这样
{
"data":{
"read_word":{
"lan":"en",
"type":"study",
"version":"6.5.0.1011",
"rec_paper":{
"read_word":{
"except_info":"28680",
"is_rejected":"false",
"total_score":"64.725080",
"sentence":[
{
"beg_pos":"0",
"content":"apple",
"end_pos":"129",
"word":{
"beg_pos":"79",
"content":"apple",
"end_pos":"129",
"total_score":"94.963020"
}
},
{
"beg_pos":"129",
"content":"banana",
"end_pos":"163",
"word":{
"beg_pos":"163",
"content":"banana",
"end_pos":"163",
"total_score":"0.000000"
}
},
{
"beg_pos":"163",
"content":"orange",
"end_pos":"226",
"word":{
"beg_pos":"163",
"content":"orange",
"end_pos":"226",
"total_score":"99.212200"
}
},
{
"content":"banana",
"end_pos":"359",
"word":{
"beg_pos":"265",
"content":"banana",
"end_pos":"318"
},
"beg_pos":"226"
}
],
"beg_pos":"0",
"content":"apple banana orange",
"end_pos":"359"
}
}
}
},
"code":"0",
"desc":"success",
"sid":"wse00000001@ll36940e324c59000100"
}
返回的就是很长的json串(评测级别是”entity”,所以会有很多),中间会有几个关键的参数
这就是评测返回的结果对应的分数字段
到这里就结束了,有不明白的可以留言。感觉写的很清楚了,哈哈!