petrovich 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/petrovich.rb +1 -1
- data/lib/petrovich/rules.rb +15 -6
- data/lib/petrovich/rules.yml +8 -0
- data/lib/tasks/evaluate.rake +1 -5
- metadata +2 -2
data/lib/petrovich.rb
CHANGED
@@ -41,7 +41,7 @@ class Petrovich
|
|
41
41
|
GENITIVE = :genitive # родительный
|
42
42
|
DATIVE = :dative # дательный
|
43
43
|
ACCUSATIVE = :accusative # винительный
|
44
|
-
|
44
|
+
INSTRUMENTAL = :instrumental # творительный
|
45
45
|
PREPOSITIONAL = :prepositional # предложный
|
46
46
|
|
47
47
|
def initialize(gender = nil)
|
data/lib/petrovich/rules.rb
CHANGED
@@ -4,6 +4,9 @@ class Petrovich
|
|
4
4
|
# Загрузка правил происходит один раз
|
5
5
|
RULES = YAML.load_file(File.dirname(__FILE__) + '/rules.yml')
|
6
6
|
|
7
|
+
class UnknownCaseException < Exception;;end
|
8
|
+
class UnknownRuleException < Exception;;end
|
9
|
+
|
7
10
|
# Набор методов для нахождения и применения правил к имени, фамилии и отчеству.
|
8
11
|
class Rules
|
9
12
|
def initialize(gender = nil)
|
@@ -21,8 +24,10 @@ class Petrovich
|
|
21
24
|
# # Винительный падеж
|
22
25
|
# lastname_accusative('Комаров') # => Комарова
|
23
26
|
#
|
24
|
-
[:lastname, :firstname, :middlename].each do |
|
25
|
-
define_method(
|
27
|
+
[:lastname, :firstname, :middlename].each do |method_name|
|
28
|
+
define_method(method_name) do |name, gcase|
|
29
|
+
find_and_apply(name, gcase, Petrovich::RULES[method_name.to_s])
|
30
|
+
end
|
26
31
|
end
|
27
32
|
|
28
33
|
protected
|
@@ -64,7 +69,11 @@ class Petrovich
|
|
64
69
|
|
65
70
|
# Найти правило и применить к имени с учетом склонения
|
66
71
|
def find_and_apply(name, gcase, rules)
|
67
|
-
|
72
|
+
rule = find_for(name, rules)
|
73
|
+
apply(name, gcase, rule)
|
74
|
+
rescue UnknownRuleException
|
75
|
+
# Если не найдено правило для имени, возвращаем неизмененное имя.
|
76
|
+
name
|
68
77
|
end
|
69
78
|
|
70
79
|
# Найти подходящее правило в исключениях или суффиксах
|
@@ -77,7 +86,7 @@ class Petrovich
|
|
77
86
|
|
78
87
|
# Не получилось, ищем в суффиксах. Если не получилось найти и в них,
|
79
88
|
# возвращаем неизмененное имя.
|
80
|
-
find(name, rules['suffixes'], false)
|
89
|
+
find(name, rules['suffixes'], false) or raise UnknownRuleException, "Cannot find rule for #{name}"
|
81
90
|
end
|
82
91
|
|
83
92
|
# Найти подходящее правило в конкретном списке правил
|
@@ -96,12 +105,12 @@ class Petrovich
|
|
96
105
|
rule['mods'][1]
|
97
106
|
when ACCUSATIVE
|
98
107
|
rule['mods'][2]
|
99
|
-
when
|
108
|
+
when INSTRUMENTAL
|
100
109
|
rule['mods'][3]
|
101
110
|
when PREPOSITIONAL
|
102
111
|
rule['mods'][4]
|
103
112
|
else
|
104
|
-
raise "Unknown grammatic case: #{gcase}"
|
113
|
+
raise UnknownCaseException, "Unknown grammatic case: #{gcase}"
|
105
114
|
end
|
106
115
|
end
|
107
116
|
|
data/lib/petrovich/rules.yml
CHANGED
@@ -46,6 +46,10 @@ lastname:
|
|
46
46
|
test: [б, в, г, д, ж, з, й, к, л, м, н, п, р, с, т, ф, х, ц, ч, ш, щ, ъ, ь]
|
47
47
|
mods: [., ., ., ., .]
|
48
48
|
|
49
|
+
- gender: androgynous
|
50
|
+
test: [гава, орота]
|
51
|
+
mods: [., ., ., ., .]
|
52
|
+
|
49
53
|
- gender: female
|
50
54
|
test: [ска, цка]
|
51
55
|
mods: [-ой, -ой, -ую, -ой, -ой]
|
@@ -229,6 +233,10 @@ firstname:
|
|
229
233
|
test: [га, ка, ха, ча, ща, жа]
|
230
234
|
mods: [-и, -е, -у, -ой, -е]
|
231
235
|
|
236
|
+
- gender: female
|
237
|
+
test: [ша]
|
238
|
+
mods: [-и, -е, -у, -ей, -е]
|
239
|
+
|
232
240
|
- gender: androgynous
|
233
241
|
test: [а]
|
234
242
|
mods: [-ы, -е, -у, -ой, -е]
|
data/lib/tasks/evaluate.rake
CHANGED
@@ -13,11 +13,7 @@ CASES = [
|
|
13
13
|
|
14
14
|
def check!(errors, correct, total, lemma, gender, gcase, expected)
|
15
15
|
inflector = Petrovich.new(gender)
|
16
|
-
inflection =
|
17
|
-
UnicodeUtils.upcase(inflector.lastname(lemma, gcase))
|
18
|
-
rescue
|
19
|
-
''
|
20
|
-
end
|
16
|
+
inflection = UnicodeUtils.upcase(inflector.lastname(lemma, gcase))
|
21
17
|
|
22
18
|
total[[gender, gcase]] += 1
|
23
19
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: petrovich
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-06-13 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: unicode_utils
|