Java實現聊天機器人

本文實例為大傢分享瞭Java實現聊天機器人的具體代碼,供大傢參考,具體內容如下

服務器的代碼:

package Day02;
import java.io.*;
import java.net.*;
 
 
public class Socket1 {
 public static void main(String[] args) throws Exception{
  int port=8899;
  ServerSocket server=null;
  MChat MC=new MChat();
  try {
   server=new ServerSocket(port);
   System.out.println("服務器啟動...");  
   while(true) {
    Socket socket=server.accept();//監聽客戶端連接,如果客戶端連接成功,返回實例化的Socket對象
    System.out.println("客戶端信息:"+socket);
    //讀取流操作
    SocketThd thd=new SocketThd(socket);
    thd.start();
   }
  } catch (Exception e) {
   // TODO: handle exception
  }
  finally {
   try {
    if(server!=null)
     server.close();
    System.out.println("服務器已停止! ");
   } catch (Exception e2) {
    // TODO: handle exception
   }
  }
 } 
static class SocketThd extends Thread{
 MChat MC=new MChat();
 Socket socket;//和客戶端進行通信的對象
 BufferedReader br=null;
 PrintWriter pw=null;
 public SocketThd(Socket socket) {
  this.socket=socket;
 }
 public void run() {
  //讀入流操作
    try {
     br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
     //寫入流操作
     pw=new PrintWriter(
       new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8")));
     //接收客戶端發送來的信息
     String str="";
     
     while((str=br.readLine())!=null) {
      System.out.println("客戶端:"+str);
      //服務器處理
      String rtn="";
      //聊天機器人處理程序
      rtn=MC.getCharResult(str);
      //處理結果返回客戶端
      pw.println(rtn);
      pw.flush();
    }
   }
   catch (Exception e) {
     e.printStackTrace();
    }
    finally {
      // TODO: handle finally clause
      try {
       if(br!=null) br.close();
       if(pw!=null) pw.close();
       if(socket!=null) socket.close();
      } catch (Exception e2) {
       // TODO: handle exception
      }
    }    
  }
 }
}

客戶端的代碼:

package Day02;
 
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
 
public class Client1 {
 public static void main(String[] args) throws Exception{
  String IP="192.168.1.*";
  int port=8899;
  Socket socket=new Socket(IP,port);//建立與服務器連接
  
  //讀取流操作
  BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
  BufferedReader brKey=new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
  //寫入流操作
  PrintWriter pw=new PrintWriter(
    new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8")));
  while(true) {
   String send=brKey.readLine();//等待阻塞命令
   if(send.equals("END")) {//鍵盤輸入的借宿條件
    break;
   }
   //向服務器發送信息
   
   pw.println(send);
   pw.flush();
   //接收服務端回復的消息
   String strRtn=br.readLine();
   
   System.out.println("服務端回復信息:"+strRtn);
  }
  //關閉連接
  br.close();
  pw.close();
  socket.close();
 }
}

對話內容引擎:

package Day02;
 
import java.util.Hashtable;
 
public class MChat {
 private Hashtable<String, String> htMS=new Hashtable<>();
 public String getCharResult(String sKey) {
  String str=htMS.get(sKey);
  if(str==null) 
   return "";
  else
   return str;
  
 }
 public MChat() {
  htMS.put("你好呀","你也好呀,你需啊什麼服務嗎?");
  htMS.put("見到你很高興","我也很高興見到你。");
  htMS.put("吃瞭嗎","今天吃的好飽呀");
  htMS.put("好累呀", "聽音樂輕松一下吧");
  htMS.put("路上好堵呀","需要哦給你選一條路線嗎");
  htMS.put("再見", "我先走瞭,有事你叫我");
 
 }
}

實現結果如下:

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: