"use strict";
// -*- mode: typescript; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Genie
//
// Copyright 2019-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>
//         Giovanni Campagna <gcampagn@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 csv_parse_1 = __importDefault(require("csv-parse"));
const csv_stringify_1 = __importDefault(require("csv-stringify"));
const seedrandom_1 = __importDefault(require("seedrandom"));
const Tp = __importStar(require("thingpedia"));
const ThingTalk = __importStar(require("thingtalk"));
const validator_1 = require("../lib/dataset-tools/mturk/validator");
const paraphrase_validation_1 = __importDefault(require("../lib/dataset-tools/mturk/paraphrase-validation"));
const stream_utils_1 = require("../lib/utils/stream-utils");
const mturk_parsers_1 = require("./lib/mturk-parsers");
const constants_1 = require("./lib/constants");
function initArgparse(subparsers) {
    const parser = subparsers.add_parser('mturk-make-validation-hits', {
        add_help: true,
        description: "Prepare the input file for the manual validation HITs."
    });
    parser.add_argument('-o', '--output', {
        required: true,
        type: fs.createWriteStream
    });
    parser.add_argument('-l', '--locale', {
        required: false,
        default: 'en-US',
        help: `BGP 47 locale tag of the language to generate (defaults to 'en-US', English)`
    });
    parser.add_argument('--thingpedia', {
        required: true,
        help: 'Path to ThingTalk file containing class definitions.'
    });
    parser.add_argument('--sentences-per-task', {
        required: false,
        type: Number,
        default: constants_1.NUM_SENTENCES_PER_TASK,
        help: "Number of sentences in each HIT"
    });
    parser.add_argument('--submissions-per-task', {
        required: false,
        type: Number,
        default: constants_1.NUM_SUBMISSIONS_PER_TASK,
        help: "Number of submissions (workers) for each HIT"
    });
    parser.add_argument('--paraphrases-per-sentence', {
        required: false,
        type: Number,
        default: constants_1.NUM_PARAPHRASES_PER_SENTENCE,
        help: "Number of paraphrases collected for each sentence"
    });
    parser.add_argument('--debug', {
        action: 'store_true',
        help: 'Enable debugging.',
        default: true
    });
    parser.add_argument('--no-debug', {
        action: 'store_false',
        dest: 'debug',
        help: 'Disable debugging.',
    });
    parser.add_argument('--random-seed', {
        default: 'almond is awesome',
        help: 'Random seed'
    });
}
exports.initArgparse = initArgparse;
async function execute(args) {
    const tpClient = new Tp.FileClient(args);
    const schemaRetriever = new ThingTalk.SchemaRetriever(tpClient, null, !args.debug);
    const rng = seedrandom_1.default.alea(args.random_seed);
    process.stdin.setEncoding('utf8');
    // read all paraphrases, auto-validate them, then accumulate them in memory
    // so we can sample a fake one to choose
    const accumulator = new stream_utils_1.ArrayAccumulator();
    process.stdin.pipe(csv_parse_1.default({
        columns: true,
        delimiter: ',',
        relax_column_count: true
    }))
        .pipe(new mturk_parsers_1.ParaphrasingParser({
        sentencesPerTask: args.sentences_per_task,
        paraphrasesPerSentence: args.paraphrases_per_sentence,
        contextual: false,
        skipRejected: true
    }))
        .pipe(new validator_1.ParaphraseValidatorFilter(schemaRetriever, {
        locale: args.locale,
        debug: args.debug
    }))
        .pipe(accumulator);
    const batch = await accumulator.read();
    (new stream_utils_1.ArrayStream(batch, { objectMode: true }))
        .pipe(new mturk_parsers_1.ParaphrasingAccumulator(args.paraphrases_per_sentence * args.submissions_per_task))
        .pipe(new paraphrase_validation_1.default(batch, {
        targetSize: args.paraphrases_per_sentence * args.submissions_per_task,
        sentencesPerTask: args.sentences_per_task,
        debug: args.debug,
        rng: rng
    }))
        .pipe(csv_stringify_1.default({ header: true, delimiter: ',' }))
        .pipe(args.output);
    await stream_utils_1.waitFinish(args.output);
}
exports.execute = execute;
//# sourceMappingURL=mturk-make-validation-hits.js.map