Skip to content

Commit

Permalink
Merge pull request #145 from stanford-oval/wip/new-dialogues
Browse files Browse the repository at this point in the history
Inference mode dialogue templates
  • Loading branch information
gcampax committed Jun 16, 2020
2 parents f397787 + 36d3ce4 commit a6faf33
Show file tree
Hide file tree
Showing 42 changed files with 2,092 additions and 415 deletions.
2 changes: 1 addition & 1 deletion languages/flags.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
thingtalk/en:
all:
always_filter, aggregation, bookkeeping, configure_actions, dialogues, extended_timers, multifilters, no_contextual_bookkeeping, nofilter, nostream, notablejoin, policies, primonly, projection, projection_with_filter, range_filters, remote_commands, schema_org, screen_selection, timer, triple_commands, undefined_filter
always_filter, aggregation, bookkeeping, configure_actions, extended_timers, multifilters, no_contextual_bookkeeping, nofilter, nostream, notablejoin, policies, primonly, projection, projection_with_filter, range_filters, remote_commands, schema_org, screen_selection, timer, triple_commands, undefined_filter
default:
always_filter, aggregation, bookkeeping, configure_actions, multifilters, policies, projection, projection_with_filter, remote_commands, schema_org, screen_selection, timer, undefined_filter
developer:
Expand Down
87 changes: 84 additions & 3 deletions languages/thingtalk/ast_manip.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,10 @@ function checkAtomFilter(table, filter) {
if (!vtype.isEntity && !vtype.isString)
return false;
vtype = Type.Array(Type.String);
} else if (filter.operator === '=~') {
if (!ptype.isEntity && !ptype.isString)
return false;
vtype = Type.String;
}

if (!filter.value.getType().equals(vtype))
Expand Down Expand Up @@ -1539,10 +1543,14 @@ function makeComputeFilterExpression(table, operation, operands, resultType, fil
if (table.schema.out[operation])
return null;

const computedTable = makeComputeExpression(table, operation, operands, resultType);
const filter = new Ast.BooleanExpression.Atom(null, operation, filterOp, filterValue);
const expression = new Ast.Value.Computation(operation, operands);
if (operation === 'distance') {
expression.overload = [Type.Location, Type.Location, Type.Measure('m')];
expression.type = Type.Measure('m');
}
const filter = new Ast.BooleanExpression.Compute(null, expression, filterOp, filterValue);
if (filter)
return addFilter(computedTable, filter);
return addFilter(table, filter);
return null;
}

Expand Down Expand Up @@ -1711,6 +1719,76 @@ function addActionInputParam(action, param) {
return new Ast.Table.Invocation(null, newInvocation, action.schema.removeArgument(param.name));
}

function replaceSlotBagPlaceholder(bag, pname, value) {
if (!value.isConstant())
return null;
let ptype = bag.schema.getArgType(pname);
if (!ptype)
return null;
if (ptype.isArray)
ptype = ptype.elem;
const vtype = value.getType();
if (!ptype.equals(vtype))
return null;
if (bag.has(pname))
return null;
const clone = bag.clone();
clone.set(pname, value);
return clone;
}

function replaceErrorMessagePlaceholder(msg, pname, value) {
const newbag = replaceSlotBagPlaceholder(msg.bag, pname, value);
if (newbag === null)
return null;
return { code: msg.code, bag: newbag };
}

/**
* Find the filter table in the context.
*
* Returns filterTable
*/
function findFilterTable(root) {
let table = root;
while (!table.isFilter) {
if (table.isSequence ||
table.isHistory ||
table.isWindow ||
table.isTimeSeries)
throw new Error('NOT IMPLEMENTED');

// do not touch these with filters
if (table.isAggregation ||
table.isVarRef ||
table.isResultRef)
return null;

// go inside these
if (table.isSort ||
table.isIndex ||
table.isSlice ||
table.isProjection ||
table.isCompute ||
table.isAlias) {
table = table.table;
continue;
}

if (table.isJoin) {
// go right on join, always
table = table.rhs;
continue;
}

assert(table.isInvocation);
// if we get here, there is no filter table at all
return null;
}

return table;
}

