How to use the thingtalk.Grammar 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 / util / validation.js View on Github external
async function loadClassDef(dbClient, req, kind, classCode, datasetCode) {
    const tpClient = new ThingpediaClient(req.user.developer_key, req.user.locale, dbClient);
    const schemaRetriever = new ThingTalk.SchemaRetriever(tpClient, null, true);

    let parsed;
    try {
        parsed = await ThingTalk.Grammar.parseAndTypecheck(`${classCode}\n${datasetCode}`, schemaRetriever, true);
    } catch(e) {
        if (e.name === 'SyntaxError' && e.location) {
            let lineNumber = e.location.start.line;
            // add 1 for the \n that we add to separate classCode and datasetCode
            console.log(classCode);
            const classLength = 1 + classCode.split('\n').length;
            const fileName = lineNumber > classLength ? 'dataset.tt' : 'manifest.tt';
            // mind the 1-based line numbers...
            lineNumber = lineNumber > classLength ? lineNumber - classLength + 1 : lineNumber;
            throw new ValidationError(`Syntax error in ${fileName} line ${lineNumber}: ${e.message}`);
        } else {
            throw new ValidationError(e.message);
        }
    }

    if (!parsed.isMeta || parsed.classes.length !== 1 ||
github stanford-oval / almond-cloud / util / dataset.js View on Github external
function rowsToExamples(rows, { editMode = false, skipId = false }) {
    // coalesce by target code

    // note: this code is designed to be fast, and avoid parsing the examples in the common
    // case of up-to-date thingpedia

    let uniqueCode = new Map;
    for (let row of rows) {
        let targetCode = row.target_code || row.program;
        if (!targetCode)
            throw new InternalError('E_DATASET_CORRUPT', `Invalid example ${row.id}, missing program`);

        if (/^[ \r\n\t\v]*let[ \r\n\t\v]/.test(targetCode)) {
            // forward compatibility: convert the declaration to example syntax
            const parsed = ThingTalk.Grammar.parse(targetCode);
            const declaration = parsed.declarations[0];

            const example = new ThingTalk.Ast.Example(-1,
                declaration.type === 'table' ? 'query' : declaration.type,
                declaration.args,
                declaration.value,
                [], [], {});
            targetCode = example.prettyprint('').trim();
        } else if (!/^[ \r\n\t\v]*(query|action|stream|program)[ \r\n\t\v]/.test(targetCode)) {
            targetCode = `program := ${targetCode}`;
        }

        if (uniqueCode.has(targetCode)) {
            const ex = uniqueCode.get(targetCode);
            ex.utterances.push(row.utterance);
            ex.preprocessed.push(row.preprocessed);
github stanford-oval / almond-android / jsapp / frontend / routes / apps.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2015 Giovanni Campagna 
//
// See COPYING for details
"use strict";

const Q = require('q');
const express = require('express');
var router = express.Router();

const ThingTalk = require('thingtalk');
const AppGrammar = ThingTalk.Grammar;

function getAllDevices(engine) {
    var devices = engine.devices.getAllDevices();

    return devices.map(function(d) {
        return { uniqueId: d.uniqueId,
                 name: d.name || "Unknown device",
                 description: d.description || "Description not available",
                 kind: d.kind,
                 ownerTier: d.ownerTier,
                 available: d.available,
                 isTransient: d.isTransient,
                 isOnlineAccount: d.hasKind('online-account'),
                 isDataSource: d.hasKind('data-source'),
                 isThingEngine: d.hasKind('thingengine-system') };
    }).filter(function(d) {
github stanford-oval / almond-cloud / routes / mturk.js View on Github external
function validateOne(dbClient, batchId, language, schemas, utterance, thingtalk) {
    return Promise.all([ThingTalk.Grammar.parseAndTypecheck(thingtalk, schemas),
                        TokenizerService.tokenize(language, utterance)]).then(([program, { tokens: preprocessed, entities }]) => {

        let target_code = ThingTalk.NNSyntax.toNN(program, entities);
        for (let name in entities) {
            if (name === '$used') continue;
            throw new BadRequestError('Unused entity ' + name);
        }
        return example.create(dbClient, {
            utterance: utterance,
            preprocessed: preprocessed.join(' '),
            target_code: target_code.join(' '),
            target_json: '', // FIXME
            type: 'turking' + batchId,
            language: language,
            is_base: 0
        });
github stanford-oval / genie-toolkit / lib / validator.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2018-2019 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Silei Xu 
//         Giovanni Campagna 
//
// See COPYING for details
"use strict";

const Stream = require('stream');

const ThingTalk = require('thingtalk');
const Grammar = ThingTalk.Grammar;
const NNSyntax = ThingTalk.NNSyntax;

const i18n = require('./i18n');
const Utils = require('./utils');

class ParaphraseValidator {
    constructor(schemaRetriever, tokenizer, locale, row, counter = {}) {
        this._schemas = schemaRetriever;
        this._tokenizer = tokenizer;
        this._locale = locale;

        this._noIdea = i18n.get(locale).NO_IDEA;
        this._counter = counter;
        this.id = row.id;
        this.target_code = row.target_code;
        this.paraphrase = row.paraphrase;
github stanford-oval / almond-dialog-agent / test / mock_schema_delegate.js View on Github external
async getAllDeviceNames() {
        await this._ensureLoaded();

        const parsed = ThingTalk.Grammar.parse(this._devices);
        let names = [];
        for (let classDef of parsed.classes) {
            names.push({
                kind: classDef.kind,
                kind_canonical: classDef.metadata.canonical
            });
        }
        return names;
    }
github stanford-oval / almond-cloud / tests / unit / file_thingpedia_client.js View on Github external
async getAllDeviceNames() {
        await this._ensureLoaded();

        const parsed = ThingTalk.Grammar.parse(this._devices);
        let names = [];
        for (let classDef of parsed.classes) {
            names.push({
                kind: classDef.kind,
                kind_canonical: classDef.metadata.canonical
            });
        }
        return names;
    }
github stanford-oval / thingengine-core / test / functional / test_remote.js View on Github external
async function testHandleInstallOkPermission(engine) {
    const messaging = engine.messaging;

    const permrule = await parseProgram(engine, `source == "mock-messaging:user2"^^tt:contact("User 1") :
        now => @org.thingpedia.builtin.test.eat_data, starts_with(data, 'foo');`);
    const permuuid = await engine.permissions.addPermission(permrule, 'some permission');

    const prog1 = await ThingTalk.Grammar.parseAndTypecheck(`now => @org.thingpedia.builtin.test.eat_data(data='foo');`, engine.schemas);
    const code = prog1.prettyprint(true).trim();
    const uniqueId = uuid.v4();

    await new Promise((resolve, reject) => {
        setTimeout(() => reject(new Error(`timed out`)), 20000);

        let count = 0;
        const listener = (feedId, msg) => {
            assert.deepStrictEqual(feedId, 'mock:feed1');
            assert.strictEqual(msg.sender, 'mock-account:user1');
            assert.strictEqual(msg.type, 'app');

            switch (count) {
            case 0:
                assert.deepStrictEqual(msg.json, {
                    v: 3,
github stanford-oval / genie-toolkit / tool / typecheck.js View on Github external
async _learn(line) {
        try {
            const program = await ThingTalk.Grammar.parseAndTypecheck(line, this._schemas, false);

            const clone = {};
            Object.assign(clone, this._entities);
            const code = ThingTalk.NNSyntax.toNN(program, this._current.preprocessed, clone).join(' ');

            const cacheEntry = { from: this._current.target_code, to: code };
            this._cache.set(this._current.target_code, cacheEntry);
            if (this._cacheOut)
                this._cacheOut.write(cacheEntry);
            this._current.target_code = code;
            this._resolve(true);
        } catch(e) {
            console.log(e.name + ': ' + e.message);
            this._rl.prompt();
        }
    }
github stanford-oval / almond-gnome / service / main.js View on Github external
async function loadAllExamples(kind) {
    const datasetCode = await _engine.thingpedia.getExamplesByKinds([kind], true);
    const parsed = await ThingTalk.Grammar.parseAndTypecheck(datasetCode, _engine.schemas);
    const dataset = parsed.datasets[0];
    let output = [];
    for (let ex of dataset.examples) {
        const loaded = loadOneExample(ex);
        if (loaded !== null)
            output.push(loaded);
    }
    return output;
}