Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filters.genie for Chinese #11

Merged
merged 4 commits into from
Mar 12, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions languages/zh-tw/filters.genie
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingTalk
//
// Copyright 2017-2018 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna <gcampagn@cs.stanford.edu>
//
// See COPYING for details

{
const assert = require('assert');

const ThingTalk = require('thingtalk');
const Ast = ThingTalk.Ast;
const Type = ThingTalk.Type;
const { clean } = require('../../lib/utils');

// import the combinator library
const C = require('../../lib/sentence-generator/ast_manip');
}

atom_filter = {
t1:constant_Time '之前' => C.timeGetPredicate($options, null, t1);
t2:constant_Time '之後' => C.timeGetPredicate($options, t2, null);
t1:constant_Time '和' t2:constant_Time '之間' => C.timeGetPredicate($options, t1, t2);
johnnychhsu marked this conversation as resolved.
Show resolved Hide resolved

'我 在' loc:constant_Location => C.locationGetPredicate($options, loc);
'我 不 在' loc:constant_Location => C.locationGetPredicate($options, loc, true);

// What is projection here?
// proj:projection_Any ('is' | 'is exactly' | 'is equal to') x:constant_Any => C.makeGetPredicate(proj, '==', x);
// proj:projection_Any ('is not' | 'is n\'t' | 'is different than') x:constant_Any => C.makeGetPredicate(proj, '==', x, true);

p:out_param_Any ('等於' | '相等於') x:constant_Any => C.makeFilter($options, p, '==', x);
johnnychhsu marked this conversation as resolved.
Show resolved Hide resolved
p:out_param_Any ('和' | '跟') x:constant_Any ('一樣' | '相同') => C.makeFilter($options, p, '==', x);
p:out_param_Any '不等於' x:constant_Any => C.makeFilter($options, p, '==', x, true);
johnnychhsu marked this conversation as resolved.
Show resolved Hide resolved
p:out_param_Any ('和' | '跟') x:constant_Any ('不一樣' | '不 相同') => C.makeFilter($options, p, '==', x, true);

p:the_out_param_Numeric '比' x:constant_Numeric ('大' | '多') => C.makeFilter($options, p, '>=', x);
p:the_out_param_Numeric '比' x:constant_Numeric ('小' | '少') => C.makeFilter($options, p, '<=', x);

johnnychhsu marked this conversation as resolved.
Show resolved Hide resolved
// I need something like timeGetPredicate for constant_Date, should I add in ast_manip?
// For example, d1:constant_Date '和' d2:constant_Date '之間' => C.dateGetPredicate($options, d1, d2);

!turking {
p:the_out_param_Date '在' x:constant_Date '之後' => C.makeFilter($options, p, '>=', x);
p:the_out_param_Date '在' x:constant_Date '之前' => C.makeFilter($options, p, '<=', x);
}

p:the_out_param_Array__Any ('有' | '包含' | '擁有' | '含有' | '存在') x:constant_Any => C.makeFilter($options, p, 'contains', x);
p:the_out_param_Array__Any ('沒有' | '不 包含' | '沒 擁有' | '不 含有' | '不 存在') x:constant_Any => C.makeFilter($options, p, 'contains', x, true);
johnnychhsu marked this conversation as resolved.
Show resolved Hide resolved

!turking range_filter;
//!turking f:either_filter => f;
}

edge_filter = {
!turking '當' p:out_param_Any ('跟' | '和') x:constant_Any ('一樣' | '相同') ('時' | '的 時候') => C.makeFilter($options, p, '==', x);
johnnychhsu marked this conversation as resolved.
Show resolved Hide resolved
'當' p:the_out_param_Numeric ('比' | '變得 比') x:constant_Numeric ('大' | '多') ('時' | '的 時候') => C.makeFilter($options, p, '>=', x);
'當' p:the_out_param_Numeric ('比' | '變得 比') x:constant_Numeric ('小' | '少') ('時' | '的 時候') => C.makeFilter($options, p, '<=', x);
}

