1
+ import java .util .*;
2
+ import java .text .*;
3
+
4
+ public class ChatBot
5
+ {
6
+ public TreeMap <String ,String > quesResponseMap =null ;
7
+ public TreeMap <String ,String > localQuesResponseMap =null ;
8
+ public Set <String > questions =null ;
9
+ public Collection <String > answers =null ;
10
+ public String botAnswer ="" ;
11
+ public String chatHistory ="" ;
12
+ public String username ="" ;
13
+
14
+ public static final String BOT_INTRO ="Hi! I am ChatBot 1.0 \n Enter your name ...\n " ;
15
+ public static final String SORRY_RESPONSE = "Sorry! I did't get you." ;
16
+ public static final String SYSTEM_ERROR ="Sorry! there is an error in my system." ;
17
+ public static final String LEARN_REQUEST ="WHat should be my answer to this statement?" ;
18
+ public static final String EXIT ="BYE! Have a nice day." ;
19
+ public static final String DATABASE ="KnowledgeBase.txt" ;
20
+ public static final String BOT_NAME ="Bot : " ;
21
+
22
+
23
+ public ChatBot () throws Exception
24
+ {
25
+ this .quesResponseMap =KnowledgeBase .getKnowledgeBase (ChatBot .DATABASE );
26
+ this .localQuesResponseMap = new TreeMap <String ,String >();
27
+ if (this .quesResponseMap == null )
28
+ this .botAnswer =ChatBot .SORRY_RESPONSE ;
29
+ else
30
+ {
31
+ questions = quesResponseMap .keySet ();
32
+ answers = quesResponseMap .values ();
33
+ }
34
+ }
35
+
36
+ public String saveQuestionAndResponse (String question , String response )
37
+ {
38
+ this .quesResponseMap .put (question ,response );
39
+ this .localQuesResponseMap .put (question ,response );
40
+ this .botAnswer ="Okey. Got it!" ;
41
+ return this .botAnswer ;
42
+ }
43
+
44
+ public String getResponse (String question )
45
+ {
46
+ double globalRank =0.200 ;
47
+ String localQuestion ="" ;
48
+ try {
49
+ for (String ques :questions )
50
+ {
51
+ double localRank =StringSimilarity .similarity (ques ,question );
52
+
53
+ if (globalRank <localRank )
54
+ {
55
+ globalRank = localRank ;
56
+ localQuestion =ques ;
57
+ }
58
+ }
59
+
60
+ if (localQuestion .trim ()!="" ){
61
+ this .botAnswer =quesResponseMap .get (localQuestion );
62
+ if (this .botAnswer .trim ().toLowerCase ().contains ("timedate" )) {this .botAnswer = new Date ().toString ();}
63
+ }
64
+ else
65
+ this .botAnswer =ChatBot .SORRY_RESPONSE +" " +ChatBot .LEARN_REQUEST ;
66
+ }catch (Exception e )
67
+ {
68
+ this .botAnswer =ChatBot .SYSTEM_ERROR ;
69
+ }
70
+ return this .botAnswer ;
71
+ }
72
+
73
+
74
+ public void checkExit (String exitStmt ) throws Exception
75
+ {
76
+ if (exitStmt .toLowerCase ().contains ("exit" ))
77
+ {
78
+ this .chatHistory += ChatBot .BOT_NAME +ChatBot .EXIT ;
79
+ System .out .print (ChatBot .BOT_NAME +ChatBot .EXIT );
80
+ KnowledgeBase .saveNewKnowledge (this .localQuesResponseMap ,ChatBot .DATABASE );
81
+ KnowledgeBase .saveLogs (this .chatHistory ,this .username );
82
+ System .exit (0 );
83
+ }
84
+ }
85
+
86
+ public static void main (String arg []) throws Exception
87
+ {
88
+ Scanner sc = new Scanner (System .in );
89
+ ChatBot cb = new ChatBot ();
90
+ System .out .println (ChatBot .BOT_INTRO );
91
+ cb .username =sc .nextLine ();
92
+ cb .chatHistory += "\n \n " +ChatBot .BOT_NAME +"Hi " +cb .username +". How can I help you?\r \n " ;
93
+ System .out .println ("\n \n " +ChatBot .BOT_NAME +"Hi " +cb .username +". How can I help you?" );
94
+ while (true )
95
+ {
96
+ System .out .print (cb .username +" : " );
97
+ String question = sc .nextLine ();
98
+ cb .chatHistory += cb .username +" : " +question +"\r \n " ;
99
+ cb .checkExit (question .trim ());
100
+ String response = cb .getResponse (question );
101
+ cb .chatHistory += ChatBot .BOT_NAME +response +"\r \n " ;
102
+ System .out .println (ChatBot .BOT_NAME +response );
103
+ if (response .toLowerCase ().contains ("sorry" ))
104
+ {
105
+ System .out .print (cb .username +" : " );
106
+ response = sc .nextLine ();
107
+ cb .chatHistory += cb .username +" : " +response ;
108
+ cb .checkExit (response .trim ());
109
+ cb .chatHistory += ChatBot .BOT_NAME +cb .saveQuestionAndResponse (question ,response )+"\r \n " ;
110
+ System .out .println (ChatBot .BOT_NAME +cb .saveQuestionAndResponse (question ,response ));
111
+ }
112
+
113
+ }
114
+
115
+ }
116
+
117
+
118
+
119
+
120
+
121
+ }
0 commit comments