Petrovich Ruby Library

repository·master·Indexed 19 days ago

https://github.com/petrovich/petrovich-ruby

A Ruby library and CLI tool for declining Russian first names, last names, and patronymics into various grammatical cases (genitive, dative, accusative, instrumental, and prepositional). It includes automatic gender detection and supports explicit gender specification (:male, :female) to ensure accurate declension. Compatible with Ruby 1.9.3 or higher and independent of Ruby on Rails.

Tokens
3.2K
Snippets
18
Records
18
Agent score
64%

What's inside petrovich-ruby

  1. Specify gender for accurate declension

    master

    Because some names and last names do not clearly indicate gender, you should provide the gender argument to ensure correct declension. If omitted, Petrovich will attempt to determine the gender automatically.

    Supported values for gender:

    • :male (Symbol)
    • 'male' (String)
    • :female (Symbol)
    • 'female' (String)
    Petrovich(
      lastname: 'Андрейчук',
      firstname: 'Саша',
      gender: :male
    ).to(:instrumental).to_s # => "Андрейчуку Саше"
  2. Decline Russian names using Petrovich

    master

    Petrovich allows you to decline Russian first names, last names, and patronymics (middlenames) into various grammatical cases. You provide the names in the nominative case, and the library returns the declined version.

    Declining the full name

    You can use specific case methods directly on the Petrovich object. To get the full string, call .to_s at the end.

    Petrovich(
      lastname: 'Салтыков-Щедрин',
      firstname: 'Михаил',
      middlename: 'Евграфович',
    ).dative.to_s # => "Салтыкову-Щедрину Михаилу Евграфовичу"

    Declining specific parts of the name

    You can use the .to(:case) method to decline a specific part and then call .middlename or .firstname to retrieve only that component.

    Petrovich(
      firstname: 'Иван',
      middlename: 'Петрович',
    ).to(:instrumental).middlename # => "Петровича"

    Supported Grammatical Cases

    MethodCase (Russian)Question (Russian)
    genitiveродительныйКого?
    dativeдательныйКому?
    accusativeвинительныйКого?
    instrumentalтворительныйКем?
    prepositionalпредложныйО ком?
  3. Install the Petrovich gem

    master

    To use Petrovich in your Ruby project, add it to your Gemfile:

    gem 'petrovich', '~> 1.0'

    Then run bundle to install. Alternatively, you can install it directly via the command line:

    gem install petrovich

    Requirements:

    • Ruby version 1.9.3 or higher.
    • The gem is independent of Ruby on Rails and can be used in any Ruby application.
  4. Use the Petrovich CLI to decline names

    master

    The petrovich CLI allows you to decline Russian names (last name, first name, and middle name) into a specific grammatical case. You can also query the gender of a name.

    To use the CLI, provide the name components via flags and specify the target case using the --case flag.

    # Example: Decline a name into the dative case
    # Assuming the CLI is in your PATH
    petrovich --lastname Ivanov --firstname Ivan --middlename Ivanovich --case dative
    
    # Example: Get only the gender of a name
    petrovich --lastname Ivanov --firstname Ivan --gender male --only-gender
  5. Detect gender using Petrovich

    master

    Petrovich provides methods to check the gender of a person based on their name components.

    • .gender: Returns the gender as a symbol (e.g., :male).
    • .male?: Returns true if the person is male.
    • .female?: Returns true if the person is female.
    • .androgynous?: Returns true if the gender cannot be determined or is ambiguous.
    Petrovich(lastname: 'Склифасовский').gender # => :male
    Petrovich(firstname: 'Саша', lastname: 'Андрейчук').androgynous? # => true
    Petrovich(lastname: 'Склифасовский').gender # => :male
  6. Use the Petrovich CLI

    master

    You can use Petrovich from the command line. Use petrovich --help for a detailed guide.

    Common flags:

    • -l: Lastname
    • -f: Firstname
    • -m: Middlename (Patronymic)
    • -g: Gender (male or female)
    • -c: Case (genitive, dative, accusative, instrumental, prepositional)
    • -n: Output only the name (no extra formatting)
    • -o: Output only the patronymic

    Example usage:

    petrovich -l Иванов -f Иван -m Иванович -g male -c accusative
  7. Initialize a Petrovich::Name object

    master

    Create a new Petrovich::Name instance by passing an options hash. The library will automatically normalize the provided name components. You can optionally provide a :gender to ensure correct declension if the name is ambiguous.

    Supported keys in the opts hash:

    • lastname: The person's last name.
    • firstname: The person's first name.
    • middlename: The person's middle name (patronymic).
    • gender: The gender of the person. Use :male, :female, or the strings 'male'/'female'. If omitted, the gem will attempt to detect the gender automatically.
    name = Petrovich::Name.new(
      lastname: 'Ivanov',
      firstname: 'Ivan',
      middlename: 'Ivanovich',
      gender: :male
    )
  8. Decline names into different grammatical cases

    master

    The Petrovich::Name class provides methods to decline a name into various Russian grammatical cases. Each method returns an Inflected object containing the declined name components.

    To decline a name, call the method corresponding to the desired case (e.g., genitive, dative, accusative, instrumental, prepositional). The library uses the internal Petrovich.CASES list to define these methods.

    You can also use the generic .to(name_case) method, where name_case must be a valid case recognized by Petrovich.assert_case!.

    name = Petrovich::Name.new(lastname: 'Ivanov', firstname: 'Ivan', gender: :male)
    
    # Using specific case methods
    dative_name = name.dative
    
    # Using the generic .to method
    accusative_name = name.to(:accusative)
  9. Validate a name object with assert_name!

    master

    Use Petrovich.assert_name!(name) to verify that an object is a valid Petrovich::Value and contains at least one name component (lastname, firstname, or middlename). It raises an ArgumentError if the validation fails.

    # name must be an instance of Petrovich::Value
    Petrovich.assert_name!(name)
  10. Configure and load inflection rules

    master

    The library uses a rule_set to manage inflection and gender rules loaded from YAML files. You can access or replace the rule set via Petrovich.rule_set. To ensure rules are loaded, call Petrovich.load_rules!. Note that load_rules! is called automatically upon library initialization.

    # Manually trigger rule loading if necessary
    Petrovich.load_rules!
    
    # Access the current rule set
    rules = Petrovich.rule_set
  11. Access name components from Petrovich::Name

    master

    You can retrieve the individual components of a name using the following methods:

    • #lastname
    • #firstname
    • #middlename

    Calling #to_s on a Petrovich::Name instance returns the full name as a single string, joining the components with a space.

    name = Petrovich::Name.new(lastname: 'Ivanov', firstname: 'Ivan')
    
    name.lastname  # => "Ivanov"
    name.firstname # => "Ivan"
    name.to_s      # => "Ivanov Ivan"