javaSE开发智能问答机器人项目

本文详细介绍了如何使用Java SE开发一个智能问答机器人,包括项目的基础知识、效果演示、API平台介绍(如青云客)、指令示例、项目框架搭建、模块划分以及服务接口实现。通过创建Request和Response模型,封装HttpUtil工具类,实现QingKeYunmpl服务类,最终在Main函数中获取用户输入并调用API进行交互。项目打包成jar文件后,用户可直接运行。
摘要由CSDN通过智能技术生成

javaSE开发智能问答机器人项目

智能问答机器人项目介绍和演示

项目所需基础知识
  • 网络请求Http知识
  • json和三方包知识
  • Javase知识
项目效果演示

在这里插入图片描述

  • 类似于 天猫精灵 苹果siri 智能客服 微软小冰…

智能问答API平台介绍

常见智能问答API平台(仅供练习使用)
API使用讲解(eg:青云客)
  • api地址:http://api.qingyunke.com/api.php?key=free&appid=0&msg=关键词

    • key 固定参数free
    • appid 设置为0,表示智能识别,可忽略此参数
    • msg 关键词,请参考下方参数示例,该参数智能识别,该值请经过 urlencode 处理后再提交
    • 返回结果: {“result”:0,“content”:“内容”}
    • result 状态, 0表示正常,其它数字表示错误
    • content 信息内容
指令示例

天气: msg=天气西安
中英翻译: msg=翻译i love you
歌词: msg=歌词 成都
笑话: msg=笑话
计算: msg=计算 1+1*2/3-4

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

流程分析
  • 用户输入指令 -> http发送请求 -> 解析结果 -> 显示内容 -> 循环上述操作
    在这里插入图片描述
分包层
  • model 存放请求响应对象
  • util 存放工具类
  • app main函数入口
  • service 相关业务接口和实现类
逻辑结构

在这里插入图片描述

项目搭建

在这里插入图片描述

创建model对象
  • Request
package model;

/**
 * 封装请求返回的三个参数
 */
package model;
//封装发起Http请求的三个参数
public class Request {
    private String key = "free";
    private int appid = 0;
    private String msg = "";

    //重写构造函数
    public Request(){}
    public Request(String msg){
        this.msg = msg;
    }

    //设置参数的get/set方法

    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;
    }
}

  • Response
package model;
//封装Http返回的两个参数
public class Response {
    private int result = 0;
    private String content = "";

    //设置参数的get/set方法

    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工具类封装
  • HttpUtil
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);
            //打开相应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;
}

  • QKYrobotmpl
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函数启动入口,获取用户输入
  • Main
package app;


import model.Response;
import service.QingKeYunmpl;
import service.RobotService;

import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class Main {
    //调用青云客API接口
    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则可以守护进程方式)
  • 4
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值