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

您所在的位置:网站首页 智能机器人英文缩写怎么写出来 如何用Java实现智能对话机器人

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

2024-07-12 15:11| 来源: 网络整理| 查看: 265

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

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

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

这篇博客涵盖的知识点 HTML网页源代码抓取JSON字符串解析

以下为需要用到的Jar

Jar备注JSONObject用于解析JSON

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

创建机器人

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

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

效果图



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3