目录

Life in Flow

知不知,尚矣;不知知,病矣。
不知不知,殆矣。

X

人工智能问答机器人

智能问答 API 平台

青云客 API

 1//接口地址  
 2http://api.qingyunke.com/api.php?key=free&appid=0&msg=关键词
 3	key 固定参数free  
 4	appid 设置为0,表示智能识别,可忽略此参数  
 5	msg 关键词,请参考下方参数示例,该参数可智能识别,该值请经过 urlencode 处理后再提交
 6
 7//返回结果
 8{"result":0,"content":"内容"}
 9	result 状态,0表示正常,其它数字表示错误  
10	content 信息内容

msg 示例

1天⽓:msg=天⽓深圳 
2中英翻译:msg=翻译i love you 
3歌词⑴:msg=歌词 成都 
4笑话:msg=笑话 
5计算⑴:msg=计算 1+1*2/3-4 
6计算⑵:msg=1+1*2/3-4

使用接口实例
请求

1http://api.qingyunke.com/api.php?key=free&appid=0&msg=你今年多大了

返回

1{
2  "result": 0,
3  "content": "女生的年龄是不能随便说的,知道不"
4}

包模块划分

  • 流程分析:⽤户输 ⼊指令 -> http 发送请求 -> 解析结果 -> 显示内容 -> 循环上述操作
  • 模块划分(包组织)
1* model 存放请求响应对象
2* util 存放⼯具类
3* app main函数⼊⼝
4* service 相关业务接⼝和实现类

model

Request

 1package model;
 2
 3public class Request {
 4
 5    private String key = "free";
 6
 7    private String appid = "0";
 8
 9    private String msg = "";
10
11
12    public Request(){}
13
14
15    public Request(String msg){
16        this.msg = msg;
17    }
18
19    public String getKey() {
20        return key;
21    }
22
23    public void setKey(String key) {
24        this.key = key;
25    }
26
27    public String getAppid() {
28        return appid;
29    }
30
31    public void setAppid(String appid) {
32        this.appid = appid;
33    }
34
35    public String getMsg() {
36        return msg;
37    }
38
39    public void setMsg(String msg) {
40        this.msg = msg;
41    }
42}

Response

 1package model;
 2
 3public class Response {
 4
 5
 6    private int code;
 7
 8    private String content;
 9
10    public int getCode() {
11        return code;
12    }
13
14    public void setCode(int code) {
15        this.code = code;
16    }
17
18    public String getContent() {
19        return content;
20    }
21
22    public void setContent(String content) {
23        this.content = content;
24    }
25}

util

 1package util;
 2
 3import java.io.BufferedReader;
 4import java.io.InputStream;
 5import java.io.InputStreamReader;
 6import java.net.HttpURLConnection;
 7import java.net.URL;
 8
 9public class HttpUtils {
10
11
12    public static String request(String api ){
13
14        HttpURLConnection connection = null;
15        int responseCode = 0;
16        try{
17            URL url = new URL(api);
18            //获取对应的连接对象
19            connection = (HttpURLConnection) url.openConnection();
20            responseCode = connection.getResponseCode();
21        }catch (Exception e){
22            e.printStackTrace();
23        }
24
25        if(200 <= responseCode && responseCode<=299){
26            try(InputStream inputStream = connection.getInputStream();
27                BufferedReader in =  new BufferedReader(new InputStreamReader(inputStream));
28            ){
29
30                StringBuilder response = new StringBuilder();
31                String currentLine;
32
33                while ((currentLine = in.readLine())!= null){
34                    response.append(currentLine);
35                }
36
37                String result = response.toString();
38                return result;
39
40            }catch (Exception e){
41                e.printStackTrace();
42            }
43        }
44        return null;
45    }
46}

service

 service 层接 ⼝抽离的好处 解耦: 后续切换平台只要 ⼼中对应的实现类即可。
RobotService

 1package service;
 2
 3import model.Response;
 4
 5import java.io.UnsupportedEncodingException;
 6
 7public interface RobotService {
 8
 9    Response qa(String msg) ;
10
11}

QkyRobotServiceImpl

app

主函数

 1package app;
 2
 3import model.Response;
 4import service.AliyunRobotServiceImpl;
 5import service.QkyRobotServiceImpl;
 6import service.RobotService;
 7
 8import java.util.Scanner;
 9
10public class Main {
11
12    private static final RobotService robotService = new QkyRobotServiceImpl();
13
14
15    public static void main(String[] args)throws Exception {
16
17        Scanner scanner = new Scanner(System.in);
18        System.out.println("老板,麻烦您给我取个响亮的名称,按回车键确定!!!!");
19        //机器人助手的名字
20        String name = scanner.nextLine();
21        
22        System.out.println("hi,我是您的小助手 "+name +", 直接对我下达指令");
23        while (true){
24
25            String input = scanner.nextLine();
26            if("886".equalsIgnoreCase(input)){
27                System.out.println("欢迎下次使用,拜拜");
28                break;
29            }else {
30              Response response = robotService.qa(input);
31
32              if(response != null && response.getCode() == 0){
33                  System.out.println(name+":"+ new String(response.getContent().getBytes(),"UTF-8"));
34              }else {
35                  System.out.println(name+": 暂时没明白您的意思,重新告诉我下吧");
36              }
37            }
38        }
39        scanner.close();
40    }
41}

测试

 1Siri
 2hi,我是您的小助手 Siri, 直接对我下达指令
 3上海天气
 4Siri:[1227] 上海天气:晴,白天 9℃,夜晚 4℃,北风转东风,<3
 5翻译: 我在看电视!
 6Siri:翻译:我在看电视!{br}结果:Im watching TV!
 7歌词 龙卷风
 8Siri: 龙卷风-梁紫丹{br}梁紫丹 - 龙卷风{br}:徐若瑄 :周杰伦{br} noni99{br}---------{br}爱像一阵风{br}吹完它就走{br}这样的节奏{br}谁都无可奈何{br}没有你以后{br}我灵魂失控{br}黑云在降落{br}我被它拖着走{br}静静悄悄默默离开{br}陷入了危险边缘Baby~{br}我的世界已狂风暴雨{br}Wu~爱情来的太快就像龙卷风{br}离不开暴风圈来不及逃{br}我不能再想 我不能再想{br}我不 我不 我不能{br}爱情走的太快就像龙卷风{br}不能承受我已无处可躲{br}我不要再想 我不要再想{br}我不 我不 我不要再想你{br}不知不觉 你已经离开我{br}不知不觉 我跟了这节奏{br}
 9886
10欢迎下次使用,拜拜后知后觉 又过了一个秋{br}后知后觉 我该好好生活{br}----{br}后知后觉

打包

  • File -> Project Structure -> artifacts -> + -> jar -> from modules-> 选主类和第 ⼀个单向按 钮-》确定后 会 ⽣成 Manifest ⽂件
  • 菜单栏 build->build artifacts -> build
  • Java -jar xxx.jar 启动(如果是 Linux 或者 Mac 则可以守护进程 ⽅式)
1PS F:\IT\PDF\配套资料\D课件\JavaSE\小课堂201912月全新javase课程笔记+源码\源码等其他资料\17\7> java -jar .\qa-project.jar
2老板,麻烦您给我取个响亮的名称,按回车键确定!!!!
3Howard
4hi,我是您的小助手 Howard, 直接对我下达指令
5翻译 我爱你
6Howard:翻译:我爱你{br}结果:I love you

作者:Soulboy