-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcommands.genie
More file actions
512 lines (447 loc) · 23.5 KB
/
Copy pathcommands.genie
File metadata and controls
512 lines (447 loc) · 23.5 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2020 The Board of Trustees of the Leland Stanford Junior University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
import { Ast, Type } from 'thingtalk';
import * as C from './ast_manip';
import ThingpediaLoader from './load-thingpedia';
forward_get_do_command : Ast.ChainExpression = {
// three forms:
// "get an instagram picture and post it on twitter" (implicit)
// "get the caption of an instagram picture and post it on twitter" (by projection)
// "get an instagram picture and post the caption on twitter" (by param name)
( ('get' | 'take' | 'retrieve') table:if_filtered_table ('and then' | 'then') action:action_coref_same_sentence with { corefType = table.implicitParamPassingType }
| table:complete_get_command ('and then' | 'then' | ',') action:action_coref_same_sentence with { corefType = table.implicitParamPassingType }
| 'after' ('you get' | 'taking' | 'getting' | 'retrieving') table:with_filtered_table action:action_coref_same_sentence with { corefType = table.implicitParamPassingType }
| ('get' | 'take' | 'retrieve') table:with_filtered_table ('and then' | 'then') action:action_coref_same_sentence with { corefType = table.implicitParamPassingType }
| ('get' | 'take' | 'retrieve') table:projection_Any ('and then' | 'then') action:action_coref_same_sentence with { corefType = table.projectionType }
| 'after' ('you get' | 'taking' | 'getting' | 'retrieving') table:projection_Any action:action_coref_same_sentence with { corefType = table.projectionType }
| ('get' | 'take' | 'retrieve') table:if_filtered_table ('and then' | 'then') action:action_coref_same_sentence with { corefFunctionName = table.functionName }
| table:complete_get_command ('and then' | 'then' | ',') action:action_coref_same_sentence with { corefFunctionName = table.functionName }
| 'after' ('you get' | 'taking' | 'getting' | 'retrieving') table:with_filtered_table action:action_coref_same_sentence with { corefFunctionName = table.functionName }
| ('get' | 'take' | 'retrieve') table:with_filtered_table ('and then' | 'then') action:action_coref_same_sentence with { corefFunctionName = table.functionName }
) => C.addParameterPassing(table, action);
// use X to do Y would be good sometimes but it gets confusing quickly
//['${(get|use)} ${with_filtered_table} to ${thingpedia_action}', checkIfIncomplete(simpleCombine((table, action) => new Ast.Command(null, table, [action])))]
!turking command:forward_get_do_command ('with the same' | 'with identical' | 'using the same') joinArg:out_param_Any with { functionName = command.functionName }
=> C.addSameNameParameterPassing($loader, command, joinArg);
}
backward_get_do_command : Ast.ChainExpression = {
( action:action_coref_same_sentence 'after' ('getting' | 'taking' | 'you get' | 'you retrieve') table:with_filtered_table with { implicitParamPassingType = action.corefType }
| action:action_coref_same_sentence 'after' ('getting' | 'taking' | 'you get' | 'you retrieve') table:projection_Any with { projectionType = action.corefType }
| action:action_coref_same_sentence 'after' ('getting' | 'taking' | 'you get' | 'you retrieve') table:with_filtered_table with { functionName = action.corefFunctionName }
) => C.addParameterPassing(table, action);
}
forward_when_do_rule : Ast.ChainExpression = {
// no pp
stream:stream action:complete_action => C.makeChainExpression(stream, action);
stream:stream action:complete_action 'if' filter:if_filter with { functionName = stream.functionName } => {
const newStream = C.addFilter($loader, stream, filter);
if (!newStream)
return null;
return C.makeChainExpression(newStream, action);
};
// pp from when to do
// note: "monitor X and then Y" makes sense only if X flows into Y
( stream:stream action:action_coref_same_sentence with { corefType = stream.implicitParamPassingType }
| stream:stream action:action_coref_same_sentence with { corefFunctionName = stream.functionName }
) => C.addParameterPassing(stream, action);
( ('monitor' | 'watch' | 'check for new') table:with_filtered_table with { is_monitorable = true } ('and then' | 'then') action:action_coref_same_sentence with { corefType = table.implicitParamPassingType }
| ('monitor' | 'watch') table:projection_Any with { is_monitorable = true } ('and then' | 'then') action:action_coref_same_sentence with { corefType = table.projectionType }
| ('monitor' | 'watch' | 'check for new') table:with_filtered_table with { is_monitorable = true } ('and then' | 'then') action:action_coref_same_sentence with { corefFunctionName = table.functionName }
) => {
const stream = C.tableToStream(table);
if (!stream)
return null;
return C.addParameterPassing(stream, action);
};
!turking rule:forward_when_do_rule ('with the same' | 'with identical' | 'using the same') joinArg:out_param_Any with { functionName = rule.functionName }
=> C.addSameNameParameterPassing($loader, rule, joinArg);
}
backward_when_do_rule : Ast.ChainExpression = {
// no pp
action:complete_action stream:stream => C.makeChainExpression(stream, action);
// pp
( action:action_coref_same_sentence stream:stream with { implicitParamPassingType = action.corefType }
| action:action_coref_same_sentence stream:stream with { functionName = action.corefFunctionName }
) => C.addParameterPassing(stream, action);
( action:action_coref_same_sentence 'after checking for new' table:complete_table with { implicitParamPassingType = action.corefType }
| action:action_coref_same_sentence 'after checking for new' table:complete_table with { functionName = action.corefFunctionName }
) => {
const stream = C.tableToStream(table);
if (!stream)
return null;
return C.addParameterPassing(stream, action);
};
}
when_get_stream : Ast.ChainExpression = {
// no pp
( stream:stream table:complete_get_command
| stream:stream imperative_verb_for_search table:with_filtered_table
| stream:stream generic_verb_for_search table:with_filtered_table
| stream:stream imperative_verb_for_search ('' | 'what is') table:projection_Any
| stream:stream generic_verb_for_search ('' | 'what is') table:projection_Any
| table:complete_get_command stream:stream
| imperative_verb_for_search table:with_filtered_table stream:stream
| generic_verb_for_search table:with_filtered_table stream:stream
| generic_preamble_for_answer table:with_filtered_table stream:stream
| imperative_verb_for_search table:projection_Any stream:stream
| generic_verb_for_search table:projection_Any stream:stream
| generic_preamble_for_answer table:projection_Any stream:stream
) => C.combineStreamQuery($loader, stream, table);
// pp from when to get
( stream:stream table:get_command_coref_same_sentence
| stream:stream imperative_verb_for_search table:query_coref_same_sentence
| stream:stream generic_verb_for_search table:query_coref_same_sentence
) => C.addParameterPassing(stream, table);
}
when_get_do_rule : Ast.ChainExpression = {
stream:stream command:forward_get_do_command => C.combineStreamCommand(stream, command);
command:forward_get_do_command stream:stream => C.combineStreamCommand(stream, command);
}
timer_rule : Ast.ChainExpression = {
// timer => get => notify
(
timer:timer table:complete_get_command
| timer:timer generic_verb_for_search table:with_filtered_table
| generic_verb_for_search table:with_filtered_table timer:timer
| timer:timer ('tell me' | 'give me' | 'show me' | 'present' | 'retrieve' | 'pull up') table:with_filtered_table
| ('tell me' | 'give me' | 'show me' | 'present' | 'retrieve' | 'pull up') table:with_filtered_table timer:timer
) => C.makeChainExpression(timer, table);
// timer => do
(
timer:timer action:complete_action
| generic_preamble_for_action action:complete_action timer:timer
) => C.makeChainExpression(timer, action);
// timer => get => do
!primonly (
timer:timer command:forward_get_do_command
| command:forward_get_do_command timer:timer
) => C.makeChainExpression(timer, command);
}
generic_preamble_for_answer : string = {
'i am looking for';
'i would like';
'i want';
'i need';
}
generic_preamble_for_action : string = {
'i am looking to';
'i would like to';
'i want to';
'i need to';
'please';
}
generic_verb_for_search : string = {
'search for';
'search';
'find';
'get';
'retrieve';
}
imperative_verb_for_search : string = {
'show me';
'tell me';
'give me';
'find me';
}
generic_search_preamble : string = {
'search';
'find';
'get';
'show me';
'give me';
'find me';
'i want';
'i need';
'i am looking for';
'i am looking for a particular';
'i am looking for a certain';
'i would like';
'i need';
'i need some information about';
'i would like information about';
'i would like to hear about';
}
generic_lookup_preamble : string = {
'show me';
'tell me';
'give me';
'find me';
'search';
'find';
'i want to know';
'i need to know';
'i am looking for';
'i would like to know';
'i would like';
'i need';
}
generic_article : string = {
'a';
'an';
'the';
'some';
'any';
'all';
'all the';
'every';
'a list of';
'a list of all';
'the list of';
'the list of all';
}
search_command : Ast.Expression = {
(
table:complete_get_command
| 'get' generic_article table:with_filtered_table
| generic_preamble_for_answer generic_article table:with_filtered_table
| generic_verb_for_search generic_article table:with_filtered_table
| generic_preamble_for_action generic_verb_for_search generic_article table:with_filtered_table
) => table;
( 'please' | '') ('list' | 'enumerate') table:with_filtered_table with { is_list = true } => table;
// now => get join get => notify
( generic_preamble_for_answer generic_article table:with_filtered_table
| generic_verb_for_search generic_article table:with_filtered_table
| generic_preamble_for_action generic_verb_for_search generic_article table:with_filtered_table
) => table;
// now => get => notify (argmin, argmax)
( generic_preamble_for_answer 'the' table:with_arg_min_max_table
| generic_verb_for_search 'the' table:with_arg_min_max_table
) => table;
// now => get => say(...)
(
generic_lookup_preamble proj:projection_Any [weight=0.45]
| ('show me' | 'tell me') 'what is' proj:projection_Any [weight=0.45]
| generic_lookup_preamble proj:pair_projection [weight=0.1]
) => C.sayProjection($loader, proj);
(
generic_lookup_preamble proj:command_projection_table
| ('show me' | 'tell me') 'what is the' proj:command_projection_table
) => C.sayProjection($loader, proj);
}
complete_question : Ast.Expression = {
(
('what' | 'which') table:verb_filtered_table '?'
| ('what' | 'which') table:one_be_filter_table '?'
| ('what' | 'which') table:one_have_filter_table '?'
| ('what' | 'which') table:one_verb_filter_table '?'
| ('what' | 'which') table:two_be_filter_table '?'
| ('what' | 'which') table:two_have_filter_table '?'
| ('what' | 'which') table:two_verb_filter_table '?'
| ('what' | 'which') ('are' | 'is') ('the' | '') table:with_filtered_table '?'
| ('what' | 'which') ('are' | 'is') ('the' | '') table:one_with_filter_table '?'
| ('what' | 'which') ('are' | 'is') ('the' | '') table:one_which_filter_table '?'
| ('what' | 'which') ('are' | 'is') ('the' | '') table:one_clean_filter_table '?'
| ('what' | 'which') ('are' | 'is') ('the' | '') table:two_with_filter_table '?'
| ('what' | 'which') ('are' | 'is') ('the' | '') table:two_which_filter_table '?'
| ('what' | 'which') ('are' | 'is') ('the' | '') table:two_clean_filter_table '?'
) => table;
(
('is' | 'are') 'there' ('any' | '') table:with_filtered_table '?'
| ('is' | 'are') 'there' ('any' | '') table:one_with_filter_table '?'
| ('is' | 'are') 'there' ('any' | '') table:one_which_filter_table '?'
| ('is' | 'are') 'there' ('any' | '') table:one_clean_filter_table '?'
| ('is' | 'are') 'there' ('any' | '') table:two_with_filter_table '?'
| ('is' | 'are') 'there' ('any' | '') table:two_which_filter_table '?'
| ('is' | 'are') 'there' ('any' | '') table:two_clean_filter_table '?'
) => table;
// now => get join get => notify
(
('what' | 'which') ('are' | 'is') ('the' | '') table:with_filtered_table_join
| ('what' | 'which') table:have_filtered_table_join
) => table;
// now => get => notify (argmin, argmax)
(
('what' | 'which') 'is the' table:with_arg_min_max_table '?'
| ('which' | 'what') table:have_arg_min_max_table '?'
) => table;
// now => get => notify (who questions)
!turking (
('who' | 'which person' | 'which individual') table:who_verb_filtered_table '?'
| ('who' | 'which person' | 'which individual') table:who_one_be_filter_table '?'
| ('who' | 'which person' | 'which individual') table:who_one_have_filter_table '?'
| ('who' | 'which person' | 'which individual') table:who_one_verb_filter_table '?'
| ('who' | 'which person' | 'which individual') table:who_two_be_filter_table '?'
| ('who' | 'which person' | 'which individual') table:who_two_have_filter_table '?'
| ('who' | 'which person' | 'which individual') table:who_two_verb_filter_table '?'
| ('who is the person' | 'who is the one' | 'who is the individual') table:who_with_filtered_table '?'
| ('who is the person' | 'who is the one' | 'who is the individual') table:who_one_with_filter_table '?'
| ('who is the person' | 'who is the one' | 'who is the individual') table:who_one_which_filter_table '?'
| ('who is the person' | 'who is the one' | 'who is the individual') table:who_one_clean_filter_table '?'
| ('who is the person' | 'who is the one' | 'who is the individual') table:who_two_with_filter_table '?'
| ('who is the person' | 'who is the one' | 'who is the individual') table:who_two_which_filter_table '?'
| ('who is the person' | 'who is the one' | 'who is the individual') table:who_two_clean_filter_table '?'
| 'whose' table:whose_filtered_table '?'
) => table;
?turking (
'who' table:who_verb_filtered_table '?'
| 'who' table:who_one_be_filter_table '?'
| 'who' table:who_one_have_filter_table '?'
| 'who' table:who_one_verb_filter_table '?'
| 'who' table:who_two_be_filter_table '?'
| 'who' table:who_two_have_filter_table '?'
| 'who' table:who_two_verb_filter_table '?'
| 'whose' table:whose_filtered_table '?'
) => table;
compute_question;
where_question;
}
projection_question : Ast.Expression = {
( 'what is' proj:projection_Any '?'
| 'what is' proj:pair_projection '?' [weight=0.1]
| 'who is' proj:projection_Any with { projectionType = (new Type.Entity('tt:username')) } '?'
| 'who is' proj:projection_Any with { projectionType = (new Type.Entity('tt:email_address')) } '?'
) => {
if (proj instanceof Ast.ProjectionExpression && proj.schema!.getArgType(proj.args[0]) instanceof Type.Array)
return null;
return C.sayProjection($loader, proj);
};
( 'what are' proj:projection_Any '?'
| 'what are' proj:pair_projection '?' [weight=0.1]
| 'who are' proj:projection_Any with { projectionType = (new Type.Entity('tt:username')) } '?'
| 'who are' proj:projection_Any with { projectionType = (new Type.Entity('tt:email_address')) } '?'
) => {
if (proj instanceof Ast.ProjectionExpression && !(proj.schema!.getArgType(proj.args[0]) instanceof Type.Array))
return null;
return C.sayProjection($loader, proj);
};
('what' | 'which') proj:verb_projection_Any '?' => C.sayProjection($loader, proj);
proj:question_projection_table '?' => C.sayProjection($loader, proj);
// now => get => say(...) (where questions)
?turking 'where is the' table:with_filtered_table =>
C.sayProjection($loader, C.makeSingleFieldProjection($loader, 'table', Type.Location, table, 'geo'));
!turking 'where' ('is' | 'are') ('' | 'the') table:with_filtered_table =>
C.sayProjection($loader, C.makeSingleFieldProjection($loader, 'table', Type.Location, table, 'geo'));
// boolean questions
proj:boolean_projection '?' => C.sayProjection($loader, proj);
}
explicit_when_condition : Ast.Expression = {
["{if|when} ${filter} in ${table}"]: (table:complete_table<is_monitorable=true>, filter:if_filter<functionName=table.functionName>) => {
if (C.hasExistentialSubquery(filter.ast))
return null;
if (!table.schema!.is_monitorable)
return null;
if ($loader.flags.turking && table.schema!.is_list)
return null;
const withFilter = C.addFilter($loader, table, filter, { ifFilter: true });
if (!withFilter)
return null;
return C.tableToStream(withFilter);
},
["{if|when} ${filter} in ${table}"]: (table:complete_table<is_monitorable=true>, filter:edge_filter<functionName=table.functionName>) => {
if (C.hasExistentialSubquery(filter.ast))
return null;
if (!table.schema!.is_monitorable || table.schema!.is_list)
return null;
const withFilter = C.addFilter($loader, table, filter, { ifFilter: true });
if (!withFilter)
return null;
return C.tableToStream(withFilter);
};
}
when_command : Ast.Expression = {
// when => notify
(
'notify me' stream:stream
| ('alert me' | 'inform me' | 'let me know' | 'i get notified' | 'i get alerted') stream:stream
| 'send me' ('a message' | 'an alert' | 'a notification' | 'a pop up notification' | 'a popup notification') stream:stream
) => stream;
!turking ('let me know' | 'notify me') ('of' | 'about') ('changes' | 'updates') 'in' proj:projection_Any with { is_monitorable = true }
=> C.tableToStream(proj);
!nofilter ('alert me' | 'tell me' | 'notify me' | 'let me know') stream:explicit_when_condition
=> stream;
!primonly {
// when => get
when_get_stream;
// when => do
forward_when_do_rule;
backward_when_do_rule;
!turking ('auto' | 'automatically' | 'continuously') stmt:action_replace_param_with_stream => stmt;
?turking 'automatically' stmt:action_replace_param_with_stream => stmt;
}
// when => get => do
?triple_commands when_get_do_rule;
}
monitor_command : Ast.Expression = {
// when => notify
( ('monitor' | 'watch') table:with_filtered_table with { is_monitorable = true }
| ('monitor' | 'watch') table:projection_Any with { is_monitorable = true }
| ('let me know' | 'notify me') ('of' | 'about') ('changes' | 'updates') 'in' table:if_filtered_table with { is_monitorable = true }
) => C.tableToStream(table);
!nofilter {
( ('monitor' | 'watch') table:complete_table with { is_monitorable = true }
'and' ('alert me' | 'notify me' | 'inform me' | 'warn me') ('if' | 'when') filter:if_filter with { functionName = table.functionName }
| ('monitor' | 'watch') table:complete_table with { is_monitorable = true }
'and' ('alert me' | 'notify me' | 'inform me' | 'warn me') ('if' | 'when') filter:if_filter with { functionName = null }
) => {
if (C.hasExistentialSubquery(filter.ast))
return null;
if (table.schema!.is_list || !table.schema!.is_monitorable)
return null;
const withFilter = C.addFilter($loader, table, filter);
if (!withFilter)
return null;
return C.tableToStream(withFilter);
};
}
}
complete_action : Ast.Expression = {
thingpedia_complete_action;
action_replace_param_with_table;
( command:complete_action param:preposition_input_param with { functionName = command.functionName }
| command:complete_action ('with' | 'having') param:npp_input_param with { functionName = command.functionName }
) => C.addActionInputParam($loader, command, param);
}
say_command : Ast.Expression = {}
if ($loader.standardSchemas.say) {
timer_rule : Ast.ChainExpression = {
// reminders without message (uses @builtin.say, agent asks "what would you like me to say?")
["send me a {reminder|message} ${timer}"]: (timer:timer) => C.makeReminder($loader, timer);
["send me a {reminder|message} for ${date}"]: (date:constant_Date) => C.makeDateReminder($loader, date);
["send me a {reminder|message} in ${duration}"]: (duration:constant_Measure_ms) => C.makeDurationReminder($loader, duration);
// reminders with message (uses @builtin.say with the given message)
["send me a {message|alert|notification|reminder|popup notification} ${timer} {saying|with the text} ${message}"]:
(timer:timer, message:constant_String) => C.makeReminder($loader, timer, message);
["{{remind me|tell me to|send me an alert|send me a reminder} to ${message} {on|at} ${date}|{set an|} alarm at ${date} named ${message}}"]:
(date:constant_Date, message:constant_String) => C.makeDateReminder($loader, date, message);
["{{remind me|tell me|{set an|} alarm|send me an alert|send me a reminder} to ${message} in ${duration}|set a timer for ${duration} {named|saying|with the text} ${message}}"]:
(duration:constant_Measure_ms, message:constant_String) => C.makeDurationReminder($loader, duration, message);
// timers/alarms (uses @builtin.alert or @builtin.timer_expire depending on the type of timer)
["set a {timer|alarm} ${timer}"]: (timer:timer) => C.makeAlarm($loader, timer);
["set a {alarm|timer} for ${date}"]:
(date:constant_Date) => C.makeDateAlarm($loader, date);
["{set a|} {timer for|alarm in} ${duration}"]:
(duration:constant_Measure_ms) => C.makeDurationAlarm($loader, duration);
}
when_command : Ast.Expression = {
!turking {
(
'alert me' stream:stream ('saying' | 'with the text') constant:constant_String
| 'show' ('the notification' | 'the message' | 'a popup notification that says' | 'a popup containing') constant:constant_String stream:stream
) => C.makeChainExpression(stream, C.builtinSayAction($loader, constant)!);
}
}
say_command : Ast.Expression = {
(
'say' c:constant_String
| 'send me a message saying' c:constant_String
| 'show a popup that says' c:constant_String
) => C.builtinSayAction($loader, c);
}
}