"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BotonicNLU = void 0;
var object_tools_1 = require("./util/object-tools");
var preprocessor_1 = require("./preprocessor");
var model_manager_1 = require("./model-manager");
var intents_processor_1 = require("./intents-processor");
var data_reader_1 = require("./data-reader");
var file_system_1 = require("./util/file-system");
var tfjs_node_1 = require("@tensorflow/tfjs-node");
var path_1 = require("path");
var constants_1 = require("./constants");
var fs_1 = require("fs");
var BotonicNLU = /** @class */ (function () {
    function BotonicNLU() {
        this._preprocessor = new preprocessor_1.Preprocessor();
        this._modelManager = new model_manager_1.ModelManager();
        this._intentsProcessor = new intents_processor_1.IntentsProcessor();
        this._dataReader = new data_reader_1.DataReader();
    }
    Object.defineProperty(BotonicNLU.prototype, "normalizer", {
        set: function (value) {
            this._preprocessor.normalizer = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BotonicNLU.prototype, "stemmer", {
        set: function (value) {
            this._preprocessor.stemmer = value;
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(BotonicNLU.prototype, "tokenizer", {
        set: function (value) {
            this._preprocessor.tokenizer = value;
        },
        enumerable: false,
        configurable: true
    });
    BotonicNLU.prototype.loadModelData = function (modelDataPath) {
        var info = file_system_1.readJSON(modelDataPath);
        this._preprocessor.language = info.language;
        this._preprocessor.maxSeqLen = info.maxSeqLen;
        this._preprocessor.vocabulary = info.vocabulary;
        this._intentsProcessor.loadEncoderDecoder(info.intents);
    };
    BotonicNLU.prototype.loadModel = function (modelPath) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this._modelManager.loadModel(modelPath)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    BotonicNLU.prototype.predict = function (sentence) {
        var input = tfjs_node_1.tensor([this._preprocessor.preprocess(sentence)]);
        var intentId = this._modelManager.predict(input);
        var intent = this._intentsProcessor.decode(intentId);
        return intent;
    };
    BotonicNLU.prototype.predictProbabilities = function (sentence) {
        var _this = this;
        var input = tfjs_node_1.tensor([this._preprocessor.preprocess(sentence)]);
        var encodedPrediction = this._modelManager.predictProbabilities(input);
        var decodedPrediction = encodedPrediction.map(function (intentConfidence) {
            return {
                intent: _this._intentsProcessor.decode(intentConfidence.intentId),
                confidence: intentConfidence.confidence,
            };
        });
        return decodedPrediction;
    };
    // TODO: Add loaded info to data structure
    BotonicNLU.prototype.loadFromUtterances = function (path) {
        var utterancesPath = path || path_1.join(process.cwd(), constants_1.NLU_DIR, constants_1.UTTERANCES_DIR);
        var pathLangs = fs_1.readdirSync(utterancesPath);
        pathLangs.forEach(function (locale) {
            var intentsForLang = fs_1.readdirSync(path_1.join(utterancesPath, locale));
            intentsForLang.forEach(function (filename) {
                var intent = filename.replace('.txt', '');
                var utterances = fs_1.readFileSync(path_1.join(utterancesPath, locale, filename), 'utf-8').split('\n');
                utterances.forEach(function (utterance) {
                    // this.addExample({ locale, intent, utterance });
                });
            });
        });
    };
    // TO DO: What to do if the path is not a csv.
    BotonicNLU.prototype.loadData = function (path, language, maxSeqLen) {
        this._data = this._dataReader.readData(path);
        this._preprocessor.language = language;
        this._preprocessor.maxSeqLen = maxSeqLen;
    };
    BotonicNLU.prototype.splitData = function (testPercentage) {
        if (testPercentage === void 0) { testPercentage = 0.25; }
        if (testPercentage > 1 || testPercentage < 0) {
            throw new RangeError('testPercentage should be a number between 0 and 1.');
        }
        var dataSize = this._data.length;
        var testSize = Math.round(dataSize * testPercentage);
        this._data = object_tools_1.shuffle(this._data);
        this._testSet = this._data.slice(0, testSize);
        this._trainSet = this._data.slice(testSize, dataSize);
        this._preprocessor.generateVocabulary(this._trainSet);
        this._intentsProcessor.generateEncoderDecoder(this._trainSet);
    };
    BotonicNLU.prototype.createModel = function (learningRate, wordEmbeddingsType, wordEmbeddingsDimension, trainableEmbeddings) {
        if (wordEmbeddingsType === void 0) { wordEmbeddingsType = '10k-fasttext'; }
        if (wordEmbeddingsDimension === void 0) { wordEmbeddingsDimension = 300; }
        if (trainableEmbeddings === void 0) { trainableEmbeddings = true; }
        return __awaiter(this, void 0, void 0, function () {
            var wordEmbeddingsConfig, parameters;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        wordEmbeddingsConfig = {
                            type: wordEmbeddingsType,
                            dimension: wordEmbeddingsDimension,
                            language: this._preprocessor.language,
                            vocabulary: this._preprocessor.vocabulary,
                        };
                        parameters = {
                            maxSeqLen: this._preprocessor.maxSeqLen,
                            learningRate: learningRate,
                            intentsCount: this._intentsProcessor.intentsCount,
                            trainableEmbeddings: trainableEmbeddings,
                        };
                        return [4 /*yield*/, this._modelManager.createModel(wordEmbeddingsConfig, parameters)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    BotonicNLU.prototype.train = function (epochs, batchSize, validationSplit) {
        if (validationSplit === void 0) { validationSplit = 0.1; }
        return __awaiter(this, void 0, void 0, function () {
            var _a, xTrain, yTrain, parameters;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = this._splitInputOutput(this._trainSet), xTrain = _a[0], yTrain = _a[1];
                        parameters = {
                            X: xTrain,
                            y: yTrain,
                            epochs: epochs,
                            batchSize: batchSize,
                            validationSplit: validationSplit,
                        };
                        return [4 /*yield*/, this._modelManager.train(parameters)];
                    case 1:
                        _b.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    BotonicNLU.prototype.evaluate = function () {
        var _a = this._splitInputOutput(this._testSet), xTest = _a[0], yTest = _a[1];
        var accuracy = this._modelManager.evaluate(xTest, yTest);
        return accuracy;
    };
    BotonicNLU.prototype._splitInputOutput = function (data) {
        var _this = this;
        var x = tfjs_node_1.tensor(data.map(function (sample) { return _this._preprocessor.preprocess(sample.feature); }));
        var y = tfjs_node_1.tensor(data.map(function (sample) { return _this._intentsProcessor.encode(sample.label); }));
        return [x, y];
    };
    BotonicNLU.prototype.saveModel = function () {
        return __awaiter(this, void 0, void 0, function () {
            var modelDir, modelDataPath, modelData;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        modelDir = path_1.join(process.cwd(), 'src', constants_1.NLU_DIR, constants_1.MODELS_DIR, this._preprocessor.language);
                        fs_1.mkdirSync(modelDir, { recursive: true });
                        modelDataPath = path_1.join(modelDir, constants_1.MODEL_DATA_FILENAME);
                        modelData = {
                            language: this._preprocessor.language,
                            intents: this._intentsProcessor.decoder,
                            maxSeqLen: this._preprocessor.maxSeqLen,
                            vocabulary: this._preprocessor.vocabulary,
                        };
                        fs_1.writeFileSync(modelDataPath, JSON.stringify(modelData));
                        return [4 /*yield*/, this._modelManager.saveModel(modelDir)];
                    case 1:
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    return BotonicNLU;
}());
exports.BotonicNLU = BotonicNLU;
//# sourceMappingURL=botonic-nlu.js.map