shevchenko 2.1.2 → 2.2.0

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.
Files changed (77) hide show
  1. package/.eslintrc.json +53 -0
  2. package/.prettierrc.json +5 -0
  3. package/dist/cjs/shevchenko.js +1307 -122
  4. package/dist/cjs/shevchenko.js.map +1 -1
  5. package/dist/es/shevchenko.js +1310 -128
  6. package/dist/es/shevchenko.js.map +1 -1
  7. package/dist/types/anthroponym-inflection/anthroponym-inflector.d.ts.map +1 -1
  8. package/dist/types/anthroponym-inflection/command-runner-factory.d.ts +1 -1
  9. package/dist/types/anthroponym-inflection/command-runner-factory.d.ts.map +1 -1
  10. package/dist/types/anthroponym-inflection/first-name-inflector.d.ts +1 -1
  11. package/dist/types/anthroponym-inflection/first-name-inflector.d.ts.map +1 -1
  12. package/dist/types/anthroponym-inflection/last-name-inflector.d.ts +1 -1
  13. package/dist/types/anthroponym-inflection/last-name-inflector.d.ts.map +1 -1
  14. package/dist/types/anthroponym-inflection/middle-name-inflector.d.ts +1 -1
  15. package/dist/types/anthroponym-inflection/middle-name-inflector.d.ts.map +1 -1
  16. package/dist/types/anthroponym-inflection/name-inflector.d.ts.map +1 -1
  17. package/dist/types/anthroponym-inflection/replace-command-runner.d.ts +1 -1
  18. package/dist/types/anthroponym-inflection/replace-command-runner.d.ts.map +1 -1
  19. package/dist/types/anthroponym-inflection/rule-inflector.d.ts.map +1 -1
  20. package/dist/types/bootstrap.d.ts.map +1 -1
  21. package/dist/types/core/validator.d.ts.map +1 -1
  22. package/dist/types/gender-detection/detect-gender.d.ts +11 -0
  23. package/dist/types/gender-detection/detect-gender.d.ts.map +1 -0
  24. package/dist/types/gender-detection/index.d.ts +2 -0
  25. package/dist/types/gender-detection/index.d.ts.map +1 -0
  26. package/dist/types/part-of-speech-recognition/neural-network.d.ts +1 -1
  27. package/dist/types/part-of-speech-recognition/neural-network.d.ts.map +1 -1
  28. package/dist/types/part-of-speech-recognition/part-of-speech-decoder.d.ts.map +1 -1
  29. package/dist/types/part-of-speech-recognition/part-of-speech-recognition-rule.d.ts +1 -1
  30. package/dist/types/part-of-speech-recognition/part-of-speech-recognition-rule.d.ts.map +1 -1
  31. package/dist/types/part-of-speech-recognition/part-of-speech-recognizer.d.ts +1 -1
  32. package/dist/types/part-of-speech-recognition/part-of-speech-recognizer.d.ts.map +1 -1
  33. package/dist/types/shevchenko.d.ts +3 -2
  34. package/dist/types/shevchenko.d.ts.map +1 -1
  35. package/dist/types/utils/string.utils.d.ts.map +1 -1
  36. package/dist/umd/shevchenko.min.js +4 -4
  37. package/dist/umd/shevchenko.min.js.map +1 -1
  38. package/nuxt/components/declension-demo.vue +7 -1
  39. package/nuxt/components/declension-form.vue +77 -10
  40. package/nuxt/components/declension-results.vue +26 -7
  41. package/nuxt/components/how-it-works.vue +10 -10
  42. package/nuxt/composables/declension.ts +47 -4
  43. package/nuxt/data/female-first-names.json +556 -0
  44. package/nuxt/data/male-first-names.json +603 -0
  45. package/nuxt/locales/en-US.json +6 -3
  46. package/nuxt/locales/uk-UA.json +8 -5
  47. package/nuxt/package-lock.json +7 -7
  48. package/nuxt/package.json +2 -2
  49. package/package.json +13 -10
  50. package/src/anthroponym-inflection/anthroponym-inflector.ts +20 -5
  51. package/src/anthroponym-inflection/command-runner-factory.ts +2 -2
  52. package/src/anthroponym-inflection/first-name-inflector.ts +5 -5
  53. package/src/anthroponym-inflection/inflector.types.ts +19 -19
  54. package/src/anthroponym-inflection/last-name-inflector.ts +10 -6
  55. package/src/anthroponym-inflection/middle-name-inflector.ts +5 -5
  56. package/src/anthroponym-inflection/name-inflector.ts +9 -3
  57. package/src/anthroponym-inflection/replace-command-runner.ts +1 -1
  58. package/src/anthroponym-inflection/rule-inflector.ts +2 -2
  59. package/src/bootstrap.ts +10 -10
  60. package/src/core/validator.ts +12 -5
  61. package/src/gender-detection/detect-gender.ts +43 -0
  62. package/src/gender-detection/given-names-genders.json +1144 -0
  63. package/src/gender-detection/index.ts +1 -0
  64. package/src/part-of-speech-recognition/neural-network.ts +10 -5
  65. package/src/part-of-speech-recognition/part-of-speech-decoder.ts +1 -1
  66. package/src/part-of-speech-recognition/part-of-speech-recognition-rule.ts +6 -2
  67. package/src/part-of-speech-recognition/part-of-speech-recognizer.ts +2 -2
  68. package/src/part-of-speech-recognition/word-encoder.ts +2 -2
  69. package/src/resources/inflector/rules.json +27 -26
  70. package/src/shevchenko.ts +4 -3
  71. package/src/utils/string.utils.ts +3 -3
  72. package/test/anthroponym-inflection.js +58 -0
  73. package/test/gender-detection.js +29 -0
  74. package/test/input-validation.js +353 -0
  75. package/test/main.test.js +18 -0
  76. package/test/samples.json +7282 -0
  77. package/tsconfig.types.json +9 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shevchenko",
