Skip to content

Commit

Permalink
refactor: remove slow utils (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Jun 23, 2024
1 parent f4f682c commit 25e9f63
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 225 deletions.
9 changes: 3 additions & 6 deletions src/common/fluentVowels.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { ALL_CHARACTERS, CONSONANT_CHARACTERS } from '../constants';

const LETTERS = new Set(ALL_CHARACTERS);
const CONSONANTS = new Set(CONSONANT_CHARACTERS);
import { ALL_LETTERS, ALL_CONSONANTS } from '../substitutions';

export function markFluentVowel(word: string, add: string): string {
let i = 0;
Expand All @@ -26,7 +23,7 @@ export function inferFluentVowel(word: string): string {

while (i > 0) {
const char = word[i];
if (!LETTERS.has(char)) {
if (!ALL_LETTERS.has(char)) {
end = i;
replaced = false;
}
Expand All @@ -53,7 +50,7 @@ function replaceFluentVowel(word: string, j: number): string {
}

function isLastSyllable(word: string, i: number, end: number): boolean {
if (i === end - 2) return CONSONANTS.has(word[i + 1]);
if (i === end - 2) return ALL_CONSONANTS.has(word[i + 1]);
if (i === end - 3) return word[i + 1] === 'n' && word[i + 2] === 'j';
return false;
}
5 changes: 0 additions & 5 deletions src/constants/alphabet.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './alphabet';
export * from './bcp47';
export * from './glagolitic';
11 changes: 6 additions & 5 deletions src/noun/declensionNoun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import { declensionAdjective } from '../adjective';
import { inferFluentVowel, markFluentVowel } from '../common';
import type { Noun } from '../partOfSpeech';
import { matchEnd, removeBrackets, replaceStringAt } from '../utils';
import { removeBrackets, replaceStringAt } from '../utils';
import { establishGender } from './establishGender';

const AEEO = ['a', 'e', 'ę', 'o'];
// endings like -i, -u are not declinable usually
const AEEO$ = /[aeęo]$/;

export function declensionNounFlat(
rawNoun: string,
Expand Down Expand Up @@ -189,7 +190,7 @@ function establish_root(noun: string, gender: string) {
noun.lastIndexOf('ȯ'),
);

const hasVowelEnding = matchEnd(noun, [AEEO]);
const hasVowelEnding = AEEO$.test(noun);

if (noun == 'lėv' || noun == 'lev') {
result = 'ljv';
Expand Down Expand Up @@ -283,7 +284,7 @@ function nominative_sg(noun: string, root: string, gender: string) {
if (gender == 'f2') {
result = root;
}
if (gender == 'f3' && root.lastIndexOf('v') == root.length - 1) {
if (gender == 'f3' && root.endsWith('v')) {
result = root.substring(0, root.length - 1) + 'ȯv';
} else if (gender == 'f3') {
result = noun;
Expand Down Expand Up @@ -361,7 +362,7 @@ function instrumental_sg(root: string, gender: string) {
result = root + 'ojų';
} else if (gender == 'f2') {
result = root + 'jų';
} else if (gender == 'f3' && root.lastIndexOf('v') == root.length - 1) {
} else if (gender == 'f3' && root.endsWith('v')) {
result = root.substring(0, root.length - 1) + 'ȯvjų';
} else if (gender == 'f3') {
result = root + 'jų';
Expand Down
23 changes: 10 additions & 13 deletions src/substitutions.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
export const ALL_CHARACTERS =
'aáàăâåąāæbcćçčdďđḓeéèĕêěëėęēǝfghiíìĭîīıjĵklĺľļłŀljmnńňñņnjoóòŏôöȯǫœpqrŕṙřsśšŠtťṱuúùŭûůũųūvwxyýzźżž'.split(
'',
);
export const ALL_LETTERS = new Set(
'aáàăâåąāæbcćçčdďđḓeéèĕêěëėęēǝfghiíìĭîīıjĵklĺľļłŀljmnńňñņnjoóòŏôöȯǫœpqrŕṙřsśšŠtťṱuúùŭûůũųūvwxyýzźżž',
);

export const ANY = ALL_CHARACTERS;
export const ALL_CONSONANTS = new Set(
'bcćçčdďđḓfghklĺľļłŀljmnńňñņnjpqrŕṙřsśštťṱvwxzźżž',
);

export const CONSONANT_CHARACTERS =
'bcćçčdďđḓfghklĺľļłŀljmnńňñņnjpqrŕṙřsśštťṱvwxzźżž'.split('');
export const ALL_VOWELS = new Set(
'aáàăâåąāæeéèĕêěëėęēǝiíìĭîīıoóòŏôöȯǫœuúùŭûůũųūyý',
);

export const CONSONANT = CONSONANT_CHARACTERS;

export const VOWEL_CHARACTERS =
'aáàăâåąāæeéèĕêěëėęēǝiíìĭîīıoóòŏôöȯǫœuúùŭûůũųūyý'.split('');

export const VOWEL = VOWEL_CHARACTERS;
export const VOWELS = new Set('aåeęěėioȯuųy');

export const LJ_NJ = ['lj', 'nj'];
export const LJj_NJj = ['lj', 'ĺj', 'ľj', 'lj', 'nj', 'ńj', 'ňj', 'ñj', 'nj'];
Expand Down
2 changes: 0 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export * from './areArraysEqual';
export * from './compactArray';
export * from './matchStart';
export * from './matchEnd';
export * from './memoizeLastCall';
export * from './removeBrackets';
export * from './replaceStringAt';
Expand Down
25 changes: 0 additions & 25 deletions src/utils/matchEnd.test.ts

This file was deleted.

51 changes: 0 additions & 51 deletions src/utils/matchEnd.ts

This file was deleted.

36 changes: 0 additions & 36 deletions src/utils/matchStart.test.ts

This file was deleted.

51 changes: 0 additions & 51 deletions src/utils/matchStart.ts

This file was deleted.

52 changes: 22 additions & 30 deletions src/verb/conjugationVerb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
* @source http://steen.free.fr/interslavic/conjugator.html
*/

import { compactArray, matchEnd } from '../utils';
import { compactArray } from '../utils';
import { parsePos, Verb } from '../partOfSpeech';
import {
BIG_YUS,
BIG_YUS_LOOSE,
IOTATED_SMALL_YUS,
SMALL_YUS,
} from '../substitutions';
import { BIG_YUS, IOTATED_SMALL_YUS, SMALL_YUS } from '../substitutions';

const _SE = [' se', ' se'];
const SE_ = ['sę ', 'se '];
const _SE = /. s[eę]$/;
const SE_ = /s[eę] $/;
const EVA_OVA = /[eo]va$/;
const NUU = /..n[uų]$/;
const OUEE = /^..?[eěou]$/;
const BDSZE = /[bdsz]ję$/;
const AEE = /[aeě]$/;
const MEUU = /[meuų-]$/;

const PREFIXES = [
'prědpo',
Expand Down Expand Up @@ -48,16 +49,7 @@ const PREFIXES = [
'v',
];

const NON_REGULAR_VERBS = [
'věděti',
'vedeti',
'jesti',
'jěsti',
'dati',
'dųti',
'byti',
'žegti',
];
const NON_REGULAR_VERBS = /(v[eě]d[eě]ti|j[eě]sti|d[aų]ti|byti|žegti)$/;

const irregular_stems = { da: 1, je: 1, : 1, ja: 1, : 1 };

Expand Down Expand Up @@ -253,9 +245,9 @@ function splitReflexive(inf: string) {

function prefix(inf: string) {
// get prefixes for some non-regular verbs
const irregular = matchEnd(inf, [NON_REGULAR_VERBS]);
if (irregular) {
const maybePrefix = inf.slice(0, -irregular.length);
const match = inf.match(NON_REGULAR_VERBS);
if (match) {
const maybePrefix = inf.slice(0, -match[1].length);
if (PREFIXES.includes(maybePrefix)) {
return maybePrefix;
}
Expand Down Expand Up @@ -370,13 +362,13 @@ function derive_present_tense_stem(infinitive_stem_string: string): string {
result = 'uměĵ';
} else if (result === 'hova') {
result = 'hovaĵ';
} else if (matchEnd(result, [['o', 'e'], 'va'])) {
} else if (EVA_OVA.test(result)) {
result = result.slice(0, -3) + 'uj';
} else if (result.length > 3 && matchEnd(result, ['n', BIG_YUS_LOOSE])) {
} else if (NUU.test(result)) {
result = result.slice(0, -1);
} else if (result.length < 4 && matchEnd(result, [['o', 'u', 'e', 'ě']])) {
} else if (OUEE.test(result)) {
result = result + 'j';
} else if (matchEnd(result, [['b', 'd', 's', 'z'], IOTATED_SMALL_YUS])) {
} else if (BDSZE.test(result)) {
result = result.slice(0, -2) + 'ȯjm';
} else if (result.endsWith(IOTATED_SMALL_YUS)) {
result = result.slice(0, -1) + 'm';
Expand All @@ -386,7 +378,7 @@ function derive_present_tense_stem(infinitive_stem_string: string): string {
result = result.slice(0, -1) /*+ 'm'*/;
} else if (result.endsWith('y')) {
result = result + 'j';
} else if (matchEnd(result, [['a', 'e', 'ě']])) {
} else if (AEE.test(result)) {
result = result + 'ĵ';
}
return result;
Expand All @@ -398,9 +390,9 @@ function present_tense_stem(pref: string, pts: string, is: string) {
if (pts.length == 0) {
result = derive_present_tense_stem(is);
} else {
if (matchEnd(pts, _SE) && pts.length > 3) {
if (_SE.test(pts)) {
pts = pts.slice(0, -3);
} else if (matchEnd(pts, SE_)) {
} else if (SE_.test(pts)) {
pts = pts.slice(3);
}

Expand All @@ -412,7 +404,7 @@ function present_tense_stem(pref: string, pts: string, is: string) {
}
}

if (matchEnd(pts, [['-', 'm', 'e', 'ų', 'u']])) {
if (MEUU.test(pts)) {
result = pts.slice(0, -1);
} else {
result = pts;
Expand Down

0 comments on commit 25e9f63

Please sign in to comment.