"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: 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 stream_1 = __importDefault(require("stream"));
const csv_parse_1 = __importDefault(require("csv-parse"));
const csv_stringify_1 = __importDefault(require("csv-stringify"));
const Tp = __importStar(require("thingpedia"));
const ThingTalk = __importStar(require("thingtalk"));
const validator_1 = require("../lib/dataset-tools/mturk/validator");
const parsers_1 = require("../lib/dataset-tools/parsers");
const StreamUtils = __importStar(require("../lib/utils/stream-utils"));
const MT = __importStar(require("./lib/mturk-parsers"));
const constants_1 = require("./lib/constants");
function initArgparse(subparsers) {
    const parser = subparsers.add_parser('mturk-validate', {
        add_help: true,
        description: "Validate the result of MTurk paraphrasing and validation."
    });
    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('--contextual', {
        action: 'store_true',
        help: 'Process a contextual dataset.',
        default: false
    });
    parser.add_argument('--paraphrasing-input', {
        required: true,
        help: 'CSV file containing the output from MTurk paraphrasing.'
    });
    parser.add_argument('--validation-input', {
        required: false,
        help: 'CSV file containing the output from MTurk validation.'
    });
    parser.add_argument('--paraphrasing-rejects', {
        required: false,
        help: 'CSV file in which to write rejections for MTurk paraphrasing.'
    });
    parser.add_argument('--validation-rejects', {
        required: false,
        help: 'CSV file in which to write rejections for MTurk validation.'
    });
    parser.add_argument('--validation-count', {
        required: false,
        type: Number,
        default: constants_1.NUM_SUBMISSIONS_PER_TASK,
        help: 'Number of workers voting on each paraphrase.'
    });
    parser.add_argument('--validation-threshold', {
        required: true,
        type: Number,
        help: 'Number of workers that must approve of each paraphrase.'
    });
    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('--id-prefix', {
        required: false,
        default: '',
        help: "Prefix to the id of each example"
    });
    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.',
    });
}
exports.initArgparse = initArgparse;
async function execute(args) {
    const tpClient = new Tp.FileClient(args);
    const schemaRetriever = new ThingTalk.SchemaRetriever(tpClient, null, !args.debug);
    let validationRejects = Promise.resolve();
    let validationCounts;
    if (args.validation_threshold > 0) {
        if (!args.validation_input)
            throw new Error(`Argument --validation-input is required when performing manual validation`);
        const validationInput = fs.createReadStream(args.validation_input)
            .pipe(csv_parse_1.default({
            columns: true,
            delimiter: ',',
            relax_column_count: true
        }))
            .pipe(new MT.ValidationRejecter({
            sentencesPerTask: args.sentences_per_task
        }));
        if (args.validation_rejects) {
            validationRejects = StreamUtils.waitFinish(validationInput
                .pipe(csv_stringify_1.default({ header: true, delimiter: ',' }))
                .pipe(fs.createWriteStream(args.validation_rejects)));
        }
        validationCounts = await validationInput
            .pipe(new MT.ValidationParser({
            sentencesPerTask: args.sentences_per_task,
            targetSize: args.paraphrases_per_sentence * args.submissions_per_task,
            skipRejected: true
        }))
            .pipe(new MT.ValidationCounter({
            targetNumVotes: args.validation_count
        }))
            .pipe(new StreamUtils.MapAccumulator()).read();
    }
    const rejectedPara = fs.createReadStream(args.paraphrasing_input)
        .pipe(csv_parse_1.default({
        columns: true,
        delimiter: ',',
        relax_column_count: true
    }))
        .pipe(new MT.ParaphrasingRejecter(schemaRetriever, {
        sentencesPerTask: args.sentences_per_task,
        paraphrasesPerSentence: args.paraphrases_per_sentence,
        locale: args.locale,
        contextual: args.contextual
    }));
    let paraphrasingRejects;
    if (args.paraphrasing_rejects) {
        paraphrasingRejects = StreamUtils.waitFinish(rejectedPara
            .pipe(csv_stringify_1.default({ header: true, delimiter: ',' }))
            .pipe(fs.createWriteStream(args.paraphrasing_rejects)));
    }
    else {
        paraphrasingRejects = Promise.resolve();
    }
    rejectedPara
        .pipe(new MT.ParaphrasingParser({
        sentencesPerTask: args.sentences_per_task,
        paraphrasesPerSentence: args.paraphrases_per_sentence,
        contextual: args.contextual,
        skipRejected: true
    }))
        .pipe(new validator_1.ParaphraseValidatorFilter(schemaRetriever, {
        locale: args.locale,
        debug: args.debug,
        validationCounts,
        validationThreshold: args.validation_threshold
    }))
        .pipe(new stream_1.default.Transform({
        objectMode: true,
        transform(ex, encoding, callback) {
            if (args.contextual) {
                callback(null, {
                    id: `${args.id_prefix}${ex.id}`,
                    context: ex.context_preprocessed,
                    preprocessed: ex.preprocessed,
                    target_code: ex.target_preprocessed
                });
            }
            else {
                callback(null, {
                    id: `${args.id_prefix}${ex.id}`,
                    preprocessed: ex.preprocessed,
                    target_code: ex.target_preprocessed
                });
            }
        },
        flush(callback) {
            callback();
        }
    }))
        .pipe(new parsers_1.DatasetStringifier())
        .pipe(args.output);
    await Promise.all([
        StreamUtils.waitFinish(args.output),
        validationRejects,
        paraphrasingRejects
    ]);
}
exports.execute = execute;
//# sourceMappingURL=mturk-validate.js.map