Skip to content

Commit

Permalink
fix linting issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jmhw0123 committed Apr 20, 2022
1 parent f72d5f9 commit 03a1c57
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
38 changes: 27 additions & 11 deletions test/unit/test_sample_synthetic_data.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingTalk
//
// Copyright 2017-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: Jake Wu <jmhw0123@gmail.com>

import assert from 'assert';
import * as fs from 'fs';
import * as ThingTalk from 'thingtalk';
import * as Tp from 'thingpedia';
import * as I18n from '../../lib/i18n';
import * as Path from 'path';
const { ArgumentParser } = require('argparse');
import sampler from '../../tool/sample-synthetic-data';

const Type = ThingTalk.Type;
import { ArgumentParser } from 'argparse';
// const Type = ThingTalk.Type;

const TEST_CASES = [

Expand All @@ -26,11 +43,11 @@ const TEST_CASES = [
];

String.prototype.format = function() {
var args = arguments;
return this.replace(/{([0-9]+)}/g, function (match, index) {
return typeof args[index] == 'undefined' ? match : args[index];
const args = arguments;
return this.replace(/{([0-9]+)}/g, (match, index) => {
return typeof args[index] === 'undefined' ? match : args[index];
});
}
};

function initArgparse() {
const parser = new ArgumentParser({
Expand Down Expand Up @@ -78,9 +95,8 @@ export default async function main() {
args.function = query;
const ret = await sampler(deviceClass, baseTokenizer, args);
const item = ret.filter((x) => {
if (typeof x.value !== undefined) {
if (typeof x.value !== undefined)
utterance = utterance.format(x.value);
}
return x.utterance.toLowerCase() === utterance.toLowerCase();
});
try {
Expand All @@ -107,4 +123,4 @@ export default async function main() {
}

if (!module.parent)
main();
main();
32 changes: 17 additions & 15 deletions tool/sample-synthetic-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ const CITIES = [
'Miramar',
'Olathe',
'Metairie'
]
];

function typeToString(type : Type) : string {
const elemType = getElementType(type);
Expand Down Expand Up @@ -314,17 +314,17 @@ function makeJSDate(year : number, month : number, day : number) : Date {
}

function generateRandomIntArray(max : number, sampleSize : number) {
return Array.from({length: sampleSize}, () => randomInt(0, max, Math.random));
return Array.from({ length : sampleSize }, () => randomInt(0, max, Math.random));
}

function generateDateArray(timezone : string, sampleSize : number) {
const _getDates = function(startDate : Date, period : number) {
let dates = [];
const dates = [];
let i = 0;
let d = new Date(startDate);
const d = new Date(startDate);
while (i++ < period) {
dates.push(new Date(d));
d.setDate(d.getDate() + 1)
d.setDate(d.getDate() + 1);
}
return dates;
};
Expand All @@ -334,7 +334,7 @@ function generateDateArray(timezone : string, sampleSize : number) {
}

function generateTimeArray(sampleSize : number) {
let times = [];
const times = [];
for (let i=0; i<sampleSize; i++) {
const newTime = new Builtin.Time(randomInt(0, 23, Math.random), randomInt(0, 59, Math.random), 0);
times.push(newTime);
Expand Down Expand Up @@ -377,10 +377,10 @@ async function retrieveSampleValues(classDef : Ast.ClassDef,
}

function toThingtalkValue(classDef : Ast.ClassDef,
sampleMeta : Record<string, Constant[]>,
fname : string,
argDef : Ast.ArgumentDef,
value : string) : { value: Ast.Value; op: string; } {
sampleMeta : Record<string, Constant[]>,
fname : string,
argDef : Ast.ArgumentDef,
value : string) : { value : Ast.Value; op : string; } {
value = value.toLowerCase();
let type = argDef.type;
if (type instanceof Type.Entity) {
Expand Down Expand Up @@ -499,12 +499,12 @@ function generateExamplesByPOS(query : Ast.FunctionDef,
if (argument.type instanceof Type.Measure) {
const argType = argument.type;
const unitName = Object.keys(WikidataUnitToTTUnit).find(
key => WikidataUnitToTTUnit[key].toLowerCase() === argType.unit.toString().toLowerCase()
(key) => WikidataUnitToTTUnit[key].toLowerCase() === argType.unit.toString().toLowerCase()
);
argumentCanonical = argumentCanonical + ` # ${unitName}`;
}
else
} else {
argumentCanonical = argumentCanonical + ' #';
}
}
}
const predicate = typeof value === 'string' ? argumentCanonical.replace('#', value) : argumentCanonical;
Expand Down Expand Up @@ -872,14 +872,16 @@ export default async function sampler(deviceClass : Ast.ClassDef,
if (example.value) {
const { value, } = toThingtalkValue(deviceClass, sampleMeta, fname, argDef, `${example.value}`);
program = generateActionAst(fname, example.argument, value);
} else
} else {
continue;
}
} else {
if (example.value) {
const { value, op } = toThingtalkValue(deviceClass, sampleMeta, fname, argDef, `${example.value}`);
program = generateFilterAst(args.device, fname, example.argument, op, value);
} else
} else {
program = generateProjectionAst(args.device, fname, example.argument);
}
}
try {
const entityDummy = EntityUtils.makeDummyEntities(prepUtterance);
Expand Down

0 comments on commit 03a1c57

Please sign in to comment.