-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnlp.js
More file actions
270 lines (203 loc) · 6.65 KB
/
nlp.js
File metadata and controls
270 lines (203 loc) · 6.65 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
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
var allowed_shape_names = [
{ id: 'circle', text: 'circle' },
{ id: 'square', text: 'square' },
{ id: 'triangle', text: 'triangle'}
]
var allowed_move_directions = [
{ id: 'up', text: 'up' },
{ id: 'up', text: 'above' },
{ id: 'down', text: 'down' },
{ id: 'down', text: 'below' },
{ id: 'left', text: 'left' },
{ id: 'left', text: 'backward' },
{ id: 'left', text: 'backwards' },
{ id: 'right', text: 'right' },
{ id: 'right', text: 'forward' },
{ id: 'right', text: 'forwards' }
]
var allowed_color_values = [
{ id: 'red', text: 'red' },
{ id: 'yellow', text: 'yellow' },
{ id: 'green', text: 'green' },
{ id: 'black', text: 'black' },
{ id: 'blue', text: 'blue' },
]
var allowed_resize_directions = [
{ id: 'increase', text: 'increase' },
{ id: 'increase', text: 'enhance' },
{ id: 'increase', text: 'bigger' },
{ id: 'increase', text: 'big' },
{ id: 'increase', text: 'large' },
{ id: 'increase', text: 'larger' },
{ id: 'decrease', text: 'decrease' },
{ id: 'decrease', text: 'reduce' },
{ id: 'decrease', text: 'decrease' },
{ id: 'decrease', text: 'smaller' },
{ id: 'decrease', text: 'small' }
]
function acceptString(test_string){
if (window.Bravey === undefined) {
throw "Bravey is not available";
}
var nlp = new Bravey.Nlp.Fuzzy();
// adding intents one by one
// draw shapes
{
nlp.addIntent('draw_shape', [
{ entity: 'shape_name', id: 'shape_name' },
{ entity: 'shape_value', id: 'shape_value' },
]);
let shape_name = new Bravey.StringEntityRecognizer('shape_name');
for (let each of allowed_shape_names) {
shape_name.addMatch(each.id, each.text)
}
nlp.addEntity(shape_name);
let shape_value = new Bravey.NumberEntityRecognizer('shape_value');
nlp.addEntity(shape_value);
// train with some examples
nlp.addDocument(
'Draw a {shape_name} with value {shape_value}',
'draw_shape'
);
nlp.addDocument(
'Create a {shape_name} of value {shape_value}',
'draw_shape'
);
}
// movement
{
nlp.addIntent('move_shape', [
{ entity: 'move_name', id: 'move_name' },
{ entity: 'move_direction', id: 'move_direction' },
{ entity: 'move_value', id: 'move_value' }
]);
let move_name = new Bravey.StringEntityRecognizer('move_name');
Object.keys(objectList).forEach(function(key){
move_name.addMatch(key,key);
console.log(key);
});
nlp.addEntity(move_name);
let move_direction = new Bravey.StringEntityRecognizer('move_direction');
for (let each of allowed_move_directions) {
move_direction.addMatch(each.id, each.text)
}
nlp.addEntity(move_direction);
let move_value = new Bravey.NumberEntityRecognizer('move_value');
nlp.addEntity(move_value);
// train with some examples
nlp.addDocument(
'Move {move_name} to the {move_direction} by {move_value}',
'move_shape'
);
nlp.addDocument(
'Shift {move_name} {move_direction} by {move_value}',
'move_shape'
);
}
// resize
{
nlp.addIntent('resize_shape', [
{ entity: 'resize_name', id: 'resize_name' },
{ entity: 'resize_direction', id: 'resize_direction' },
{ entity: 'resize_value', id: 'resize_value' }
]);
let resize_name = new Bravey.StringEntityRecognizer('resize_name');
Object.keys(objectList).forEach(function(key){
resize_name.addMatch(key,key);
console.log(key);
});
nlp.addEntity(resize_name);
let resize_direction = new Bravey.StringEntityRecognizer('resize_direction');
for (let each of allowed_resize_directions) {
resize_direction.addMatch(each.id, each.text)
}
nlp.addEntity(resize_direction);
let resize_value = new Bravey.NumberEntityRecognizer('resize_value');
nlp.addEntity(resize_value);
// train with some examples
nlp.addDocument(
'{resize_direction} size of {resize_name} by {resize_value}',
'resize_shape'
);
nlp.addDocument(
'Change size of {resize_name} to {resize_value} times {resize_direction}',
'resize_shape'
);
nlp.addDocument(
'Resize the {resize_name} by {resize_value} times',
'resize_shape'
);
}
// rotate
{
nlp.addIntent('rotate_shape', [
{ entity: 'rotate_name', id: 'rotate_name' },
{ entity: 'rotate_value', id: 'rotate_value' }
]);
let rotate_name = new Bravey.StringEntityRecognizer('rotate_name');
Object.keys(objectList).forEach(function(key){
rotate_name.addMatch(key,key);
console.log(key);
});
nlp.addEntity(rotate_name);
let rotate_value = new Bravey.NumberEntityRecognizer('rotate_value');
nlp.addEntity(rotate_value);
// train with some examples
nlp.addDocument(
'Rotate the {rotate_name} by {rotate_value} radians',
'rotate_shape'
);
nlp.addDocument(
'Rotate {rotate_name} by {rotate_value} degrees',
'rotate_shape'
);
}
// fill color
{
nlp.addIntent('color_shape', [
{ entity: 'color_name', id: 'color_name' },
{ entity: 'color_value', id: 'color_value' }
]);
let color_name = new Bravey.StringEntityRecognizer('color_name');
Object.keys(objectList).forEach(function(key){
color_name.addMatch(key,key);
console.log(key);
});
nlp.addEntity(color_name);
let color_value = new Bravey.StringEntityRecognizer('color_value');
for (let each of allowed_color_values) {
color_value.addMatch(each.id, each.text)
}
nlp.addEntity(color_value);
//train with some examples
nlp.addDocument(
'Fill color of {color_name} by {color_value}',
'color_shape'
);
nlp.addDocument(
'Change the color of {color_name} to {color_value}',
'color_shape'
);
nlp.addDocument(
'Paint {color_name} {color_value}',
'color_shape'
);
}
{
nlp.addDocument('Clear canvas','clear_shape',{ fromFullSentence: true, expandIntent: true });
nlp.addDocument('Clear the screen','clear_shape',{ fromFullSentence: true, expandIntent: true });
}
{
nlp.addDocument('Save file','save_shape',{ fromFullSentence: true, expandIntent: true });
nlp.addDocument('Save the screen','save_shape',{ fromFullSentence: true, expandIntent: true });
}
showResults(nlp.test(test_string));
}
function showResults(result) {
if (result) {
console.log(result);
listener(result);
} else {
console.log('something failed here')
}
}