morpher_inflect 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG +5 -0
- data/README.rdoc +9 -4
- data/lib/morpher_inflect.rb +46 -46
- data/lib/morpher_inflect/version.rb +1 -1
- data/spec/morpher_inflect_spec.rb +54 -60
- metadata +59 -59
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 41834ce079747b9bafd2bdc747abf194ee4c7a37
|
4
|
+
data.tar.gz: a0da09d00b82d1cdccef179e7955f957fd3bb7ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '0905fb58a6de271990493d048b77e3ea1c7c441a880d4174023482f9236a15e01fe1dcb96492f71f7c8d6572ca028ed298253548cb2ed6ed11bf67689722cf15'
|
7
|
+
data.tar.gz: d7aca8afdde2cce550e4b659a40647c88ecf968ab8343c095ca133e2c6d82bd584116473d59f656afa8f7b9b4cf7b8d8abc1b2ad0002278c3a2ca34e33f2054a
|
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -12,12 +12,16 @@ based on yandex_inflect gem code for Yandex.Inflect webservice client
|
|
12
12
|
от яндекс.инфлект - склоняет и неодушевленные существительные
|
13
13
|
(еду в "Ростов", а не в "Ростова").
|
14
14
|
|
15
|
-
|
15
|
+
В попытке сохранить совместимость с оригинальным API yandex_inflect, не использует
|
16
|
+
многие новые возможности morpher.ru. Если у вас есть в них потребность,
|
17
|
+
свяжитесь со мной (codesnik@gmail.com). Если вы написали другой, более свежий gem,
|
18
|
+
тоже свяжитесь со мной, пожалуйста, я впишу его в это README.
|
19
|
+
PR's are welcome!
|
16
20
|
|
17
|
-
Установка
|
21
|
+
== Установка
|
18
22
|
|
19
23
|
gem install morpher_inflect
|
20
|
-
|
24
|
+
|
21
25
|
== Использование
|
22
26
|
|
23
27
|
> MorpherInflect.inflections("рубин")
|
@@ -25,7 +29,7 @@ based on yandex_inflect gem code for Yandex.Inflect webservice client
|
|
25
29
|
> MorpherInflect.inflections("ЭтогоСловаНетВСловаре")
|
26
30
|
=> ["ЭтогоСловаНетВСловаре", "ЭтогоСловаНетВСловаре", "ЭтогоСловаНетВСловаре",
|
27
31
|
"ЭтогоСловаНетВСловаре", "ЭтогоСловаНетВСловаре", "ЭтогоСловаНетВСловаре"]
|
28
|
-
|
32
|
+
|
29
33
|
Если во время общения с веб-сервисом произошла ошибка, возвращается массив, забитый оригинальной строкой.
|
30
34
|
|
31
35
|
Успешные ответы от веб-сервиса кешируются, кеш можно очистить с помощью
|
@@ -36,4 +40,5 @@ based on yandex_inflect gem code for Yandex.Inflect webservice client
|
|
36
40
|
|
37
41
|
* оригинальный yandex_inflect: Ярослав Маркин <yaroslav@markin.net>
|
38
42
|
* адаптация под morpher: Алексей Трофименко <codesnik@gmail.com>
|
43
|
+
* адаптация под morpher API version 3: github.com/ipcross
|
39
44
|
|
data/lib/morpher_inflect.rb
CHANGED
@@ -8,67 +8,67 @@ require 'httparty'
|
|
8
8
|
module MorpherInflect
|
9
9
|
# Число доступных вариантов склонений
|
10
10
|
INFLECTIONS_COUNT = 6
|
11
|
-
|
11
|
+
|
12
12
|
# Класс для получения данных с веб-сервиса Морфера.
|
13
13
|
class Inflection
|
14
14
|
include HTTParty
|
15
|
-
base_uri '
|
15
|
+
base_uri 'ws3.morpher.ru'
|
16
16
|
|
17
17
|
# Получить склонения для имени <tt>name</tt>
|
18
18
|
def get(text)
|
19
19
|
options = {}
|
20
|
-
options[:query] = { :
|
21
|
-
|
22
|
-
|
23
|
-
return inflections["ArrayOfString"]["string"]
|
20
|
+
options[:query] = { s: text, format: 'json' }
|
21
|
+
self.class.get("/russian/declension", options)
|
24
22
|
end
|
25
23
|
end
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
inflections
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
# Морфер вернул массив склонений
|
44
|
-
inflections = [word] + get
|
45
|
-
# Кладем в кеш
|
46
|
-
cache_store(word, inflections)
|
47
|
-
when String
|
48
|
-
# Морфер вернул не массив склонений (слово не найдено в словаре),
|
49
|
-
# а только строку. Скорее всего это ошибка. Забиваем оригинальным словом
|
50
|
-
inflections.fill(word, 0..INFLECTIONS_COUNT-1)
|
24
|
+
|
25
|
+
class << self
|
26
|
+
# Кеширование успешных результатов запроса к веб-сервису
|
27
|
+
@@cache = {}
|
28
|
+
|
29
|
+
# Возвращает массив склонений (им., род., дат., вин., твор., предл.) для слова word.
|
30
|
+
#
|
31
|
+
# Если слово не найдено в словаре, в массиве вместо всех склонений будет word.
|
32
|
+
# Повторяет исторический API gem yandex-inflect.
|
33
|
+
def inflections(word)
|
34
|
+
lookup = cache_lookup(word)
|
35
|
+
return lookup if lookup
|
36
|
+
|
37
|
+
response = Inflection.new.get(word) rescue nil
|
38
|
+
if response && response.code == 200
|
39
|
+
# Морфер вернул хеш склонений
|
40
|
+
inflections = successful_result(word, response)
|
51
41
|
# Кладем в кеш
|
52
42
|
cache_store(word, inflections)
|
53
43
|
else
|
54
|
-
#
|
55
|
-
|
44
|
+
# Морфер вернул код ошибки (слово не найдено в словаре),
|
45
|
+
# Забиваем оригинальным словом
|
46
|
+
inflections = problematic_result(word)
|
47
|
+
# Не сохраняем в кэше, может в другой раз больше повезет.
|
48
|
+
end
|
49
|
+
inflections
|
56
50
|
end
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
51
|
+
|
52
|
+
def clear_cache
|
53
|
+
@@cache.clear
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def successful_result(word, response)
|
59
|
+
[word] + response.parsed_response.values_at(*%W(Р Д В Т П))
|
60
|
+
end
|
61
|
+
|
62
|
+
def problematic_result(word)
|
63
|
+
Array.new(INFLECTIONS_COUNT, word)
|
64
|
+
end
|
65
|
+
|
66
|
+
def cache_lookup(word)
|
68
67
|
@@cache[word.to_s]
|
69
68
|
end
|
70
|
-
|
71
|
-
def
|
69
|
+
|
70
|
+
def cache_store(word, value)
|
72
71
|
@@cache[word.to_s] = value
|
73
72
|
end
|
73
|
+
end
|
74
74
|
end
|
@@ -2,79 +2,73 @@
|
|
2
2
|
require File.dirname(__FILE__) + '/spec_helper.rb'
|
3
3
|
|
4
4
|
describe MorpherInflect do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
let(:parsed_response) {
|
6
|
+
{
|
7
|
+
"Р" => "рубина",
|
8
|
+
"Д" => "рубину",
|
9
|
+
"В" => "рубин",
|
10
|
+
"Т" => "рубином",
|
11
|
+
"П" => "рубине",
|
12
|
+
"множественное" => {
|
13
|
+
"И" => "рубины",
|
14
|
+
"Р" => "рубинов",
|
15
|
+
"Д" => "рубинам",
|
16
|
+
"В" => "рубины",
|
17
|
+
"Т" => "рубинами",
|
18
|
+
"П" => "рубинах"
|
19
|
+
}
|
20
|
+
}
|
21
|
+
}
|
22
|
+
let(:sample_inflection) { %w(рубин рубина рубину рубин рубином рубине) }
|
23
|
+
let(:sample_dumb_inflection) { %w(рубин рубин рубин рубин рубин рубин) }
|
24
|
+
let(:inflector) { mock('MorpherInflect::Inflection') }
|
25
|
+
let(:response_code) { 200 }
|
26
|
+
let(:response) { mock('HTTParty::Response', code: response_code, parsed_response: parsed_response) }
|
27
|
+
|
10
28
|
before(:each) do
|
11
|
-
@inflection = mock(:inflection)
|
12
29
|
MorpherInflect::clear_cache
|
30
|
+
inflector.stub!(:get).and_return(response)
|
31
|
+
MorpherInflect::Inflection.stub!(:new).and_return(inflector)
|
13
32
|
end
|
14
33
|
|
15
34
|
it "should return an array of inflections when webservice returns an array" do
|
16
|
-
|
17
|
-
MorpherInflect::Inflection.should_receive(:new).and_return(@inflection)
|
18
|
-
MorpherInflect.inflections("рубин").should == @sample_inflection
|
35
|
+
MorpherInflect.inflections("рубин").should == sample_inflection
|
19
36
|
end
|
20
37
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
38
|
+
context "when webservice returns an error string" do
|
39
|
+
let(:parsed_response) { {"code"=>5, "message"=>"Не найдено русских слов."} }
|
40
|
+
let(:response_code) { 496 }
|
41
|
+
it "should return an array filled with identical strings" do
|
42
|
+
MorpherInflect.inflections("рубин").should == sample_dumb_inflection
|
43
|
+
end
|
25
44
|
end
|
26
45
|
|
27
46
|
it "should return an array filled with identical strings of original word when webservice does not return anything meaningful or throws an exception" do
|
28
|
-
|
29
|
-
MorpherInflect
|
30
|
-
MorpherInflect.inflections("рубин").should == %w(рубин рубин рубин рубин рубин рубин)
|
47
|
+
inflector.stub!(:get).and_raise(Errno::ECONNREFUSED)
|
48
|
+
MorpherInflect.inflections("рубин").should == sample_dumb_inflection
|
31
49
|
end
|
32
|
-
end
|
33
50
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
MorpherInflect::clear_cache
|
38
|
-
end
|
51
|
+
context "with caching" do
|
52
|
+
it "should cache successful lookups" do
|
53
|
+
MorpherInflect::Inflection.should_receive(:new).once.and_return(inflector)
|
39
54
|
|
40
|
-
|
41
|
-
|
42
|
-
@inflection.stub!(:get).and_return(sample)
|
43
|
-
MorpherInflect::Inflection.should_receive(:new).once.and_return(@inflection)
|
44
|
-
|
45
|
-
2.times { MorpherInflect.inflections("рубин") }
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should NOT cache unseccussful lookups" do
|
49
|
-
sample = nil
|
50
|
-
@inflection.stub!(:get).and_return(sample)
|
51
|
-
MorpherInflect::Inflection.should_receive(:new).twice.and_return(@inflection)
|
52
|
-
|
53
|
-
2.times { MorpherInflect.inflections("рубин") }
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should allow to clear cache" do
|
57
|
-
sample = "рубин"
|
58
|
-
@inflection.stub!(:get).and_return(sample)
|
59
|
-
MorpherInflect::Inflection.should_receive(:new).twice.and_return(@inflection)
|
60
|
-
|
61
|
-
MorpherInflect.inflections("рубин")
|
62
|
-
MorpherInflect.clear_cache
|
63
|
-
MorpherInflect.inflections("рубин")
|
64
|
-
end
|
65
|
-
end
|
55
|
+
2.times { MorpherInflect.inflections("рубин") }
|
56
|
+
end
|
66
57
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
it "should get inflections for a word" do
|
76
|
-
MorpherInflect::Inflection.should_receive(:get).and_return(@sample_answer)
|
77
|
-
MorpherInflect::Inflection.new.get("рубин").should == @sample_inflection
|
78
|
-
end
|
58
|
+
context "on unseccussful lookups" do
|
59
|
+
let(:response_code) { 496 }
|
60
|
+
it "should NOT cache result" do
|
61
|
+
MorpherInflect::Inflection.should_receive(:new).twice.and_return(inflector)
|
62
|
+
2.times { MorpherInflect.inflections("рубин") }
|
63
|
+
end
|
64
|
+
end
|
79
65
|
|
66
|
+
it "should allow to clear cache" do
|
67
|
+
MorpherInflect::Inflection.should_receive(:new).twice.and_return(inflector)
|
68
|
+
|
69
|
+
MorpherInflect.inflections("рубин")
|
70
|
+
MorpherInflect.clear_cache
|
71
|
+
MorpherInflect.inflections("рубин")
|
72
|
+
end
|
73
|
+
end
|
80
74
|
end
|
metadata
CHANGED
@@ -1,66 +1,71 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: morpher_inflect
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 0.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
6
5
|
platform: ruby
|
7
|
-
authors:
|
6
|
+
authors:
|
8
7
|
- Alexey Trofimenko
|
9
8
|
- Yaroslav Markin
|
10
9
|
autorequire:
|
11
10
|
bindir: bin
|
12
11
|
cert_chain: []
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
dependencies:
|
17
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2018-01-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
18
15
|
name: httparty
|
19
|
-
|
20
|
-
|
21
|
-
none: false
|
22
|
-
requirements:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
23
18
|
- - ">="
|
24
|
-
- !ruby/object:Gem::Version
|
25
|
-
version:
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
26
21
|
type: :runtime
|
27
|
-
version_requirements: *id001
|
28
|
-
- !ruby/object:Gem::Dependency
|
29
|
-
name: rspec
|
30
22
|
prerelease: false
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rspec
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '2.6'
|
37
35
|
type: :development
|
38
|
-
version_requirements: *id002
|
39
|
-
- !ruby/object:Gem::Dependency
|
40
|
-
name: rake
|
41
36
|
prerelease: false
|
42
|
-
|
43
|
-
|
44
|
-
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '2.6'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
45
46
|
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
48
49
|
type: :development
|
49
|
-
|
50
|
-
|
51
|
-
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: Morpher.ru inflections for russian proper and common nouns. Code based
|
57
|
+
on yandex_inflect gem by Yaroslav Markin
|
58
|
+
email:
|
52
59
|
- codesnik@gmail.com
|
53
60
|
executables: []
|
54
|
-
|
55
61
|
extensions: []
|
56
|
-
|
57
|
-
extra_rdoc_files:
|
62
|
+
extra_rdoc_files:
|
58
63
|
- README.rdoc
|
59
64
|
- LICENSE
|
60
65
|
- TODO
|
61
66
|
- CHANGELOG
|
62
|
-
files:
|
63
|
-
- .gitignore
|
67
|
+
files:
|
68
|
+
- ".gitignore"
|
64
69
|
- CHANGELOG
|
65
70
|
- Gemfile
|
66
71
|
- LICENSE
|
@@ -75,35 +80,30 @@ files:
|
|
75
80
|
- spec/morpher_inflect_spec.rb
|
76
81
|
- spec/spec.opts
|
77
82
|
- spec/spec_helper.rb
|
78
|
-
has_rdoc: true
|
79
83
|
homepage: http://github.com/codesnik/morpher_inflect/
|
80
84
|
licenses: []
|
81
|
-
|
85
|
+
metadata: {}
|
82
86
|
post_install_message:
|
83
87
|
rdoc_options: []
|
84
|
-
|
85
|
-
require_paths:
|
88
|
+
require_paths:
|
86
89
|
- lib
|
87
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
-
|
89
|
-
requirements:
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
90
92
|
- - ">="
|
91
|
-
- !ruby/object:Gem::Version
|
92
|
-
version:
|
93
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
-
|
95
|
-
requirements:
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
96
97
|
- - ">="
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
version:
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
99
100
|
requirements: []
|
100
|
-
|
101
101
|
rubyforge_project: morpher_inflect
|
102
|
-
rubygems_version:
|
102
|
+
rubygems_version: 2.5.2
|
103
103
|
signing_key:
|
104
|
-
specification_version:
|
104
|
+
specification_version: 4
|
105
105
|
summary: Morpher.ru webservice client (Russian language inflection)
|
106
|
-
test_files:
|
106
|
+
test_files:
|
107
107
|
- spec/morpher_inflect_integration_spec.rb
|
108
108
|
- spec/morpher_inflect_spec.rb
|
109
109
|
- spec/spec.opts
|