shevchenko 2.1.0 → 2.1.1
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/dist/cjs/shevchenko.js +1 -1
- package/dist/es/shevchenko.js +1 -1
- package/dist/umd/shevchenko.min.js +1 -1
- package/package.json +1 -1
- package/src/anthroponym-inflection/anthroponym-inflector.ts +38 -0
- package/src/anthroponym-inflection/append-command-runner.ts +18 -0
- package/src/anthroponym-inflection/command-runner-factory.ts +23 -0
- package/src/anthroponym-inflection/first-name-inflector.ts +41 -0
- package/src/anthroponym-inflection/index.ts +5 -0
- package/src/anthroponym-inflection/inflector-command-runner.ts +3 -0
- package/src/anthroponym-inflection/inflector.types.ts +42 -0
- package/src/anthroponym-inflection/last-name-inflector.ts +54 -0
- package/src/anthroponym-inflection/middle-name-inflector.ts +29 -0
- package/src/anthroponym-inflection/name-inflector.ts +19 -0
- package/src/anthroponym-inflection/replace-command-runner.ts +18 -0
- package/src/anthroponym-inflection/rule-inflector.ts +40 -0
- package/src/bootstrap.ts +53 -0
- package/src/core/anthroponym.interface.ts +8 -0
- package/src/core/gender.enum.ts +4 -0
- package/src/core/grammatical-case.enum.ts +9 -0
- package/src/core/index.ts +4 -0
- package/src/core/validator.ts +39 -0
- package/src/internal.ts +1 -0
- package/src/part-of-speech-recognition/index.ts +5 -0
- package/src/part-of-speech-recognition/neural-network-config.ts +5 -0
- package/src/part-of-speech-recognition/neural-network-training-data.ts +5 -0
- package/src/part-of-speech-recognition/neural-network.ts +66 -0
- package/src/part-of-speech-recognition/part-of-speech-decoder.ts +26 -0
- package/src/part-of-speech-recognition/part-of-speech-encoder.ts +20 -0
- package/src/part-of-speech-recognition/part-of-speech-recognition-rule.ts +33 -0
- package/src/part-of-speech-recognition/part-of-speech-recognizer.ts +26 -0
- package/src/part-of-speech-recognition/part-of-speech.enum.ts +4 -0
- package/src/part-of-speech-recognition/word-encoder.ts +23 -0
- package/src/part-of-speech-recognition/word-transformer.ts +8 -0
- package/src/resources/inflector/rules.json +7114 -0
- package/src/resources/neural-networks/kosmii/README.md +5 -0
- package/src/resources/neural-networks/kosmii/cache.json +8 -0
- package/src/resources/neural-networks/kosmii/samples.json +9435 -0
- package/src/resources/neural-networks/kosmii/structure.json +47898 -0
- package/src/resources/neural-networks/pelykh/README.md +5 -0
- package/src/resources/neural-networks/pelykh/cache.json +1 -0
- package/src/resources/neural-networks/pelykh/samples.json +221 -0
- package/src/resources/neural-networks/pelykh/structure.json +47898 -0
- package/src/resources/neural-networks/pohorielova/README.md +5 -0
- package/src/resources/neural-networks/pohorielova/cache.json +542 -0
- package/src/resources/neural-networks/pohorielova/samples.json +32147 -0
- package/src/resources/neural-networks/pohorielova/structure.json +47898 -0
- package/src/shevchenko.ts +63 -0
- package/src/utils/lang.utils.ts +13 -0
- package/src/utils/regexp.utils.ts +11 -0
- package/src/utils/string.utils.ts +49 -0
package/dist/cjs/shevchenko.js
CHANGED
package/dist/es/shevchenko.js
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Anthroponym, GrammaticalCase } from '../core';
|
|
2
|
+
import { FirstNameInflector } from './first-name-inflector';
|
|
3
|
+
import { LastNameInflector } from './last-name-inflector';
|
|
4
|
+
import { MiddleNameInflector } from './middle-name-inflector';
|
|
5
|
+
|
|
6
|
+
export class AnthroponymInflector {
|
|
7
|
+
private readonly firstNameInflector: FirstNameInflector;
|
|
8
|
+
private readonly middleNameInflector: MiddleNameInflector;
|
|
9
|
+
private readonly lastNameInflector: LastNameInflector;
|
|
10
|
+
|
|
11
|
+
// tslint:disable-next-line max-line-length
|
|
12
|
+
constructor(firstNameInflector: FirstNameInflector, middleNameInflector: MiddleNameInflector, lastNameInflector: LastNameInflector) {
|
|
13
|
+
this.firstNameInflector = firstNameInflector;
|
|
14
|
+
this.middleNameInflector = middleNameInflector;
|
|
15
|
+
this.lastNameInflector = lastNameInflector;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Inflects the anthroponym in the given grammatical case.
|
|
20
|
+
*/
|
|
21
|
+
inflect(anthroponym: Anthroponym, grammaticalCase: GrammaticalCase): Anthroponym {
|
|
22
|
+
const result: Anthroponym = { gender: anthroponym.gender };
|
|
23
|
+
|
|
24
|
+
if (anthroponym.firstName != null) {
|
|
25
|
+
result.firstName = this.firstNameInflector.inflect(anthroponym.firstName, anthroponym.gender, grammaticalCase);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (anthroponym.middleName != null) {
|
|
29
|
+
result.middleName = this.middleNameInflector.inflect(anthroponym.middleName, anthroponym.gender, grammaticalCase);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (anthroponym.lastName != null) {
|
|
33
|
+
result.lastName = this.lastNameInflector.inflect(anthroponym.lastName, anthroponym.gender, grammaticalCase);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { InflectorCommandRunner } from './inflector-command-runner';
|
|
2
|
+
import { InflectorCommand } from './inflector.types';
|
|
3
|
+
|
|
4
|
+
export class AppendCommandRunner implements InflectorCommandRunner {
|
|
5
|
+
private readonly command: InflectorCommand;
|
|
6
|
+
|
|
7
|
+
constructor(command: InflectorCommand) {
|
|
8
|
+
this.command = command;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Appends the command value to the given value.
|
|
13
|
+
* Returns a new value.
|
|
14
|
+
*/
|
|
15
|
+
exec(value: string): string {
|
|
16
|
+
return value + this.command.value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { InflectorCommand, InflectorCommandType } from './inflector.types';
|
|
2
|
+
import { AppendCommandRunner } from './append-command-runner';
|
|
3
|
+
import { ReplaceCommandRunner } from './replace-command-runner';
|
|
4
|
+
import { InflectorCommandRunner } from './inflector-command-runner';
|
|
5
|
+
|
|
6
|
+
export class CommandRunnerFactory {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new command runner for the given command.
|
|
9
|
+
*/
|
|
10
|
+
make(command: InflectorCommand): InflectorCommandRunner {
|
|
11
|
+
switch (command.type) {
|
|
12
|
+
case InflectorCommandType.Append: {
|
|
13
|
+
return new AppendCommandRunner(command);
|
|
14
|
+
}
|
|
15
|
+
case InflectorCommandType.Replace: {
|
|
16
|
+
return new ReplaceCommandRunner(command);
|
|
17
|
+
}
|
|
18
|
+
default: {
|
|
19
|
+
throw new TypeError('Invalid command type.');
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { InflectorRule } from './inflector.types';
|
|
2
|
+
import { Gender, GrammaticalCase } from '../core';
|
|
3
|
+
import { RuleInflector } from './rule-inflector';
|
|
4
|
+
import { NameInflector } from './name-inflector';
|
|
5
|
+
|
|
6
|
+
export class FirstNameInflector extends NameInflector {
|
|
7
|
+
private readonly rules: InflectorRule[];
|
|
8
|
+
|
|
9
|
+
constructor(rules: InflectorRule[]) {
|
|
10
|
+
super();
|
|
11
|
+
this.rules = rules;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @inheritdoc
|
|
16
|
+
*/
|
|
17
|
+
protected inflectWord(word: string, gender: Gender, grammaticalCase: GrammaticalCase): string {
|
|
18
|
+
const [rule] = this.rules
|
|
19
|
+
.filter(rule => rule.gender.includes(gender))
|
|
20
|
+
.filter(rule => rule.usage.length === 0 || rule.usage.includes('firstName'))
|
|
21
|
+
.filter(rule => new RegExp(rule.pattern.find, 'gi').test(word))
|
|
22
|
+
.sort((firstRule, secondRule) => {
|
|
23
|
+
if (firstRule.usage.length === 0) {
|
|
24
|
+
return 0;
|
|
25
|
+
}
|
|
26
|
+
if (secondRule.usage.length > 0) {
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
if (secondRule.usage.includes('firstName')) {
|
|
30
|
+
return 0;
|
|
31
|
+
}
|
|
32
|
+
return 1;
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (!rule) {
|
|
36
|
+
return word;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return new RuleInflector(rule).inflect(word, grammaticalCase);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { AnthroponymInflector } from './anthroponym-inflector';
|
|
2
|
+
export { FirstNameInflector } from './first-name-inflector';
|
|
3
|
+
export { MiddleNameInflector } from './middle-name-inflector';
|
|
4
|
+
export { LastNameInflector } from './last-name-inflector';
|
|
5
|
+
export { InflectorRule } from './inflector.types';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Gender, GrammaticalCase } from '../core';
|
|
2
|
+
import { PartOfSpeech } from '../part-of-speech-recognition';
|
|
3
|
+
|
|
4
|
+
export type InflectorRule = {
|
|
5
|
+
description: string,
|
|
6
|
+
examples: string[],
|
|
7
|
+
partOfSpeech: PartOfSpeech,
|
|
8
|
+
gender: Gender[],
|
|
9
|
+
priority: number,
|
|
10
|
+
usage: string[],
|
|
11
|
+
pattern: InflectorPattern,
|
|
12
|
+
grammaticalCases: GrammaticalCases,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type InflectorPattern = {
|
|
16
|
+
find: string,
|
|
17
|
+
modify: string,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type GrammaticalCases = {
|
|
21
|
+
[GrammaticalCase.Nominative]: InflectorCommandGroup[],
|
|
22
|
+
[GrammaticalCase.Genitive]: InflectorCommandGroup[],
|
|
23
|
+
[GrammaticalCase.Dative]: InflectorCommandGroup[],
|
|
24
|
+
[GrammaticalCase.Accusative]: InflectorCommandGroup[],
|
|
25
|
+
[GrammaticalCase.Ablative]: InflectorCommandGroup[],
|
|
26
|
+
[GrammaticalCase.Locative]: InflectorCommandGroup[],
|
|
27
|
+
[GrammaticalCase.Vocative]: InflectorCommandGroup[],
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type InflectorCommandGroup = {
|
|
31
|
+
[groupIndex: string]: InflectorCommand;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type InflectorCommand = {
|
|
35
|
+
type: InflectorCommandType,
|
|
36
|
+
value: string,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export enum InflectorCommandType {
|
|
40
|
+
Replace = 'replace',
|
|
41
|
+
Append = 'append',
|
|
42
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { InflectorRule } from './inflector.types';
|
|
2
|
+
import { Gender, GrammaticalCase } from '../core';
|
|
3
|
+
import { PartOfSpeechRecognizer } from '../part-of-speech-recognition';
|
|
4
|
+
import { countVowels } from '../utils/lang.utils';
|
|
5
|
+
import { NameInflector } from './name-inflector';
|
|
6
|
+
import { RuleInflector } from './rule-inflector';
|
|
7
|
+
|
|
8
|
+
export class LastNameInflector extends NameInflector {
|
|
9
|
+
private readonly rules: InflectorRule[];
|
|
10
|
+
private readonly partOfSpeechRecognizer: PartOfSpeechRecognizer;
|
|
11
|
+
|
|
12
|
+
constructor(rules: InflectorRule[], partOfSpeechRecognizer: PartOfSpeechRecognizer) {
|
|
13
|
+
super();
|
|
14
|
+
this.rules = rules;
|
|
15
|
+
this.partOfSpeechRecognizer = partOfSpeechRecognizer;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @inheritdoc
|
|
20
|
+
*/
|
|
21
|
+
// tslint:disable-next-line max-line-length
|
|
22
|
+
protected inflectWord(word: string, gender: Gender, grammaticalCase: GrammaticalCase, isLastWord: boolean): string {
|
|
23
|
+
if (!isLastWord && countVowels(word) === 1) {
|
|
24
|
+
return word;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const [rule] = this.rules
|
|
28
|
+
.filter(rule => rule.gender.includes(gender))
|
|
29
|
+
.filter(rule => rule.usage.length === 0 || rule.usage.includes('lastName'))
|
|
30
|
+
.filter(rule => new RegExp(rule.pattern.find, 'gi').test(word))
|
|
31
|
+
.filter((rule) => {
|
|
32
|
+
const partOfSpeech = this.partOfSpeechRecognizer.recognize(word, gender);
|
|
33
|
+
return partOfSpeech === rule.partOfSpeech || partOfSpeech === null;
|
|
34
|
+
})
|
|
35
|
+
.sort((firstRule, secondRule) => {
|
|
36
|
+
if (firstRule.usage.length === 0) {
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
if (secondRule.usage.length > 0) {
|
|
40
|
+
return 0;
|
|
41
|
+
}
|
|
42
|
+
if (secondRule.usage.includes('lastName')) {
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
return 1;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (!rule) {
|
|
49
|
+
return word;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return new RuleInflector(rule).inflect(word, grammaticalCase);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { InflectorRule } from './inflector.types';
|
|
2
|
+
import { Gender, GrammaticalCase } from '../core';
|
|
3
|
+
import { RuleInflector } from './rule-inflector';
|
|
4
|
+
import { NameInflector } from './name-inflector';
|
|
5
|
+
|
|
6
|
+
export class MiddleNameInflector extends NameInflector {
|
|
7
|
+
private readonly rules: InflectorRule[];
|
|
8
|
+
|
|
9
|
+
constructor(rules: InflectorRule[]) {
|
|
10
|
+
super();
|
|
11
|
+
this.rules = rules;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @inheritdoc
|
|
16
|
+
*/
|
|
17
|
+
protected inflectWord(word: string, gender: Gender, grammaticalCase: GrammaticalCase): string {
|
|
18
|
+
const [rule] = this.rules
|
|
19
|
+
.filter(rule => rule.gender.includes(gender))
|
|
20
|
+
.filter(rule => rule.usage.includes('middleName'))
|
|
21
|
+
.filter(rule => new RegExp(rule.pattern.find, 'gi').test(word));
|
|
22
|
+
|
|
23
|
+
if (!rule) {
|
|
24
|
+
return word;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return new RuleInflector(rule).inflect(word, grammaticalCase);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Gender, GrammaticalCase } from '../core';
|
|
2
|
+
|
|
3
|
+
export abstract class NameInflector {
|
|
4
|
+
/**
|
|
5
|
+
* Inflects the name in the given grammatical case.
|
|
6
|
+
*/
|
|
7
|
+
inflect(name: string, gender: Gender, grammaticalCase: GrammaticalCase): string {
|
|
8
|
+
const words = name.split('-');
|
|
9
|
+
return words
|
|
10
|
+
.map((word, wordIndex) => this.inflectWord(word, gender, grammaticalCase, wordIndex === words.length - 1))
|
|
11
|
+
.join('-');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Inflects the word of the compound name in the given grammatical case.
|
|
16
|
+
*/
|
|
17
|
+
// tslint:disable-next-line max-line-length
|
|
18
|
+
protected abstract inflectWord(word: string, gender: Gender, grammaticalCase: GrammaticalCase, isLastWord: boolean): string;
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { InflectorCommandRunner } from './inflector-command-runner';
|
|
2
|
+
import { InflectorCommand } from './inflector.types';
|
|
3
|
+
|
|
4
|
+
export class ReplaceCommandRunner implements InflectorCommandRunner {
|
|
5
|
+
private readonly command: InflectorCommand;
|
|
6
|
+
|
|
7
|
+
constructor(command: InflectorCommand) {
|
|
8
|
+
this.command = command;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Replaces the given value with the command value.
|
|
13
|
+
* Returns a new value.
|
|
14
|
+
*/
|
|
15
|
+
exec(value: string): string {
|
|
16
|
+
return this.command.value;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { countGroups } from '../utils/regexp.utils';
|
|
2
|
+
import { copyLetterCase } from '../utils/string.utils';
|
|
3
|
+
import { GrammaticalCase } from '../core';
|
|
4
|
+
import { InflectorRule } from './inflector.types';
|
|
5
|
+
import { CommandRunnerFactory } from './command-runner-factory';
|
|
6
|
+
|
|
7
|
+
export class RuleInflector {
|
|
8
|
+
private readonly rule: InflectorRule;
|
|
9
|
+
private readonly commandRunnerFactory: CommandRunnerFactory;
|
|
10
|
+
|
|
11
|
+
constructor(rule: InflectorRule) {
|
|
12
|
+
this.rule = rule;
|
|
13
|
+
this.commandRunnerFactory = new CommandRunnerFactory();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Inflects the given word in the given grammatical case using the rule.
|
|
18
|
+
*/
|
|
19
|
+
inflect(word: string, grammaticalCase: GrammaticalCase): string {
|
|
20
|
+
const [commands] = this.rule.grammaticalCases[grammaticalCase];
|
|
21
|
+
if (commands) {
|
|
22
|
+
const searchValue = new RegExp(this.rule.pattern.modify, 'gi');
|
|
23
|
+
const inflectedWord = word.replace(searchValue, (match, ...groups) => {
|
|
24
|
+
let replacer = '';
|
|
25
|
+
const groupCount = countGroups(this.rule.pattern.modify);
|
|
26
|
+
for (let groupIndex = 0; groupIndex < groupCount; groupIndex += 1) {
|
|
27
|
+
let value = groups[groupIndex];
|
|
28
|
+
const command = commands[groupIndex];
|
|
29
|
+
if (command != null) {
|
|
30
|
+
value = this.commandRunnerFactory.make(command).exec(value);
|
|
31
|
+
}
|
|
32
|
+
replacer += value;
|
|
33
|
+
}
|
|
34
|
+
return replacer;
|
|
35
|
+
});
|
|
36
|
+
return copyLetterCase(word, inflectedWord);
|
|
37
|
+
}
|
|
38
|
+
return word;
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/bootstrap.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AnthroponymInflector,
|
|
3
|
+
FirstNameInflector,
|
|
4
|
+
InflectorRule,
|
|
5
|
+
LastNameInflector,
|
|
6
|
+
MiddleNameInflector,
|
|
7
|
+
} from './anthroponym-inflection';
|
|
8
|
+
import { Gender } from './core';
|
|
9
|
+
import {
|
|
10
|
+
NeuralNetwork,
|
|
11
|
+
NeuralNetworkTrainingData,
|
|
12
|
+
PartOfSpeechRecognizer,
|
|
13
|
+
PartOfSpeechRecognitionRule,
|
|
14
|
+
} from './part-of-speech-recognition';
|
|
15
|
+
// tslint:disable-next-line import-name
|
|
16
|
+
import inflectorRules from './resources/inflector/rules.json';
|
|
17
|
+
// tslint:disable-next-line import-name
|
|
18
|
+
import kosmiiCache from './resources/neural-networks/kosmii/cache.json';
|
|
19
|
+
// tslint:disable-next-line import-name
|
|
20
|
+
import kosmiiStructure from './resources/neural-networks/kosmii/structure.json';
|
|
21
|
+
// tslint:disable-next-line import-name
|
|
22
|
+
import pelykhCache from './resources/neural-networks/pelykh/cache.json';
|
|
23
|
+
// tslint:disable-next-line import-name
|
|
24
|
+
import pelykhStructure from './resources/neural-networks/pelykh/structure.json';
|
|
25
|
+
// tslint:disable-next-line import-name
|
|
26
|
+
import pohorielovaCache from './resources/neural-networks/pohorielova/cache.json';
|
|
27
|
+
// tslint:disable-next-line import-name
|
|
28
|
+
import pohorielovaStructure from './resources/neural-networks/pohorielova/structure.json';
|
|
29
|
+
|
|
30
|
+
const partOfSpeechRecognizer = new PartOfSpeechRecognizer([
|
|
31
|
+
new PartOfSpeechRecognitionRule(
|
|
32
|
+
(word: string, gender: Gender) => gender === Gender.Female && /[ая]$/i.test(word),
|
|
33
|
+
NeuralNetwork.fromJSON(pohorielovaStructure),
|
|
34
|
+
pohorielovaCache as NeuralNetworkTrainingData,
|
|
35
|
+
),
|
|
36
|
+
new PartOfSpeechRecognitionRule(
|
|
37
|
+
(word: string, gender: Gender) => gender === Gender.Male && /(ой|ий|ій)$/i.test(word),
|
|
38
|
+
NeuralNetwork.fromJSON(kosmiiStructure),
|
|
39
|
+
kosmiiCache as NeuralNetworkTrainingData,
|
|
40
|
+
),
|
|
41
|
+
new PartOfSpeechRecognitionRule(
|
|
42
|
+
(word: string, gender: Gender) => gender === Gender.Male && /(их)$/i.test(word),
|
|
43
|
+
NeuralNetwork.fromJSON(pelykhStructure),
|
|
44
|
+
pelykhCache as NeuralNetworkTrainingData,
|
|
45
|
+
),
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
const firstNameInflector = new FirstNameInflector(inflectorRules as InflectorRule[]);
|
|
49
|
+
const middleNameInflector = new MiddleNameInflector(inflectorRules as InflectorRule[]);
|
|
50
|
+
const lastNameInflector = new LastNameInflector(inflectorRules as InflectorRule[], partOfSpeechRecognizer);
|
|
51
|
+
const anthroponymInflector = new AnthroponymInflector(firstNameInflector, middleNameInflector, lastNameInflector);
|
|
52
|
+
|
|
53
|
+
export { anthroponymInflector };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Gender } from './gender.enum';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validates given anthroponym object.
|
|
5
|
+
* Throws an error if validation fails.
|
|
6
|
+
*
|
|
7
|
+
* @throws TypeError
|
|
8
|
+
*/
|
|
9
|
+
export function validateAnthroponym(anthroponym: any): void {
|
|
10
|
+
if (anthroponym == null) {
|
|
11
|
+
throw new TypeError('"anthroponym" must be an object.');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (typeof anthroponym !== 'object') {
|
|
15
|
+
throw new TypeError('"anthroponym" must be an object.');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (![Gender.Male, Gender.Female].includes(anthroponym.gender)) {
|
|
19
|
+
throw new TypeError(`"anthroponym.gender" must be one of the following: "${Gender.Male}", "${Gender.Female}".`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// tslint:disable-next-line max-line-length
|
|
23
|
+
if (typeof anthroponym.firstName === 'undefined' && typeof anthroponym.middleName === 'undefined' && typeof anthroponym.lastName === 'undefined') {
|
|
24
|
+
// tslint:disable-next-line max-line-length
|
|
25
|
+
throw new TypeError('At least one of the following fields must present: "anthroponym.firstName", "anthroponym.middleName", "anthroponym.lastName".');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof anthroponym.firstName !== 'undefined' && typeof anthroponym.firstName !== 'string') {
|
|
29
|
+
throw new TypeError('"anthroponym.firstName" must be a string.');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (typeof anthroponym.middleName !== 'undefined' && typeof anthroponym.middleName !== 'string') {
|
|
33
|
+
throw new TypeError('"anthroponym.middleName" must be a string.');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (typeof anthroponym.lastName !== 'undefined' && typeof anthroponym.lastName !== 'string') {
|
|
37
|
+
throw new TypeError('"anthroponym.lastName" must be a string.');
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/internal.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { NeuralNetwork } from './part-of-speech-recognition';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { NeuralNetwork } from './neural-network';
|
|
2
|
+
export { NeuralNetworkTrainingData } from './neural-network-training-data';
|
|
3
|
+
export { PartOfSpeech } from './part-of-speech.enum';
|
|
4
|
+
export { PartOfSpeechRecognizer } from './part-of-speech-recognizer';
|
|
5
|
+
export { PartOfSpeechRecognitionRule } from './part-of-speech-recognition-rule';
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import synaptic from 'synaptic';
|
|
2
|
+
import { PartOfSpeech } from './part-of-speech.enum';
|
|
3
|
+
import { WordEncoder } from './word-encoder';
|
|
4
|
+
import { PartOfSpeechDecoder } from './part-of-speech-decoder';
|
|
5
|
+
import { neuralNetworkConfig } from './neural-network-config';
|
|
6
|
+
import { PartOfSpeechEncoder } from './part-of-speech-encoder';
|
|
7
|
+
import { NeuralNetworkTrainingData } from './neural-network-training-data';
|
|
8
|
+
|
|
9
|
+
export class NeuralNetwork {
|
|
10
|
+
private network: synaptic.Network;
|
|
11
|
+
|
|
12
|
+
constructor() {
|
|
13
|
+
this.network = new synaptic.Architect.Perceptron(
|
|
14
|
+
neuralNetworkConfig.inputLayerSize,
|
|
15
|
+
neuralNetworkConfig.hiddenLayerSize,
|
|
16
|
+
neuralNetworkConfig.outputLayerSize,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Creates a neural network from JSON.
|
|
22
|
+
*/
|
|
23
|
+
static fromJSON(structure: any): NeuralNetwork {
|
|
24
|
+
const instance = new this();
|
|
25
|
+
instance.network = synaptic.Network.fromJSON(structure);
|
|
26
|
+
return instance;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Returns a JSON representation of the neural network.
|
|
31
|
+
*/
|
|
32
|
+
toJSON() {
|
|
33
|
+
return this.network.toJSON();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Serializes the neural network to JSON string.
|
|
38
|
+
*/
|
|
39
|
+
toString(): string {
|
|
40
|
+
return JSON.stringify(this.toJSON(), null, 2);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Trains the neural network using the given training data.
|
|
45
|
+
*/
|
|
46
|
+
train(trainingData: NeuralNetworkTrainingData, trainingOptions: any): NeuralNetwork {
|
|
47
|
+
const trainingSet = Object.entries(trainingData).map(([word, partOfSpeech]) => {
|
|
48
|
+
return {
|
|
49
|
+
input: new WordEncoder().encode(word),
|
|
50
|
+
output: new PartOfSpeechEncoder().encode(partOfSpeech),
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
new synaptic.Trainer(this.network).train(trainingSet, trainingOptions);
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Activates the neural network for the given word.
|
|
59
|
+
* Returns a part of speech of the given word.
|
|
60
|
+
*/
|
|
61
|
+
activate(word: string): PartOfSpeech {
|
|
62
|
+
const input = new WordEncoder().encode(word);
|
|
63
|
+
const output = this.network.activate(input);
|
|
64
|
+
return new PartOfSpeechDecoder().decode(output);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PartOfSpeech } from './part-of-speech.enum';
|
|
2
|
+
import { DIGIT_ADJECTIVE, DIGIT_NOUN } from './part-of-speech-encoder';
|
|
3
|
+
|
|
4
|
+
export class PartOfSpeechDecoder {
|
|
5
|
+
/**
|
|
6
|
+
* Decodes an output of a neural network.
|
|
7
|
+
* Returns a part of speech.
|
|
8
|
+
*/
|
|
9
|
+
decode(input: number[]): PartOfSpeech {
|
|
10
|
+
if (input.length !== 1) {
|
|
11
|
+
throw new TypeError('Invalid input.');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const digit = input[0];
|
|
15
|
+
|
|
16
|
+
if (Math.round(digit) === DIGIT_NOUN) {
|
|
17
|
+
return PartOfSpeech.Noun;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (Math.round(digit) === DIGIT_ADJECTIVE) {
|
|
21
|
+
return PartOfSpeech.Adjective;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
throw new TypeError('Invalid input.');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PartOfSpeech } from './part-of-speech.enum';
|
|
2
|
+
|
|
3
|
+
export const DIGIT_NOUN = 0;
|
|
4
|
+
export const DIGIT_ADJECTIVE = 1;
|
|
5
|
+
|
|
6
|
+
export class PartOfSpeechEncoder {
|
|
7
|
+
/**
|
|
8
|
+
* Encodes a part of speech for use in the neural network.
|
|
9
|
+
*/
|
|
10
|
+
encode(input: PartOfSpeech): number[] {
|
|
11
|
+
switch (input) {
|
|
12
|
+
case PartOfSpeech.Noun:
|
|
13
|
+
return [DIGIT_NOUN];
|
|
14
|
+
case PartOfSpeech.Adjective:
|
|
15
|
+
return [DIGIT_ADJECTIVE];
|
|
16
|
+
default:
|
|
17
|
+
throw new TypeError('Invalid input.');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|