"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataAugmenter = void 0;
var DataAugmenter = /** @class */ (function () {
    function DataAugmenter(augmenter, entities) {
        this.augmenter = augmenter;
        this.entities = entities;
        // eslint-disable-next-line no-useless-escape
        this.KEYWORD_PATTERN = /(\[([^\[\]\(\)]*?)\])(?:[^\(]|$)/;
    }
    DataAugmenter.prototype.augment = function (sentences) {
        var _this = this;
        return sentences
            .map(function (s) { return _this.augmentSentence(s); })
            .reduce(function (augmentedSentences, augmentations) {
            return augmentedSentences.concat(augmentations);
        }, []);
    };
    DataAugmenter.prototype.augmentSentence = function (sentence) {
        var finalSentences = [];
        var unprocessedSentences = [sentence];
        while (unprocessedSentences.length > 0) {
            var processedSentence = unprocessedSentences.pop();
            var variations = this.generateVariations(processedSentence);
            if (variations.length == 0) {
                finalSentences.push(processedSentence);
            }
            else {
                unprocessedSentences = unprocessedSentences.concat(variations);
            }
        }
        return finalSentences;
    };
    DataAugmenter.prototype.generateVariations = function (sentence) {
        var _this = this;
        var match = this.KEYWORD_PATTERN.exec(sentence);
        if (match) {
            var start_1 = match.index;
            var definition_1 = match[1];
            var keyword_1 = match[2];
            if (keyword_1 in this.augmenter) {
                return this.augmenter[keyword_1].map(function (word) {
                    return _this.createVariation(sentence, word, start_1, definition_1, keyword_1);
                });
            }
            else {
                throw new Error("Unable to augment the data. Undefined keyword: " + keyword_1);
            }
        }
        return [];
    };
    DataAugmenter.prototype.createVariation = function (sentence, word, start, definition, keyword) {
        return sentence
            .slice(0, start)
            .concat(this.entities.includes(keyword) ? "[" + word + "](" + keyword + ")" : word, sentence.slice(start + definition.length));
    };
    return DataAugmenter;
}());
exports.DataAugmenter = DataAugmenter;
//# sourceMappingURL=data-augmenter.js.map