-
Notifications
You must be signed in to change notification settings - Fork 2
/
Logic.java
135 lines (121 loc) · 4.47 KB
/
Logic.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
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
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Logic {
SmartBot b;
Communication c;
Memory m;
/**
* Trust starts at 1.0 because as a baby, our minds are impressionable,
* similarly, this bot trusts everything you say at first... but once you say
* something false, the trust value will decrease by the set constant decrement.
* value.
*/
double trust = 1.0;
final double DECREMENT = .01;
public Logic (SmartBot bot, Communication com, Memory mem) {
b = bot;
c = com;
m = mem;
}
public void interpret(String s) {
if (isDebugCommand(s)) {
interpretAsDebugCommand(s);
return;
}
m.appendTempLog(s);
if (isQuestion(s)) {
Pattern pattern;
Matcher matcher;
for(String delimiter : m.getDelimeters()) {
pattern = Pattern.compile(delimiter + " (.*)\\?");
matcher = pattern.matcher(s);
if (matcher.find()) {
ArrayList<String[]> relations = m.getRelations(matcher.group(1), delimiter);
if (relations != null) {
c.say((getHighestConfidenceRelation(relations))[0]);
} else {
c.say("I don't know.");
}
return;
}
}
} else if (isExclamation(s)) {
Pattern pattern;
Matcher matcher;
for(String delimiter : m.getDelimeters()) {
pattern = Pattern.compile("(.*) " + delimiter + " ([a-zA-Z1-9]*)\\.?");
matcher = pattern.matcher(s);
if (matcher.find()) {
m.addNewRelation(matcher.group(1), delimiter, matcher.group(2), trust, 0.7, true);
//c.say((getHighestConfidenceRelation(m.getRelations(matcher.group(1), delimiter)))[0]);
c.say(s);
return;
}
}
} else {
// Treat as a Statement.
Pattern pattern;
Matcher matcher;
for(String delimiter : m.getDelimeters()) {
pattern = Pattern.compile("(.*) " + delimiter + " ([a-zA-Z1-9]*)\\.?");
matcher = pattern.matcher(s);
if (matcher.find()) {
m.addNewRelation(matcher.group(1), delimiter, matcher.group(2), trust, 0.5, true);
//c.say((getHighestConfidenceRelation(m.getRelations(matcher.group(1), delimiter)))[0]);
c.say(s);
return;
}
}
}
// Imitate/Mimick user input if it does not understand.
c.say(s);
}
public static boolean isQuestion(String s) {
return s.endsWith("?");
}
public static boolean isExclamation(String s) {
return s.endsWith("!");
}
public static boolean isDebugCommand(String s) {
return s.startsWith("$");
}
public void interpretAsDebugCommand(String s) {
if (s.contains("sleep")) {
b.sleep();
} else if (s.contains("log")) {
c.say(m.tempLogToString());
} else if (s.contains("temporary relations")) {
c.say(m.relationsToString(true));
} else if (s.contains("relations")) {
c.say(m.relationsToString(false));
}
}
public String[] getHighestConfidenceRelation(ArrayList<String[]> list) {
String[] result = list.get(0);
for (int i = 1; i < list.size(); i++) {
if (Double.parseDouble((list.get(i))[1]) > Double.parseDouble(result[1])) {
result = list.get(i);
}
}
return result;
}
public static int levenshtein_distance(String a, String b) {
a = a.toLowerCase();
b = b.toLowerCase();
// i == 0
int [] costs = new int [b.length() + 1];
for (int j = 0; j < costs.length; j++)
costs[j] = j;
for (int i = 1; i <= a.length(); i++) {
costs[0] = i;
int nw = i - 1;
for (int j = 1; j <= b.length(); j++) {
int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
nw = costs[j];
costs[j] = cj;
}
}
return costs[b.length()];
}
}