新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要讲解了“如何用Java实现简单聊天机器人”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何用Java实现简单聊天机器人”吧!
创新互联主要从事网站制作、网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务临西,十载网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18982081108
创建数据库和表,准备表数据(MySQL)
首先创建一个数据库android
然后创建一个表dictionary,字段:
id int
receive varchar(100)
response varchar(100)
receive 表示受到的信息
response 表示回应的信息
create database android; use android; create table dictionary( id int AUTO_INCREMENT, receive varchar(100), response varchar(100), PRIMARY KEY (id) ) DEFAULT CHARSET=utf8; insert into dictionary values(null,'你好','好你妹!'); insert into dictionary values(null,'你叫什么','你想泡我啊?'); insert into dictionary values(null,'你叫什么','同志,不约'); insert into dictionary values(null,'打你哦','来啊,来打我啊');
对象设置(ORM)
对象关系映射,设置对象对应数据库中的各字段。
package socket; public class Dictionary { public int id; public String receive; // 接收 public String response; // 回应 }
数据访问对象(DAO)
package socket; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class DictionaryDAO { public DictionaryDAO() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public Connection getConnection() throws SQLException { return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/android?characterEncoding=UTF-8", "root", "admin"); } public Listquery(String recieve) { List ds = new ArrayList (); String sql = "select * from dictionary where receive = ? "; try (Connection c = getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setString(1, recieve); ResultSet rs = ps.executeQuery(); while (rs.next()) { Dictionary d = new Dictionary(); int id = rs.getInt(1); String receive = rs.getString("receive"); String response = rs.getString("response"); d.id = id; d.receive = receive; d.response = response; ds.add(d); } } catch (SQLException e) { e.printStackTrace(); } return ds; } }
此处,如果不是代码编写问题,出现jdbc错误,请注意是否打开MySQL数据库。
server端
主要是正常的socket入门编程。
通过输入流收到Client端发来的数据(打印),然后查询语料对话库,查到返回正常对话,查不到表示听不懂。
package socket; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Server { private static ListcannotUnderstand= new ArrayList<>(); static{ cannotUnderstand.add("听求不懂啊"); cannotUnderstand.add("说人话"); cannotUnderstand.add("再说一遍?"); cannotUnderstand.add("大声点"); cannotUnderstand.add("老子在忙,一边玩儿去"); } public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8888); System.out.println("监听在端口号:8888"); Socket s = ss.accept(); InputStream is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); while (true) { String msg = dis.readUTF(); System.out.println(msg); List ds= new DictionaryDAO().query(msg); String response = null; if(ds.isEmpty()){ Collections.shuffle(cannotUnderstand); response = cannotUnderstand.get(0); } else{ Collections.shuffle(ds); response = ds.get(0).response; } dos.writeUTF(response); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Client端
正常的收发信息
package socket; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; public class Client { public static void main(String[] args) { try { // 连接到本机的8888端口 Socket s = new Socket("127.0.0.1", 8888); // OutputStream os = s.getOutputStream(); DataOutputStream dos = new DataOutputStream(os); InputStream is = s.getInputStream(); DataInputStream dis = new DataInputStream(is); while (true) { Scanner sc = new Scanner(System.in); String str = sc.next(); dos.writeUTF(str); String msg = dis.readUTF(); System.out.println(msg); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
感谢各位的阅读,以上就是“如何用Java实现简单聊天机器人”的内容了,经过本文的学习后,相信大家对如何用Java实现简单聊天机器人这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!