⼈⼯智能问答机器⼈

智能问答API平台介绍

常⻅的智能问答API平台(仅供练习使⽤)
    图灵api: http://www.turingapi.com/
    阿⾥云服务: https://aliyun.com/
    亚⻢逊云服务:https://aws.amazon.com
    腾讯云服务:https://cloud.tencent.com/
    ⻘云客(选⽤,地址可能会改版,如果不可⽤,可以直接⽤课程的或者换平台) http://api.qi
    ngyunke.com/
⻘云客API使⽤讲解
 api地址: http://api.qingyunke.com/api.php?key=free&appid=0&msg=关键词
    key 固定参数free
    appid 设置为0,表示智能识别,可忽略此参数
    msg 关键词,请参考下⽅参数示例,该参数可智能识别,该值请经过 urlencode 处理
    后再提交
    返回结果:{"result":0,"content":"内容"}     
    result 状态,0表示正常,其它数字表示错误
    content 信息内容

eg:

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

项⽬基本框架搭建和模块划分

流程分析
    ⽤户输⼊指令 -> http发送请求 -> 解析结果 -> 显示内容 -> 循环上述操作
包分层
    model 存放请求响应对象
    util 存放⼯具类
    app main函数⼊⼝
    service 相关业务接⼝和实现类


创建model对象

public class Request {
    private String key = "free";
    private String appid = "0";
    private String msg = "";
}
    
public class Response {
    private int code;
    private String content;
}

http⼯具类封装和gson依赖导⼊


    gson依赖导⼊
    http⼯具类封装
       

package com.lj.Ai.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {

    public static String request(String api ){
        HttpURLConnection connection = null;
        int responseCode = 0;
        try{
            URL url = new URL(api);
            //获取对应的连接对象
            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;
    }
}

service层接⼝定义和实现

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 Ai(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的时候,或者出错
        Response response = gson.fromJson(result,Response.class);

        return response;
    }

核⼼⼊⼝类实现

定义Main函数启动⼊⼝,获取⽤户输⼊
   
 

public static void main(String[] args)throws Exception {

        Scanner scanner = new Scanner(System.in);
        System.out.println("老板,麻烦您给我取个响亮的名称,按回车键确定!!!!");
        String name = scanner.nextLine();
        System.out.println("hi,我是您的小助手 "+name +", 直接对我下达指令");
        while (true){
            String input = scanner.nextLine();
            if("886".equalsIgnoreCase(input)){
                System.out.println("欢迎下次使用,拜拜");
                break;
            }else {
              Response response = robotService.Ai(input);

              if(response != null && response.getCode() == 0){
                  System.out.println(name+":"+ new String(response.getContent().getBytes(),"UTF-8"));
              }else {
                  System.out.println(name+": 暂时没明白您的意思,重新告诉我下吧");
              }
            }
        }
        scanner.close();
    }
}

结果演示:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值