@jirimracek/conjugate-esp
Version:
Spanish verb conjugator, castellano, voseo, canarias, formal
198 lines • 7.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Conjugator = void 0;
const definitions_json_1 = __importDefault(require("../data/definitions.json"));
const factory_1 = require("./factory");
const stringutils_1 = require("./stringutils");
class Conjugator {
constructor(ortho = '2010', highlightMarks) {
this.templates = definitions_json_1.default;
this.factory = new factory_1.ModelFactory();
this.ortho = '2010';
this.highlightMarks = { start: '<mark>', end: '</mark>', del: '\u2027' };
this.highlight = false;
this.setOrthography(ortho);
if (highlightMarks) {
this.highlightMarks = highlightMarks;
}
}
setOrthography(ortho) {
if (ortho === '1999' || ortho === '2010') {
this.ortho = ortho;
}
else {
console.error(`Ignoring invalid orthography (${ortho}) `);
}
}
useHighlight(use = null) {
if (this.highlightMarks.start + this.highlightMarks.end + this.highlightMarks.del === '') {
this.highlight = false;
return;
}
this.highlight = use;
}
conjugateSync(verb, region = 'castellano') {
const result = [];
try {
if (!this.getVerbListSync().includes(verb)) {
throw new Error(`Unknown verb ${verb}`);
}
if (!['castellano', 'voseo', 'canarias', 'formal'].includes(region)) {
throw new Error(`Unknown region ${region}`);
}
const modelTemplates = [];
const modelData = this.templates[verb];
const models = [];
if (typeof modelData === 'string' || !Array.isArray(modelData)) {
models.push(modelData);
}
else {
models.push(...modelData);
}
models.forEach(model => {
if (typeof model === 'string') {
modelTemplates.push([model, region, {}]);
}
else {
Object.entries(model).forEach(([name, attributes]) => {
const attrs = attributes;
if (this.ortho !== '2010' || typeof attrs['M'] === 'undefined' ||
attrs['M'] !== 'false') {
modelTemplates.push([name, region, attrs]);
}
});
}
});
modelTemplates.forEach(template => {
const [modelName, region, attributes] = template;
const model = this.factory.getModel(verb, modelName, region, attributes);
const info = {
model: modelName,
region: region,
};
if (attributes['D']) {
info.defective = true;
}
if (typeof attributes['M'] !== 'undefined') {
if (attributes.M === 'true') {
info.ortho = '2010';
}
else {
info.ortho = '1999';
}
}
const conjugated = model.getConjugation();
if (!['hablar', 'temer', 'partir'].includes(modelName) && this.highlight !== false) {
const simulatedModel = this.factory.getModel(verb, modelName, region, {}, true);
stringutils_1.insertTags(simulatedModel.getConjugation(), conjugated, this.highlightMarks, this.highlight);
}
result.push({
info,
conjugation: conjugated
});
});
return result;
}
catch (error) {
return error.message;
}
}
conjugate(verb, region = 'castellano') {
return new Promise((resolve, reject) => {
const result = this.conjugateSync(verb, region);
if (Array.isArray(result)) {
resolve(result);
}
else {
reject(result);
}
});
}
getVerbListSync() {
return Object.keys(this.templates).sort(function (a, b) { return a.localeCompare(b); });
}
getVerbList() {
return new Promise(resolve => resolve(this.getVerbListSync()));
}
getModelsSync() {
return this.factory.getModels();
}
getModels() {
return new Promise(resolve => resolve(this.factory.getModels()));
}
getDefectiveVerbListSync(pure = false) {
const listAll = [];
const listPure = [];
Object.entries(this.templates).forEach(([verb, modelsData]) => {
let count = 0;
const models = [];
if (typeof modelsData === 'string' || !Array.isArray(modelsData)) {
models.push(modelsData);
}
else {
models.push(...modelsData);
}
models.forEach(model => {
if (typeof model !== 'string') {
Object.values(model).forEach(value => {
Object.keys(value).forEach(attrKey => {
if (attrKey === 'D')
++count;
});
});
}
});
if (count === models.length) {
listPure.push(verb);
}
if (count > 0) {
listAll.push(verb);
}
});
if (pure) {
return [listAll.sort((a, b) => a.localeCompare(b)),
listPure.sort((a, b) => a.localeCompare(b))];
}
else {
return listAll.sort((a, b) => a.localeCompare(b));
}
}
getDefectiveVerbList(exact = false) {
return new Promise(resolve => resolve(this.getDefectiveVerbListSync(exact)));
}
getOrthoVerbListSync() {
const set = new Set();
Object.entries(this.templates).forEach(([verb, modelsData]) => {
const models = [];
if (typeof modelsData === 'string' || !Array.isArray(modelsData)) {
models.push(modelsData);
}
else {
models.push(...modelsData);
}
models.forEach(model => {
if (typeof model !== 'string') {
Object.values(model).forEach(value => {
Object.keys(value).forEach(attrKey => {
if (attrKey === 'M') {
set.add(verb);
}
});
});
}
});
});
return Array.from(set).sort((a, b) => a.localeCompare(b));
}
getOrthoVerbList() {
return new Promise(resolve => resolve(this.getOrthoVerbListSync()));
}
getVersion() {
return 'v2.3.6, Sun 03 Jan 2021 07:14:54 PM CET';
}
}
exports.Conjugator = Conjugator;
//# sourceMappingURL=conjugator.js.map