lvovich

repository·master·Indexed 20 days ago

https://github.com/nodkz/lvovich

A lightweight JavaScript library for inflecting Russian city names, personal names, surnames, and patronymics into various grammatical cases. It includes utilities to detect gender by name (first, last, or middle) and supports cases such as Nominative, Genitive, Dative, Accusative, Instrumental, and Prepositional. The library provides functions like incline(), getGender(), and cityIn() to generate natural language content for emails, PDFs, and SEO titles.

Tokens
4.3K
Snippets
15
Records
19
Agent score
70%

What's inside lvovich

  1. Install Lvovich

    master

    You can install Lvovich via npm for use in Node.js or bundler-based frontend projects, or include it directly in the browser via CDN.

    npm installation:

    npm install lvovich

    Browser installation (CDN):

    <script src="https://cdn.jsdelivr.net/npm/lvovich/dist/lvovich.min.js"></script>
    <script>
      var city = 'Москва';
      document.writeln('Найдено в ' + lvovich.cityIn(city) + '<br/>');
      document.writeln('Из ' + lvovich.cityFrom(city) + '<br/>');
      document.writeln('Еду в ' + lvovich.cityTo(city) + '<br/>');
    </script>
    npm install lvovich
  2. Define a DeclensionRuleSetT for custom declension

    master

    To use inclineByRules, you must provide a DeclensionRuleSetT object. This object defines how words should be transformed.

    Structure:

    • exceptions?: An array of DeclensionRuleT applied for exact matches before suffix checking.
    • suffixes?: An array of DeclensionRuleT applied if the word ends with specific characters.

    DeclensionRuleT properties:

    • gender?: The required gender (GenderStrT or GenderConstT). Rules can also be androgynous.
    • test: An array of strings used to match the input.
    • mods: A DeclentionModsT array (5 strings) representing the transformations for [GENITIVE, DATIVE, ACCUSATIVE, INSTRUMENTAL, PREPOSITIONAL].
      • Use '-' to remove the last character.
      • Use '.' to keep the character as is.
      • Use any other string to append characters.
    • tags?: Optional tags used to filter rules (e.g., when checking if a word is the firstWord).
    import type { DeclensionRuleSetT } from 'lvovich/src/inclineRules';
    
    const myRuleSet: DeclensionRuleSetT = {
      suffixes: [
        {
          test: ['ov'],
          mods: ['ova', 'ovu', 'ov', 'ovom', 'ovoy'], // mods for Gen, Dat, Acc, Ins, Prep
        }
      ]
    };
  3. Determine gender from names

    master

    Use these functions to identify the gender of a person based on their first name, last name, or patronymic (middle name).

    Return values (GenderStrT):

    • male - masculine
    • female - feminine
    • androgynous - gender-neutral/ambiguous
    • null - could not be determined

    Available methods:

    • getGender(fio: FioT): ?GenderStrT: Takes a full name object and returns the inferred gender.
    • getFirstnameGender(str: string): ?GenderStrT: Returns gender for a first name.
    • getLastnameGender(str: string): ?GenderStrT: Returns gender for a last name.
    • getMiddlenameGender(str: string): ?GenderStrT: Returns gender for a patronymic.
    import { getGender, getFirstnameGender, getLastnameGender, getMiddlenameGender } from 'lvovich';
    
    // Using FioT object
    getGender({ last: 'Друзь', first: 'Саша', middle: 'Петрович' }); // 'male'
    getGender({ first: 'Саша' }); // 'androgynous'
    
    // Using strings
    getFirstnameGender('Павел'); // 'male'
    getLastnameGender('Таптыгина'); // 'female'
    getMiddlenameGender('Павлович'); // 'male'
  4. Inflect city names

    master

    Lvovich provides functions to inflect Russian city names into different grammatical cases. This works for most compound names like 'Санкт-Петербург' or 'Ростов-на-Дону'.

    • cityIn(name: string, gender?: GenderStrT): string: Returns the city in the prepositional case (e.g., "in which city do you live?").
    • cityFrom(name: string, gender?: GenderStrT): string: Returns the city in the genitive case (e.g., "from which city did you arrive?").
    • cityTo(name: string): string: Returns the city in the accusative/directional case (e.g., "to which city are you heading?").
    import { cityIn, cityFrom, cityTo } from 'lvovich';
    
    cityIn('Санкт-Петербург'); // 'Санкт-Петербурге'
    cityFrom('Санкт-Петербург'); // 'Санкт-Петербурга'
    cityTo('Москва'); // 'Москву'
  5. Inflect Russian names, surnames, and patronymics

    master

    Inflect Russian personal names into various grammatical cases.

    Available cases (DeclentionStrT):

    • nominative (именительный)
    • genitive (родительный)
    • dative (дательный)
    • accusative (винительный) - Default if no case is specified
    • instrumental (творительный)
    • prepositional (предложный)

    Methods:

    • incline(person: LvovichPersonT, declension?: DeclentionStrT): LvovichPersonT: Inflects a full person object. Returns a new object with inflected fields and the detected gender.
    • inclineFirstname(str: string, declension?: DeclentionStrT, gender?: GenderStrT): string: Inflects a first name. If gender is omitted, it attempts auto-detection.
    • inclineLastname(str: string, declension?: DeclentionStrT, gender?: GenderStrT): string: Inflects a last name.
    • inclineMiddlename(str: string, declension?: DeclentionStrT, gender?: GenderStrT): string: Inflects a patronymic.
    import { incline, inclineFirstname, inclineLastname, inclineMiddlename } from 'lvovich';
    
    // Inflecting a person object
    incline({ first: 'Саша', last: 'Иванов' }, 'dative'); 
    // returns { first: 'Саше', last: 'Иванову', gender: 'male' }
    
    // Inflecting individual parts
    inclineFirstname('Павел', 'genitive'); // 'Павла'
    inclineLastname('Иванова', 'genitive'); // 'Ивановой'
    inclineMiddlename('Львович', 'genitive'); // 'Львовича'
    
    // Note: If gender is unknown for names like 'Женя', provide it explicitly
    inclineFirstname('Женя', 'instrumental', 'male'); // 'Женей'
  6. Reference: FioT and LvovichPersonT types

    master

    These types define the structure for passing person data to the API.

    FioT (used in getGender):

    type FioT = {
      first?: ?string,
      last?: ?string,
      middle?: ?string,
    }

    LvovichPersonT (used in incline):

    type LvovichPersonT = {
      first?: ?string,
      last?: ?string,
      middle?: ?string,
      gender?: ?GenderStrT,
    }
    // FioT structure
    {
      first: 'string',
      last: 'string',
      middle: 'string'
    }
    
    // LvovichPersonT structure
    {
      first: 'string',
      last: 'string',
      middle: 'string',
      gender: 'male' | 'female' | 'androgynous' | null
    }
  7. Inflect a full person object with `incline()`

    master

    The incline function allows you to inflect an entire person's name (first, last, and middle) in a single call. It automatically detects the gender of the person based on the provided name components and applies that gender to the inflection rules for all parts.

    Input Object (Partial<LvovichPersonT>)

    • first: First name string.
    • last: Last name string.
    • middle: Middle name (patronymic) string.

    Return Object (LvovichPersonT)

    Returns an object containing the inflected strings and the detected gender.

    Parameters

    • person: An object containing first, last, and/or middle.
    • declension: (Optional) The target case (type DeclentionStrT).
    import { incline } from 'lvovich';
    
    const person = {
      first: 'Иван',
      last: 'Иванов',
      middle: 'Иванович'
    };
    
    const inflected = incline(person, 'genitive');
    // Result: { first: 'Ивана', last: 'Иванова', middle: 'Ивановича', gender: 'male' }
  8. Determine gender of individual name components

    master

    If you only have a single part of a name, you can use the following functions to get the gender as a string ('male', 'female', or 'androgynous') or null:

    • getFirstnameGender(str: string): Returns gender for a first name.
    • getLastnameGender(str: string): Returns gender for a last name.
    • getMiddlenameGender(str: string): Returns gender for a middle name (patronymic).
    import { getFirstnameGender, getLastnameGender } from 'lvovich';
    
    const firstGender = getFirstnameGender('Анна'); // 'female'
    const lastGender = getLastnameGender('Петров'); // 'male'
  9. Inflect Russian city names using city functions

    master

    Use these functions to transform city names into specific Russian grammatical cases for use in sentences.

    Available functions:

    • cityIn(name: string, gender?: GenderStrT): string - Returns the city in the Prepositional case (e.g., "In which city do you live?").
    • cityFrom(name: string, gender?: GenderStrT): string - Returns the city in the Genitive case (e.g., "From which city did you arrive?").
    • cityTo(name: string): string - Returns the city in the Accusative/Directional case (e.g., "To which city are you heading?").
  10. Determine gender of a full name (FIO)

    master

    Use getGender(fio) to determine the gender of a person based on their Full Name (FIO) structure. The function returns a string representation of the gender ('male', 'female', or 'androgynous') or null if it cannot be determined.

    To use this, provide an object conforming to the FioT type, which can contain first, last, and middle name strings.

    import { getGender } from 'lvovich';
    
    const gender = getGender({
      first: 'Иван',
      last: 'Иванов',
      middle: 'Иванович'
    }); // returns 'male'
  11. Convert case names between strings and constants

    master

    Use these utility functions to normalize or convert grammatical case identifiers.

    • getDeclensionConst(key): Converts a string (e.g., 'genitive') or a constant to a numeric DeclentionT constant. Returns null if invalid.
    • getDeclensionStr(cnst): Converts a numeric DeclentionT constant to its string representation (e.g., 'genitive'). Returns null if invalid.
    import { getDeclensionConst, getDeclensionStr, GENITIVE } from 'lvovich/src/inclineRules';
    
    const constVal = getDeclensionConst('genitive'); // returns 2
    const strVal = getDeclensionStr(GENITIVE);      // returns 'genitive'
  12. Inflect Russian first names, last names, and middle names

    master

    Use the inclineFirstname, inclineLastname, or inclineMiddlename functions to inflect individual components of a Russian name. By default, these functions use the accusative declension. If the gender is not provided, the library attempts to infer it automatically using internal gender detection logic.

    Parameters

    • str: The name string to inflect.
    • declension: The target case (type DeclentionStrT). Defaults to 'accusative'.
    • gender: (Optional) The gender of the person (type GenderStrT). If omitted, it is inferred from the string.
    import { inclineFirstname, inclineLastname, inclineMiddlename } from 'lvovich';
    
    // Inflect a first name
    inclineFirstname('Иван', 'genitive');
    
    // Inflect a last name with explicit gender
    inclineLastname('Иванов', 'dative', 'male');