-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatBot.java
More file actions
283 lines (238 loc) · 6.25 KB
/
Copy pathChatBot.java
File metadata and controls
283 lines (238 loc) · 6.25 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//package ChatBot;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatBot extends JFrame implements ActionListener
{
static JTextArea area=new JTextArea();
JTextField field=new JTextField();
JScrollPane sp;
JButton send;
LocalTime time=LocalTime.now();
LocalDate date=LocalDate.now();
Random random=new Random();
public ChatBot(String title)
{
super(title);
setVisible(true);
bot("Hello! Welcome to Symptoms Detector!");
bot("Thank you! Please enter your age : (number in years) ");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setResizable(false);
getContentPane().setBackground(Color.DARK_GRAY);
field=new JTextField();
send=new JButton(">");
send.setFont(new Font("Serif",Font.BOLD,25));
send.setBackground(Color.white);
send.setBounds(735,520,50,35);
add(send);
//For Text area
area.setEditable(false);
area.setBackground(Color.white);
add(area);
area.setFont(new Font("Serif",Font.PLAIN,20));
//scrollbar
sp=new JScrollPane(area,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setBounds(10,20,775,470);
add(sp);
//For TextField
field.setSize(725,35);
field.setLocation(10,520);
field.setForeground(Color.black);
field.setFont(new Font("Serif",Font.BOLD,25));
add(field);
send.addActionListener(this);
getRootPane().setDefaultButton(send);
}
public void actionPerformed(ActionEvent e)
{
String message=field.getText().toLowerCase();
area.append("You : "+field.getText()+"\n");
field.setText("");
Socket sock=new Socket();
if(message.contains("how are you"))
{
int num=random.nextInt(3);
if(num==0)
{
bot("I'm fine !,What about you ? ");
}
else if(num==1)
{
bot("I am good , thanks for asking !");
}
else
{
bot("I am great ,thanks for asking !");
}
}
else if(message.contains("you")&&(message.contains("smart")||message.contains("good")))
{
bot("Thank you !");
}
else if(message.charAt(0)=='0'||message.charAt(0)=='1'||message.charAt(0)=='2'||message.charAt(0)=='3'||message.charAt(0)=='4'||message.charAt(0)=='5'||message.charAt(0)=='6'||message.charAt(0)=='7'||message.charAt(0)=='8'||message.charAt(0)=='9'){
bot("Thank you! Please enter the Symptoms you are experiencing (Seperated with commas) \nEg. fever, headache");
}
else if(message.contains("welcome"))
{
bot("You are so polite.How can i help you ?");
}
else if(message.contains(","))
{
String sym = message;
//
}
else if(message.contains("hi")&&message.charAt(0)=='h'||message.contains("hello")||message.contains("hey"))
{
int num=random.nextInt(3);
if(num==0)
{
bot("Hii");
}
else if(num==1)
{
bot("Hello");
}
else if(num==2)
{
bot("Heyy");
}
else if(num==3)
{
bot("hello buddy");
}
}
else if(message.contains("by"))
{
bot("Bye,See you soon ..!");
}
else if(message.contains("i am good")||message.contains("i am great")||message.contains("i am ")&&message.contains("fine"))
{
bot("Good to hear.");
}
else if(message.contains("thank"))
{
int num=random.nextInt(3);
if(num==0)
{
bot("Welcome..");
}
else if(num==1)
{
bot("My plesure");
}
else if(num==2)
{
bot("Happy to help");
}
else if(num==3)
{
bot("That's why i'm here for..");
}
}
else if(message.contains("what") && message.contains("name"))
{
if(message.contains("your"))
{
bot("I'm Bot...");
}
}
else if(message.contains("change"))
{
bot("Sorry,I can't change anything...");
}
else if( message.contains("time"))
{
String ctime=new String();
if(time.getHour()>12)
{
int hour=time.getHour()-12;
ctime=ctime+hour+":"+time.getMinute()+":"+time.getSecond()+" PM";
}
else
{
ctime=ctime+time.getHour()+":"+time.getMinute()+":"+time.getSecond()+" AM";
}
bot(ctime);
}
else if(message.contains("date")||message.contains("month")||message.contains("year")||message.contains("day"))
{
String cdate=new String();
cdate=cdate + date.getDayOfWeek()+","+date.getDayOfMonth()+" "+date.getMonth()+" "+date.getYear();
bot(cdate);
}
else if(message.contains("good morning"))
{
bot("Good morning,Have a nice day !");
}
else if(message.contains("good night"))
{
bot("Good night,Have a nice dreams !");
}
else if(message.contains("good evening"))
{
bot("Good Evening ...!");
}
else if(message.contains("good") && message.contains("noon"))
{
bot("Good Afternoon ...!");
}
else if(message.contains("clear")&&(message.contains("screen")||message.contains("chat")))
{
bot("wait a few second...");
area.setText("");
}
else
{
try
{
try
{
URL url=new URL("https://en.wikipedia.org/wiki/");
URLConnection connection=url.openConnection();
connection.connect();
bot("Here some results for you ...");
java.awt.Desktop.getDesktop().browse(java.net.URI.create("https://en.wikipedia.org/wiki/"+message));
}
catch(Exception ee)
{
bot("Connect with internet connection for get better results...");
}
}
catch(Exception eee)
{
int num=random.nextInt(3);
if(num==0)
{
bot("Sorry ,I can't understand you !");
}
else if(num==1)
{
bot("Sorry,I don't understand ");
}
else if(num==2)
{
bot("My apologies...I don't understand ");
}
}
}
}
public static void bot(String message)
{
area.append("Bot : "+message+"\n");
}
// TODO Auto-generated method stub
}