如何用Java实现智能对话机器人

如何用Java实现智能对话机器人

前言 这个时代人工智能如此火爆,身为圈内人我们应该多少对他有些了解,为了靠他近一些今天我们动手用Java实现一个智能聊天机器人,当然此处我们需要依赖图灵机器人的Api


点我进入图灵机器人官网注册帐号

这篇博客涵盖的知识点

  • HTML网页源代码抓取
  • JSON字符串解析

以下为需要用到的Jar

Jar备注
JSONObject用于解析JSON

首先我们要注册一个图灵机器人的帐号 并创建我们自己的机器人

在这里插入图片描述

这里可以根据个人需求填写
然后拿到我们刚刚创建的机器人的APIkey

从这里我们可以拿到我们的api请求地址,和我们的机器人的APIkey


图灵机器人现在维护的请求方式为POST 但是GET请求还能使用,这里我们使用GET请求接口。
接口的请求地址为 http://www.tuling123.com/openapi/api?key= [APPkey]&info=[你需要发送的消息]

准备完毕后 我们开始代码部分

  • 第一步,编写的工具类
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @author 4everlynn
 * @date 2017/12/17
 */
public class Util {
    //存储APIkey
    public static final String API_KEY = "填写你的APPKEY";
    //存储接口请求地址
    public static final String API_URL = "http://www.tuling123.com/openapi/api";

    /**
     * 拼接出我们的接口请求地址
     *
     * @param msg 需要发送的消息
     * @return
     */
    private String setParameter(String msg) {
        //在接口请求中 中文要用URLEncoder encode成UTF-8
        try {
            return API_URL + "?key=" + API_KEY + "&info=" + URLEncoder.encode(msg, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 拿到消息回复的内容的方法
     * @param json 请求接口得到的JSON
     * @return text的部分
     */
    private String getString(String json){
        try {
            JSONObject object = new JSONObject(json);
            return object.getString("text");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 提供对外公开的方法用于最终拿到机器人回复的消息
     * @param msg 传入你需要发送的信息
     * @return 机器人对你的回复
     */
    public String getMessage(String msg){
        return getString(getHTML(setParameter(msg)));
    }


    private String getHTML(String url) {
        StringBuffer buffer = new StringBuffer();
        BufferedReader bufferedReader = null;
        try {
            //创建URL对象
            URL u = new URL(url);
            //打开连接
            HttpURLConnection connection = (HttpURLConnection) u.openConnection();
            //从连接中拿到InputStream并由BufferedReader进行读取
            bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line = "";
            //循环每次加入一行HTML内容 直到最后一行
            while ((line = bufferedReader.readLine()) != null) {
                buffer.append(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //结束时候关闭释放资源
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer.toString();
    }
}


  • 第二步,编写测试类
package com.disware;

import java.util.Scanner;

/**
 * @author 4everlynn
 * @date 2017/12/17
 */
public class Main {

    public static void main(String[] args) {
        //声明并实例化我们刚刚封装好的工具类
        Util util = new Util();
        //接收用户输入
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            //直接输出机器人的回复
            System.err.println("Ta 对你说 -> " + util.getMessage(scanner.nextLine()));
        }
    }
}

在这里插入图片描述

  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值