這篇文章主要講解了“如何用Java實現(xiàn)簡單聊天機器人”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何用Java實現(xiàn)簡單聊天機器人”吧!
創(chuàng)新互聯(lián)主要從事網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)臨西,十載網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
創(chuàng)建數(shù)據(jù)庫和表,準備表數(shù)據(jù)(MySQL)
首先創(chuàng)建一個數(shù)據(jù)庫android
然后創(chuàng)建一個表dictionary,字段:
id int
receive varchar(100)
response varchar(100)
receive 表示受到的信息
response 表示回應(yīng)的信息
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,'打你哦','來啊,來打我啊');
對象設(shè)置(ORM)
對象關(guān)系映射,設(shè)置對象對應(yīng)數(shù)據(jù)庫中的各字段。
package socket; public class Dictionary { public int id; public String receive; // 接收 public String response; // 回應(yīng) }
數(shù)據(jù)訪問對象(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; } }
此處,如果不是代碼編寫問題,出現(xiàn)jdbc錯誤,請注意是否打開MySQL數(shù)據(jù)庫。
server端
主要是正常的socket入門編程。
通過輸入流收到Client端發(fā)來的數(shù)據(jù)(打印),然后查詢語料對話庫,查到返回正常對話,查不到表示聽不懂。
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("監(jiān)聽在端口號: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端
正常的收發(fā)信息
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實現(xiàn)簡單聊天機器人”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對如何用Java實現(xiàn)簡單聊天機器人這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!