收藏
回答

AI开发接口不识别英文

无论英文中文,最终识别总是中文结果

请求链接:

http://api.weixin.qq.com/cgi-bin/media/voice/addvoicetorecofortext?access_token=12_zH-Qc8l9-jod1JbkupX0L_ngw7aF7PQzUZJah_Bs3Al4PMAKGitRdWca6osx35cBlIqfHNeG7QDqAqT0SPeXwXfsozJgFbF-FS6XhEnw-vrHmflF89qVmJCLPRwh-Bbfw0kXT6yxqPEaY-CrXUYgAJASPT&format=mp3&voice_id=15343204288521980024257105842&lang=en_US


识别结果:

15:56:12.252 [main] INFO com.adp5.demo.common.utils.JwAIApi - {"result":"哈哈哈哈。","is_end":true}

15:56:12.286 [main] INFO com.adp5.demo.common.utils.JwAIApi - --获取语音识别结果---{"result":"哈哈哈哈。","is_end":true}

最后一次编辑于  2018-08-15  (未经腾讯允许,不得转载)
邀请回答
复制链接收藏关注问题回答

2 个回答

  • 简成
    简成
    2018-08-15


    import com.alibaba.fastjson.JSONObject;

    import org.apache.log4j.Logger;


    import java.io.*;

    import java.net.ConnectException;

    import java.net.HttpURLConnection;

    import java.net.URL;


    /**

     * 微信--AI开放接口

     * 

     */

    public class JwAIApi {


    /**

    * 提交语音

    */

    public static final String ADD_VOICE_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/addvoicetorecofortext?";

    /**

    * 获取语音翻译结果

    */

    public static final String VOICE_RESULT_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/queryrecoresultfortext?";

    /**

    * 翻译接口

    */

    public static final String TRANSLATE_TEXT_URL = "http://api.weixin.qq.com/cgi-bin/media/voice/translatecontent?";

    private static Logger logger = Logger.getLogger(JwAIApi.class);

    /**

    * 微信翻译语音<br>

    * 参数voice请用带参构造器创建对象,若用默认构造器则需要赋值属性有:

    * accessToken,

    * format(只支持mp3,16k,单声道,最大1M),

    * voice_id(语音唯一标识),

    * file(文件地址),

    * lang(语言,zh_CN 或 en_US,默认中文 非必填)

    * @return

    */

    public static String translateVoice(Voice voice){

    JSONObject json = uploadVoice(voice);

    if(json!=null && json.containsKey("errcode")&&"0".equals(json.getString("errcode"))){

    try {

    Thread.sleep(voice.getWaitMill());

    } catch (InterruptedException e) {

    e.printStackTrace();

    }

    return getVoiceResult(voice.getAccessToken(), voice.getVoice_id(),voice.getLang());

    }

    return null;

    }

    /**

    * 微信翻译

    * @param accessToken

    * @param lfrom 源语言,zh_CN 或 en_US

    * @param lto 目标语言,zh_CN 或 en_US

    * @param text 需要翻译的文字

    * @return

    */

    public static String translateText(String accessToken,String lfrom,String lto,String text){

    String result = null;

    String requestUrl = TRANSLATE_TEXT_URL+"access_token="+accessToken+"&lfrom="+lfrom+"&lto="+lto;

    logger.info("------微信翻译地址----"+ requestUrl);

    logger.info("------微信翻译原文----"+ text);

    JSONObject obj = httpRequest(requestUrl, "POST", text);

    if(obj!=null && obj.containsKey("to_content")){

    result =  obj.getString("to_content");

    }

    logger.info("------微信翻译结果----"+ result);

    return result;

    }

    /**

    * 提交语音

    * @param voice

    * @return

    */

    private static JSONObject uploadVoice(Voice voice){

    JSONObject jsonobject = new JSONObject();

    String result = null;

    File file = new File(voice.getFile());

    if(!file.exists()||!file.isFile()){

    jsonobject = null;

    logger.info("--提交语音接口,文件不存在------");

    }else{

    HttpURLConnection con =null;

    OutputStream out =null;

    DataInputStream in = null;

    String requestUrl = "http://api.weixin.qq.com/cgi-bin/media/voice/addvoicetorecofortext?access_token="+voice.getAccessToken()+"&format=mp3&voice_id=" + voice.getVoice_id() + "&lang="+voice.getLang();

    logger.info("--提交语音接口请求:"+requestUrl);

    try {

    URL urlObj = new URL(requestUrl);  

    con = (HttpURLConnection) urlObj.openConnection();  

    con.setRequestMethod("POST"); // 以Post方式提交表单,默认get方式  

        con.setDoInput(true);  

        con.setDoOutput(true);  

        con.setUseCaches(false); // post方式不能使用缓存  

        

        //设置网络超时

                    con.setConnectTimeout(8000);  

                    con.setReadTimeout(8000);

                    

                    con.setRequestProperty("Connection", "Keep-Alive");// 设置请求头信息

        con.setRequestProperty("Charset", "UTF-8");  

        String BOUNDARY = "----------" + System.currentTimeMillis();// 设置边界  

        con.setRequestProperty("Content-Type", "multipart/form-data; boundary="+ BOUNDARY);

    // 请求正文信息  

        // 第一部分:  

        StringBuilder sb = new StringBuilder();  

        sb.append("--"); // 必须多两道线  

        sb.append(BOUNDARY);  

        sb.append("\r\n");  

        

        sb.append("Content-Disposition: form-data;name=\"media\";filelength=\""+file.length()+"\";filename=\""+ file.getName() + "\"\r\n");  

        sb.append("Content-Type:application/octet-stream\r\n\r\n");  

        byte[] head = sb.toString().getBytes("utf-8");  

        // 获得输出流  

        out = new DataOutputStream(con.getOutputStream());  

        // 输出表头  

        out.write(head);  

        // 文件正文部分  

        // 把文件已流文件的方式 推入到url中  

        in = new DataInputStream(new FileInputStream(file));  

        int bytes = 0;  

        byte[] bufferOut = new byte[1024];  

        while ((bytes = in.read(bufferOut)) != -1) {  

        out.write(bufferOut, 0, bytes);  

        }  

        in.close();  

        // 结尾部分  

        byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线  

        out.write(foot);  

        out.flush();  

        out.close();  

        StringBuffer buffer = new StringBuffer();  

        BufferedReader reader = null;

        

        try {

        // 定义BufferedReader输入流来读取URL的响应  

            reader = new BufferedReader(new InputStreamReader(con.getInputStream()));  

            String line = null;  

            while ((line = reader.readLine()) != null) {  

            buffer.append(line);  

            } 

           

            if(result==null){  

            result = buffer.toString();  

            }

            logger.info("--提交语音接口返回:"+buffer.toString());

    } catch (Exception e) {

    logger.info(e.getMessage());

    e.printStackTrace();

    }finally {  

            if(reader!=null){  

            reader.close();  

            }  

    }

        jsonobject = JSONObject.parseObject(result);

    } catch (Exception e) {

    logger.info(e.getMessage());

    e.printStackTrace();

    }finally{

            try {

            if(in!=null){

            in.close();

    }

    if(out!=null){

    out.close();

    }

    if(con!=null){

    con.disconnect();

    }

    } catch (Exception e) {

    }

            }

    }

    return jsonobject;

    }

    /**

    * 获取语音识别结果

    * @param accessToken

    * @param voice_id

    * @param lang

    * @return

    */

    private static String getVoiceResult(String accessToken,String voice_id,String lang){

    lang = (lang==null ||"".equals(lang))?"zh_CN":lang;

    String requestUrl = VOICE_RESULT_URL+"access_token="+accessToken+"&voice_id="+voice_id+"&lang="+lang;

    logger.info("--获取语音识别结果请求参数:"+requestUrl);

    JSONObject result =httpRequest(requestUrl, "POST",null);

    if(result!=null){

    logger.info("--获取语音识别结果---"+result);

    }else{

    logger.info("--获取语音识别结果为空---");

    }

    Object error = result.get(WeiXinConstant.RETURN_ERROR_INFO_CODE);

    if(error == null){

    return result.getString("result");

    }

    return null;

    }

    private static JSONObject httpRequest(String requestUrl,

    String requestMethod, String outputStr) {

    JSONObject jsonObject = null;

    StringBuffer buffer = new StringBuffer();

    HttpURLConnection httpUrlConn = null;

    try {

    // 创建SSLContext对象,并使用我们指定的信任管理器初始化

    URL url = new URL(requestUrl);

        httpUrlConn = (HttpURLConnection) url.openConnection();

    httpUrlConn.setDoOutput(true);

    httpUrlConn.setDoInput(true);

    httpUrlConn.setUseCaches(false);

    httpUrlConn.setConnectTimeout(500000);

          // httpUrlConn.setRequestProperty("content-type", "application/json");

    // 设置请求方式(GET/POST)

    httpUrlConn.setRequestMethod(requestMethod);

    if ("GET".equalsIgnoreCase(requestMethod))

    httpUrlConn.connect();


    // 当有数据需要提交时

    if (null != outputStr) {

    OutputStream outputStream = httpUrlConn.getOutputStream();

    // 注意编码格式,防止中文乱码

    outputStream.write(outputStr.getBytes("UTF-8"));

    outputStream.close();

    }


    // 将返回的输入流转换成字符串

    InputStream inputStream = httpUrlConn.getInputStream();

    InputStreamReader inputStreamReader = new InputStreamReader(

    inputStream, "utf-8");

    BufferedReader bufferedReader = new BufferedReader(

    inputStreamReader);


    String str = null;

    while ((str = bufferedReader.readLine()) != null) {

    buffer.append(str);

    }

    bufferedReader.close();

    inputStreamReader.close();

    // 释放资源

    inputStream.close();

    inputStream = null;

    httpUrlConn.disconnect();

    logger.info(buffer.toString());

    //jsonObject = JSONObject.parseObject(buffer.toString());

    jsonObject = JSONObject.parseObject(buffer.toString());

    } catch (ConnectException ce) {

    ce.printStackTrace();

    logger

    .info("Weixin server connection timed out.");

    } catch (Exception e) {

    e.printStackTrace();

    logger.info("https request error:{}"

    + e.getMessage());

    }finally{

    try {

    httpUrlConn.disconnect();

    }catch (Exception e) {

    e.printStackTrace();

    logger.info("http close error:{}"+ e.getMessage());

    }

    }

    return jsonObject;

    }

    public static void main(String[] args) {

    String lfrom = "zh_CN", lto = "en_US";

    String file = "D:\\work-sb\\demo\\src\\main\\resources\\static\\test3.mp3";

    String accessToken = WxTokenUtil.genToken();

    String voice_id = System.currentTimeMillis() + "-" + System.nanoTime();

    Voice voice = new Voice(accessToken, "mp3", voice_id, lto, file);

    String voiceContent = translateVoice(voice);

    System.out.println(voiceContent);

    System.out.println(voice_id);


    String text = "我是中国人啊";

    String text2 = "I'm Chinese, you know?";

    translateText(accessToken, lfrom, lto, text);

    translateText(accessToken, lto, lfrom, text2);

    }

    }

    2018-08-15
    赞同
    回复 1
    • 简成
      简成
      2018-08-15

      这是相关实现代码

      2018-08-15
      赞同
      回复