module.exports = {
typeToStringSafe,
getFunctionNames,
Expand Down Expand Up @@ -1752,6 +1830,8 @@ module.exports = {
whenGetStream,
addInvocationInputParam,
addActionInputParam,
replaceSlotBagPlaceholder,
replaceErrorMessagePlaceholder,

// filters
hasUniqueFilter,
Expand All @@ -1765,6 +1845,7 @@ module.exports = {
addFilter,
hasGetPredicate,
makeGetPredicate,
findFilterTable,

makeListExpression,
makeSortedTable,
Expand Down
50 changes: 13 additions & 37 deletions languages/thingtalk/common-constants.genie
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,24 @@ const MAX_SMALL_INTEGER = 12;

for (let i = 0; i <= MAX_SMALL_INTEGER; i++)
constant_Number = #(String(i)) => new Ast.Value.Number(i);
// NOTE: normally, we would use _tpLoader.flags.dialogues for these checks, but this code
// executes before load-thingpedia.js, so _tpLoader cannot be used in this file
if ($options.flags.dialogues) {
for (let i = 0; i < MAX_CONSTANTS; i++)
constant_Number = #('NUMBER_' + i) => new Ast.Value.Number(MAX_SMALL_INTEGER + 1 + i);

for (let i = 0; i < MAX_CONSTANTS; i++) {
constant_String = #('QUOTED_STRING_' + i) => new Ast.Value.String('str:QUOTED_STRING::' + i + ':');
constant_Entity__tt__url = #('URL_' + i) => new Ast.Value.Entity('str:URL::' + i, 'tt:url', null);
constant_Entity__tt__username = #('USERNAME_' + i) => new Ast.Value.Entity('str:USERNAME::' + i + ':', 'tt:url', null);
constant_Entity__tt__hashtag = #('HASHTAG_' + i) => new Ast.Value.Entity('str:HASHTAG::' + i + ':', 'tt:hashtag', null);
constant_Entity__tt__phone_number = #('PHONE_NUMBER_' + i) => new Ast.Value.Entity('str:PHONE_NUMBER::' + i + ':', 'tt:phone_number', null);
constant_Entity__tt__email_address = #('EMAIL_ADDRESS_' + i) => new Ast.Value.Entity('str:EMAIL_ADDRESS::' + i + ':', 'tt:email_address', null);
constant_Entity__tt__path_name = #('PATH_NAME_' + i) => new Ast.Value.Entity('str:PATH_NAME::' + i + ':', 'tt:path_name', null);
constant_String = const(QUOTED_STRING, Type.String);

constant_Currency = #('CURRENCY_' + i) => new Ast.Value.Currency(2 + i, 'usd');
constant_Measure_ms = #('DURATION_' + i) => new Ast.Value.Measure(2 + i, 'ms');
constant_Entity__tt__url = const(URL, Type.Entity('tt:url'));

constant_Location = #('LOCATION_' + i) => new Ast.Value.Location(new Ast.Location.Absolute(2 + i, 2 + i, null));
constant_Entity__tt__username = const(USERNAME, Type.Entity('tt:username'));
constant_Entity__tt__hashtag = const(HASHTAG, Type.Entity('tt:hashtag'));
constant_Entity__tt__phone_number = const(PHONE_NUMBER, Type.Entity('tt:phone_number'));
constant_Entity__tt__email_address = const(EMAIL_ADDRESS, Type.Entity('tt:email_address'));
constant_Entity__tt__path_name = const(PATH_NAME, Type.Entity('tt:path_name'));

constant_Date = #('DATE_' + i) => new Ast.Value.Date(new Date(2018, 0, 2 + i));
constant_Time = #('TIME_' + i) => new Ast.Value.Time(new Ast.Time.Absolute(Math.floor(i/4), [0, 15, 30, 45][i % 4], 0));
}
} else {
constant_String = const(QUOTED_STRING, Type.String);

constant_Entity__tt__url = const(URL, Type.Entity('tt:url'));

constant_Entity__tt__username = const(USERNAME, Type.Entity('tt:username'));
constant_Entity__tt__hashtag = const(HASHTAG, Type.Entity('tt:hashtag'));
constant_Entity__tt__phone_number = const(PHONE_NUMBER, Type.Entity('tt:phone_number'));
constant_Entity__tt__email_address = const(EMAIL_ADDRESS, Type.Entity('tt:email_address'));
constant_Entity__tt__path_name = const(PATH_NAME, Type.Entity('tt:path_name'));
constant_Number = const(NUMBER, Type.Number);
constant_Currency = const(CURRENCY, Type.Currency);
constant_Time = const(TIME, Type.Time);
constant_Date = const(DATE, Type.Date);
constant_Location = const(LOCATION, Type.Location);

constant_Number = const(NUMBER, Type.Number);
constant_Currency = const(CURRENCY, Type.Currency);
constant_Time = const(TIME, Type.Time);
constant_Date = const(DATE, Type.Date);
constant_Location = const(LOCATION, Type.Location);

constant_Measure_ms = const(DURATION, Type.Measure('ms'));
}
constant_Measure_ms = const(DURATION, Type.Measure('ms'));

constant_Entity__tt__picture = {}
constant_Entity__tt__function = {}
Expand Down
31 changes: 31 additions & 0 deletions languages/thingtalk/common.genie
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ thingpedia_who_question = {}
thingpedia_search_question = {}
thingpedia_slot_fill_question = {}

thingpedia_result = {}
thingpedia_action_past = {}
thingpedia_error_message = {}

out_param_Numeric = {
out_param_Number;
out_param_Currency;
Expand Down Expand Up @@ -172,6 +176,24 @@ for (let [pname, [typestr,]] of _tpLoader.params.in.values()) {
a:thingpedia_action v:$('constant_' + typestr) [-> pname { isConstant: true }] => C.replacePlaceholderWithConstant(a, pname, v);
}

thingpedia_action_past = {
a:thingpedia_action v:$('constant_' + typestr) [-> pname { isConstant: true }] => C.replacePlaceholderWithConstant(a, pname, v);
}

thingpedia_error_message = {
msg:thingpedia_error_message v:$('constant_' + typestr) [-> pname { isConstant: true }] => C.replaceErrorMessagePlaceholder(msg, pname, v);
}

if (typestr.startsWith('Entity')) {
thingpedia_action_past = {
a:thingpedia_action v:constant_name [-> pname { isConstant: true }] => C.replacePlaceholderWithConstant(a, pname, v);
}

thingpedia_error_message = {
msg:thingpedia_error_message v:constant_name [-> pname { isConstant: true }] => C.replaceErrorMessagePlaceholder(msg, pname, v);
}
}

thingpedia_program = {
p:thingpedia_program v:$('constant_' + typestr) [-> pname { isConstant: true }] => C.replacePlaceholderWithConstant(p, pname, v);
}
Expand Down Expand Up @@ -199,3 +221,12 @@ for (let [pname, [typestr,]] of _tpLoader.params.in.values()) {
}
}
}

for (let [pname, typestr] of _tpLoader.params.out.values()) {
if (typestr !== null) {
thingpedia_result = {
bag:thingpedia_result v:$('constant_name') [-> pname { isConstant: true }] => C.replaceSlotBagPlaceholder(bag, pname, v);
bag:thingpedia_result v:$('constant_' + typestr) [-> pname { isConstant: true }] => C.replaceSlotBagPlaceholder(bag, pname, v);
}
}
}
Loading

0 comments on commit a6faf33

Please sign in to comment.