图灵机器人(Java文字版)

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 14:50   11   0

系列文章:
Java 语音记录(录音,存储为WAV文件):https://blog.csdn.net/haoranhaoshi/article/details/87888382
Java 语音识别(百度语音API):https://blog.csdn.net/haoranhaoshi/article/details/87888407
Java 语音合成并播放(百度语音API):https://blog.csdn.net/haoranhaoshi/article/details/87888430
Java 语音聊天机器人(百度语音API)(当前预置问答,可用图灵机器人框架扩展):
https://blog.csdn.net/haoranhaoshi/article/details/87888469 (依赖前三篇博客代码)
百度语音实战下载:https://download.csdn.net/download/haoranhaoshi/10976591
图灵机器人(Java文字版):https://blog.csdn.net/haoranhaoshi/article/details/87992548
图灵机器人(Java语音版):https://blog.csdn.net/haoranhaoshi/article/details/87992661

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 输入你的沟通语句,返回应答
 * 包含丰富的语料库,即使同一个沟通语句,也有不同的回答
 */
public class TulingCommunicationByText {
    private static final String requestUrl = "http://openapi.tuling123.com/openapi/api/v2";
    /**
     * 在图灵机器人官网注册登陆后,在右上角产生你的useId;
     * 创建机器人后,生成apiKey
     */
    private static final String apiKey = "3ec0310e2e4047318e142709c6513a31";
    private static final String userId = "407154";

    private static final String contentType = "text/plain";

    private static final String encoding = getContentType();

    public static void main(String[] args){
        TulingCommunicationByText tulingCommunicationByText = new TulingCommunicationByText();
        String response = tulingCommunicationByText.getResponse("哈哈哈哈哈哈你快乐吗");
        System.out.println(response);
    }

    public String getResponse(String request){
        JSONObject perception = new JSONObject();
        JSONObject inputText = new JSONObject();
        inputText.put("text", request);
        perception.put("inputText", inputText);

        JSONObject userInfo = new JSONObject();
        userInfo.put("apiKey", apiKey);
        userInfo.put("userId", userId);

        JSONObject root = new JSONObject();
        root.put("reqType", 0);
        root.put("perception", perception);
        root.put("userInfo", userInfo);
        String params = root.toString();
        // {"userInfo":{"apiKey":"3ec0310e2e4047318e142709c6513a31","userId":"407154"},"reqType":0,"perception":{"inputText":{"text":"你开心吗"}}}
        System.out.println(params);

        try {
            String resultString = HttpUtil.postGeneralUrl(requestUrl, contentType, params, encoding);
            // {"emotion":{"robotEmotion":{"a":0,"d":0,"emotionId":0,"p":0},"userEmotion":{"a":0,"d":0,"emotionId":0,"p":0}},"intent":{"actionName":"","code":10004,"intentName":""},"results":[{"groupType":1,"resultType":"text","values":{"text":"有你陪伴才开心"}}]}
            System.out.println(resultString);

            JSONObject resultJson = new JSONObject(resultString);
            JSONArray results = resultJson.getJSONArray("results");
            JSONObject values = ((JSONObject)(results.get(0))).getJSONObject("values");
            String text = values.getString("text");
            return text;
        } catch (Exception e) {
            e.printStackTrace();
            return "我不知道怎么回答你";
        }
    }

    private static String getContentType(){
        String encoding = "UTF-8";
        if (requestUrl.contains("nlp")) {
            encoding = "GBK";
        }

        return encoding;
    }
}

依赖我的其他博客:
http 工具类:https://blog.csdn.net/haoranhaoshi/article/details/87952162

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP