declension 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9a5828c76a77a2f659b84eb06b615322183c9384
4
+ data.tar.gz: 12c8e3099cbe2adf4aa1e5deb3c86d1c570245ca
5
+ SHA512:
6
+ metadata.gz: e26e07b26855ef73850197dac4eefc13a644038d63fcdbc114ad5c534bb20f14c52ece8b943b5e8938e5f0c1283c049100f46afd2488d027bb46b7bb010f83ee
7
+ data.tar.gz: 36321ff52fa26989c2015f39806263d249479e5472ba19029e71c51682518cfb21165f0a8efd3685a261a3802c8547fbefbad30aa80dcc85b4690a6ff117736c
@@ -0,0 +1,9 @@
1
+ module Declension
2
+ NOMINATIVE_CASE = :nominative
3
+ GENITIVE_CASE = :genitive
4
+ DATIVE_CASE = :dative
5
+ ACCUSATIVE_CASE = :accusative
6
+ INSTRUMENTAL_CASE = :instrumental
7
+ LOCATIVE_CASE = :locative
8
+ VOCATIVE_CASE = :vocative
9
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def inflect(grammar_case, options = {})
3
+ Declension::Wrappers::String.new(self).inflect(grammar_case, options)
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'declension/core_ext/string'
@@ -0,0 +1,15 @@
1
+ module Declension
2
+ module Languages
3
+ class Base
4
+ attr_accessor :word
5
+
6
+ def initialize(word)
7
+ self.word = word
8
+ end
9
+
10
+ def inflect(grammar_case, options = {})
11
+ word
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,62 @@
1
+ module Declension
2
+ module Languages
3
+ class Lv::Inflections
4
+ attr_accessor :declension, :word, :word_base
5
+
6
+ INFLECTIONS = {
7
+ 2 => {
8
+ 1 => {'b' => 'bj', 'm' => 'mj', 'p' => 'pj', 'v' => 'vj', 't' => 'š', 'd' => 'ž', 'c' => 'č', 's' => 'š', 'z' => 'ž', 'n' => 'ņ', 'l' => 'ļ'},
9
+ 2 => {'dz' => 'dž', 'sn' => 'šņ', 'zn' => 'žņ', 'sl' => 'šļ', 'zl' => 'žļ', 'ln' => 'ļņ'}
10
+ },
11
+ 5 => {
12
+ 1 => {'b' => 'bj', 'm' => 'mj', 'p' => 'pj', 'v' => 'vj', 'c' => 'č', 't' => 'š', 'd' => 'ž', 's' => 'š', 'z' => 'ž', 'n' => 'ņ', 'l' => 'ļ'},
13
+ 2 => {'sn' => 'šņ', 'zn' => 'žņ', 'dz' => 'dž'},
14
+ 3 => {'kst' => 'kp'}
15
+ },
16
+ 6 => {
17
+ 1 => {'v' => 'vj', 't' => 'š', 'd' => 'ž', 's' => 'š', 'z' => 'ž', 'n' => 'ņ', 'l' => 'ļ'},
18
+ 2 => {'sn' => 'šņ', 'st' => 'š'}
19
+ }
20
+ }
21
+
22
+ def self.inflect(word, word_base, declension)
23
+ inflection = new(word, word_base, declension)
24
+ if inflection.exceptionable?
25
+ word_base
26
+ else
27
+ inflection.inflect
28
+ end
29
+ end
30
+
31
+ def initialize(word, word_base, declension)
32
+ self.word = word
33
+ self.word_base = word_base
34
+ self.declension = declension
35
+ end
36
+
37
+ def inflect
38
+ [3, 2, 1].each do|iteration|
39
+ ending = word_base[-iteration, iteration]
40
+ INFLECTIONS.fetch(declension, {}).fetch(iteration, {}).each_pair do|key, value|
41
+ return (word_base[0..-(iteration + 1)] + value) if key == ending
42
+ end
43
+ end
44
+
45
+ word_base
46
+ end
47
+
48
+ def exceptionable?
49
+ if declension == 2
50
+ word == 'tētis' ||
51
+ word == 'viesis' ||
52
+ word[-5, 5] == 'astis' ||
53
+ word[-3, 3] == 'jis' ||
54
+ word[-3, 3] == 'ķis' ||
55
+ word[-3, 3] == 'ģis' ||
56
+ word[-3, 3] == 'ris' ||
57
+ word[-6, 6] == 'skatis'
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,209 @@
1
+ module Declension
2
+ module Languages
3
+ class Lv < Declension::Languages::Base
4
+ require 'declension/languages/lv/inflections'
5
+
6
+ GENDERS = [
7
+ GENDER_MALE = :male,
8
+ GENDER_FEMALE = :female
9
+ ]
10
+ EXCEPTIONAL_MALE_WORDS = %w(mēness akmens asmens rudens ūdens zibens suns sāls)
11
+ CASES = [NOMINATIVE_CASE, GENITIVE_CASE, DATIVE_CASE, ACCUSATIVE_CASE, INSTRUMENTAL_CASE, LOCATIVE_CASE, VOCATIVE_CASE]
12
+
13
+ attr_accessor :declension, :ending, :gender, :exception, :word_base
14
+
15
+ def analyze_word
16
+ if gender == GENDER_MALE
17
+ analyze_male_gender_word
18
+ elsif gender == GENDER_FEMALE
19
+ analyze_female_gender_word
20
+ else
21
+ raise ArgumentError, "No valid gender specified"
22
+ end
23
+
24
+ self.word_base = word[0..-(ending.length + 1)]
25
+ end
26
+
27
+ def analyze_male_gender_word
28
+ short_ending = word[-1, 1]
29
+ long_ending = word[-2, 2]
30
+
31
+ if EXCEPTIONAL_MALE_WORDS.include? word
32
+ self.exception = true
33
+ self.declension = 2
34
+ self.ending = 's'
35
+ elsif word == 'ļaudis'
36
+ self.exception = true
37
+ self.declension = 6
38
+ self.ending = 'is'
39
+ elsif long_ending == 'is'
40
+ self.declension = 2
41
+ self.ending = long_ending
42
+ elsif long_ending == 'us'
43
+ self.declension = 3
44
+ self.ending = long_ending
45
+ elsif short_ending == 's' || short_ending == 'š'
46
+ self.declension = 1
47
+ self.ending = short_ending
48
+ elsif short_ending == 'a'
49
+ self.declension = 4
50
+ self.ending = short_ending
51
+ elsif short_ending == 'e'
52
+ self.declension = 5
53
+ self.ending = short_ending
54
+ end
55
+ end
56
+
57
+ def analyze_female_gender_word
58
+ self.ending = word[-1, 1]
59
+
60
+ if ending == 'a'
61
+ self.declension = 4
62
+ elsif ending == 'e'
63
+ self.declension = 5
64
+ elsif ending == 's'
65
+ self.declension = 6
66
+ end
67
+ end
68
+
69
+ def assign_options(options)
70
+ self.gender = Array(options[:as]).map(&:to_sym).find{|option| GENDERS.include? option }
71
+ end
72
+
73
+ def inflect(grammar_case, options = {})
74
+ assign_options(options)
75
+ analyze_word
76
+
77
+ if declension == 1
78
+ first_declension(grammar_case, options = {})
79
+ elsif declension == 2
80
+ second_declension(grammar_case, options = {})
81
+ elsif declension == 3
82
+ third_declension(grammar_case, options = {})
83
+ elsif declension == 4
84
+ forth_declension(grammar_case, options = {})
85
+ elsif declension == 5
86
+ fifth_declension(grammar_case, options = {})
87
+ elsif declension == 6
88
+ sixth_declension(grammar_case, options = {})
89
+ end
90
+ end
91
+
92
+ def first_declension(grammar_case, options = {})
93
+ case grammar_case
94
+ when NOMINATIVE_CASE
95
+ word_base + ending
96
+ when GENITIVE_CASE
97
+ word_base + 'a'
98
+ when DATIVE_CASE
99
+ word_base + 'am'
100
+ when ACCUSATIVE_CASE
101
+ word_base + 'u'
102
+ when INSTRUMENTAL_CASE
103
+ 'ar ' + word_base + 'u'
104
+ when LOCATIVE_CASE
105
+ word_base + 'ā'
106
+ when VOCATIVE_CASE
107
+ word_base + (ending == 's' ? 's' : 'š') + "!"
108
+ end
109
+ end
110
+
111
+ def second_declension(grammar_case, options = {})
112
+ inflected_base = exception ? word_base : Lv::Inflections.inflect(word, word_base, declension)
113
+
114
+ case grammar_case
115
+ when NOMINATIVE_CASE
116
+ word_base + ending
117
+ when GENITIVE_CASE
118
+ inflected_base + (ending == 'is' ? 'a' : 's')
119
+ when DATIVE_CASE
120
+ word_base + 'im'
121
+ when ACCUSATIVE_CASE
122
+ word_base + 'i'
123
+ when INSTRUMENTAL_CASE
124
+ 'ar ' + word_base + 'i'
125
+ when LOCATIVE_CASE
126
+ word_base + 'ī'
127
+ when VOCATIVE_CASE
128
+ word_base + (ending == 'is' ? 'i!' : '!')
129
+ end
130
+ end
131
+
132
+ def third_declension(grammar_case, options = {})
133
+ case grammar_case
134
+ when NOMINATIVE_CASE
135
+ word_base + ending
136
+ when GENITIVE_CASE
137
+ word_base + 'us'
138
+ when DATIVE_CASE
139
+ word_base + 'um'
140
+ when ACCUSATIVE_CASE
141
+ word_base + 'u'
142
+ when INSTRUMENTAL_CASE
143
+ 'ar ' + word_base + 'u'
144
+ when LOCATIVE_CASE
145
+ word_base + 'ū'
146
+ when VOCATIVE_CASE
147
+ word_base + '(us|u)!'
148
+ end
149
+ end
150
+
151
+ def forth_declension(grammar_case, options = {})
152
+ case grammar_case
153
+ when NOMINATIVE_CASE
154
+ word_base + ending
155
+ when GENITIVE_CASE
156
+ word_base + 'as'
157
+ when DATIVE_CASE
158
+ word_base + (gender == GENDER_MALE ? 'am' : 'ai')
159
+ when ACCUSATIVE_CASE
160
+ word_base + 'u'
161
+ when INSTRUMENTAL_CASE
162
+ 'ar ' + word_base + 'u'
163
+ when LOCATIVE_CASE
164
+ word_base + 'ā'
165
+ when VOCATIVE_CASE
166
+ word_base + 'a!'
167
+ end
168
+ end
169
+
170
+ def fifth_declension(grammar_case, options = {})
171
+ case grammar_case
172
+ when NOMINATIVE_CASE
173
+ word_base + ending
174
+ when GENITIVE_CASE
175
+ word_base + 'es'
176
+ when DATIVE_CASE
177
+ word_base + (gender == GENDER_MALE ? 'em' : 'ei')
178
+ when ACCUSATIVE_CASE
179
+ word_base + 'i'
180
+ when INSTRUMENTAL_CASE
181
+ 'ar ' + word_base + 'i'
182
+ when LOCATIVE_CASE
183
+ word_base + 'ē'
184
+ when VOCATIVE_CASE
185
+ word_base + 'e!'
186
+ end
187
+ end
188
+
189
+ def sixth_declension(grammar_case, options = {})
190
+ case grammar_case
191
+ when NOMINATIVE_CASE
192
+ word_base + ending
193
+ when GENITIVE_CASE
194
+ word_base + 's'
195
+ when DATIVE_CASE
196
+ word_base + 'ij'
197
+ when ACCUSATIVE_CASE
198
+ word_base + 'i'
199
+ when INSTRUMENTAL_CASE
200
+ 'ar ' + word_base + 'i'
201
+ when LOCATIVE_CASE
202
+ word_base + 'ī'
203
+ when VOCATIVE_CASE
204
+ word_base + 's!'
205
+ end
206
+ end
207
+ end
208
+ end
209
+ end
@@ -0,0 +1,2 @@
1
+ require 'declension/languages/base'
2
+ require 'declension/languages/lv'
@@ -0,0 +1,3 @@
1
+ module Declension
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,13 @@
1
+ module Declension
2
+ class Word
3
+ attr_accessor :word
4
+
5
+ def initialize(word)
6
+ self.word = word
7
+ end
8
+
9
+ def inflect(grammar_case, options)
10
+ Declension::Languages::Lv.new(word).inflect(grammar_case.to_sym, options)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Declension
2
+ module Wrappers
3
+ class String
4
+ attr_accessor :string
5
+
6
+ def initialize(string)
7
+ @string = string
8
+ end
9
+
10
+ def inflect(grammar_case, options)
11
+ string.split(" ").map{|word| Declension::Word.new(word).inflect(grammar_case, options) }.join(" ")
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ require 'declension/wrappers/string'
data/lib/declension.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'active_support/inflector'
3
+ require 'active_support/core_ext/object'
4
+
5
+ require 'declension/core_ext'
6
+ require 'declension/cases'
7
+ require 'declension/version'
8
+ require 'declension/word'
9
+ require 'declension/wrappers'
10
+ require 'declension/languages'
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: declension
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Miks Mikelsons
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jeweler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Apply grammatical cases to words.
70
+ email: miks.mikelsons@gmail.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - lib/declension.rb
76
+ - lib/declension/cases.rb
77
+ - lib/declension/core_ext.rb
78
+ - lib/declension/core_ext/string.rb
79
+ - lib/declension/languages.rb
80
+ - lib/declension/languages/base.rb
81
+ - lib/declension/languages/lv.rb
82
+ - lib/declension/languages/lv/inflections.rb
83
+ - lib/declension/version.rb
84
+ - lib/declension/word.rb
85
+ - lib/declension/wrappers.rb
86
+ - lib/declension/wrappers/string.rb
87
+ homepage: http://github.com/miks/declension
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.6
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Apply grammatical cases to words
111
+ test_files: []