In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use Java to achieve a simple chat robot". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Java to achieve a simple chat robot".
Create databases and tables and prepare table data (MySQL)
First create a database android
Then create a table dictionary with fields:
Id int
Receive varchar (100)
Response varchar (100)
Receive indicates the information received
Response indicates the message of response
Create database android;use android;create table dictionary (id int AUTO_INCREMENT, receive varchar, response varchar, PRIMARY KEY (id)) DEFAULT CHARSET=utf8;insert into dictionary values (null,' Hello', 'Hello sister!') Insert into dictionary values (null,', what's your name','do you want to hit on me?') ; insert into dictionary values (what's your name null,', 'comrade, no date'); insert into dictionary values (null,' hits you', 'come on, hit me')
Object Settings (ORM)
Object-relational mapping, setting the object corresponding to each field in the database.
Package socket;public class Dictionary {public int id; public String receive; / / receive public String response; / / 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 query (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;}}
Here, if there is not a coding problem and a jdbc error occurs, pay attention to whether or not to open the MySQL database.
Server end
Mainly normal socket entry programming.
Through the input stream to receive the data sent by the client (print), and then query the corpus dialogue database, found to return to the normal dialogue, can not find that do not understand.
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 cannotUnderstand= new ArrayList (); static {cannotUnderstand.add ("I don't understand"); cannotUnderstand.add ("speak human words") CannotUnderstand.add ("excuse me?") ; cannotUnderstand.add ("louder"); cannotUnderstand.add ("I'm busy while playing");} public static void main (String [] args) {try {ServerSocket ss = new ServerSocket (8888); System.out.println ("listening on port number: 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 end
Send and receive messages normally
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 {/ / connected to local port 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 () Thank you for your reading. The above is the content of "how to achieve a simple chat robot with Java". After the study of this article, I believe you have a deeper understanding of how to use Java to achieve a simple chat robot, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.