javaSE开发智能问答机器人项目
智能问答机器人项目
javaSE开发智能问答机器人项目
智能问答机器人项目介绍和演示
项目所需基础知识
-
网络请求Http知识
-
json和三方包知识
-
Javase知识
-
项目效果演示
-
类似于 天猫精灵 苹果siri 智能客服 微软小冰…
-
智能问答API平台介绍
常见智能问答API平台(仅供练习使用)
API使用讲解(eg:青云客)
指令示例
天气: msg=天气西安
中英翻译: msg=翻译i love you
歌词: msg=歌词 成都
笑话: msg=笑话
计算: msg=计算 1+1*2/3-4
项目基本框架搭建和模块划分
流程分析
-
用户输入指令 -> http发送请求 -> 解析结果 -> 显示内容 -> 循环上述操作
-
-
分包层
-
model 存放请求响应对象
-
util 存放工具类
-
app main函数入口
-
service 相关业务接口和实现类
-
逻辑结构
项目搭建
创建model对象
package model
; package model
; public class Request { private String key
= "free"; private int 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 int getAppid() { return appid
; } public void setAppid(int appid
) { this.appid
= appid
; } public String
getMsg() { return msg
; } public void setMsg(String msg
) { this.msg
= msg
; } }
package model
;
public class Response { private int result
= 0;
private String content
= "";
public int getResult() { return result; }
public void setResult(int result) { this.result = result; }
public String getContent() { return content; }
public void setContent(String content) { this.content = content; } }
http工具类封装
package util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUtil
{
public static String request(String api){
try {
URL url = new URL(api);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
int responseCode = httpURLConnection.getResponseCode();
if (200 <= responseCode && responseCode <= 299){
try (InputStream inputStream = httpURLConnection.getInputStream())
{ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String input; StringBuilder sb = new StringBuilder();
while ((input = bufferedReader.readLine()) != null )
{ sb.append(input); } String result = sb.toString(); return result; }catch (Exception e){ e.printStackTrace(); } }else {
System.out.println("服务器相应异常,请检查指令或网络连接状况"); } }catch (Exception e){ e.printStackTrace(); }
return null; } }
service层接口定义和实现
-
service层接口抽离的好处 解耦: 后续切换平台只要心中对应的实现类即可
-
RobotServise
-
package service;
import model.Request;
import model.Response;
import java.io.UnsupportedEncodingException;
public interface RobotService { Response AI(String msg) throws UnsupportedEncodingException; }
package service;
import com.google.gson.Gson;
import model.Response;
import util.HttpUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class QingKeYunmpl implements RobotService {
String apiTpl = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=%s"; Gson gson = new Gson();
@Override public Response AI(String msg) throws UnsupportedEncodingException {
String api = String.format(apiTpl, URLEncoder.encode(msg,"utf-8"));
String result = HttpUtil.request(api);
Response response = gson.fromJson(result, Response.class);
return response; } }
-
Note :需要导入三方包“gson-2.6.2.jar”解析json格式数据
-
定义Main函数启动入口,获取用户输入
package app;
import model.Response;
import service.QingKeYunmpl;
import service.RobotService;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
public class Main {
public static final RobotService service = new QingKeYunmpl();
public static void main(String[] args) throws UnsupportedEncodingException {
String name = "";
String msg = ""; Scanner scanner = new Scanner(System.in);
System.out.println("主人你好了,请给我起一个响亮的名字吧!");
name = scanner.nextLine(); System.out.println("我是您的小助手“"+name+"”很高兴为您服务。");
while (true){ msg = scanner.nextLine(); if ("886".equalsIgnoreCase(msg)){ System.out.println("欢迎下次使用,拜拜啦!");
break; }else {
Response response = service.AI(msg);
if (response != null && response.getResult() == 0){
System.out.println(name+":"+new String(response.getContent().getBytes(),"UTF-8"));
}else { System.out.println("小助手还在学习中,暂时没明白您的意思,请重新输入。"); } } } } }
智能问答机器器人项目打包和使用《完结》
项目怎么打包—>jar包方式
-
File -> Project Structure -> artifacts -> + -> jar -> from modules-> 选主类和第⼀一个单向按钮->确定后会生成Manifest文件
-
勾选include in project build(不用)
-
菜单栏 build->build artifacts -> build
-
java -jar xxx.jar 启动(如果是linux或者mac则可以守护进程方式)