How to use the thingtalk.SchemaRetriever function in thingtalk

To help you get started, we’ve selected a few thingtalk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stanford-oval / almond-cloud / training / tasks / evaluate.js View on Github external
module.exports = async function main(task, argv) {
    task.handleKill();

    const jobdir = await AbstractFS.download(task.jobDir + '/');
    const datadir = path.resolve(jobdir, 'dataset');
    const outputdir = path.resolve(jobdir, 'output');

    const tpClient = new AdminThingpediaClient(task.language);
    const schemas = new ThingTalk.SchemaRetriever(tpClient, null, true);
    const parser = new LocalParserClient(outputdir, task.language);
    await parser.start();

    const output = fs.createReadStream(path.resolve(datadir, 'eval.tsv'))
        .setEncoding('utf8')
        .pipe(byline())
        .pipe(new Genie.DatasetParser({
            contextual: task.modelInfo.contextual,
            preserveId: true,
            parseMultiplePrograms: true
        }))
        .pipe(new Genie.SentenceEvaluatorStream(parser, schemas, true /* tokenized */, argv.debug))
        .pipe(new Genie.CollectSentenceStatistics());

    const result = await output.read();
    await task.setMetrics(result);
github stanford-oval / almond-dialog-agent / test / test_unit.js View on Github external
[`now => @com.twitter.home_timeline() => @com.facebook.post(status=text);`, 'com.facebook'],
        [`now => @org.thingpedia.builtin.thingengine.builtin.get_random_between() => @com.twitter.post(status="foo");`, 'com.twitter'],
        [`class @__dyn_0 extends @org.thingpedia.builtin.thingengine.remote {
    action send(in req __principal: Entity(tt:contact),
                in req __program_id: Entity(tt:program_id),
                in req __flow: Number,
                in req __kindChannel: Entity(tt:function),
                in req title: String,
                in req picture_url: Entity(tt:picture),
                in req link: Entity(tt:url),
                in req alt_text: String);
  }
  now => @com.xkcd.get_comic() => @__dyn_0.send(__principal="mock-account:123456-SELF"^^tt:contact("me"), __program_id=$event.program_id, __flow=0, __kindChannel=$event.type, title=title, picture_url=picture_url, link=link, alt_text=alt_text);`, 'com.xkcd'],
    ];

    const schemas = new ThingTalk.SchemaRetriever(_mockThingpediaClient, null, true);

    for (let [code, expected] of TEST_CASES) {
        const program = await ThingTalk.Grammar.parseAndTypecheck(code, schemas, false);

        const icon = Helpers.getProgramIcon(program);
        assert.strictEqual(icon, expected);
    }
}
github stanford-oval / almond-cloud / tests / nlp / index.js View on Github external
const ThingTalk = require('thingtalk');
const Gettext = require('node-gettext');
const Tp = require('thingpedia');

const Almond = require('almond-dialog-agent');
const Intent = Almond.Intent;
const ParserClient = require('./parserclient');

const db = require('../../util/db');
const Config = require('../../config');
assert.strictEqual(Config.WITH_THINGPEDIA, 'external');

const gettext = new Gettext();
gettext.setLocale('en-US');

const schemas = new ThingTalk.SchemaRetriever(new Tp.HttpClient({
    getDeveloperKey() {
        return null;
    },
    locale: 'en-US',
}, Config.THINGPEDIA_URL), null, true);

