欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现简单聊天机器人

程序员文章站 2022-03-23 14:33:47
本文实例为大家分享了java实现简单聊天机器人的具体代码,供大家参考,具体内容如下整个小案例:整合了java socket编程、jdbc知识(orm/dao)创建数据库和表,准备表数据(mysql)首...

本文实例为大家分享了java实现简单聊天机器人的具体代码,供大家参考,具体内容如下

整个小案例:整合了java socket编程、jdbc知识(orm/dao)

创建数据库和表,准备表数据(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,'打你哦','来啊,来打我啊');

Java实现简单聊天机器人

对象设置(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 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;
    }
}

此处,如果不是代码编写问题,出现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 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();
        }

    }
}

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();
        }
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。