3
- "version": "2.1.2",
3
+ "version": "2.2.0",
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",
@@ -16,10 +16,9 @@
16
16
  "build:pelykh-cache": "gulp build:pelykh-cache",
17
17
  "build:network-cache": "gulp build:network-cache",
18
18
  "prebuild": "rimraf ./dist",
19
- "build": "rollup -c && tsc -p ./tsconfig.declaration.json",
20
- "lint": "tslint --fix -p ./tsconfig.json -c ./tslint.json",
21
- "test": "mocha --bail ./tests/main.js",
22
- "build:docs": "node ./docs/src/main.mjs"
19
+ "build": "rollup -c && tsc -p ./tsconfig.types.json",
20
+ "lint": "eslint --fix ./src ./test",
21
+ "test": "jest ./test"
23
22
  },
24
23
  "keywords": [
25
24
  "ukrainian",
@@ -62,18 +61,22 @@
62
61
  "@rollup/plugin-typescript": "^6.1.0",
63
62
  "@types/lodash": "^4.14.165",
64
63
  "@types/synaptic": "^1.0.3",
64
+ "@typescript-eslint/eslint-plugin": "^5.48.2",
65
+ "@typescript-eslint/parser": "^5.48.2",
65
66
  "axios": "^0.21.0",
66
- "chai": "^4.2.0",
67
67
  "ejs": "^3.1.7",
68
+ "eslint": "^8.32.0",
69
+ "eslint-config-prettier": "^8.6.0",
70
+ "eslint-plugin-import": "^2.27.5",
71
+ "eslint-plugin-prettier": "^4.2.1",
68
72
  "gulp": "^4.0.2",
69
73
  "i18n": "^0.13.2",
70
- "mocha": "^8.2.1",
74
+ "jest": "^29.3.1",
71
75
  "nuxi": "*",
76
+ "prettier": "2.8.3",
72
77
  "rimraf": "^3.0.2",
73
78
  "rollup": "^2.34.2",
74
79
  "rollup-plugin-terser": "^7.0.2",
75
- "tslint": "^6.1.3",
76
- "tslint-config-airbnb": "^5.11.2",
77
80
  "typescript": "^4.1.2"
78
81
  }
