Rumah >Java >javaTutorial >Bagaimana untuk melaksanakan chatbot mudah di Java
Buat pangkalan data dan jadual, sediakan data jadual (MySQL)
Mula-mula buat pangkalan data android
dan kemudian buat kamus jadual, medan:
id int
receive varchar(100)
response varchar(100)
receive mewakili maklumat yang diterima
respons mewakili maklumat respons
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,'打你哦','来啊,来打我啊');
Tetapan objek (ORM)
Pemetaan hubungan objek, tetapkan objek yang sepadan dengan setiap medan dalam pangkalan data.
package socket; public class Dictionary { public int id; public String receive; // 接收 public String response; // 回应 }
Data Access Object (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 List<Dictionary> query(String recieve) { List<Dictionary> ds = new ArrayList<Dictionary>(); 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; } }
Di sini, jika ia bukan masalah penulisan kod dan ralat jdbc berlaku, sila ambil perhatian sama ada pangkalan data MySQL dibuka.
Sebelah pelayan
Terutamanya pengaturcaraan pengenalan soket biasa.
Terima data (cetak) daripada klien melalui aliran input, dan kemudian tanya pangkalan data dialog korpus Jika ditemui, dialog biasa akan dikembalikan Jika ia tidak ditemui, ini bermakna anda tidak faham.
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 List<String> cannotUnderstand= 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<Dictionary> 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(); } } }
Pelanggan
Menghantar dan menerima maklumat biasa
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(); } } }
Atas ialah kandungan terperinci Bagaimana untuk melaksanakan chatbot mudah di Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!