简易对话机器人

来源:互联网 发布:网络技术培训 北京 编辑:程序博客网 时间:2024/05/19 04:07
import java.io.*; 
import javax.swing.*; 
import java.awt.*; 
import java.util.*; 
import java.awt.event.*; 


class robots extends JFrame 
{
JLabel j1;
JLabel j2;
JPanel p1,p2;
JButton b1;
JTextField f1;
JTextArea a1,a2;
FileReader fr1;
String question[]=new String[100];
String answer[]=new String[100];
int count,a,b=0;
int ans=0;
robots(){
setTitle("机器人对话");
setSize(600,300);
setLayout(new GridLayout(1,2));
p1=new Mypanel1();
p2=new Mypanel2();
add(p1);
add(p2);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
String s="";
ArrayList<String> list=new ArrayList();
try{
fr1=new FileReader(new File("E:\\Editplus\\GUI\\机器人\\对话内容.txt"));
BufferedReader br1=new BufferedReader(fr1);
while((s=br1.readLine())!=null){
if(count%2==0)
question[a++]=s;
count++;
}
br1.close();
}
catch(Exception e){
System.out.println(e.toString());
}
try{
fr1=new FileReader(new File("E:\\Editplus\\GUI\\机器人\\对话内容.txt"));
BufferedReader br2=new BufferedReader(fr1);
count=0;
while((s=br2.readLine())!=null){
if(count%2==1)
answer[b++]=s;
count++;
}
br2.close();
fr1.close();
}
catch(Exception e){
System.out.println(e.toString());
}
Random rd=new Random();
b1.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
String str=f1.getText().trim();//清楚空格
char []ch=str.toCharArray();
try{
if(check(ch)){
throw new StringException();
}
}
catch(StringException x){
a1.append("请输入正确中文或数字字符!\n");
}
//比较字字符串与数组里的字符串
finally{
int z=0;
list.clear();//符合条件的相同问题不同答案
str=filter(ch);
String mohu[]=mohuchaxun(str);//将问题切分成若干关键字
for(int i=0;i<count/2;i++){ 
for(int j=0;j<mohu.length;j++)
{
if(question[i].contains(mohu[j]))//在答案种寻找关键字
{
list.add(answer[i]);//找到了就添加到答案列表里
z++;
}
}
}
System.out.println("z="+z);
if(z!=0){
int num=0;
if(list.size()>1)num=rd.nextInt(list.size()-1);//对答案列表里的回答随机显示
String answer=list.get(num);
a1.append(f1.getText()+"\n");
a1.append(answer+"\n");
}
else{
String hd="";
int more = rd.nextInt(5); 
switch(more)
{
case 0:hd="这个问题太难了,换一个吧";break;
case 1:hd="我偏不告诉你";break;
case 2:hd="总么问这个问题";break;
case 3:hd="你猜这个问题我会不会";break;
case 4:hd="我才不告诉你";break;
}
a1.append(hd+"\n");
}
}
}
}
);
}
public boolean check(char ch[]){//用已判断必须是数字或汉子
boolean X=false;
for(int i=0;i<ch.length;i++){
if(!((ch[i]>=48&&ch[i]<=57)||(vd(ch[i])))){
X=true;
}
}
return X;
}
public boolean vd(char a){  
byte[] bytes=(""+a).getBytes();   
               if(bytes.length==2){   
                           int[] ints=new int[2];   
                           ints[0]=bytes[0]& 0xff;   
                           ints[1]=bytes[1]& 0xff;   
                           if(ints[0]>=0x81 && ints[0]<=0xFE && ints[1]>=0x40 && ints[1]<=0xFE){   
                                       return true;
                            }   
               }
               return false;
}
public static String[] mohuchaxun(String s){
char ch[]=s.toCharArray();
if(ch.length%2==0){
String snum[]=new String[ch.length/2];
int t=0;
for(int i=0;i<ch.length;i+=2){
snum[t++]=""+ch[i]+ch[i+1];
}
return snum;
}
else{
String snum[]=new String[ch.length/2+1];
int t=0;
for(int i=0;i<ch.length-1;i+=2){
snum[t++]=""+ch[i]+ch[i+1];
}
snum[ch.length/2]=""+ch[ch.length-1];
return snum;
}
}
public String filter(char[] ch){
ArrayList list=new ArrayList();
for(int i=0;i<ch.length;i++){
if(vd(ch[i])){
list.add(ch[i]);
}
}
StringBuilder sb=new StringBuilder();
for(int i=0;i<list.size();i++)sb.append(list.get(i));
String s=sb.toString();
return s;
}
class Mypanel1 extends JPanel
{
Mypanel1(){
j1=new JLabel("输入问题");
f1=new JTextField(25);
b1=new JButton("发送");
this.add(j1);
this.add(f1);
this.add(b1);
}
}
class Mypanel2 extends JPanel
{
Mypanel2(){
j2=new JLabel("机器人的回答");
a1=new JTextArea(10,20);
this.add(j2);
this.add(new JScrollPane(a1));

}
}
public static void main(String[] args) 
{
new robots(); 
}
}
class StringException extends Exception
{
public String toString(){
return "请输入正确中文或数字字符";
}
}