From d6aa6737821f5db61e932714322f2401f86b5200 Mon Sep 17 00:00:00 2001 From: James Maa Date: Thu, 23 May 2024 15:23:10 -0700 Subject: Basic Spanish Transforms (#908) * Spanish transforms * Add more spanish transforms * Address comments * Fix types * Undo prefix change --- ext/js/language/es/spanish-transforms.js | 171 +++++++++++++++++++++++++++++++ ext/js/language/language-descriptors.js | 4 +- ext/js/language/language-transformer.js | 2 +- ext/js/language/language-transforms.js | 18 ++++ 4 files changed, 193 insertions(+), 2 deletions(-) create mode 100644 ext/js/language/es/spanish-transforms.js (limited to 'ext/js/language') diff --git a/ext/js/language/es/spanish-transforms.js b/ext/js/language/es/spanish-transforms.js new file mode 100644 index 00000000..cf145f6a --- /dev/null +++ b/ext/js/language/es/spanish-transforms.js @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2024 Yomitan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import {suffixInflection, wholeWordInflection} from '../language-transforms.js'; + +const ACCENTS = new Map([ + ['a', 'á'], + ['e', 'é'], + ['i', 'í'], + ['o', 'ó'], + ['u', 'ú'] +]); + + +/** + * @param {string} char + * @returns {string} + */ +function addAccent(char) { + return ACCENTS.get(char) || char; +} + +/** @type {import('language-transformer').LanguageTransformDescriptor} */ +export const spanishTransforms = { + language: 'es', + conditions: { + v: { + name: 'Verb', + isDictionaryForm: true, + subConditions: ['v_ar', 'v_er', 'v_ir'] + }, + v_ar: { + name: '-ar verb', + isDictionaryForm: true + }, + v_er: { + name: '-er verb', + isDictionaryForm: true + }, + v_ir: { + name: '-ir verb', + isDictionaryForm: true + }, + n: { + name: 'Noun', + isDictionaryForm: true, + subConditions: ['ns', 'np'] + }, + np: { + name: 'Noun plural', + isDictionaryForm: true + }, + ns: { + name: 'Noun singular', + isDictionaryForm: true + }, + adj: { + name: 'Adjective', + isDictionaryForm: true + } + }, + transforms: [ + { + name: 'plural', + description: 'Plural form of a noun', + rules: [ + suffixInflection('s', '', ['np'], ['ns']), + suffixInflection('es', '', ['np'], ['ns']), + suffixInflection('ces', 'z', ['np'], ['ns']), // 'lápices' -> lápiz + ...[...'aeiou'].map((v) => suffixInflection(`${v}ses`, `${addAccent(v)}s`, ['np'], ['ns'])), // 'autobuses' -> autobús + ...[...'aeiou'].map((v) => suffixInflection(`${v}nes`, `${addAccent(v)}n`, ['np'], ['ns'])) // 'canciones' -> canción + ] + }, + { + name: 'feminine adjective', + description: 'feminine form of an adjective', + rules: [ + suffixInflection('a', 'o', ['adj'], ['adj']) + ] + }, + { + name: 'present indicative', + description: 'Present indicative form of a verb', + rules: [ + // -ar verbs + suffixInflection('o', 'ar', ['v'], ['v']), + suffixInflection('as', 'ar', ['v'], ['v']), + suffixInflection('a', 'ar', ['v'], ['v']), + suffixInflection('amos', 'ar', ['v'], ['v']), + suffixInflection('áis', 'ar', ['v'], ['v']), + suffixInflection('an', 'ar', ['v'], ['v']), + // -er verbs + suffixInflection('o', 'er', ['v'], ['v']), + suffixInflection('es', 'er', ['v'], ['v']), + suffixInflection('e', 'er', ['v'], ['v']), + suffixInflection('emos', 'er', ['v'], ['v']), + suffixInflection('éis', 'er', ['v'], ['v']), + suffixInflection('en', 'er', ['v'], ['v']), + // -ir verbs + suffixInflection('o', 'ir', ['v'], ['v']), + suffixInflection('es', 'ir', ['v'], ['v']), + suffixInflection('e', 'ir', ['v'], ['v']), + suffixInflection('imos', 'ir', ['v'], ['v']), + suffixInflection('ís', 'ir', ['v'], ['v']), + suffixInflection('en', 'ir', ['v'], ['v']), + // -tener verbs + suffixInflection('tengo', 'tener', ['v'], ['v']), + suffixInflection('tienes', 'tener', ['v'], ['v']), + suffixInflection('tiene', 'tener', ['v'], ['v']), + suffixInflection('tenemos', 'tener', ['v'], ['v']), + suffixInflection('tenéis', 'tener', ['v'], ['v']), + suffixInflection('tienen', 'tener', ['v'], ['v']), + // Verbs with Irregular Yo Forms + // -guir, -ger, or -gir verbs + suffixInflection('go', 'guir', ['v'], ['v']), + suffixInflection('jo', 'ger', ['v'], ['v']), + suffixInflection('jo', 'gir', ['v'], ['v']), + suffixInflection('aigo', 'aer', ['v'], ['v']), + suffixInflection('zco', 'cer', ['v'], ['v']), + suffixInflection('zco', 'cir', ['v'], ['v']), + suffixInflection('hago', 'hacer', ['v'], ['v']), + suffixInflection('pongo', 'poner', ['v'], ['v']), + suffixInflection('lgo', 'lir', ['v'], ['v']), + suffixInflection('lgo', 'ler', ['v'], ['v']), + wholeWordInflection('quepo', 'caber', ['v'], ['v']), + wholeWordInflection('doy', 'dar', ['v'], ['v']), + wholeWordInflection('sé', 'saber', ['v'], ['v']), + wholeWordInflection('veo', 'ver', ['v'], ['v']), + // Ser, estar, ir, haber + wholeWordInflection('soy', 'ser', ['v'], ['v']), + wholeWordInflection('eres', 'ser', ['v'], ['v']), + wholeWordInflection('es', 'ser', ['v'], ['v']), + wholeWordInflection('somos', 'ser', ['v'], ['v']), + wholeWordInflection('sois', 'ser', ['v'], ['v']), + wholeWordInflection('son', 'ser', ['v'], ['v']), + wholeWordInflection('estoy', 'estar', ['v'], ['v']), + wholeWordInflection('estás', 'estar', ['v'], ['v']), + wholeWordInflection('está', 'estar', ['v'], ['v']), + wholeWordInflection('estamos', 'estar', ['v'], ['v']), + wholeWordInflection('estáis', 'estar', ['v'], ['v']), + wholeWordInflection('están', 'estar', ['v'], ['v']), + wholeWordInflection('voy', 'ir', ['v'], ['v']), + wholeWordInflection('vas', 'ir', ['v'], ['v']), + wholeWordInflection('va', 'ir', ['v'], ['v']), + wholeWordInflection('vamos', 'ir', ['v'], ['v']), + wholeWordInflection('vais', 'ir', ['v'], ['v']), + wholeWordInflection('van', 'ir', ['v'], ['v']), + wholeWordInflection('he', 'haber', ['v'], ['v']), + wholeWordInflection('has', 'haber', ['v'], ['v']), + wholeWordInflection('ha', 'haber', ['v'], ['v']), + wholeWordInflection('hemos', 'haber', ['v'], ['v']), + wholeWordInflection('habéis', 'haber', ['v'], ['v']), + wholeWordInflection('han', 'haber', ['v'], ['v']) + ] + } + ] +}; diff --git a/ext/js/language/language-descriptors.js b/ext/js/language/language-descriptors.js index baf53f81..3a78aff5 100644 --- a/ext/js/language/language-descriptors.js +++ b/ext/js/language/language-descriptors.js @@ -19,6 +19,7 @@ import {removeArabicScriptDiacritics} from './ar/arabic-text-preprocessors.js'; import {eszettPreprocessor} from './de/german-text-preprocessors.js'; import {germanTransforms} from './de/german-transforms.js'; import {englishTransforms} from './en/english-transforms.js'; +import {spanishTransforms} from './es/spanish-transforms.js'; import { alphabeticToHiragana, alphanumericWidthVariants, @@ -78,7 +79,8 @@ const languageDescriptors = [ iso: 'es', name: 'Spanish', exampleText: 'acabar de', - textPreprocessors: capitalizationPreprocessors + textPreprocessors: capitalizationPreprocessors, + languageTransforms: spanishTransforms }, { iso: 'fa', diff --git a/ext/js/language/language-transformer.js b/ext/js/language/language-transformer.js index 47f31b5f..f859ebf2 100644 --- a/ext/js/language/language-transformer.js +++ b/ext/js/language/language-transformer.js @@ -132,7 +132,7 @@ export class LanguageTransformer { const isCycle = trace.some((frame) => frame.transform === name && frame.ruleIndex === j && frame.text === text); if (isCycle) { - log.warn(new Error(`Cycle detected in transform[${name}] rule[${j}] for text: ${text}`)); + log.warn(new Error(`Cycle detected in transform[${name}] rule[${j}] for text: ${text}\nTrace: ${JSON.stringify(trace)}`)); continue; } diff --git a/ext/js/language/language-transforms.js b/ext/js/language/language-transforms.js index ee8af88b..f3e36560 100644 --- a/ext/js/language/language-transforms.js +++ b/ext/js/language/language-transforms.js @@ -52,3 +52,21 @@ export function prefixInflection(inflectedPrefix, deinflectedPrefix, conditionsI conditionsOut }; } + +/** + * @param {string} inflectedWord + * @param {string} deinflectedWord + * @param {string[]} conditionsIn + * @param {string[]} conditionsOut + * @returns {import('language-transformer').Rule} + */ +export function wholeWordInflection(inflectedWord, deinflectedWord, conditionsIn, conditionsOut) { + const regex = new RegExp('^' + inflectedWord + '$'); + return { + type: 'wholeWord', + isInflected: regex, + deinflect: () => deinflectedWord, + conditionsIn, + conditionsOut + }; +} -- cgit v1.2.3