79
- }
82
+ }
@@ -8,8 +8,11 @@ export class AnthroponymInflector {
8
8
  private readonly middleNameInflector: MiddleNameInflector;
9
9
  private readonly lastNameInflector: LastNameInflector;
10
10
 
11
- // tslint:disable-next-line max-line-length
12
- constructor(firstNameInflector: FirstNameInflector, middleNameInflector: MiddleNameInflector, lastNameInflector: LastNameInflector) {
11
+ constructor(
12
+ firstNameInflector: FirstNameInflector,
13
+ middleNameInflector: MiddleNameInflector,
14
+ lastNameInflector: LastNameInflector,
15
+ ) {
13
16
  this.firstNameInflector = firstNameInflector;
14
17
  this.middleNameInflector = middleNameInflector;
15
18
  this.lastNameInflector = lastNameInflector;
@@ -22,15 +25,27 @@ export class AnthroponymInflector {
22
25
  const result: Anthroponym = { gender: anthroponym.gender };
23
26
 
24
27
  if (anthroponym.firstName != null) {
25
- result.firstName = this.firstNameInflector.inflect(anthroponym.firstName, anthroponym.gender, grammaticalCase);
28
+ result.firstName = this.firstNameInflector.inflect(
29
+ anthroponym.firstName,
30
+ anthroponym.gender,
31
+ grammaticalCase,
32
+ );
26
33
  }
27
34
 
28
35
  if (anthroponym.middleName != null) {
29
- result.middleName = this.middleNameInflector.inflect(anthroponym.middleName, anthroponym.gender, grammaticalCase);
36
+ result.middleName = this.middleNameInflector.inflect(
37
+ anthroponym.middleName,
38
+ anthroponym.gender,
39
+ grammaticalCase,
40
+ );
30
41
  }
31
42
 
32
43
  if (anthroponym.lastName != null) {
33
- result.lastName = this.lastNameInflector.inflect(anthroponym.lastName, anthroponym.gender, grammaticalCase);
44
+ result.lastName = this.lastNameInflector.inflect(
45
+ anthroponym.lastName,
46
+ anthroponym.gender,
47
+ grammaticalCase,
48
+ );
34
49
  }
35
50
 
36
51
  return result;
@@ -1,7 +1,7 @@
1
- import { InflectorCommand, InflectorCommandType } from './inflector.types';
2
1
  import { AppendCommandRunner } from './append-command-runner';
3
- import { ReplaceCommandRunner } from './replace-command-runner';
4
2
  import { InflectorCommandRunner } from './inflector-command-runner';
3
+ import { InflectorCommand, InflectorCommandType } from './inflector.types';
4
+ import { ReplaceCommandRunner } from './replace-command-runner';
5
5
 
6
6
  export class CommandRunnerFactory {
7
7
  /**
@@ -1,7 +1,7 @@
1
- import { InflectorRule } from './inflector.types';
2
1
  import { Gender, GrammaticalCase } from '../core';
3
- import { RuleInflector } from './rule-inflector';
2
+ import { InflectorRule } from './inflector.types';
4
3
  import { NameInflector } from './name-inflector';
4
+ import { RuleInflector } from './rule-inflector';
5
5
 
6
6
  export class FirstNameInflector extends NameInflector {
7
7
  private readonly rules: InflectorRule[];
@@ -16,9 +16,9 @@ export class FirstNameInflector extends NameInflector {
16
16
  */
17
17
  protected inflectWord(word: string, gender: Gender, grammaticalCase: GrammaticalCase): string {
18
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))
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
22
  .sort((firstRule, secondRule) => {
23
23
  if (firstRule.usage.length === 0) {
24
24
  return 0;
@@ -2,29 +2,29 @@ import { Gender, GrammaticalCase } from '../core';
2
2
  import { PartOfSpeech } from '../part-of-speech-recognition';
3
3
 
4
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,
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
13
  };
14
14
 
15
15
  export type InflectorPattern = {
16
- find: string,
17
- modify: string,
16
+ find: string;
17
+ modify: string;
18
18
  };
19
19
 
20
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[],
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
28
  };
29
29
 
30
30
  export type InflectorCommandGroup = {
@@ -32,8 +32,8 @@ export type InflectorCommandGroup = {
32
32
  };
33
33
 
34
34
  export type InflectorCommand = {
35
- type: InflectorCommandType,
36
- value: string,
35
+ type: InflectorCommandType;
36
+ value: string;
37
37
  };
38
38
 
39
39
  export enum InflectorCommandType {
@@ -1,7 +1,7 @@
1
- import { InflectorRule } from './inflector.types';
2
1
  import { Gender, GrammaticalCase } from '../core';
3
2
  import { PartOfSpeechRecognizer } from '../part-of-speech-recognition';
4
3
  import { countVowels } from '../utils/lang.utils';
4
+ import { InflectorRule } from './inflector.types';
5
5
  import { NameInflector } from './name-inflector';
6
6
  import { RuleInflector } from './rule-inflector';
7
7
 
@@ -18,16 +18,20 @@ export class LastNameInflector extends NameInflector {
18
18
  /**
19
19
  * @inheritdoc
20
20
  */
21
- // tslint:disable-next-line max-line-length
22
- protected inflectWord(word: string, gender: Gender, grammaticalCase: GrammaticalCase, isLastWord: boolean): string {
21
+ protected inflectWord(
22
+ word: string,
23
+ gender: Gender,
24
+ grammaticalCase: GrammaticalCase,
25
+ isLastWord: boolean,
26
+ ): string {
23
27
  if (!isLastWord && countVowels(word) === 1) {
24
28
  return word;
25
29
  }
26
30
 
27
31
  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))
32
+ .filter((rule) => rule.gender.includes(gender))
33
+ .filter((rule) => rule.usage.length === 0 || rule.usage.includes('lastName'))
34
+ .filter((rule) => new RegExp(rule.pattern.find, 'gi').test(word))
31
35
  .filter((rule) => {
32
36
  const partOfSpeech = this.partOfSpeechRecognizer.recognize(word, gender);
33
37
  return partOfSpeech === rule.partOfSpeech || partOfSpeech === null;
@@ -1,7 +1,7 @@
1
- import { InflectorRule } from './inflector.types';
2
1
  import { Gender, GrammaticalCase } from '../core';
3
- import { RuleInflector } from './rule-inflector';
2
+ import { InflectorRule } from './inflector.types';
4
3
  import { NameInflector } from './name-inflector';
4
+ import { RuleInflector } from './rule-inflector';
5
5
 
6
6
  export class MiddleNameInflector extends NameInflector {
7
7
  private readonly rules: InflectorRule[];
@@ -16,9 +16,9 @@ export class MiddleNameInflector extends NameInflector {
16
16
  */
17
17
  protected inflectWord(word: string, gender: Gender, grammaticalCase: GrammaticalCase): string {
18
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));
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
22
 
23
23
  if (!rule) {
24
24
  return word;
@@ -7,13 +7,19 @@ export abstract class NameInflector {
7
7
  inflect(name: string, gender: Gender, grammaticalCase: GrammaticalCase): string {
8
8
  const words = name.split('-');
9
9
  return words
10
- .map((word, wordIndex) => this.inflectWord(word, gender, grammaticalCase, wordIndex === words.length - 1))
10
+ .map((word, wordIndex) =>
11
+ this.inflectWord(word, gender, grammaticalCase, wordIndex === words.length - 1),
12
+ )
11
13
  .join('-');
12
14
  }
13
15
 
14
16
  /**
15
17
  * Inflects the word of the compound name in the given grammatical case.
16
18
  */
17
- // tslint:disable-next-line max-line-length
18
- protected abstract inflectWord(word: string, gender: Gender, grammaticalCase: GrammaticalCase, isLastWord: boolean): string;
19
+ protected abstract inflectWord(
20
+ word: string,
21
+ gender: Gender,
22
+ grammaticalCase: GrammaticalCase,
23
+ isLastWord: boolean,
24
+ ): string;
19
25
  }
@@ -12,7 +12,7 @@ export class ReplaceCommandRunner implements InflectorCommandRunner {
12
12
  * Replaces the given value with the command value.
13
13
  * Returns a new value.
14
14
  */
15
- exec(value: string): string {
15
+ exec(): string {
16
16
  return this.command.value;
17
17
  }
18
18
  }
@@ -1,8 +1,8 @@
1
+ import { GrammaticalCase } from '../core';
1
2
  import { countGroups } from '../utils/regexp.utils';
2
3
  import { copyLetterCase } from '../utils/string.utils';
3
- import { GrammaticalCase } from '../core';
4
- import { InflectorRule } from './inflector.types';
5
4
  import { CommandRunnerFactory } from './command-runner-factory';
5
+ import { InflectorRule } from './inflector.types';
6
6
 
7
7
  export class RuleInflector {
8
8
  private readonly rule: InflectorRule;
package/src/bootstrap.ts CHANGED
@@ -9,22 +9,15 @@ import { Gender } from './core';
9
9
  import {
10
10
  NeuralNetwork,
11
11
  NeuralNetworkTrainingData,
12
- PartOfSpeechRecognizer,
13
12
  PartOfSpeechRecognitionRule,
13
+ PartOfSpeechRecognizer,
14
14
  } from './part-of-speech-recognition';
15
- // tslint:disable-next-line import-name
16
15
  import inflectorRules from './resources/inflector/rules.json';
17
- // tslint:disable-next-line import-name
18
16
  import kosmiiCache from './resources/neural-networks/kosmii/cache.json';
19
- // tslint:disable-next-line import-name
20
17
  import kosmiiStructure from './resources/neural-networks/kosmii/structure.json';
21
- // tslint:disable-next-line import-name
22
18
  import pelykhCache from './resources/neural-networks/pelykh/cache.json';
23
- // tslint:disable-next-line import-name
24
19
  import pelykhStructure from './resources/neural-networks/pelykh/structure.json';
25
- // tslint:disable-next-line import-name
26
20
  import pohorielovaCache from './resources/neural-networks/pohorielova/cache.json';
27
- // tslint:disable-next-line import-name
28
21
  import pohorielovaStructure from './resources/neural-networks/pohorielova/structure.json';
29
22
 
30
23
  const partOfSpeechRecognizer = new PartOfSpeechRecognizer([
@@ -47,7 +40,14 @@ const partOfSpeechRecognizer = new PartOfSpeechRecognizer([
47
40
 
48
41
  const firstNameInflector = new FirstNameInflector(inflectorRules as InflectorRule[]);
49
42
  const middleNameInflector = new MiddleNameInflector(inflectorRules as InflectorRule[]);
50
- const lastNameInflector = new LastNameInflector(inflectorRules as InflectorRule[], partOfSpeechRecognizer);
51
- const anthroponymInflector = new AnthroponymInflector(firstNameInflector, middleNameInflector, lastNameInflector);
43
+ const lastNameInflector = new LastNameInflector(
44
+ inflectorRules as InflectorRule[],
45
+ partOfSpeechRecognizer,
46
+ );
47
+ const anthroponymInflector = new AnthroponymInflector(
48
+ firstNameInflector,
49
+ middleNameInflector,
50
+ lastNameInflector,
51
+ );
52
52
 
53
53
  export { anthroponymInflector };
@@ -6,6 +6,7 @@ import { Gender } from './gender.enum';
6
6
  *
7
7
  * @throws TypeError
8
8
  */
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
9
10
  export function validateAnthroponym(anthroponym: any): void {
10
11
  if (anthroponym == null) {
11
12
  throw new TypeError('"anthroponym" must be an object.');
@@ -16,13 +17,19 @@ export function validateAnthroponym(anthroponym: any): void {
16
17
  }
17
18
 
18
19
  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
+ throw new TypeError(
21
+ `"anthroponym.gender" must be one of the following: "${Gender.Male}", "${Gender.Female}".`,
22
+ );
20
23
  }
21
24
 
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".');
25
+ if (
26
+ typeof anthroponym.firstName === 'undefined' &&
27
+ typeof anthroponym.middleName === 'undefined' &&
28
+ typeof anthroponym.lastName === 'undefined'
29
+ ) {
30
+ throw new TypeError(
31
+ 'At least one of the following fields must present: "anthroponym.firstName", "anthroponym.middleName", "anthroponym.lastName".',
32
+ );
26
33
  }
27
34
 
28
35
  if (typeof anthroponym.firstName !== 'undefined' && typeof anthroponym.firstName !== 'string') {
@@ -0,0 +1,43 @@
1
+ import { Anthroponym, Gender } from '../core';
2
+ import givenNamesGendersJson from './given-names-genders.json';
3
+
4
+ const MASCULINE_PATRONYMIC_PATTERN = /(и|і)ч$/;
5
+ const FEMININE_PATRONYMIC_PATTERN = /на$/;
6
+ const APOSTROPHE_VARIATION_PATTERN = /[`"]/g;
7
+
8
+ type KnownGivenName = keyof typeof givenNamesGendersJson;
9
+ export type GenderlessAnthroponym = Omit<Anthroponym, 'gender'>;
10
+
11
+ /**
12
+ * Detects the grammatical gender of the anthroponym using
13
+ * patronymic name endings and the dictionary of known given names.
14
+ *
15
+ * Returns the grammatical gender of the anthroponym.
16
+ * Returns null if the grammatical gender of the anthroponym cannot be detected.
17
+ */
18
+ export function detectGender(anthroponym: GenderlessAnthroponym): Gender | null {
19
+ if (anthroponym.middleName) {
20
+ const patronymicName = anthroponym.middleName
21
+ .replace(APOSTROPHE_VARIATION_PATTERN, "'")
22
+ .toLocaleLowerCase();
23
+
24
+ if (MASCULINE_PATRONYMIC_PATTERN.test(patronymicName)) {
25
+ return Gender.Male;
26
+ } else if (FEMININE_PATRONYMIC_PATTERN.test(patronymicName)) {
27
+ return Gender.Female;
28
+ }
29
+ }
30
+
31
+ if (anthroponym.firstName) {
32
+ const givenName = anthroponym.firstName
33
+ .replace(APOSTROPHE_VARIATION_PATTERN, "'")
34
+ .toLocaleLowerCase() as KnownGivenName;
35
+
36
+ const gender = givenNamesGendersJson[givenName];
37
+ if (gender != null) {
38
+ return gender as Gender;
39
+ }
40
+ }
41
+
42
+ return null;
43
+ }