"use strict";
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 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: Silei Xu <silei@cs.stanford.edu>
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.execute = exports.initArgparse = void 0;
const fs = __importStar(require("fs"));
const assert_1 = __importDefault(require("assert"));
const util_1 = __importDefault(require("util"));
const ThingTalk = __importStar(require("thingtalk"));
const StreamUtils = __importStar(require("../../lib/utils/stream-utils"));
const constant_file_1 = require("../lib/constant-file");
const annotation_generator_1 = __importDefault(require("./lib/annotation-generator"));
async function loadClassDefs(thingpedia) {
    const library = ThingTalk.Syntax.parse(await util_1.default.promisify(fs.readFile)(thingpedia, { encoding: 'utf8' }));
    assert_1.default(library instanceof ThingTalk.Ast.Library);
    return library.classes;
}
function initArgparse(subparsers) {
    const parser = subparsers.add_parser('auto-annotate', {
        add_help: true,
        description: "Automatically generate annotations including canonicals"
    });
    parser.add_argument('--dataset', {
        required: true,
        choices: ['schemaorg', 'sgd', 'multiwoz', 'wikidata', 'custom'],
        help: 'The dataset to run autoQA on.'
    });
    parser.add_argument('-o', '--output', {
        required: true,
        type: fs.createWriteStream
    });
    parser.add_argument('-l', '--locale', {
        default: 'en-US',
        help: `BGP 47 locale tag of the natural language being processed (defaults to en-US).`
    });
    parser.add_argument('--constants', {
        required: true,
        help: 'TSV file containing constant values to use.'
    });
    parser.add_argument('--thingpedia', {
        required: true,
        help: 'Path to ThingTalk file containing class definitions.'
    });
    parser.add_argument('--functions', {
        required: false,
        default: null,
        help: `List of functions to include, split by comma (no space). Include all functions if not specified`,
    });
    parser.add_argument('--remove-existing-canonicals', {
        action: 'store_true',
        help: 'Remove all existing canonical annotations in the schema',
        default: false
    });
    parser.add_argument('--algorithms', {
        nargs: '*',
        help: 'Different algorithms to generate canonicals including bert-property-synonyms, bart-paraphrase, bert-adjectives, bert-domain-synonyms'
    });
    parser.add_argument('--batch-size', {
        required: false,
        type: Number,
        default: 64,
        help: `The batch size for auto paraphrase`
    });
    parser.add_argument('--pruning', {
        required: false,
        type: Number,
        default: 0.5,
        help: `The minimum fraction required for a candidate to be added`
    });
    parser.add_argument('--parameter-datasets', {
        required: true,
        help: `TSV file containing the paths to datasets for strings and entity types.`
    });
    parser.add_argument('--language-model', {
        required: false,
        default: 'bert-large-uncased',
        help: `A string specifying a model name recognizable by the Transformers package (e.g. bert-base-uncased), `
            + `or a path to the directory where the model is saved`
    });
    parser.add_argument('--paraphraser-model', {
        required: false,
        help: `A path to the directory where the bart paraphraser model is saved`
    });
    parser.add_argument('--gpt2-ordering', {
        required: false,
        default: false,
        action: 'store_true',
        help: `Set to True to use gpt2 to decide where to put value`
    });
    parser.add_argument('--filtering', {
        required: false,
        default: false,
        action: 'store_true',
        help: `Enable filtering for phrase extraction.`
    });
    parser.add_argument('--debug', {
        required: false,
        default: false,
        action: 'store_true',
        help: `Enable debugging, which will output intermediate result into files.`
    });
}
exports.initArgparse = initArgparse;
async function execute(args) {
    const classDefs = await loadClassDefs(args.thingpedia);
    if (!args.algorithms) {
        for (let classDef of classDefs)
            args.output.write(classDef.prettyprint() + '\n');
        args.output.end();
    }
    else {
        const options = args;
        const constants = await constant_file_1.parseConstantFile(args.locale, args.constants);
        const functions = args.functions ? args.functions.split(',') : null;
        for (let classDef of classDefs) {
            const generator = new annotation_generator_1.default(classDef, constants, functions, args.parameter_datasets, options);
            const annotatedClassDef = await generator.generate();
            args.output.write(annotatedClassDef.prettyprint());
        }
        args.output.end();
    }
    StreamUtils.waitFinish(args.output);
}
exports.execute = execute;
//# sourceMappingURL=auto-annotate.js.map