Class: RussianInflect
- Inherits:
-
Object
- Object
- RussianInflect
- Defined in:
- lib/russian_inflect.rb,
lib/russian_inflect/rules.rb,
lib/russian_inflect/version.rb
Defined Under Namespace
Classes: Rules, UnknownCaseException, UnknownRuleException
Constant Summary collapse
- NOMINATIVE =
именительный
:nominative
- GENITIVE =
родительный
:genitive
- DATIVE =
дательный
:dative
- ACCUSATIVE =
винительный
:accusative
- INSTRUMENTAL =
творительный
:instrumental
- PREPOSITIONAL =
предложный
:prepositional
- CASES =
[NOMINATIVE, GENITIVE, DATIVE, ACCUSATIVE, INSTRUMENTAL, PREPOSITIONAL]
- GROUPS =
[nil, :first, :second, :third]
- UnknownError =
Class.new(StandardError)
- VERSION =
"1.0.1"
Instance Attribute Summary collapse
-
#case_group ⇒ Object
Returns the value of attribute case_group.
-
#noun ⇒ Object
Returns the value of attribute noun.
-
#source ⇒ Object
Returns the value of attribute source.
-
#words ⇒ Object
Returns the value of attribute words.
Class Method Summary collapse
- .detect_case_group(noun) ⇒ Object
- .detect_type(word) ⇒ Object
- .inflect(text, gcase, force_downcase: false) ⇒ Object
Instance Method Summary collapse
-
#initialize(source, options = nil) ⇒ RussianInflect
constructor
A new instance of RussianInflect.
- #preposition?(word) ⇒ Boolean
- #to_case(gcase, force_downcase: false) ⇒ Object
Constructor Details
#initialize(source, options = nil) ⇒ RussianInflect
Returns a new instance of RussianInflect.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/russian_inflect.rb', line 19 def initialize(source, = nil) ||= {} @source = source @words = source.split @noun = case [:noun] when String then [:noun] when Fixnum then @words[[:noun]] else @words.detect { |w| self.class.detect_type(w) == :noun } end group = .fetch(:group) { self.class.detect_case_group(@noun) } @case_group = GROUPS[group] end |
Instance Attribute Details
#case_group ⇒ Object
Returns the value of attribute case_group.
17 18 19 |
# File 'lib/russian_inflect.rb', line 17 def case_group @case_group end |
#noun ⇒ Object
Returns the value of attribute noun.
17 18 19 |
# File 'lib/russian_inflect.rb', line 17 def noun @noun end |
#source ⇒ Object
Returns the value of attribute source.
17 18 19 |
# File 'lib/russian_inflect.rb', line 17 def source @source end |
#words ⇒ Object
Returns the value of attribute words.
17 18 19 |
# File 'lib/russian_inflect.rb', line 17 def words @words end |
Class Method Details
.detect_case_group(noun) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/russian_inflect.rb', line 67 def self.detect_case_group(noun) case noun when /(а|я|и)$/i then 1 when /(о|е|ы)$/i then 2 when /(мя|ь)$/i then 3 else 2 end end |
.detect_type(word) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/russian_inflect.rb', line 76 def self.detect_type(word) case word when /(ая|яя|ые|ие|ый|ой|[^р]ий|ое|ее)$/i then :adjective else :noun end end |
.inflect(text, gcase, force_downcase: false) ⇒ Object
63 64 65 |
# File 'lib/russian_inflect.rb', line 63 def self.inflect(text, gcase, force_downcase: false) new(text).to_case(gcase, force_downcase: force_downcase) end |
Instance Method Details
#preposition?(word) ⇒ Boolean
59 60 61 |
# File 'lib/russian_inflect.rb', line 59 def preposition?(word) Rules.prepositions.include?(word) || GROUPS[self.class.detect_case_group(word)] != @case_group end |
#to_case(gcase, force_downcase: false) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/russian_inflect.rb', line 34 def to_case(gcase, force_downcase: false) after_prepositions = false inflected_words = words.map do |word| unless after_prepositions downcased = UnicodeUtils.downcase(word) if preposition?(downcased) after_prepositions = true else result = Rules[case_group].inflect(downcased, gcase) word = if force_downcase result else len = downcased.each_char.take_while.with_index { |x, n| x == result[n] }.size word[0, len] << result[len..-1] end end end word end inflected_words.join(' ') end |