function candidateToString(cand) {
    if (cand.isProgram)
        return `Program(${cand.program.prettyprint(true)})`;
    else if (cand.isSetup)
        return `Setup(${cand.program.prettyprint(true)})`;
    else if (cand.isPermissionRule)
        return `PermissionRule(${cand.rule.prettyprint(true)})`;
    else
        return String(cand);
}
github stanford-oval / almond-cloud / scripts / migrate_nn_dataset_file.js View on Github external
async function main() {
    const language = process.argv[2];

    const tpClient = new AdminThingpediaClient(language, null);
    const schemaRetriever = new ThingTalk.SchemaRetriever(tpClient, null, true);

    process.stdin.setEncoding('utf8');
    const input = byline(process.stdin);

    const transformer = new stream.Transform({
        writableObjectMode: true,

        flush() {},
        transform(line, encoding, callback) {
            if (!line) {
                callback();
                return;
            }

            const [id,sentence,program] = line.trim().split('\t');
github stanford-oval / genie-toolkit / tool / typecheck.js View on Github external
async execute(args) {
        const tpClient = new Tp.FileClient(args);
        const schemas = new ThingTalk.SchemaRetriever(tpClient, null, !args.debug);

        let cache, cacheOut;
        if (args.cache) {
            if (await util.promisify(fs.exists)(args.cache)) {
                cache = await readAllLines([fs.createReadStream(args.cache)])
                    .pipe(new CacheParser())
                    .pipe(new StreamUtils.MapAccumulator('from'))
                    .read();
            } else {
                cache = new Map;
            }
            cacheOut = new CacheSerializer();
            cacheOut.pipe(fs.createWriteStream(args.cache, { flags: 'a' }));
        } else {
            cache = new Map;
            cacheOut = null;
github stanford-oval / almond-cloud / routes / thingpedia_translate.js View on Github external
async function validateDataset(req, dbClient) {
    const tpClient = new ThingpediaClient(req.user.developer_key, req.user.locale, dbClient);
    const schemaRetriever = new ThingTalk.SchemaRetriever(tpClient, null, true);

    const parsed = await ThingTalk.Grammar.parseAndTypecheck(req.body.dataset, schemaRetriever, false);

    if (parsed.datasets.length !== 1 ||
        parsed.datasets[0].name !== '@' + req.params.kind ||
        parsed.datasets[0].language !== req.body.language)
        throw new Validation.ValidationError("Invalid dataset file: must contain exactly one dataset, with the same identifier as the class and the correct language");

    const dataset = parsed.datasets[0];
    await Validation.tokenizeDataset(dataset);
    await Validation.validateDataset(dataset);

    return dataset;
}
github stanford-oval / almond-cloud / training / replace_parameters.js View on Github external
async initialize() {
        if (this._dbClient === null)
            this._dbClient = await db.connect();
        if (this._schemas === null) {
            let tpClient = new AdminThingpediaClient(this._language, this._dbClient);
            this._schemas = new ThingTalk.SchemaRetriever(tpClient, null, true);
        }
        this._loader = new ValueListLoader(this._language, this._dbClient);
    }
github stanford-oval / genie-toolkit / tool / manual-annotate-dialog.js View on Github external
constructor(rl, options) {
        super({ objectMode: true });

        this._rl = rl;

        const tpClient = new Tp.FileClient(options);
        this._schemas = new ThingTalk.SchemaRetriever(tpClient, null, true);
        this._parser = ParserClient.get(options.server, 'en-US');

        this._state = 'loading';

        this._serial = 0;

        this._currentDialog = [];

        this._dialogState = undefined;
        this._context = undefined;
        this._utterance = undefined;
        this._preprocessed = undefined;
        this._entities = undefined;
        this._candidates = undefined;

        rl.on('line', async (line) => {
github stanford-oval / genie-toolkit / tool / extract-contexts.js View on Github external
async execute(args) {
        const rng = seedrandom.alea(args.random_seed);
        const tpClient = new Tp.FileClient(args);
        const schemas = new ThingTalk.SchemaRetriever(tpClient, null, !args.debug);

        let allprograms = await readAllLines(args.input_file)
            .pipe(new DatasetParser())
            .pipe(new ContextExtractor(schemas, rng))
            .read();

        for (let prog of allprograms) {
            args.output.write(prog);
            args.output.write('\n');
        }
        args.output.end();

        await StreamUtils.waitFinish(args.output);
    }
};