java 对话 智能,如何用Java实现智能对话机器人
如何用Java實現智能對話機器人
前言 這個時代人工智能如此火爆,身為圈內人我們應該多少對他有些了解,為了靠他近一些今天我們動手用Java實現一個智能聊天機器人,當然此處我們需要依賴圖靈機器人的Api
這篇博客涵蓋的知識點
HTML網頁源代碼抓取
JSON字符串解析
以下為需要用到的Jar
Jar
備注
JSONObject
用于解析JSON
首先我們要注冊一個圖靈機器人的帳號 并創建我們自己的機器人
這里可以根據個人需求填寫
然后拿到我們剛剛創建的機器人的APIkey
從這里我們可以拿到我們的api請求地址,和我們的機器人的APIkey
圖靈機器人現在維護的請求方式為POST 但是GET請求還能使用,這里我們使用GET請求接口。
接口的請求地址為 http://www.tuling123.com/openapi/api?key= [APPkey]&info=[你需要發送的消息]
準備完畢后 我們開始代碼部分
第一步,編寫的工具類
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* @author 4everlynn
* @date 2017/12/17
*/
public class Util {
//存儲APIkey
public static final String API_KEY = "填寫你的APPKEY";
//存儲接口請求地址
public static final String API_URL = "http://www.tuling123.com/openapi/api";
/**
* 拼接出我們的接口請求地址
*
* @param msg 需要發送的消息
* @return
*/
private String setParameter(String msg) {
//在接口請求中 中文要用URLEncoder encode成UTF-8
try {
return API_URL + "?key=" + API_KEY + "&info=" + URLEncoder.encode(msg, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* 拿到消息回復的內容的方法
* @param json 請求接口得到的JSON
* @return text的部分
*/
private String getString(String json){
try {
JSONObject object = new JSONObject(json);
return object.getString("text");
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* 提供對外公開的方法用于最終拿到機器人回復的消息
* @param msg 傳入你需要發送的信息
* @return 機器人對你的回復
*/
public String getMessage(String msg){
return getString(getHTML(setParameter(msg)));
}
private String getHTML(String url) {
StringBuffer buffer = new StringBuffer();
BufferedReader bufferedReader = null;
try {
//創建URL對象
URL u = new URL(url);
//打開連接
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
//從連接中拿到InputStream并由BufferedReader進行讀取
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
//循環每次加入一行HTML內容 直到最后一行
while ((line = bufferedReader.readLine()) != null) {
buffer.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//結束時候關閉釋放資源
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer.toString();
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94[/code]
第二步,編寫測試類
package com.disware;
import java.util.Scanner;
/**
* @author 4everlynn
* @date 2017/12/17
*/
public class Main {
public static void main(String[] args) {
//聲明并實例化我們剛剛封裝好的工具類
Util util = new Util();
//接收用戶輸入
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
//直接輸出機器人的回復
System.err.println("Ta 對你說 -> " + util.getMessage(scanner.nextLine()));
}
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22[/code]
總結
以上是生活随笔為你收集整理的java 对话 智能,如何用Java实现智能对话机器人的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言中用文件处理数据,C语言文件处理-
- 下一篇: java arraylist strin