?? 作者 :“大數(shù)據(jù)小禪”
?? **文章簡(jiǎn)介 **:最近有小伙伴留言,“很久沒人找我聊天了,可以寫一個(gè)陪聊機(jī)器人展現(xiàn)一下程序員的浪漫嗎?”,小禪:“安排!”
?? **文章源碼獲取 **:本文的源碼,小伙伴們可以關(guān)注文章底部的公眾號(hào),點(diǎn)擊“聯(lián)系我”備注源碼獲取哦。
?? 歡迎小伙伴們 點(diǎn)贊??、收藏?、留言??
1.智能陪聊機(jī)器人演示
人工智能一直是最近的熱點(diǎn)話題,自動(dòng)人工智能但是以來應(yīng)用領(lǐng)域就不斷的擴(kuò)大,在未來人工智能也會(huì)在人們的生活中不斷普及與應(yīng)用。這篇博文中的陪聊機(jī)器人,使用java進(jìn)行編寫,可以根據(jù)你發(fā)的信息進(jìn)行智能的回應(yīng),還算挺有意思的一個(gè)小玩意。最終效果的演示如下圖~
2.智能問答平臺(tái)API介紹
這個(gè)陪聊機(jī)器人項(xiàng)目使用了青云課的智能API,通過調(diào)用API得到信息反饋。
具體的調(diào)用格式如下:
http://api.qingyunke.com/api.php?key=free&appid=0&msg=%s
其中的%s傳入我們需要發(fā)送給機(jī)器人的內(nèi)容,就可以得到API調(diào)用結(jié)果的反饋。
- key 固定參數(shù) free
- appid 設(shè)置成0,為智能識(shí)別
- msg 為搜索關(guān)鍵詞
- result 表示返回狀態(tài),返回0表示正常
- content api返回的信息內(nèi)容
可以看到數(shù)據(jù)是以JSON的形式進(jìn)行返回。
3.整合第三方JSON開源庫(kù)
Gson是Google提供的類庫(kù),可以用來處理java對(duì)象與JSON數(shù)據(jù)之間的映射,將一個(gè)JSON字符串轉(zhuǎn)換成一個(gè)java對(duì)象,方便我們對(duì)API返回的JSON格式的數(shù)據(jù)進(jìn)行處理,下面演示如何將Gson類庫(kù)導(dǎo)入到我們的工程中。
首先可以去官網(wǎng)下載對(duì)應(yīng)的jar包,或者直接私信我獲取。獲取jar包之后找個(gè)全英文路徑進(jìn)行保存。這里我們使用的編輯器是IDEA,所以使用IDEA進(jìn)行演示,小伙伴們使用的是其他編輯器的話導(dǎo)入方法都是類似的哦。在IDEA打開如下界面,找到j(luò)ar包導(dǎo)入即可。
4.智能機(jī)器人項(xiàng)目框架搭建與模塊劃分
項(xiàng)目搭建:搭建的部分無太多要求,只需要使用IDEA創(chuàng)建一個(gè)新的普通java工程即可
項(xiàng)目模塊搭建:
- model 類 用來存放請(qǐng)求所返回的對(duì)象
- util 類用來存放工程所用到的工具類,比如說HTTP請(qǐng)求解析類
- app 類用來當(dāng)作機(jī)器人項(xiàng)目的入口
- service 類用來實(shí)現(xiàn)業(yè)務(wù)的接口
相關(guān)的兩個(gè)實(shí)體類如下:
public class Request {
private String key = "free";
private String appid = "0";
private String msg = "";
public Request(){}
public Request(String msg){
this.msg = msg;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getAppid() {
return appid;
}
public void setAppid(String appid) {
this.appid = appid;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
public class Response {
private int code;
private String content;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
5.封裝一個(gè)機(jī)器人HTTP工具類
HTTP工具類主要用來對(duì)api進(jìn)行請(qǐng)求,獲取返回的內(nèi)容
public class HttpUtils {
public static String request(String api){
HttpURLConnection connection = null;
int responseCode = 0;
try{
URL url = new URL(api);
//獲取對(duì)應(yīng)的連接對(duì)象
connection = (HttpURLConnection) url.openConnection();
responseCode = connection.getResponseCode();
}catch (Exception e){
e.printStackTrace();
}
if(200 <= responseCode && responseCode<=299){
try(InputStream inputStream = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
){
StringBuilder response = new StringBuilder();
String currentLine;
while ((currentLine = in.readLine())!= null){
response.append(currentLine);
}
String result = response.toString();
return result;
}catch (Exception e){
e.printStackTrace();
}
}
return null;
}
}
6.實(shí)現(xiàn)機(jī)器人service層的接口與定義
實(shí)現(xiàn)機(jī)器人接口層
public interface RobotService {
Response qa(String msg) ;
}
實(shí)現(xiàn)機(jī)器人接口實(shí)現(xiàn)類,這個(gè)類用來實(shí)現(xiàn)API的請(qǐng)求,將結(jié)果進(jìn)行封裝成實(shí)體類返回
public class QkyRobotServiceImpl implements RobotService {
private static final String apiTpl = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=%s";
private static final Gson gson = new Gson();
@Override
public Response qa(String msg) {
String api = null;
try {
api = String.format(apiTpl, URLEncoder.encode(msg,"UTF-8") );
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String result = HttpUtils.request(api);
//可以做邏輯判斷,比如null的時(shí)候,或者出錯(cuò)
Response response = gson.fromJson(result,Response.class);
return response;
}
}
7.制作專屬于你的機(jī)器人入口
編寫入口主類,調(diào)用封裝好的模塊進(jìn)行機(jī)器人入口主類的編寫
public class Main {
private static final RobotService robotService = new QkyRobotServiceImpl();
public static void main(String[] args)throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("尊敬的C站大佬,請(qǐng)給我取個(gè)響亮的名字??!");
System.out.println("-------------------------------");
String name = scanner.nextLine();
System.out.println("大佬好,我是大數(shù)據(jù)小禪博客里的機(jī)器人,直接給我下達(dá)指令哦~");
System.out.println("-------------------------------");
while (true){
String input = scanner.nextLine();
if("88".equalsIgnoreCase(input)){
System.out.println("歡迎下次使用,拜拜");
break;
}else {
Response response = robotService.qa(input);
if(response != null && response.getCode() == 0){
System.out.println("-------------------------------");
System.out.println(name+":"+ new String(response.getContent().getBytes(),"UTF-8"));
System.out.println("-------------------------------");
}else {
System.out.println(name+": 大佬你剛剛這句話我沒聽懂,可否再陳述一次~");
}
}
}
scanner.close();
}
}
8.把你的機(jī)器人打包使用
為了方便我們對(duì)項(xiàng)目的使用,這里我們使用IDEA將項(xiàng)目打包成jar包。通過下面的步驟,就可以將我們項(xiàng)目里的全部模塊與類庫(kù)打包,需要調(diào)用的時(shí)候只需要使用 java -jar jar名字 即可。
首先點(diǎn)開IDEA的Project Structure之后找到Artifacts選項(xiàng)
點(diǎn)擊Bulid,將項(xiàng)目進(jìn)行打包
最后回產(chǎn)生一個(gè)out文件夾,這里面的jar包也就是我們打包后的最終結(jié)果。
之后上傳到有java環(huán)境的終端就可以運(yùn)行。
9.總結(jié)
打包完成后我們的機(jī)器人項(xiàng)目就完成啦,希望小伙伴們通過這篇博文可以有所收獲。??文章來源:http://www.zghlxwxcb.cn/news/detail-481105.html
最近小禪也是建立了一個(gè)???大數(shù)據(jù)技術(shù)社區(qū),在這個(gè)社區(qū)大家可以分享自己寫的優(yōu)質(zhì)博文,進(jìn)行提問,也可以分享生活中遇到的美好瞬間,獲取想要的學(xué)習(xí)資源。歡迎大家加入我的社區(qū),一起學(xué)習(xí),成長(zhǎng),內(nèi)卷!文章來源地址http://www.zghlxwxcb.cn/news/detail-481105.html
到了這里,關(guān)于沒人陪你聊天?教你使用java制作專屬智能陪聊機(jī)器人的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!