-
Notifications
You must be signed in to change notification settings - Fork 2
/
Predicates.java
109 lines (98 loc) · 3.88 KB
/
Predicates.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
package org.alicebot.ab;
/* Program AB Reference AIML 2.0 implementation
Copyright (C) 2013 ALICE A.I. Foundation
Contact: [email protected]
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
import org.alicebot.ab.utils.JapaneseUtils;
import java.io.*;
import java.util.HashMap;
/**
* Manage client predicates
*
*/
public class Predicates extends HashMap<String, String> {
/**
* save a predicate value
*
* @param key predicate name
* @param value predicate value
* @return predicate value
*/
public String put(String key, String value) {
//MagicBooleans.trace("predicates.put(key: " + key + ", value: " + value + ")");
if (MagicBooleans.jp_tokenize) {
if (key.equals("topic")) value = JapaneseUtils.tokenizeSentence(value);
}
if (key.equals("topic") && value.length()==0) value = MagicStrings.default_get;
if (value.equals(MagicStrings.too_much_recursion)) value = MagicStrings.default_list_item;
// MagicBooleans.trace("Setting predicate key: " + key + " to value: " + value);
String result = super.put(key, value);
//MagicBooleans.trace("in predicates.put, returning: " + result);
return result;
}
/**
* get a predicate value
*
* @param key predicate name
* @return predicate value
*/
public String get(String key) {
//MagicBooleans.trace("predicates.get(key: " + key + ")");
String result = super.get(key);
if (result == null) result = MagicStrings.default_get;
//MagicBooleans.trace("in predicates.get, returning: " + result);
return result;
}
/**
* Read predicate default values from an input stream
*
* @param in input stream
*/
public void getPredicateDefaultsFromInputStream (InputStream in) {
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
try {
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
if (strLine.contains(":")) {
String property = strLine.substring(0, strLine.indexOf(":"));
String value = strLine.substring(strLine.indexOf(":")+1);
put(property, value);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
/** read predicate defaults from a file
*
* @param filename name of file
*/
public void getPredicateDefaults (String filename) {
try{
// Open the file that is the first
// command line parameter
File file = new File(filename);
if (file.exists()) {
FileInputStream fstream = new FileInputStream(filename);
// Get the object
getPredicateDefaultsFromInputStream(fstream);
fstream.close();
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}