java开发腾讯AI,共探人工智能

前言:跟你们说说羊群效应嘛,听过的切勿吐槽,就是1000只羊在一边吃草,这时来了一只狗,汪汪几声,1000只养竟然跑到另一边。对,是竟然,1000只羊打不过一只狗?每只羊舔一下那只狗都能舔死那只狗辣,试想1000个女轮着舔一个男的,当然羊不是这样想的,羊会想我上去舔,其他的羊却没有上去,这就是1对1,这不是舔咯,是濑,濑是服务咯。。。现在,是AI时代,大数据时代。你还不开发AI,你就后悔了。当然,有人问我为什么选择TC,李白已经预言了。床前明月光,年尾钱紧张。。。好了,说了那么多,是时候展现真正的技术了。。。

这里写图片描述

腾讯ai官网:https://ai.qq.com/
这里只实现自然语言处理这模块的接口。。。
1、通用自然语言处理接口

public static String getNaturalLanguage(String baseUrl, String text, int type, String encoding) throws Exception{
        SignEntity signEntity = new SignEntity(text,type);
        String url = baseUrl+"?app_id=" + signEntity.getApp_id() + "&time_stamp="
                + signEntity.getTime_stamp() + "&nonce_str=" + signEntity.getNonce_str() + "&sign="
                + signEntity.getSign() + "&text=" + URLEncoder.encode(text.trim(), encoding);
        String message = _httpUtil.doHttpGet(url,encoding);
        System.out.println(message);
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        return data;
    }

参数说明:
baseUrl:腾讯ai接口地址
text:文本文字
type:1POST处理 2GET处理
encoding:编码 UTF-8、GBK

首先这个类就是获取sign的:SignEntity signEntity = new SignEntity(text,type);
要传入文本文字和编码类型,有些要GBK编码,有些要UTF-8编码,详情请参考官网。

然后就是拼接请求参数,最后返回json格式字符串

2、机器人聊天
这个接口好像图灵机器人一样

/**
     * 机器人聊天
     * @param baseUrl
     * @param text
     * @param session
     * @return
     * @throws Exception
     */
    public static String getNaturalLanguageChat(String baseUrl, String text, String session) throws Exception{
        int app_id = TencentAPI.APP_ID_AI;
        String app_key = TencentAPI.APP_KEY_AI;
        int time_stamp = (int) (System.currentTimeMillis() / 1000) ;
        String nonce_str = _stringUtil.getCharAndNumr(17, 3);
        Map<String, String> person_Id_body = new HashMap<String, String>();
        person_Id_body.put("app_id", app_id+"");
        person_Id_body.put("time_stamp", time_stamp+"");
        person_Id_body.put("nonce_str", nonce_str);
        person_Id_body.put("question", text);
        person_Id_body.put("session", session);
        String sign = TencentAISignSort.getSignature(person_Id_body);
        SignEntity signEntity = new SignEntity();
        signEntity.setApp_id(app_id);
        signEntity.setApp_key(app_key);
        signEntity.setNonce_str(nonce_str);
        signEntity.setSign(sign.toUpperCase());
        signEntity.setTime_stamp(time_stamp);
        String url = baseUrl+"?app_id=" + signEntity.getApp_id() + "&time_stamp="
                + signEntity.getTime_stamp() + "&nonce_str=" + signEntity.getNonce_str() + "&sign="
                + signEntity.getSign() + "&session="+URLEncoder.encode(session.trim(), "UTF-8")+"&question=" + URLEncoder.encode(text.trim(), "UTF-8");
        String message = _httpUtil.doHttpGet(url,"UTF-8");
        System.out.println(message);
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        return data;
    }

3、中译英文本翻译
这也与百度翻译一样效果。。。

/**
     * 文本翻译
     * @param baseUrl
     * @param text
     * @param type
     * @return
     * @throws Exception
     */
    public static String getNaturalLanguageTrans(String baseUrl, String text, int type) throws Exception{
        int app_id = TencentAPI.APP_ID_AI;
        String app_key = TencentAPI.APP_KEY_AI;
        int time_stamp = (int) (System.currentTimeMillis() / 1000) ;
        String nonce_str = _stringUtil.getCharAndNumr(17, 3);
        Map<String, String> person_Id_body = new HashMap<String, String>();
        person_Id_body.put("app_id", app_id+"");
        person_Id_body.put("time_stamp", time_stamp+"");
        person_Id_body.put("nonce_str", nonce_str);
        person_Id_body.put("text", text);
        person_Id_body.put("type", type+"");
        String sign = TencentAISignSort.getSignature(person_Id_body);
        SignEntity signEntity = new SignEntity();
        signEntity.setApp_id(app_id);
        signEntity.setApp_key(app_key);
        signEntity.setNonce_str(nonce_str);
        signEntity.setSign(sign.toUpperCase());
        signEntity.setTime_stamp(time_stamp);
        String url = baseUrl+"?app_id=" + signEntity.getApp_id() + "&time_stamp="
                + signEntity.getTime_stamp() + "&nonce_str=" + signEntity.getNonce_str() + "&sign="
                + signEntity.getSign() + "&type="+type+"&text=" + URLEncoder.encode(text.trim(), "UTF-8");
        String message = _httpUtil.doHttpGet(url,"UTF-8");
        System.out.println(message);
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        return data;
    }

