-
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.regex.*;
import java.util.Random;
import static java.awt.BorderLayout.*;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class RobotDemo
{
final JTextArea jta=new JTextArea(30,40);
final JTextField jtf=new JTextField(10);
final JTextField jtf1=new JTextField(10);
final JTextField jtf2=new JTextField(10);
Link lk,lk1,lk2,lk3,lk4,lk5;
ResultSet rs ,rs1,rs2,rs3,rs4,rs5;
Statement sta;
String answer,res;
Random r=new Random();
public void init()
{
final JFrame jf=new JFrame();
JPanel jp=new JPanel();
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JButton jb=new JButton("发送");
JButton jb1=new JButton("训练");
JButton jb2=new JButton("确定");
JButton jb3=new JButton("再加一条");
final JDialog jd=new JDialog(jf,"训练");
final JDialog jd1=new JDialog(jf,"提示信息");
JTextArea jta1=new JTextArea(5,10);
jd1.setLayout(new BorderLayout());
jd1.add(jta1,NORTH);
JPanel jp3=new JPanel();
jd1.add(jp3,SOUTH);
jp3.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton jb4=new JButton("确定");
jp3.add(jb4);
jta1.setText("不能发送空白内容");
jf.add(jp);
jp.setLayout(new BorderLayout());
jp.add(jta,NORTH);
jp.add(jp1,SOUTH);
jp1.add(jtf);
jp1.add(jb);
jp1.add(jb1);
jd.add(jp2);
jp2.add(jtf1);
jp2.add(jtf2);
jp2.add(jb3);
jp2.add(jb2);
jb4.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jd1.setVisible(false);
jta.setText("");} }); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String out=""; String result=""; String s=jtf.getText(); if(s.equals("")) { jd1.pack(); jd1.setVisible(true); } if(s.length()<4) { result=find(s); } else { result=find1(s); } out="我:"+s+"\n"+"机器人:"+result+"\n"; jta.setText(out); jtf.setText(""); } }); jb2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String question =jtf1.getText(); String answer=jtf2.getText(); try { if(question.equals("")&&answer.equals("")) { JOptionPane.showMessageDialog(null, "不能添加空白内容"); } else { Statement sta = null; String sql="insert into chat1 values('" +question+ "','" +answer+"')"; lk=new Link(sql); lk.sta.executeUpdate(sql); JOptionPane.showMessageDialog(null, "添加成功"); } } catch(Exception a) { a.printStackTrace(); JOptionPane.showMessageDialog(null, "添加失败"); } finally { lk.closeConn(); } } }); jb3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jtf2.setText(""); } }); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jd.pack(); jd.setVisible(true); } }); jf.pack(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true);
}
public String find(String s)
{
String result="";try { String sql="select *from chat1"; lk=new Link(sql); rs=lk.sta.executeQuery(sql); while(rs.next()) { String string =new String(rs.getString("question")); if(string.equals(s)||string.contains(s)||s.contains(string)) { try { String sql1="select * from chat1 where question='"+string+"'"; lk1=new Link(sql1); rs1=lk1.sta.executeQuery(sql1); while(rs1.next()) { result=new String (rs1.getString("answer")); } } catch(Exception e) { e.printStackTrace(); } finally { lk1.closeConn(); } } } } catch(Exception e) { e.printStackTrace(); } finally { lk.closeConn(); } if(result.equals("")) { int i=3; int k=r.nextInt(i); if(k==0) result="asdfsdaf"; else if(k==1) result="rtfdsg"; else if(k==2) result="fgasdd"; } return result;
}
public String find1(String s){
Boolean flag=false;
char c[];
int j=0,m=0,max=0;
String result=null,ans="";
String string[]=new String[20];
String regex="[a-zA-Z0-9,。、;‘【】、=-《》?:的“{}——+|) _(*&……%¥#@!~·哦他她你我了吗嘛呢吧 ]";
Pattern p1=Pattern.compile(regex);
Matcher m1=p1.matcher(s);
ans=m1.replaceAll("").trim();
c=new char[ans.length()];
for(int i=0;i<ans.length();i++){
c[i]=ans.charAt(i);
}for(j=0;j<c.length-1;j++){ if(j==0){ try{ String sql="drop table if exists possible"; lk=new Link(sql); lk.sta.executeUpdate(sql); String sql1="create table possible(question varchar(60))"; lk1=new Link(sql1); lk1.sta.executeUpdate(sql1); }catch(Exception e){ e.printStackTrace(); } finally{ lk1.closeConn(); lk.closeConn(); } } try{ String sql2="select *from chat1"; lk2=new Link(sql2); rs2=lk2.sta.executeQuery(sql2); while(rs2.next()){ int i=0; String str=new String (rs2.getString("question")); do{ flag=false; int k=i+1; int ij=i+j; if(ij<c.length){ char c1=str.charAt(i); if(c[ij]==c1&&str.length()==k){ String sql3="insert into possible values('"+str+"')"; lk3=new Link(sql3); lk3.sta.executeUpdate(sql3); } else if(c[ij]==c1&&str.length()!=k){ flag=true; i++; } } }while(flag); } }catch(Exception e2){ e2.printStackTrace(); } finally{ lk2.closeConn(); if(lk3!=null){ lk3.closeConn(); } } } try{ String sql4="select *from possible"; lk4=new Link(sql4); rs4=lk4.sta.executeQuery(sql4); while(rs4.next()){ string[m]=new String(rs4.getString("question")); m++; } if(m!=0){ max=string[0].length(); ans=string[0]; for(int k=1;k<=m;k++){ if(max<string[k-1].length()){ ans=string[k-1]; max=string[k-1].length(); } } String sql5="select *from chat1 where question='"+ans+"'"; lk5=new Link(sql5); rs5=lk5.sta.executeQuery(sql5); while(rs5.next()){ ans=new String(rs5.getString("answer")); } } else{ int i=4; int k=r.nextInt(i); if(k==0){ result="rd6dfg"; }else if(k==1){ result="8ujhgjghg"; }else if(k==2){ result="dftyghf"; } } }catch(Exception ea){ ea.printStackTrace(); } finally{ lk4.closeConn(); if(lk5!=null){ lk5.closeConn(); } } if(result.equals("")){ result="tfyrtyrt"; } return result;
}
public static void main(String[] args)
{
new RobotDemo().init();
String MUSIC_FILE="相逢一笑.wav";
try
{
AudioInputStream audioInputStream= AudioSystem.getAudioInputStream(new File(MUSIC_FILE));
AudioFormat audioFormat=audioInputStream.getFormat();
DataLine.Info dataLineInfo=new DataLine.Info(SourceDataLine.class,audioFormat,AudioSystem.NOT_SPECIFIED);
SourceDataLine sourceDataLine=(SourceDataLine)AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
int count;
byte tempBuffer[]=new byte[1024];
while ((count = audioInputStream.read(tempBuffer, 0,
tempBuffer.length)) != -1)
{
if(count>0)
{
sourceDataLine.write(tempBuffer,0,count);
}
}
sourceDataLine.drain();
sourceDataLine.close();
}
catch(Exception a)
{
a.printStackTrace();
}}
}