Skip to content

Commit 28c00e0

Browse files
Add files via upload
1 parent 3960ff1 commit 28c00e0

File tree

5 files changed

+237
-0
lines changed

5 files changed

+237
-0
lines changed

Capture.PNG

9.76 KB
Loading

ChatBot.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 \nEnter 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+
}

KnowledgeBase.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.*;
2+
import java.io.*;
3+
import java.text.*;
4+
5+
public class KnowledgeBase
6+
{
7+
public static TreeMap<String,String> quesResponseMap=null;
8+
9+
public static TreeMap<String,String> getKnowledgeBase(String fileName) throws Exception
10+
{
11+
quesResponseMap = new TreeMap<String,String>();
12+
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
13+
raf.seek(0);
14+
while(raf.getFilePointer()<raf.length())
15+
{
16+
String line= raf.readLine().trim();
17+
if(!line.trim().isEmpty()){
18+
String[] lines = line.split("~");
19+
String question = lines[0];
20+
String answer = lines[1];
21+
quesResponseMap.put(question,answer);
22+
}
23+
}
24+
raf.close();
25+
return quesResponseMap;
26+
}
27+
28+
public static void saveNewKnowledge(TreeMap<String,String> newKnowledge ,String fileName) throws Exception
29+
{
30+
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
31+
long fileLength = raf.length();
32+
raf.seek(fileLength);
33+
String toBeWritten="\n";
34+
for(Map.Entry<String,String> entry : newKnowledge.entrySet()) {
35+
String key = entry.getKey();
36+
String value = entry.getValue();
37+
toBeWritten += key+" ~ "+value+"\r\n";
38+
}
39+
raf.writeBytes(toBeWritten);
40+
raf.close();
41+
}
42+
43+
public static void saveLogs(String chat,String username) throws Exception
44+
{
45+
46+
File dir = new File(System.getProperty("user.dir")+"/logs");
47+
dir.mkdir();
48+
Date date = new Date() ;
49+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;
50+
String fileName="Chat History with "+username+" on "+dateFormat.format(date)+".txt";
51+
File file = new File(dir,fileName);
52+
file.createNewFile();
53+
RandomAccessFile raf = new RandomAccessFile(file, "rw");
54+
raf.writeBytes(chat);
55+
raf.close();
56+
}
57+
58+
}

KnowledgeBase.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
How are You ~ I am Fine. What About you?
2+
What is your name ~ My name is ChatBot 1.0
3+
where do you live ~ I live in your system
4+
I am Fine ~ That's Good.
5+
6+
7+
8+
9+
10+
11+
12+

StringSimilarity.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.Scanner;
2+
public class StringSimilarity {
3+
4+
public static double similarity(String s1, String s2) {
5+
String longer = s1, shorter = s2;
6+
if (s1.length() < s2.length()) {
7+
longer = s2; shorter = s1;
8+
}
9+
int longerLength = longer.length();
10+
if (longerLength == 0) { return 1.0; }
11+
return (longerLength - editDistance(longer, shorter)) / (double) longerLength;
12+
}
13+
14+
15+
public static int editDistance(String s1, String s2) {
16+
s1 = s1.toLowerCase();
17+
s2 = s2.toLowerCase();
18+
19+
int[] costs = new int[s2.length() + 1];
20+
for (int i = 0; i <= s1.length(); i++) {
21+
int lastValue = i;
22+
for (int j = 0; j <= s2.length(); j++) {
23+
if (i == 0)
24+
costs[j] = j;
25+
else {
26+
if (j > 0) {
27+
int newValue = costs[j - 1];
28+
if (s1.charAt(i - 1) != s2.charAt(j - 1))
29+
newValue = Math.min(Math.min(newValue, lastValue),
30+
costs[j]) + 1;
31+
costs[j - 1] = lastValue;
32+
lastValue = newValue;
33+
}
34+
}
35+
}
36+
if (i > 0)
37+
costs[s2.length()] = lastValue;
38+
}
39+
return costs[s2.length()];
40+
}
41+
42+
public static void printSimilarity(String s, String t) {
43+
System.out.println(String.format(
44+
"%.3f is the similarity between \"%s\" and \"%s\"", similarity(s, t), s, t));
45+
}
46+
}

0 commit comments

Comments
 (0)