4、图片翻译
这个挺有意思,好像ocr一样,但腾讯ai也有ocr接口,这个是将识别图片中的文字和文字的位置等,这里是用image base64编码,有点长。。。

/**
     * 图片翻译
     * @param appId
     * @param nonce_str
     * @param image
     * @param session_id
     * @param time_stamp
     * @return
     * @throws Exception
     */
    public static String getNaturalLanguageImgTrans(String baseUrl, String image) throws Exception{
        String sign = "";
        int app_id = TencentAPI.APP_ID_AI;
        int time_stamp = (int) (System.currentTimeMillis() / 1000) ;
        String nonce_str = _stringUtil.getCharAndNumr(17, 3);
        String session_id = _stringUtil.getCharAndNumr(10, 1);
        sign = appSignAI4ImgDetect(TencentAPI.APP_ID_AI, nonce_str,image,session_id,time_stamp);
        String message = _httpUtil.doHttpPost(baseUrl,"app_id=" + URLEncoder.encode((app_id+"").trim(), "UTF-8") 
                                                                                + "&image="+URLEncoder.encode(image.trim(), "UTF-8")+"&nonce_str=" + URLEncoder.encode(nonce_str.trim(), "UTF-8") 
                                                                                + "&scene="+URLEncoder.encode("doc", "UTF-8")+"&session_id=" + URLEncoder.encode(session_id.trim(), "UTF-8")
                                                                                +"&sign="+sign+"&source="+URLEncoder.encode("zh", "UTF-8")+"&target="+URLEncoder.encode("en", "UTF-8")
                                                                                + "&time_stamp=" + URLEncoder.encode((time_stamp+"").trim(), "UTF-8"));
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        System.out.println("session_id:"+session_id);
        return data;
    }

5、语音翻译
给一个mp3,要求有时间限制的,然后能识别出文字出来。。。

/**
     * 语音翻译
     * @param baseUrl
     * @param speechChunk
     * @return
     * @throws Exception
     */
    public static String getNaturalLanguageSpeechTrans(String baseUrl, String speechChunk) throws Exception{
        String sign = "";
        int app_id = TencentAPI.APP_ID_AI;
        int time_stamp = (int) (System.currentTimeMillis() / 1000) ;
        String nonce_str = _stringUtil.getCharAndNumr(17, 3);
        String session_id = _stringUtil.getCharAndNumr(10, 1);
        int format = 8;
        int seq = 0;
        int end = 1;
        String source = "zh";
        String target = "en";
        sign = appSignAI4SpeechDetect(TencentAPI.APP_ID_AI, time_stamp, nonce_str, format , 
                seq, end, session_id, speechChunk,source, target);
        String message = _httpUtil.doHttpPost(baseUrl,"app_id=" + URLEncoder.encode(app_id+"","UTF-8") + "&end=" + URLEncoder.encode(end+"","UTF-8") 
        + "&format=" + URLEncoder.encode(format+"","UTF-8") 
        +"&nonce_str=" + URLEncoder.encode(nonce_str,"UTF-8")+ "&seq="+URLEncoder.encode(seq+"", "UTF-8")
        +"&session_id=" + URLEncoder.encode(session_id.trim(), "UTF-8") +"&sign=" + sign +"&source="+URLEncoder.encode(source, "UTF-8")
        +"&speech_chunk="+URLEncoder.encode(speechChunk, "UTF-8")
        +"&target="+URLEncoder.encode(target, "UTF-8") + "&time_stamp=" + URLEncoder.encode(time_stamp+"","UTF-8"));
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        System.out.println("session_id:"+session_id);
        return data;
    }

6、语种识别
输入一段文本,可以识别文字是中文还是英文或者其他语言。。。

/**
     * 语种识别
     * @param baseUrl
     * @param speechChunk
     * @return
     * @throws Exception
     */
    public static String getNaturalLanguageTextDetect(String baseUrl, String text) throws Exception{
        String sign = "";
        int app_id = TencentAPI.APP_ID_AI;
        int time_stamp = (int) (System.currentTimeMillis() / 1000) ;
        String nonce_str = _stringUtil.getCharAndNumr(17, 3);
        int force = 0;
        String candidate_langs = "zh";
        sign = appSignAI4SpeechDetect(TencentAPI.APP_ID_AI,candidate_langs, force, nonce_str, text , time_stamp);

        String message = _httpUtil.doHttpPost(baseUrl,"app_id=" + URLEncoder.encode(app_id+"","UTF-8") + "&candidate_langs=" + URLEncoder.encode(candidate_langs+"","UTF-8") 
        + "&force=" + URLEncoder.encode(force+"","UTF-8") 
        +"&nonce_str=" + URLEncoder.encode(nonce_str,"UTF-8")+"&sign=" + sign+ "&text="+URLEncoder.encode(text+"", "UTF-8")
        + "&time_stamp=" + URLEncoder.encode(time_stamp+"","UTF-8"));
        JSONObject demoJson = JSONObject.fromObject(message);
        String data = demoJson.getString("data");
        return data;
    }

当然,ai还有很多,这只是一部分,有图片的,视频的等等。。。

需要所有源代码,可加QQ490647751回复‘java开发腾讯AI,共探人工智能’获取。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值