Skip to content

Commit

Permalink
stricten condition types
Browse files Browse the repository at this point in the history
  • Loading branch information
Casheeew committed Jul 15, 2024
1 parent 506ecc1 commit b6112c3
Show file tree
Hide file tree
Showing 11 changed files with 492 additions and 450 deletions.
86 changes: 46 additions & 40 deletions ext/js/language/en/english-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

import {prefixInflection, suffixInflection} from '../language-transforms.js';

/** @typedef {keyof typeof conditions} Condition */

/**
* @param {string} consonants
* @param {string} suffix
* @param {string[]} conditionsIn
* @param {string[]} conditionsOut
* @returns {import('language-transformer').SuffixRule[]}
* @param {Condition[]} conditionsIn
* @param {Condition[]} conditionsOut
* @returns {import('language-transformer').SuffixRule<Condition>[]}
*/
function doubledConsonantInflection(consonants, suffix, conditionsIn, conditionsOut) {
const inflections = [];
Expand Down Expand Up @@ -64,7 +66,9 @@ const phrasalVerbPrepositions = ['aback', 'about', 'above', 'across', 'after', '
const particlesDisjunction = phrasalVerbParticles.join('|');
const phrasalVerbWordSet = new Set([...phrasalVerbParticles, ...phrasalVerbPrepositions]);
const phrasalVerbWordDisjunction = [...phrasalVerbWordSet].join('|');
/** @type {import('language-transformer').Rule} */
/**
* @type {import('language-transformer').Rule<Condition>}
*/
const phrasalVerbInterposedObjectRule = {
type: 'other',
isInflected: new RegExp(`^\\w* (?:(?!\\b(${phrasalVerbWordDisjunction})\\b).)+ (?:${particlesDisjunction})`),
Expand All @@ -78,7 +82,7 @@ const phrasalVerbInterposedObjectRule = {
/**
* @param {string} inflected
* @param {string} deinflected
* @returns {import('language-transformer').Rule}
* @returns {import('language-transformer').Rule<Condition>}
*/
function createPhrasalVerbInflection(inflected, deinflected) {
return {
Expand All @@ -93,8 +97,8 @@ function createPhrasalVerbInflection(inflected, deinflected) {
}

/**
* @param {import('language-transformer').SuffixRule[]} sourceRules
* @returns {import('language-transformer').Rule[]}
* @param {import('language-transformer').SuffixRule<Condition>[]} sourceRules
* @returns {import('language-transformer').Rule<Condition>[]}
*/
function createPhrasalVerbInflectionsFromSuffixInflections(sourceRules) {
return sourceRules.flatMap(({isInflected, deinflected}) => {
Expand All @@ -105,41 +109,43 @@ function createPhrasalVerbInflectionsFromSuffixInflections(sourceRules) {
});
}

/** @type {import('language-transformer').LanguageTransformDescriptor} */
const conditions = {
v: {
name: 'Verb',
isDictionaryForm: true,
subConditions: ['v_phr'],
},
v_phr: {
name: 'Phrasal verb',
isDictionaryForm: true,
},
n: {
name: 'Noun',
isDictionaryForm: true,
subConditions: ['np', 'ns'],
},
np: {
name: 'Noun plural',
isDictionaryForm: true,
},
ns: {
name: 'Noun singular',
isDictionaryForm: true,
},
adj: {
name: 'Adjective',
isDictionaryForm: true,
},
adv: {
name: 'Adverb',
isDictionaryForm: true,
},
};

/** @type {import('language-transformer').LanguageTransformDescriptor<Condition>} */
export const englishTransforms = {
language: 'en',
conditions: {
v: {
name: 'Verb',
isDictionaryForm: true,
subConditions: ['v_phr'],
},
v_phr: {
name: 'Phrasal verb',
isDictionaryForm: true,
},
n: {
name: 'Noun',
isDictionaryForm: true,
subConditions: ['np', 'ns'],
},
np: {
name: 'Noun plural',
isDictionaryForm: true,
},
ns: {
name: 'Noun singular',
isDictionaryForm: true,
},
adj: {
name: 'Adjective',
isDictionaryForm: true,
},
adv: {
name: 'Adverb',
isDictionaryForm: true,
},
},
conditions,
transforms: {
'plural': {
name: 'plural',
Expand Down
76 changes: 39 additions & 37 deletions ext/js/language/es/spanish-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,47 @@ function addAccent(char) {
return ACCENTS.get(char) || char;
}

/** @type {import('language-transformer').LanguageTransformDescriptor} */
const conditions = {
n: {
name: 'Noun',
isDictionaryForm: true,
subConditions: ['ns', 'np'],
},
np: {
name: 'Noun plural',
isDictionaryForm: false,
},
ns: {
name: 'Noun singular',
isDictionaryForm: false,
},
v: {
name: 'Verb',
isDictionaryForm: true,
subConditions: ['v_ar', 'v_er', 'v_ir'],
},
v_ar: {
name: '-ar verb',
isDictionaryForm: false,
},
v_er: {
name: '-er verb',
isDictionaryForm: false,
},
v_ir: {
name: '-ir verb',
isDictionaryForm: false,
},
adj: {
name: 'Adjective',
isDictionaryForm: true,
},
};

/** @type {import('language-transformer').LanguageTransformDescriptor<keyof typeof conditions>} */
export const spanishTransforms = {
language: 'es',
conditions: {
n: {
name: 'Noun',
isDictionaryForm: true,
subConditions: ['ns', 'np'],
},
np: {
name: 'Noun plural',
isDictionaryForm: false,
},
ns: {
name: 'Noun singular',
isDictionaryForm: false,
},
v: {
name: 'Verb',
isDictionaryForm: true,
subConditions: ['v_ar', 'v_er', 'v_ir'],
},
v_ar: {
name: '-ar verb',
isDictionaryForm: false,
},
v_er: {
name: '-er verb',
isDictionaryForm: false,
},
v_ir: {
name: '-ir verb',
isDictionaryForm: false,
},
adj: {
name: 'Adjective',
isDictionaryForm: true,
},
},
conditions,
transforms: {
'plural': {
name: 'plural',
Expand Down
Loading

0 comments on commit b6112c3

Please sign in to comment.