-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.js
340 lines (308 loc) · 8.65 KB
/
inference.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* Input must match this state:
* All PLURAL-NOUN are PLURAL-NOUN.
* No PLURAL-NOUN are PLURAL-NOUN.
* Some PLURAL-NOUN are PLURAL-NOUN.
* Are All PLURAL-NOUN PLURAL-NOUN?
* Are Some PLURAL-NOUN PLURAL-NOUN?
* Are No PLURAL-NOUN PLURAL-NOUN?
* Describe PLURAL-NOUN.
*
*
* TODO:
* transitive descriptions
* conflicting truths
*
*
* a <= b == all b are a
* a <- b == some b are a
* a </ b == no b are a
*
* Tree Structure:
* Mammals <= Dogs <= Beagles </ Poodles
*
*/
(function(){
/**
* http://stackoverflow.com/a/1961068 unique array elements
* @return {[type]} [description]
*/
function getUnique (arr){
var u = {}, a = [];
for(var i = 0, l = arr.length; i < l; ++i){
if(u.hasOwnProperty(arr[i])) {
continue;
}
a.push(arr[i]);
u[arr[i]] = 1;
}
return a;
};
/**
* Alias to console.log, as well as adding a statement to the #log on the web page
* @param {string} argument what you want to log
* @return {}
*/
function clog (argument,classes) {
console.log(argument);
$("#log").prepend("<div class='message "+classes+"'>"+argument+"</div>");
}
/**
* looks for a needle within a haystack, one level deep
* @param {object} needle what is being asked for
* @param {array} haystack where to look
* @return {boolean} returns if object was found
*/
function in_array(needle, haystack){
for(var key in haystack){
if(needle === haystack[key]){
return true;
}
}
return false;
}
/**
* given an array, remove non letters, and set letters to lower case
* This allows the parser to lessen its scope
* @param {array} arr array of string to parse over
* @return {}
*/
function clean(arr){
for(var key in arr){
var statement = arr[key];
statement = statement.toLowerCase();
arr[key] = statement.replace(/\W/g, '');
}
}
/**
* prints everythin in the given list, assumes list contains strings
* @param {array[string]} list array to print
* @return {}
*/
function printlist(list){
for(var key in list){
clog(list[key]);
}
}
function swapRelationalWord(word){
if(word === "all"){
return "no";
} else if(word === "no"){
return "all";
} else {
return word;
}
}
/**
* Node in the object graph
* @param {string} object noun
*/
ObjectMap = function(object){
this.thing = object;
this.are = [];
this.may = [];
this.not = [];
};
/**
* getter of noun
* @return {string} noun that this ObjectMap was initialized with
*/
ObjectMap.prototype.getSubject = function(){
return this.thing;
};
ObjectMap.prototype.getAres = function(){
return this.are;
};
ObjectMap.prototype.getMays = function(){
return this.may;
};
ObjectMap.prototype.getNots = function(){
return this.not;
};
ObjectMap.prototype.relation = function(relation,thing){
var msg = "Something went wrong adding the relation";
if(relation === "all"){
if(_i.tQuery(thing,relation,this.thing,true) === 1){
msg = "That is a conflicting statement";
} else if(!in_array(thing,this.are)){
msg = "OK";
this.are.push(thing);
} else {
msg = "I already knew that";
}
} else if (relation === "no"){
if(!in_array(thing,this.not)){
msg = "OK";
this.not.push(thing);
} else {
msg = "I already knew that";
}
} else if (relation === "some"){
if(!in_array(thing,this.may)){
msg = "OK";
this.may.push(thing);
} else {
msg = "I already knew that";
}
}
return msg;
};
// all can be worked on
ObjectMap.prototype.transitiveQuery = function(query,relation){
if(relation === "all"){
if( in_array(query,this.are) ){
return 1;
} else {
for(var i = 0; i < this.are.length; i++){
return _i.findOrAdd(this.are[i]).transitiveQuery(query,relation);
}
}
} else if (relation === "no"){
if( in_array(query,this.not) ){
return 1;
} else {
for(var i = 0; i < this.not.length; i++){
return _i.findOrAdd(this.not[i]).transitiveQuery(query,relation);
}
return -1;
}
} else if (relation === "some"){
if( in_array(query,this.may) ){
return 1;
} else {
for(var i = 0; i < this.may.length; i++){
return _i.findOrAdd(this.may[i]).transitiveQuery(query,relation);
}
}
}
return 0;
};
ObjectMap.prototype.containsInfoOn = function(subject){
return in_array(subject,this.are.concat(this.may.concat(this.not)));
};
InferenceEngine = function(){
this.descWords = ["describe"];
this.questionWords = ["are"];
this.additiveWords = ["all","no","some"];
this.breakWords = ["are"];
this.stateTree = [];
};
InferenceEngine.prototype.statement = function(statement) {
clog(statement,'statement');
var blocks = statement.split(" ");
clean(blocks);
if( this.checkQuerySyntax(blocks) === true){
return clog(this.tQuery(blocks[2],blocks[1],blocks[3]));
} else if( this.checkDescSyntax(blocks) === true){
return printlist(this.describe(blocks[1]));
} else if(this.checkAddSyntax(blocks) === true){
var obj = this.findOrAdd(blocks[1]);
var relative = this.findOrAdd(blocks[3]);
if(relative.transitiveQuery(blocks[0],obj) === 0){
return clog(obj.relation(blocks[0],blocks[3]));
} else {
return clog("?");
}
}
return clog("Invalid syntax: '"+statement+"'");
};
InferenceEngine.prototype.checkFirst = function(blocks){
var word = blocks[0];
return in_array(word,this.additiveWords);
};
InferenceEngine.prototype.checkAre = function(blocks){
return in_array(this.breakWords[0],blocks);
};
InferenceEngine.prototype.checkAddSyntax = function(blocks){
return this.checkFirst(blocks) && this.checkAre(blocks) && blocks.length === 4;
};
InferenceEngine.prototype.checkQuerySyntax = function(blocks){
return in_array(blocks[0],this.questionWords) && blocks.length === 4;
};
InferenceEngine.prototype.checkDescSyntax = function(blocks){
return in_array(blocks[0],this.descWords) && blocks.length === 2;
};
InferenceEngine.prototype.findOrAdd = function(object){
if(typeof(this.stateTree[object]) === "undefined"){
this.stateTree[object] = new ObjectMap(object);
}
return this.stateTree[object];
};
InferenceEngine.prototype.tQuery = function(object, relation, query,num){
var retVal = this.findOrAdd(object).transitiveQuery(query,relation);
if(num === true) return retVal;
if( retVal === 0){
return "I dont have enough information to go off of...";
} else if (retVal === 1){
return "Yes, " + relation + " " + object + " are " + query;
} else if (retVal === -1){
return "No, " + (relation === "no" ? "some" : "not all") + " " + object + " are " + query;
}
};
InferenceEngine.prototype.describe = function(object, topQuery){
var subject = this.findOrAdd(object);
var list = [];
var noun= topQuery || object;
for(var i = 0; i < subject.getAres().length; i ++){
list.push("All " + noun + " are "+ subject.getAres()[i] + ".");
list = list.concat(_i.describe(subject.getAres()[i],noun));
}
for(var i = 0; i < subject.getMays().length; i ++){
list.push("Some " + noun + " are "+ subject.getMays()[i] + ".");
list = list.concat(_i.describe(subject.getMays()[i],noun));
}
for(var i = 0; i < subject.getNots().length; i ++){
list.push("No " + noun + " are "+ subject.getNots()[i] + ".");
list = list.concat(_i.describe(subject.getNots()[i],noun));
}
list = getUnique(list);
return list;
};
window._i = new InferenceEngine();
})();
_i.statement("All mammals are hairy.");
_i.statement("All dogs are mammals.");
_i.statement("All beagles are dogs.");
_i.statement("Are all beagles hairy?");
_i.statement("All cats are mammals.");
_i.statement("All cats are hairy.");
_i.statement("Are all cats dogs?");
// I don't know.
_i.statement("No cats are dogs.");
_i.statement("Are all cats dogs?");
// No, not all cats are dogs.
_i.statement("Are no cats dogs?");
// Yes, no cats are dogs.
_i.statement("All mammals are dogs.");
// Sorry, that contradicts what I already know.
_i.statement("Some mammals are brown.");
_i.statement("Are some mammals dogs?");
// Yes, some mammals are dogs.
_i.statement("Are some dogs brown?");
// I don't know.
_i.statement("Some dogs are brown.");
_i.statement("Are some dogs brown?");
// Yes, some dogs are brown.
_i.statement("Describe dogs.");
// All dogs are mammals.
// All dogs are hairy.
// No dogs are cats.
// Some dogs are beagles.
// Some dogs are brown animals.
// Some dogs are brown things.
_i.statement("Are all goldfish mammals?");
// I don't know anything about goldfish.
/**
* All mammals are hairy
* all dogs are mammals
* all beagles are dogs
* some dogs are gray
* no mammals are blue
* describe dogs
* all dogs are mammals
* all dogs are hairy
* no dogs are blue
* some dogs are beagles
* some dogs are gray
*
*/