-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenlighten.js
More file actions
112 lines (99 loc) · 2.91 KB
/
Copy pathenlighten.js
File metadata and controls
112 lines (99 loc) · 2.91 KB
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
var enlighten = {qualifiers: [], db: {}};
enlighten.help = function () {
console.info('Teach me with node("Thing", qualifier, "Thing");')
console.info('Get my confidence with con("Thing", qualifier, "Thing");')
console.info('Available qualifiers:', enlighten.qualifiers);
};
// Bidirectional qualifiers
var bi = {
'is_a': 'attribute_of', // Cat is a mammal. Mammal is an attribute of cat.
'is_part_of': 'has_a', // Mouth is part of cat. Cat has a mouth.
'causes': 'caused_by', // Fire causes burning. Burning is caused by fire.
'can_cause': 'can_be_caused_by', // Less confident version
'uses': 'used_for', // Books are used for education. Education uses books.
'can_use': 'can_be_used_for', // Less confident version
'relates_to': 'relates_to' // Ambiguous relationship
};
Object.keys(bi).forEach(function (key1) {
var key2 = bi[key1];
window[key1] = {$fwd: key1, $rev: key2};
window[key2] = {$fwd: key2, $rev: key1};
if (enlighten.qualifiers.indexOf(key1) === -1) {
enlighten.qualifiers.push(key1);
}
if (enlighten.qualifiers.indexOf(key2) === -1) {
enlighten.qualifiers.push(key2);
}
});
function node () {
var args = Array.prototype.slice.apply(arguments);
/**
* Forward
*/
var current = args[0];
var next;
if (typeof current !== 'string') {
throw new Error('Must start with a concept (string)');
}
if (!enlighten.db[current]) {
enlighten.db[current] = [];
}
current = enlighten.db[current];
for (var i=1; i < args.length; i++) {
/**
* Look for existing qualifier
*/
var next = false;
for (var j=0; j < current.length; j++) {
if (current[j][0] === args[i]) {
next = current[j][1];
}
}
/**
* Not found
*/
if (next === false) {
next = [];
current.push([args[i], next]);
}
current = next;
}
/**
* Reverse
*/
var current = args[args.length - 1];
var next;
if (typeof current !== 'string') {
throw new Error('Must end with a concept (string)');
}
if (!enlighten.db[current]) {
enlighten.db[current] = [];
}
current = enlighten.db[current];
for (var i=args.length - 2; i >= 0; i--) {
/**
* Look for existing qualifier
*/
var next = false;
for (var j=0; j < current.length; j++) {
if (current[j][0] === args[i]) {
next = current[j][1];
}
}
/**
* Not found
*/
if (next === false) {
next = [];
current.push([args[i], next]);
}
current = next;
}
return JSON.stringify([args[0], enlighten.db[args[0]]]);
};
function con () {
console.log('Confidence');
};
function why () {
console.log('Why');
};