shevchenko 3.0.8 → 3.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/dist/cjs/anthroponym-declension/anthroponym-inflector.js +3 -3
- package/dist/cjs/anthroponym-declension/bootstrap.js +3 -3
- package/dist/cjs/anthroponym-declension/family-name-classifier/artifacts/model.bundle.json.js +3 -3
- package/dist/cjs/anthroponym-declension/family-name-classifier/cache/incorrect-predictions.json.js +3 -3
- package/dist/cjs/anthroponym-declension/family-name-classifier/family-name-class-transformer.js +3 -3
- package/dist/cjs/anthroponym-declension/family-name-classifier/family-name-classifer.js +3 -3
- package/dist/cjs/anthroponym-declension/family-name-classifier/model-bundle-loader.js +3 -3
- package/dist/cjs/anthroponym-declension/family-name-classifier/model-config.js +3 -3
- package/dist/cjs/anthroponym-declension/family-name-classifier/word-transformer.js +3 -3
- package/dist/cjs/anthroponym-declension/family-name-inflector.js +4 -4
- package/dist/cjs/anthroponym-declension/family-name-inflector.js.map +1 -1
- package/dist/cjs/anthroponym-declension/given-name-inflector.js +4 -4
- package/dist/cjs/anthroponym-declension/given-name-inflector.js.map +1 -1
- package/dist/cjs/anthroponym-declension/name-inflector.js +3 -3
- package/dist/cjs/anthroponym-declension/patronymic-name-inflector.js +5 -5
- package/dist/cjs/anthroponym-declension/patronymic-name-inflector.js.map +1 -1
- package/dist/cjs/gender-detection/detect-gender.js +3 -3
- package/dist/cjs/gender-detection/given-names-genders.json.js +3 -3
- package/dist/cjs/index.js +5 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/input-validation.js +54 -49
- package/dist/cjs/input-validation.js.map +1 -1
- package/dist/cjs/language/alphabet.js +3 -3
- package/dist/cjs/language/grammatical-case.js +3 -3
- package/dist/cjs/language/grammatical-gender.js +3 -3
- package/dist/cjs/language/letter-case.js +3 -3
- package/dist/cjs/language/linguistics.js +3 -3
- package/dist/cjs/language/word-class.js +3 -3
- package/dist/cjs/shevchenko.js +97 -18
- package/dist/cjs/shevchenko.js.map +1 -1
- package/dist/cjs/word-declension/append-command-runner.js +3 -3
- package/dist/cjs/word-declension/bootstrap.js +4 -4
- package/dist/cjs/word-declension/command-runner-factory.js +3 -3
- package/dist/cjs/word-declension/declension-rule-inflector.js +3 -3
- package/dist/cjs/word-declension/declension-types.js +3 -3
- package/dist/cjs/word-declension/declension-types.js.map +1 -1
- package/dist/cjs/word-declension/replace-command-runner.js +3 -3
- package/dist/cjs/word-declension/{declension-rules.json.js → rules/artifacts/declension-rules.json.js} +97 -97
- package/dist/cjs/word-declension/word-inflector.js +7 -7
- package/dist/cjs/word-declension/word-inflector.js.map +1 -1
- package/dist/es/anthroponym-declension/anthroponym-inflector.js +3 -3
- package/dist/es/anthroponym-declension/anthroponym.d.ts +2 -2
- package/dist/es/anthroponym-declension/anthroponym.d.ts.map +1 -1
- package/dist/es/anthroponym-declension/bootstrap.js +3 -3
- package/dist/es/anthroponym-declension/family-name-classifier/artifacts/model.bundle.json.js +3 -3
- package/dist/es/anthroponym-declension/family-name-classifier/cache/incorrect-predictions.json.js +3 -3
- package/dist/es/anthroponym-declension/family-name-classifier/data-utils.d.ts +3 -3
- package/dist/es/anthroponym-declension/family-name-classifier/data-utils.d.ts.map +1 -1
- package/dist/es/anthroponym-declension/family-name-classifier/family-name-class-transformer.js +3 -3
- package/dist/es/anthroponym-declension/family-name-classifier/family-name-class.d.ts +2 -2
- package/dist/es/anthroponym-declension/family-name-classifier/family-name-class.d.ts.map +1 -1
- package/dist/es/anthroponym-declension/family-name-classifier/family-name-classifer.js +3 -3
- package/dist/es/anthroponym-declension/family-name-classifier/model-bundle-loader.js +3 -3
- package/dist/es/anthroponym-declension/family-name-classifier/model-config.js +3 -3
- package/dist/es/anthroponym-declension/family-name-classifier/scripts/train-model.d.ts +2 -2
- package/dist/es/anthroponym-declension/family-name-classifier/scripts/train-model.d.ts.map +1 -1
- package/dist/es/anthroponym-declension/family-name-classifier/word-transformer.js +3 -3
- package/dist/es/anthroponym-declension/family-name-inflector.js +4 -4
- package/dist/es/anthroponym-declension/family-name-inflector.js.map +1 -1
- package/dist/es/anthroponym-declension/given-name-inflector.js +4 -4
- package/dist/es/anthroponym-declension/given-name-inflector.js.map +1 -1
- package/dist/es/anthroponym-declension/name-inflector.js +3 -3
- package/dist/es/anthroponym-declension/patronymic-name-inflector.d.ts.map +1 -1
- package/dist/es/anthroponym-declension/patronymic-name-inflector.js +5 -5
- package/dist/es/anthroponym-declension/patronymic-name-inflector.js.map +1 -1
- package/dist/es/contracts.d.ts +10 -4
- package/dist/es/contracts.d.ts.map +1 -1
- package/dist/es/gender-detection/detect-gender.js +3 -3
- package/dist/es/gender-detection/given-names-genders.json.js +3 -3
- package/dist/es/index.js +4 -3
- package/dist/es/index.js.map +1 -1
- package/dist/es/input-validation.d.ts +6 -4
- package/dist/es/input-validation.d.ts.map +1 -1
- package/dist/es/input-validation.js +54 -50
- package/dist/es/input-validation.js.map +1 -1
- package/dist/es/language/alphabet.js +3 -3
- package/dist/es/language/grammatical-case.js +3 -3
- package/dist/es/language/grammatical-gender.js +3 -3
- package/dist/es/language/letter-case.js +3 -3
- package/dist/es/language/linguistics.js +3 -3
- package/dist/es/language/word-class.js +3 -3
- package/dist/es/shevchenko.d.ts +87 -15
- package/dist/es/shevchenko.d.ts.map +1 -1
- package/dist/es/shevchenko.js +97 -18
- package/dist/es/shevchenko.js.map +1 -1
- package/dist/es/word-declension/append-command-runner.js +3 -3
- package/dist/es/word-declension/bootstrap.js +4 -4
- package/dist/es/word-declension/command-runner-factory.js +3 -3
- package/dist/es/word-declension/declension-rule-inflector.js +3 -3
- package/dist/es/word-declension/declension-rule-matchers.d.ts +5 -5
- package/dist/es/word-declension/declension-rule-matchers.d.ts.map +1 -1
- package/dist/es/word-declension/declension-types.d.ts +12 -11
- package/dist/es/word-declension/declension-types.d.ts.map +1 -1
- package/dist/es/word-declension/declension-types.js +3 -3
- package/dist/es/word-declension/declension-types.js.map +1 -1
- package/dist/es/word-declension/replace-command-runner.js +3 -3
- package/dist/es/word-declension/{declension-rules.json.js → rules/artifacts/declension-rules.json.js} +97 -97
- package/dist/es/word-declension/rules/scripts/build-declension-rules.d.ts +2 -0
- package/dist/es/word-declension/rules/scripts/build-declension-rules.d.ts.map +1 -0
- package/dist/es/word-declension/word-inflector.d.ts +5 -7
- package/dist/es/word-declension/word-inflector.d.ts.map +1 -1
- package/dist/es/word-declension/word-inflector.js +7 -7
- package/dist/es/word-declension/word-inflector.js.map +1 -1
- package/dist/umd/shevchenko.min.js +188 -188
- package/dist/umd/shevchenko.min.js.map +1 -1
- package/package.json +4 -5
- package/src/anthroponym-declension/anthroponym.ts +2 -2
- package/src/anthroponym-declension/family-name-classifier/data-utils.ts +3 -3
- package/src/anthroponym-declension/family-name-classifier/family-name-class.ts +2 -2
- package/src/anthroponym-declension/family-name-classifier/model-bundle-saver.ts +4 -4
- package/src/anthroponym-declension/family-name-classifier/scripts/cache-incorrect-predictions.ts +6 -6
- package/src/anthroponym-declension/family-name-classifier/scripts/train-model.ts +7 -7
- package/src/anthroponym-declension/family-name-inflector.ts +1 -1
- package/src/anthroponym-declension/given-name-inflector.ts +1 -1
- package/src/anthroponym-declension/patronymic-name-inflector.ts +3 -2
- package/src/contracts.ts +12 -4
- package/src/input-validation.ts +65 -51
- package/src/shevchenko.ts +127 -23
- package/src/word-declension/README.md +1 -1
- package/src/word-declension/bootstrap.ts +1 -1
- package/src/word-declension/declension-rule-matchers.ts +10 -7
- package/src/word-declension/declension-types.ts +13 -11
- package/src/word-declension/rules/adjective-declension-rules.json +649 -0
- package/src/word-declension/{declension-rules.json → rules/artifacts/declension-rules.json} +94 -94
- package/src/word-declension/rules/exception-declension-rules.json +842 -0
- package/src/word-declension/rules/noun-declension-rules.json +6301 -0
- package/src/word-declension/rules/scripts/build-declension-rules.ts +40 -0
- package/src/word-declension/word-inflector.ts +13 -11
- package/dist/es/word-declension/scripts/load-declension-rules.d.ts +0 -2
- package/dist/es/word-declension/scripts/load-declension-rules.d.ts.map +0 -1
- package/src/word-declension/scripts/load-declension-rules.ts +0 -14
- /package/dist/cjs/word-declension/{declension-rules.json.js.map → rules/artifacts/declension-rules.json.js.map} +0 -0
- /package/dist/es/word-declension/{declension-rules.json.js.map → rules/artifacts/declension-rules.json.js.map} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shevchenko",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.9",
|
|
4
4
|
"description": "JavaScript library for declension of Ukrainian anthroponyms",
|
|
5
5
|
"browser": "./dist/umd/shevchenko.min.js",
|
|
6
6
|
"main": "./dist/cjs/shevchenko.js",
|
|
@@ -37,11 +37,10 @@
|
|
|
37
37
|
"type": "git",
|
|
38
38
|
"url": "git+https://github.com/tooleks/shevchenko-js.git"
|
|
39
39
|
},
|
|
40
|
-
"author": "Oleksandr Tolochko <tooleks
|
|
40
|
+
"author": "Oleksandr Tolochko <shevchenko-js@tooleks.com>",
|
|
41
41
|
"contributors": [
|
|
42
|
-
"Anna Tolochko
|
|
43
|
-
"Tetyana Tolochko"
|
|
44
|
-
"spetrik <sergei.petrik@gmail.com> (https://github.com/spetrik)"
|
|
42
|
+
"Anna Tolochko",
|
|
43
|
+
"Tetyana Tolochko"
|
|
45
44
|
],
|
|
46
45
|
"license": "MIT",
|
|
47
46
|
"bugs": {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Represents a split of a dataset into training and validation sets.
|
|
3
3
|
*/
|
|
4
|
-
export
|
|
4
|
+
export type SplitData<T> = {
|
|
5
5
|
trainingData: T[];
|
|
6
6
|
validationData: T[];
|
|
7
|
-
}
|
|
7
|
+
};
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Splits an array of objects into training and validation datasets, grouped by a specific feature and shuffled randomly within each group.
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { writeFile } from 'fs/promises';
|
|
2
|
-
import * as path from 'path';
|
|
1
|
+
import { writeFile } from 'node:fs/promises';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
3
|
import * as tf from '@tensorflow/tfjs';
|
|
4
4
|
|
|
5
5
|
const MODEL_BUNDLE_FILEPATH = path.join(__dirname, 'artifacts/model.bundle.json');
|
|
6
6
|
|
|
7
7
|
type ModelMetadata = Omit<tf.io.ModelJSON, 'modelTopology' | 'weightsManifest'>;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
type ModelBundle = {
|
|
10
10
|
modelInfo: tf.io.ModelArtifactsInfo;
|
|
11
11
|
modelTopology: Record<string, unknown>;
|
|
12
12
|
weightSpecs: tf.io.WeightsManifestEntry[];
|
|
13
13
|
weightData: string;
|
|
14
14
|
modelMetadata: ModelMetadata;
|
|
15
|
-
}
|
|
15
|
+
};
|
|
16
16
|
|
|
17
17
|
// Use Buffer on Node.js instead of Blob/atob/btoa
|
|
18
18
|
const useNodeBuffer =
|
package/src/anthroponym-declension/family-name-classifier/scripts/cache-incorrect-predictions.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { createReadStream } from 'fs';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { createReadStream } from 'node:fs';
|
|
2
|
+
import * as fs from 'node:fs/promises';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
4
|
import * as tf from '@tensorflow/tfjs-node';
|
|
5
5
|
import { parse as createCsvParser } from 'csv';
|
|
6
6
|
import { FamilyNameClassTransformer } from '../family-name-class-transformer';
|
|
@@ -8,8 +8,8 @@ import { ModelBundleLoader } from '../model-bundle-loader';
|
|
|
8
8
|
import { MODEL_INPUT_SIZE } from '../model-config';
|
|
9
9
|
import { WordTransformer } from '../word-transformer';
|
|
10
10
|
|
|
11
|
-
const TRAINING_DATASET_FILEPATH =
|
|
12
|
-
const INCORRECT_PREDICTIONS_FILEPATH =
|
|
11
|
+
const TRAINING_DATASET_FILEPATH = path.join(__dirname, '../datasets/training.csv');
|
|
12
|
+
const INCORRECT_PREDICTIONS_FILEPATH = path.join(__dirname, '../cache/incorrect-predictions.json');
|
|
13
13
|
|
|
14
14
|
type IncorrectPredictions = Record<string, string>;
|
|
15
15
|
|
|
@@ -35,7 +35,7 @@ async function retrieveIncorrectPredictions(): Promise<IncorrectPredictions> {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
async function saveIncorrectPredictions(cache: IncorrectPredictions): Promise<void> {
|
|
38
|
-
await writeFile(INCORRECT_PREDICTIONS_FILEPATH, JSON.stringify(cache, null, 2), 'utf-8');
|
|
38
|
+
await fs.writeFile(INCORRECT_PREDICTIONS_FILEPATH, JSON.stringify(cache, null, 2), 'utf-8');
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
async function main(): Promise<void> {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
3
|
import * as tf from '@tensorflow/tfjs-node';
|
|
4
4
|
import { parse as createCsvParser } from 'csv';
|
|
5
5
|
import { ALPHABET_SIZE, WordClass } from '../../../language';
|
|
@@ -10,18 +10,18 @@ import { ModelBundleSaver } from '../model-bundle-saver';
|
|
|
10
10
|
import { MODEL_INPUT_SIZE } from '../model-config';
|
|
11
11
|
import { WordTransformer } from '../word-transformer';
|
|
12
12
|
|
|
13
|
-
const TRAINING_DATASET_FILEPATH =
|
|
14
|
-
const MODEL_ARTIFACTS_SOURCE = 'file://' +
|
|
13
|
+
const TRAINING_DATASET_FILEPATH = path.join(__dirname, '../datasets/training.csv');
|
|
14
|
+
const MODEL_ARTIFACTS_SOURCE = 'file://' + path.join(__dirname, '../artifacts');
|
|
15
15
|
|
|
16
|
-
export
|
|
16
|
+
export type DataItem = FamilyNameClass & {
|
|
17
17
|
familyName: string;
|
|
18
18
|
wordEnding: string;
|
|
19
|
-
}
|
|
19
|
+
};
|
|
20
20
|
|
|
21
21
|
async function loadData(): Promise<SplitData<DataItem>> {
|
|
22
22
|
const data: DataItem[] = [];
|
|
23
23
|
const dataParser = createCsvParser({ columns: true });
|
|
24
|
-
createReadStream(TRAINING_DATASET_FILEPATH).pipe(dataParser);
|
|
24
|
+
fs.createReadStream(TRAINING_DATASET_FILEPATH).pipe(dataParser);
|
|
25
25
|
|
|
26
26
|
for await (const dataRow of dataParser) {
|
|
27
27
|
data.push({
|
|
@@ -21,8 +21,9 @@ export class PatronymicNameInflector extends NameInflector {
|
|
|
21
21
|
return this.wordInflector.inflect(patronymicName, {
|
|
22
22
|
grammaticalCase: grammaticalCase,
|
|
23
23
|
gender: gender,
|
|
24
|
-
|
|
25
|
-
customRuleFilter: (declensionRule) =>
|
|
24
|
+
applicationType: 'patronymicName',
|
|
25
|
+
customRuleFilter: (declensionRule) =>
|
|
26
|
+
declensionRule.applicationType.includes('patronymicName'),
|
|
26
27
|
});
|
|
27
28
|
}
|
|
28
29
|
}
|
package/src/contracts.ts
CHANGED
|
@@ -1,20 +1,28 @@
|
|
|
1
|
-
import { Anthroponym } from './anthroponym-declension';
|
|
2
1
|
import { GrammaticalGender } from './language';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* The input parameters for anthroponym declension.
|
|
6
5
|
*/
|
|
7
|
-
export type DeclensionInput =
|
|
6
|
+
export type DeclensionInput = {
|
|
7
|
+
gender: GrammaticalGender;
|
|
8
|
+
givenName?: string;
|
|
9
|
+
patronymicName?: string;
|
|
10
|
+
familyName?: string;
|
|
11
|
+
};
|
|
8
12
|
|
|
9
13
|
/**
|
|
10
14
|
* The output result of anthroponym declension.
|
|
11
15
|
*/
|
|
12
|
-
export type DeclensionOutput =
|
|
16
|
+
export type DeclensionOutput<T extends DeclensionInput = DeclensionInput> = Omit<T, 'gender'>;
|
|
13
17
|
|
|
14
18
|
/**
|
|
15
19
|
* The input parameters for gender detection.
|
|
16
20
|
*/
|
|
17
|
-
export type GenderDetectionInput =
|
|
21
|
+
export type GenderDetectionInput = {
|
|
22
|
+
givenName?: string;
|
|
23
|
+
patronymicName?: string;
|
|
24
|
+
familyName?: string;
|
|
25
|
+
};
|
|
18
26
|
|
|
19
27
|
/**
|
|
20
28
|
* The output result of gender detection.
|
package/src/input-validation.ts
CHANGED
|
@@ -1,80 +1,94 @@
|
|
|
1
|
-
import { Anthroponym } from './anthroponym-declension';
|
|
2
1
|
import { DeclensionInput, GenderDetectionInput } from './contracts';
|
|
3
2
|
import { GrammaticalGender } from './language';
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
* Validates if a given value is a valid input for declension.
|
|
7
|
-
*
|
|
8
|
-
* @throws {TypeError}
|
|
9
|
-
*/
|
|
10
|
-
export function validateDeclensionInput(input: DeclensionInput): void {
|
|
11
|
-
validateObject(input);
|
|
12
|
-
validateGender(input.gender);
|
|
13
|
-
validateAnthroponym(input);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Validates if a given value is a valid input for gender detection.
|
|
18
|
-
*
|
|
19
|
-
* @throws {TypeError}
|
|
20
|
-
*/
|
|
21
|
-
export function validateGenderDetectionInput(input: GenderDetectionInput): void {
|
|
22
|
-
validateObject(input);
|
|
23
|
-
validateAnthroponym(input);
|
|
24
|
-
}
|
|
4
|
+
export class InputValidationError extends TypeError {}
|
|
25
5
|
|
|
26
6
|
/**
|
|
27
|
-
* Validates if a given value is
|
|
7
|
+
* Validates if a given value is a valid input for declension.
|
|
28
8
|
*
|
|
29
|
-
* @throws {
|
|
9
|
+
* @throws {InputValidationError}
|
|
30
10
|
*/
|
|
31
|
-
function
|
|
32
|
-
if (
|
|
33
|
-
throw new
|
|
11
|
+
export function validateDeclensionInput(value: unknown): asserts value is DeclensionInput {
|
|
12
|
+
if (!isObject(value)) {
|
|
13
|
+
throw new InputValidationError('The input type must be an object.');
|
|
34
14
|
}
|
|
35
|
-
}
|
|
36
15
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
*
|
|
40
|
-
* @throws {TypeError}
|
|
41
|
-
*/
|
|
42
|
-
function validateGender(gender: GrammaticalGender): void {
|
|
43
|
-
if (![GrammaticalGender.MASCULINE, GrammaticalGender.FEMININE].includes(gender)) {
|
|
44
|
-
throw new TypeError(
|
|
16
|
+
if (!isIn(value.gender, Object.values(GrammaticalGender))) {
|
|
17
|
+
throw new InputValidationError(
|
|
45
18
|
`The "gender" parameter must be one of the following: "${GrammaticalGender.MASCULINE}", "${GrammaticalGender.FEMININE}".`,
|
|
46
19
|
);
|
|
47
20
|
}
|
|
21
|
+
|
|
22
|
+
if (
|
|
23
|
+
!isDefined(value.givenName) &&
|
|
24
|
+
!isDefined(value.patronymicName) &&
|
|
25
|
+
!isDefined(value.familyName)
|
|
26
|
+
) {
|
|
27
|
+
throw new InputValidationError(
|
|
28
|
+
'At least one of the following parameters must present: "givenName", "patronymicName", "familyName".',
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (isDefined(value.givenName) && !isString(value.givenName)) {
|
|
33
|
+
throw new InputValidationError('The "givenName" parameter must be a string.');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (isDefined(value.patronymicName) && !isString(value.patronymicName)) {
|
|
37
|
+
throw new InputValidationError('The "patronymicName" parameter must be a string.');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (isDefined(value.familyName) && !isString(value.familyName)) {
|
|
41
|
+
throw new InputValidationError('The "familyName" parameter must be a string.');
|
|
42
|
+
}
|
|
48
43
|
}
|
|
49
44
|
|
|
50
45
|
/**
|
|
51
|
-
* Validates if a given value is a valid
|
|
46
|
+
* Validates if a given value is a valid input for gender detection.
|
|
52
47
|
*
|
|
53
|
-
* @throws {
|
|
48
|
+
* @throws {InputValidationError}
|
|
54
49
|
*/
|
|
55
|
-
function
|
|
50
|
+
export function validateGenderDetectionInput(
|
|
51
|
+
value: unknown,
|
|
52
|
+
): asserts value is GenderDetectionInput {
|
|
53
|
+
if (!isObject(value)) {
|
|
54
|
+
throw new InputValidationError('The input type must be an object.');
|
|
55
|
+
}
|
|
56
|
+
|
|
56
57
|
if (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
58
|
+
!isDefined(value.givenName) &&
|
|
59
|
+
!isDefined(value.patronymicName) &&
|
|
60
|
+
!isDefined(value.familyName)
|
|
60
61
|
) {
|
|
61
|
-
throw new
|
|
62
|
+
throw new InputValidationError(
|
|
62
63
|
'At least one of the following parameters must present: "givenName", "patronymicName", "familyName".',
|
|
63
64
|
);
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
if (
|
|
67
|
-
throw new
|
|
67
|
+
if (isDefined(value.givenName) && !isString(value.givenName)) {
|
|
68
|
+
throw new InputValidationError('The "givenName" parameter must be a string.');
|
|
68
69
|
}
|
|
69
70
|
|
|
70
|
-
if (
|
|
71
|
-
|
|
72
|
-
typeof anthroponym.patronymicName !== 'string'
|
|
73
|
-
) {
|
|
74
|
-
throw new TypeError('The "patronymicName" parameter must be a string.');
|
|
71
|
+
if (isDefined(value.patronymicName) && !isString(value.patronymicName)) {
|
|
72
|
+
throw new InputValidationError('The "patronymicName" parameter must be a string.');
|
|
75
73
|
}
|
|
76
74
|
|
|
77
|
-
if (
|
|
78
|
-
throw new
|
|
75
|
+
if (isDefined(value.familyName) && !isString(value.familyName)) {
|
|
76
|
+
throw new InputValidationError('The "familyName" parameter must be a string.');
|
|
79
77
|
}
|
|
80
78
|
}
|
|
79
|
+
|
|
80
|
+
function isDefined<T>(value: T | undefined): value is T {
|
|
81
|
+
return typeof value !== 'undefined';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
85
|
+
return value != null && typeof value === 'object';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function isString(value: unknown): value is string {
|
|
89
|
+
return typeof value === 'string';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function isIn<T>(value: unknown, values: T[]): value is T {
|
|
93
|
+
return values.includes(value as T);
|
|
94
|
+
}
|
package/src/shevchenko.ts
CHANGED
|
@@ -6,88 +6,192 @@ import {
|
|
|
6
6
|
GenderDetectionOutput,
|
|
7
7
|
} from './contracts';
|
|
8
8
|
import { detectGender as autoDetectGender } from './gender-detection';
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
11
|
+
InputValidationError,
|
|
12
|
+
validateDeclensionInput,
|
|
13
|
+
validateGenderDetectionInput,
|
|
14
|
+
} from './input-validation';
|
|
10
15
|
import { GrammaticalCase } from './language';
|
|
11
16
|
|
|
12
17
|
export * from './contracts';
|
|
18
|
+
export { InputValidationError } from './input-validation';
|
|
13
19
|
export { GrammaticalGender, GrammaticalCase } from './language';
|
|
14
20
|
export { Anthroponym } from './anthroponym-declension';
|
|
15
21
|
|
|
16
22
|
/**
|
|
17
23
|
* Inflects an anthroponym in nominative grammatical case.
|
|
18
24
|
*
|
|
19
|
-
* @
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const anthroponym = await shevchenko.inNominative({
|
|
28
|
+
* gender: shevchenko.GrammaticalGender.MASCULINE,
|
|
29
|
+
* givenName: 'Тарас',
|
|
30
|
+
* patronymicName: 'Григорович',
|
|
31
|
+
* familyName: 'Шевченко',
|
|
32
|
+
* });
|
|
33
|
+
* ```
|
|
34
|
+
* @throws {InputValidationError} Input validation error.
|
|
20
35
|
*/
|
|
21
|
-
export async function inNominative
|
|
36
|
+
export async function inNominative<T extends DeclensionInput>(
|
|
37
|
+
input: T,
|
|
38
|
+
): Promise<DeclensionOutput<T>> {
|
|
22
39
|
validateDeclensionInput(input);
|
|
23
|
-
|
|
40
|
+
const result = await anthroponymInflector.inflect(
|
|
41
|
+
input,
|
|
42
|
+
input.gender,
|
|
43
|
+
GrammaticalCase.NOMINATIVE,
|
|
44
|
+
);
|
|
45
|
+
return result as DeclensionOutput<T>;
|
|
24
46
|
}
|
|
25
47
|
|
|
26
48
|
/**
|
|
27
49
|
* Inflects an anthroponym in genitive grammatical case.
|
|
28
50
|
*
|
|
29
|
-
* @
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const anthroponym = await shevchenko.inGenitive({
|
|
54
|
+
* gender: shevchenko.GrammaticalGender.MASCULINE,
|
|
55
|
+
* givenName: 'Тарас',
|
|
56
|
+
* patronymicName: 'Григорович',
|
|
57
|
+
* familyName: 'Шевченко',
|
|
58
|
+
* });
|
|
59
|
+
* ```
|
|
60
|
+
* @throws {InputValidationError} Input validation error.
|
|
30
61
|
*/
|
|
31
|
-
export async function inGenitive
|
|
62
|
+
export async function inGenitive<T extends DeclensionInput>(
|
|
63
|
+
input: T,
|
|
64
|
+
): Promise<DeclensionOutput<T>> {
|
|
32
65
|
validateDeclensionInput(input);
|
|
33
|
-
|
|
66
|
+
const result = await anthroponymInflector.inflect(input, input.gender, GrammaticalCase.GENITIVE);
|
|
67
|
+
return result as DeclensionOutput<T>;
|
|
34
68
|
}
|
|
35
69
|
|
|
36
70
|
/**
|
|
37
71
|
* Inflects an anthroponym in dative grammatical case.
|
|
38
72
|
*
|
|
39
|
-
* @
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const anthroponym = await shevchenko.inDative({
|
|
76
|
+
* gender: shevchenko.GrammaticalGender.MASCULINE,
|
|
77
|
+
* givenName: 'Тарас',
|
|
78
|
+
* patronymicName: 'Григорович',
|
|
79
|
+
* familyName: 'Шевченко',
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
* @throws {InputValidationError} Input validation error.
|
|
40
83
|
*/
|
|
41
|
-
export async function inDative(input:
|
|
84
|
+
export async function inDative<T extends DeclensionInput>(input: T): Promise<DeclensionOutput<T>> {
|
|
42
85
|
validateDeclensionInput(input);
|
|
43
|
-
|
|
86
|
+
const result = await anthroponymInflector.inflect(input, input.gender, GrammaticalCase.DATIVE);
|
|
87
|
+
return result as DeclensionOutput<T>;
|
|
44
88
|
}
|
|
45
89
|
|
|
46
90
|
/**
|
|
47
91
|
* Inflects an anthroponym in accusative grammatical case.
|
|
48
92
|
*
|
|
49
|
-
* @
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* const anthroponym = await shevchenko.inAccusative({
|
|
96
|
+
* gender: shevchenko.GrammaticalGender.MASCULINE,
|
|
97
|
+
* givenName: 'Тарас',
|
|
98
|
+
* patronymicName: 'Григорович',
|
|
99
|
+
* familyName: 'Шевченко',
|
|
100
|
+
* });
|
|
101
|
+
* ```
|
|
102
|
+
* @throws {InputValidationError} Input validation error.
|
|
50
103
|
*/
|
|
51
|
-
export async function inAccusative
|
|
104
|
+
export async function inAccusative<T extends DeclensionInput>(
|
|
105
|
+
input: T,
|
|
106
|
+
): Promise<DeclensionOutput<T>> {
|
|
52
107
|
validateDeclensionInput(input);
|
|
53
|
-
|
|
108
|
+
const result = await anthroponymInflector.inflect(
|
|
109
|
+
input,
|
|
110
|
+
input.gender,
|
|
111
|
+
GrammaticalCase.ACCUSATIVE,
|
|
112
|
+
);
|
|
113
|
+
return result as DeclensionOutput<T>;
|
|
54
114
|
}
|
|
55
115
|
|
|
56
116
|
/**
|
|
57
117
|
* Inflects an anthroponym in ablative grammatical case.
|
|
58
118
|
*
|
|
59
|
-
* @
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const anthroponym = await shevchenko.inAblative({
|
|
122
|
+
* gender: shevchenko.GrammaticalGender.MASCULINE,
|
|
123
|
+
* givenName: 'Тарас',
|
|
124
|
+
* patronymicName: 'Григорович',
|
|
125
|
+
* familyName: 'Шевченко',
|
|
126
|
+
* });
|
|
127
|
+
* ```
|
|
128
|
+
* @throws {InputValidationError} Input validation error.
|
|
60
129
|
*/
|
|
61
|
-
export async function inAblative
|
|
130
|
+
export async function inAblative<T extends DeclensionInput>(
|
|
131
|
+
input: T,
|
|
132
|
+
): Promise<DeclensionOutput<T>> {
|
|
62
133
|
validateDeclensionInput(input);
|
|
63
|
-
|
|
134
|
+
const result = await anthroponymInflector.inflect(input, input.gender, GrammaticalCase.ABLATIVE);
|
|
135
|
+
return result as DeclensionOutput<T>;
|
|
64
136
|
}
|
|
65
137
|
|
|
66
138
|
/**
|
|
67
139
|
* Inflects an anthroponym in locative grammatical case.
|
|
68
140
|
*
|
|
69
|
-
* @
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* const anthroponym = await shevchenko.inLocative({
|
|
144
|
+
* gender: shevchenko.GrammaticalGender.MASCULINE,
|
|
145
|
+
* givenName: 'Тарас',
|
|
146
|
+
* patronymicName: 'Григорович',
|
|
147
|
+
* familyName: 'Шевченко',
|
|
148
|
+
* });
|
|
149
|
+
* ```
|
|
150
|
+
* @throws {InputValidationError} Input validation error.
|
|
70
151
|
*/
|
|
71
|
-
export async function inLocative
|
|
152
|
+
export async function inLocative<T extends DeclensionInput>(
|
|
153
|
+
input: T,
|
|
154
|
+
): Promise<DeclensionOutput<T>> {
|
|
72
155
|
validateDeclensionInput(input);
|
|
73
|
-
|
|
156
|
+
const result = await anthroponymInflector.inflect(input, input.gender, GrammaticalCase.LOCATIVE);
|
|
157
|
+
return result as DeclensionOutput<T>;
|
|
74
158
|
}
|
|
75
159
|
|
|
76
160
|
/**
|
|
77
161
|
* Inflects an anthroponym in vocative grammatical case.
|
|
78
162
|
*
|
|
79
|
-
* @
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* const anthroponym = await shevchenko.inVocative({
|
|
166
|
+
* gender: shevchenko.GrammaticalGender.MASCULINE,
|
|
167
|
+
* givenName: 'Тарас',
|
|
168
|
+
* patronymicName: 'Григорович',
|
|
169
|
+
* familyName: 'Шевченко',
|
|
170
|
+
* });
|
|
171
|
+
* ```
|
|
172
|
+
* @throws {InputValidationError} Input validation error.
|
|
80
173
|
*/
|
|
81
|
-
export async function inVocative
|
|
174
|
+
export async function inVocative<T extends DeclensionInput>(
|
|
175
|
+
input: T,
|
|
176
|
+
): Promise<DeclensionOutput<T>> {
|
|
82
177
|
validateDeclensionInput(input);
|
|
83
|
-
|
|
178
|
+
const result = await anthroponymInflector.inflect(input, input.gender, GrammaticalCase.VOCATIVE);
|
|
179
|
+
return result as DeclensionOutput<T>;
|
|
84
180
|
}
|
|
85
181
|
|
|
86
182
|
/**
|
|
87
183
|
* Returns the grammatical gender of an anthroponym.
|
|
88
184
|
* Returns null if the grammatical gender cannot be detected.
|
|
89
185
|
*
|
|
90
|
-
* @
|
|
186
|
+
* @example
|
|
187
|
+
* ```ts
|
|
188
|
+
* const gender = await shevchenko.detectGender({
|
|
189
|
+
* givenName: 'Тарас',
|
|
190
|
+
* patronymicName: 'Григорович',
|
|
191
|
+
* familyName: 'Шевченко',
|
|
192
|
+
* });
|
|
193
|
+
* ```
|
|
194
|
+
* @throws {InputValidationError} Input validation error.
|
|
91
195
|
*/
|
|
92
196
|
export async function detectGender(input: GenderDetectionInput): Promise<GenderDetectionOutput> {
|
|
93
197
|
validateGenderDetectionInput(input);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import untypedDeclensionRules from './declension-rules.json';
|
|
2
1
|
import { DeclensionRule } from './declension-types';
|
|
2
|
+
import untypedDeclensionRules from './rules/artifacts/declension-rules.json';
|
|
3
3
|
import { WordInflector } from './word-inflector';
|
|
4
4
|
|
|
5
5
|
const declensionRules = untypedDeclensionRules as DeclensionRule[];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GrammaticalGender } from '../language';
|
|
2
|
-
import { DeclensionRule } from './declension-types';
|
|
2
|
+
import { ApplicationType, DeclensionRule } from './declension-types';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Determines if a given declension rule applies to a specified gender.
|
|
@@ -9,17 +9,20 @@ export function isGenderApplicable(rule: DeclensionRule, gender: GrammaticalGend
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* Determines if a given declension rule applies to a specified application.
|
|
12
|
+
* Determines if a given declension rule applies to a specified application type.
|
|
13
13
|
*/
|
|
14
|
-
export function isApplicable(rule: DeclensionRule,
|
|
15
|
-
return rule.
|
|
14
|
+
export function isApplicable(rule: DeclensionRule, applicationType: ApplicationType): boolean {
|
|
15
|
+
return rule.applicationType.length === 0 || rule.applicationType.includes(applicationType);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* Determines if a given declension rule strictly applies to a specified application.
|
|
19
|
+
* Determines if a given declension rule strictly applies to a specified application type.
|
|
20
20
|
*/
|
|
21
|
-
export function isStriclyApplicable(
|
|
22
|
-
|
|
21
|
+
export function isStriclyApplicable(
|
|
22
|
+
rule: DeclensionRule,
|
|
23
|
+
applicationType: ApplicationType,
|
|
24
|
+
): boolean {
|
|
25
|
+
return rule.applicationType.includes(applicationType);
|
|
23
26
|
}
|
|
24
27
|
|
|
25
28
|
/**
|