-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.java
125 lines (108 loc) · 2.87 KB
/
chat.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Color;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.lang.Math;
public class ChatBot extends JFrame implements KeyListener{
JPanel p=new JPanel();
JTextArea dialog=new JTextArea(20,50);
JTextArea input=new JTextArea(1,50);
JScrollPane scroll=new JScrollPane(
dialog,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER
);
String[][] chatBot={
//standard greetings
{"hi","hello","hola","ola","howdy"},
{"hi","hello","hey"},
//question greetings
{"how are you","how r you","how r u","how are u"},
{"good","doing well"},
//yes
{"yes"},
{"no","NO","NO!!!!!!!"},
//default
{"good","bye", "I am not supposed to say"}
};
public static void main(String[] args){
new ChatBot();
}
public ChatBot(){
super("Chat Bot");
setSize(600,400);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
dialog.setEditable(false);
input.addKeyListener(this);
p.add(scroll);
p.add(input);
p.setBackground(new Color(255,200,0));
add(p);
setVisible(true);
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(false);
//-----grab quote-----------
String quote=input.getText();
input.setText("");
addText("-->You:\t"+quote);
quote.trim();
while(
quote.charAt(quote.length()-1)=='!' ||
quote.charAt(quote.length()-1)=='.' ||
quote.charAt(quote.length()-1)=='?'
){
quote=quote.substring(0,quote.length()-1);
}
quote.trim();
byte response=0;
/*
0:we're searching through chatBot[][] for matches
1:we didn't find anything
2:we did find something
*/
//-----check for matches----
int j=0;//which group we're checking
while(response==0){
if(inArray(quote.toLowerCase(),chatBot[j*2])){
response=2;
int r=(int)Math.floor(Math.random()*chatBot[(j*2)+1].length);
addText("\n-->Michael\t"+chatBot[(j*2)+1][r]);
}
j++;
if(j*2==chatBot.length-1 && response==0){
response=1;
}
}
//-----default--------------
if(response==1){
int r=(int)Math.floor(Math.random()*chatBot[chatBot.length-1].length);
addText("\n-->Michael\t"+chatBot[chatBot.length-1][r]);
}
addText("\n");
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
input.setEditable(true);
}
}
public void keyTyped(KeyEvent e){}
public void addText(String str){
dialog.setText(dialog.getText()+str);
}
public boolean inArray(String in,String[] str){
boolean match=false;
for(int i=0;i<str.length;i++){
if(str[i].equals(in)){
match=true;
}
}
return match;
}
}