shevchenko 2.1.0 → 2.1.2
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 +121 -32
- package/dist/cjs/shevchenko.js.map +1 -1
- package/dist/es/shevchenko.js +121 -32
- package/dist/es/shevchenko.js.map +1 -1
- package/dist/umd/shevchenko.min.js +3 -3
- package/dist/umd/shevchenko.min.js.map +1 -1
- package/nuxt/.eslintrc.json +27 -0
- package/nuxt/.prettierrc.json +6 -0
- package/nuxt/README.md +27 -0
- package/nuxt/app.config.ts +3 -0
- package/nuxt/app.vue +5 -0
- package/nuxt/assets/img/shevchenko_pixelized_304x304.jpg +0 -0
- package/nuxt/assets/img/shevchenko_pixelized_304x304.png +0 -0
- package/nuxt/assets/img/shevchenko_pixelized_608x608.jpg +0 -0
- package/nuxt/assets/style/main.scss +3 -0
- package/nuxt/components/about-modal.vue +58 -0
- package/nuxt/components/api-spec.vue +25 -0
- package/nuxt/components/contact-me-button.vue +11 -0
- package/nuxt/components/contact-me-modal.vue +63 -0
- package/nuxt/components/copy-button.vue +38 -0
- package/nuxt/components/declension-demo.vue +32 -0
- package/nuxt/components/declension-form.vue +91 -0
- package/nuxt/components/declension-preview.vue +170 -0
- package/nuxt/components/declension-results.vue +116 -0
- package/nuxt/components/error-404.vue +15 -0
- package/nuxt/components/github-issue-count.vue +18 -0
- package/nuxt/components/how-it-works.vue +51 -0
- package/nuxt/components/installation-instructions.vue +33 -0
- package/nuxt/components/library-docs.vue +27 -0
- package/nuxt/components/page-copyright.vue +15 -0
- package/nuxt/components/page-footer.vue +32 -0
- package/nuxt/components/page-header.vue +4 -0
- package/nuxt/components/preview-banner.vue +32 -0
- package/nuxt/components/share-links.vue +108 -0
- package/nuxt/components/top-nav-bar.vue +165 -0
- package/nuxt/components/ukrainian-flag.vue +37 -0
- package/nuxt/components/usage-example.vue +32 -0
- package/nuxt/composables/declension.ts +81 -0
- package/nuxt/composables/page-i18n.ts +11 -0
- package/nuxt/composables/page-meta.ts +5 -0
- package/nuxt/composables/route-utils.ts +17 -0
- package/nuxt/config.ts +20 -0
- package/nuxt/error.vue +25 -0
- package/nuxt/layouts/default.vue +5 -0
- package/nuxt/locales/en-US.json +60 -0
- package/nuxt/locales/uk-UA.json +62 -0
- package/nuxt/nuxt.config.ts +99 -0
- package/nuxt/package-lock.json +19524 -0
- package/nuxt/package.json +37 -0
- package/nuxt/pages/en-US/index.vue +41 -0
- package/nuxt/pages/index.vue +68 -0
- package/nuxt/plugins/i18n.ts +26 -0
- package/nuxt/public/.nojekyll +0 -0
- package/nuxt/public/CNAME +1 -0
- package/nuxt/public/android-icon-144x144.png +0 -0
- package/nuxt/public/android-icon-192x192.png +0 -0
- package/nuxt/public/android-icon-36x36.png +0 -0
- package/nuxt/public/android-icon-48x48.png +0 -0
- package/nuxt/public/android-icon-72x72.png +0 -0
- package/nuxt/public/android-icon-96x96.png +0 -0
- package/nuxt/public/apple-icon-114x114.png +0 -0
- package/nuxt/public/apple-icon-120x120.png +0 -0
- package/nuxt/public/apple-icon-144x144.png +0 -0
- package/nuxt/public/apple-icon-152x152.png +0 -0
- package/nuxt/public/apple-icon-180x180.png +0 -0
- package/nuxt/public/apple-icon-57x57.png +0 -0
- package/nuxt/public/apple-icon-60x60.png +0 -0
- package/nuxt/public/apple-icon-72x72.png +0 -0
- package/nuxt/public/apple-icon-76x76.png +0 -0
- package/nuxt/public/apple-icon-precomposed.png +0 -0
- package/nuxt/public/apple-icon.png +0 -0
- package/nuxt/public/browserconfig.xml +2 -0
- package/nuxt/public/favicon-16x16.png +0 -0
- package/nuxt/public/favicon-32x32.png +0 -0
- package/nuxt/public/favicon-96x96.png +0 -0
- package/nuxt/public/favicon.ico +0 -0
- package/nuxt/public/manifest.json +41 -0
- package/nuxt/public/ms-icon-144x144.png +0 -0
- package/nuxt/public/ms-icon-150x150.png +0 -0
- package/nuxt/public/ms-icon-310x310.png +0 -0
- package/nuxt/public/ms-icon-70x70.png +0 -0
- package/nuxt/public/preview-608x608.jpg +0 -0
- package/nuxt/scripts/postgenerate.js +7 -0
- package/nuxt/scripts/publish.js +7 -0
- package/nuxt/server/plugins/google-tag-manager.js +37 -0
- package/nuxt/tsconfig.json +11 -0
- package/package.json +3 -2
- 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 +7202 -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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { PartOfSpeech } from './part-of-speech.enum';
|
|
2
|
+
import { Gender } from '../core';
|
|
3
|
+
import { NeuralNetwork } from './neural-network';
|
|
4
|
+
import { NeuralNetworkTrainingData } from './neural-network-training-data';
|
|
5
|
+
import { WordTransformer } from './word-transformer';
|
|
6
|
+
|
|
7
|
+
export interface PartOfSpeechRecognitionGuard {
|
|
8
|
+
(word: string, gender: Gender): boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class PartOfSpeechRecognitionRule {
|
|
12
|
+
readonly guard: PartOfSpeechRecognitionGuard;
|
|
13
|
+
readonly cache: NeuralNetworkTrainingData;
|
|
14
|
+
private readonly neuralNetwork: NeuralNetwork;
|
|
15
|
+
|
|
16
|
+
constructor(guard: PartOfSpeechRecognitionGuard, neuralNetwork: NeuralNetwork, cache: NeuralNetworkTrainingData) {
|
|
17
|
+
this.guard = guard;
|
|
18
|
+
this.neuralNetwork = neuralNetwork;
|
|
19
|
+
this.cache = cache;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Applies the part of speech recognizer rule to the given word.
|
|
24
|
+
* Returns a part of speech of the given word.
|
|
25
|
+
*/
|
|
26
|
+
apply(word: string): PartOfSpeech {
|
|
27
|
+
const transformedWord = new WordTransformer().transform(word);
|
|
28
|
+
if (this.cache[transformedWord]) {
|
|
29
|
+
return this.cache[transformedWord];
|
|
30
|
+
}
|
|
31
|
+
return this.neuralNetwork.activate(transformedWord);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { PartOfSpeechRecognitionRule } from './part-of-speech-recognition-rule';
|
|
2
|
+
import { Gender } from '../core';
|
|
3
|
+
import { PartOfSpeech } from './part-of-speech.enum';
|
|
4
|
+
import { WordTransformer } from './word-transformer';
|
|
5
|
+
|
|
6
|
+
export class PartOfSpeechRecognizer {
|
|
7
|
+
private readonly rules: PartOfSpeechRecognitionRule[];
|
|
8
|
+
|
|
9
|
+
constructor(rules: PartOfSpeechRecognitionRule[]) {
|
|
10
|
+
this.rules = rules;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Recognizes the part of speech of the given word.
|
|
15
|
+
* Returns part of speech of the given word.
|
|
16
|
+
* Returns null if part of speech was not recognized.
|
|
17
|
+
*/
|
|
18
|
+
recognize(word: string, gender: Gender): PartOfSpeech | null {
|
|
19
|
+
const transformedWord = new WordTransformer().transform(word);
|
|
20
|
+
const rule = this.rules.find(rule => rule.guard(transformedWord, gender));
|
|
21
|
+
if (!rule) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
return rule.apply(transformedWord);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { neuralNetworkConfig } from './neural-network-config';
|
|
2
|
+
|
|
3
|
+
export class WordEncoder {
|
|
4
|
+
private readonly size: number;
|
|
5
|
+
|
|
6
|
+
constructor(size: number = neuralNetworkConfig.inputLayerSize) {
|
|
7
|
+
this.size = size;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Encodes a word for use in the neural network.
|
|
12
|
+
*/
|
|
13
|
+
encode(input: string): number[] {
|
|
14
|
+
return input
|
|
15
|
+
.toLowerCase()
|
|
16
|
+
.split('')
|
|
17
|
+
.map(char => char.charCodeAt(0).toString(2))
|
|
18
|
+
.join('')
|
|
19
|
+
.padStart(this.size, '0')
|
|
20
|
+
.split('')
|
|
21
|
+
.map(digit => Number.parseInt(digit, 2));
|
|
22
|
+
}
|
|
23
|
+
}
|