-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathKnowledgeBase.java
57 lines (52 loc) · 1.72 KB
/
KnowledgeBase.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
import java.util.*;
import java.io.*;
import java.text.*;
public class KnowledgeBase
{
public static TreeMap<String,String> quesResponseMap=null;
public static TreeMap<String,String> getKnowledgeBase(String fileName) throws Exception
{
quesResponseMap = new TreeMap<String,String>();
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
raf.seek(0);
while(raf.getFilePointer()<raf.length())
{
String line= raf.readLine().trim();
if(!line.trim().isEmpty()){
String[] lines = line.split("~");
String question = lines[0];
String answer = lines[1];
quesResponseMap.put(question,answer);
}
}
raf.close();
return quesResponseMap;
}
public static void saveNewKnowledge(TreeMap<String,String> newKnowledge ,String fileName) throws Exception
{
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
long fileLength = raf.length();
raf.seek(fileLength);
String toBeWritten="";
for(Map.Entry<String,String> entry : newKnowledge.entrySet()) {
String key = entry.getKey().trim();
String value = entry.getValue().trim();
toBeWritten += key+" ~ "+value+"\r\n";
}
raf.writeBytes(toBeWritten);
raf.close();
}
public static void saveLogs(String chat,String username) throws Exception
{
File dir = new File(System.getProperty("user.dir")+"/logs");
dir.mkdir();
Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;
String fileName="Chat History with "+username+" on "+dateFormat.format(date)+".txt";
File file = new File(dir,fileName);
file.createNewFile();
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.writeBytes(chat);
raf.close();
}
}