天天看點

圖靈機器人(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