图灵机器人(Java文字版)

系列文章:
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃峰顶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值