either_filter = {
param:out_param_Any ('等於' | '相等於' | '是') v1:constant_Any ('或' | '或是' | '或者是') v2:constant_Any => {
// param is a Value.VarRef
//console.log('param: ' + param.name);
if (!v1.getType().equals(v2.getType()))
return null;
if (v1.equals(v2)) // can happen with constants (now, 0, 1, etc.)
return null;
if (v1.isVarRef && v1.constNumber !== undefined && v2.isVarRef && v2.constNumber !== undefined &&
v1.constNumber + 1 !== v2.constNumber) // optimization: avoid CONST_X CONST_Y with X + 1 != Y earlier (before the NN catches it)
return null;
let vtype = v1.getType();
if (vtype.isBoolean) // "is equal to true or false" does not make sense
return null;
if (!$options.allOutParams.has(param.name + '+' + vtype))
return null;
return new Ast.BooleanExpression.Atom(param.name, 'in_array', Ast.Value.Array([v1, v2]));
};
param:out_param_Any '不是' v1:constant_Any '也不是' v2:constant_Any => {
// param is a Value.VarRef
//console.log('param: ' + param.name);
if (!v1.getType().equals(v2.getType()))
return null;
if (v1.equals(v2)) // can happen with constants (now, 0, 1, etc.)
return null;
if (v1.isVarRef && v1.constNumber !== undefined && v2.isVarRef && v2.constNumber !== undefined &&
v1.constNumber + 1 !== v2.constNumber) // optimization: avoid CONST_X CONST_Y with X + 1 != Y earlier (before the NN catches it)
return null;
let vtype = v1.getType();
if (vtype.isBoolean) // "is neither true nor false" does not make sense
return null;
if (!$options.allOutParams.has(param.name + '+' + vtype))
return null;
return new Ast.BooleanExpression.Not(new Ast.BooleanExpression.Atom(param.name, 'in_array', Ast.Value.Array([v1, v2])));
};
}

range = {
v1:constant_Numeric ('和' | '跟') v2:constant_Numeric '之間'=> {
if (!v1.getType().equals(v2.getType()))
return null;
if (v1.equals(v2)) // can happen with constants (now, 0, 1, etc.)
return null;
if (v1.isVarRef && v1.constNumber !== undefined && v2.isVarRef && v2.constNumber !== undefined &&
v1.constNumber + 1 !== v2.constNumber) // optimization: avoid CONST_X CONST_Y with X + 1 != Y earlier (before the NN catches it)
return null;
return [v1, v2];
};
v1:constant_Date ('和' | '跟') v2:constant_Date '之間'=> {
if (!v1.getType().equals(v2.getType()))
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will never trigger, because range_filter only pairs the range to a numeric (number, currency, measure) parameter, not a date parameter.

Also, the v1.getType().equals( v2.getType()) check is redundant because you're asking for Date constants through the grammar non-terminal.

return [v1, v2];
};
}

range_filter = {
param:the_out_param_Numeric '在' range:range => {
const [v1, v2] = range;
return new Ast.BooleanExpression.And([
Ast.BooleanExpression.Atom(param.name, '>=', v1),
Ast.BooleanExpression.Atom(param.name, '<=', v2)
]);
};
}

with_filter = {
johnnychhsu marked this conversation as resolved.
Show resolved Hide resolved
p:out_param_Any ('和' | '跟') x:constant_Any ('一樣' | '相同')=> C.makeFilter($options, p, '==', x);
p:out_param_Numeric '比' x:constant_Numeric ('大' | '多') => C.makeFilter($options, p, '>=', x);
p:out_param_Numeric '比' x:constant_Numeric ('小' | '少') => C.makeFilter($options, p, '<=', x);

!turking {
f:range_with_filter => f;
('無' | '沒有' | '零個') param:out_param_Number => new Ast.BooleanExpression.Atom(param.name, '==', Ast.Value.Number(0));
}
}
range_with_filter = {
param:out_param_Numeric range:range => {
const [v1, v2] = range;
return new Ast.BooleanExpression.And([
Ast.BooleanExpression.Atom(param.name, '>=', v1),
Ast.BooleanExpression.Atom(param.name, '<=', v2)
]);
};
}