yaroslav-yandex_inflect 0.0.2

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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Yaroslav Markin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ = YandexInflect
2
+
3
+ Yandex.Inflect webservice client (provides Russian language pluralization)
4
+
5
+ Клиент сервиса Яндекс.Склонятор (склонение слов на русском языке)
6
+
7
+ * http://nano.yandex.ru/project/inflect/
8
+ * http://nano.yandex.ru/post/27/
9
+
10
+ == Установка
11
+
12
+ Установка в качестве gem с GitHub:
13
+
14
+ gem sources -a http://gems.github.com
15
+ gem install yaroslav-yandex_inflect
16
+
17
+ == Использование
18
+
19
+ > YandexInflect.inflections("рубин")
20
+ => ["рубин", "рубина", "рубину", "рубин", "рубином", "рубине"]
21
+ > YandexInflect.inflections("ЭтогоСловаНетВСловаре")
22
+ => ["ЭтогоСловаНетВСловаре", "ЭтогоСловаНетВСловаре", "ЭтогоСловаНетВСловаре",
23
+ "ЭтогоСловаНетВСловаре", "ЭтогоСловаНетВСловаре", "ЭтогоСловаНетВСловаре"]
24
+
25
+ Если во время общения с веб-сервисом произошла ошибка, возвращается массив, забитый оригинальной строкой.
26
+
27
+ Успешные ответы от веб-сервиса кешируются, кеш можно очистить с помощью
28
+
29
+ > YandexInflect.clear_cache
30
+
31
+ == Автор
32
+
33
+ * Ярослав Маркин <yaroslav@markin.net>
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'spec/rake/spectask'
4
+ require 'rubygems/specification'
5
+ require 'date'
6
+
7
+ GEM = "yandex_inflect"
8
+ GEM_VERSION = "0.0.2"
9
+ AUTHOR = "Yaroslav Markin"
10
+ EMAIL = "yaroslav@markin.net"
11
+ HOMEPAGE = "http://github.com/yaroslav/yandex_inflect/"
12
+ SUMMARY = "Yandex.Inflect webservice client (Russian language inflection)"
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = GEM
16
+ s.version = GEM_VERSION
17
+ s.platform = Gem::Platform::RUBY
18
+ s.has_rdoc = true
19
+ s.extra_rdoc_files = ["README.rdoc", "LICENSE", 'TODO']
20
+ s.summary = SUMMARY
21
+ s.description = s.summary
22
+ s.author = AUTHOR
23
+ s.email = EMAIL
24
+ s.homepage = HOMEPAGE
25
+
26
+ # Uncomment this to add a dependency
27
+ s.add_dependency "httparty"
28
+
29
+ s.require_path = 'lib'
30
+ s.autorequire = GEM
31
+ s.files = %w(LICENSE README.rdoc Rakefile TODO init.rb) + Dir.glob("{lib,spec}/**/*")
32
+ end
33
+
34
+ Rake::GemPackageTask.new(spec) do |pkg|
35
+ pkg.gem_spec = spec
36
+ end
37
+
38
+ task :default => :spec
39
+ desc "Run specs"
40
+ Spec::Rake::SpecTask.new do |t|
41
+ t.spec_files = FileList['spec/**/*_spec.rb']
42
+ t.spec_opts = %w(-fs --color)
43
+ end
44
+
45
+ desc "install the gem locally"
46
+ task :install => [:package] do
47
+ sh %{sudo gem install pkg/#{GEM}-#{GEM_VERSION}}
48
+ end
49
+
50
+ desc "create a gemspec file"
51
+ task :make_spec do
52
+ File.open("#{GEM}.gemspec", "w") do |file|
53
+ file.puts spec.to_ruby
54
+ end
55
+ end
data/TODO ADDED
File without changes
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ # Rails plugin init
2
+ require 'yandex_inflect'
3
+
@@ -0,0 +1,75 @@
1
+ $KCODE = 'u'
2
+
3
+ $:.unshift(File.dirname(__FILE__)) unless
4
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
5
+
6
+ require 'rubygems'
7
+ require 'httparty'
8
+
9
+ module YandexInflect
10
+ # Число доступных вариантов склонений
11
+ INFLECTIONS_COUNT = 6
12
+
13
+ # Класс для получения данных с веб-сервиса Яндекса.
14
+ class Inflection
15
+ include HTTParty
16
+ base_uri 'http://export.yandex.ru/'
17
+
18
+ # Получить склонения для имени <tt>name</tt>
19
+ def get(name)
20
+ options = {}
21
+ options[:query] = { :name => name }
22
+ inflections = self.class.get("/inflect.xml", options)
23
+
24
+ return inflections["inflections"]["inflection"]
25
+ end
26
+ end
27
+
28
+ # Кеширование успешных результатов запроса к веб-сервису
29
+ @@cache = {}
30
+
31
+ # Возвращает массив склонений (размером <tt>INFLECTIONS_COUNT</tt>) для слова <tt>word</tt>.
32
+ #
33
+ # Если слово не найдено в словаре, будет возвращен массив размерностью <tt>INFLECTIONS_COUNT</tt>,
34
+ # заполненный этим словом.
35
+ def self.inflections(word)
36
+ inflections = []
37
+
38
+ # TODO ugly
39
+ lookup = cache_lookup(word)
40
+ return lookup if lookup
41
+
42
+ get = Inflection.new.get(word) rescue nil # если поднято исключение, переходим к третьему варианту и не кешируем
43
+ case get
44
+ when Array then
45
+ # Яндекс вернул массив склонений
46
+ inflections = get
47
+ # Кладем в кеш
48
+ cache_store(word, inflections)
49
+ when String then
50
+ # Яндекс не вернул массива склонений (слово не найдено в словаре),
51
+ # а только строку, то забиваем этой строкой весь массив
52
+ inflections.fill(get, 0..INFLECTIONS_COUNT-1)
53
+ # Кладем в кеш
54
+ cache_store(word, inflections)
55
+ else
56
+ # Забиваем варианты склонений оригиналом
57
+ inflections.fill(word, 0..INFLECTIONS_COUNT-1)
58
+ end
59
+
60
+ inflections
61
+ end
62
+
63
+ def self.clear_cache
64
+ @@cache.clear
65
+ end
66
+
67
+ private
68
+ def self.cache_lookup(word)
69
+ @@cache[word.to_s]
70
+ end
71
+
72
+ def self.cache_store(word, value)
73
+ @@cache[word.to_s] = value
74
+ end
75
+ end
@@ -0,0 +1,9 @@
1
+ module YandexInflect
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 2
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format specdoc
@@ -0,0 +1,10 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ gem 'rspec'
6
+ require 'spec'
7
+ end
8
+
9
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
10
+ require 'yandex_inflect'
@@ -0,0 +1,77 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe YandexInflect do
4
+ before(:all) do
5
+ @sample_inflection = ["рубин", "рубина", "рубину", "рубин", "рубином", "рубине"]
6
+ end
7
+
8
+ before(:each) do
9
+ @inflection = mock(:inflection)
10
+ YandexInflect::clear_cache
11
+ end
12
+
13
+ it "should return an array of inflections when webservice returns an array" do
14
+ @inflection.stub!(:get).and_return(@sample_inflection)
15
+ YandexInflect::Inflection.should_receive(:new).and_return(@inflection)
16
+ YandexInflect.inflections("рубин").should == @sample_inflection
17
+ end
18
+
19
+ it "should return an array filled with identical strings when webservice returns a string" do
20
+ @inflection.stub!(:get).and_return("рубин1")
21
+ YandexInflect::Inflection.should_receive(:new).and_return(@inflection)
22
+ YandexInflect.inflections("рубин").should == %w(рубин1 рубин1 рубин1 рубин1 рубин1 рубин1)
23
+ end
24
+
25
+ it "should return an array filled with identical strings of original word when webservice does not return anything meaningful or throws an exception" do
26
+ @inflection.stub!(:get).and_return(nil)
27
+ YandexInflect::Inflection.should_receive(:new).and_return(@inflection)
28
+ YandexInflect.inflections("рубин").should == %w(рубин рубин рубин рубин рубин рубин)
29
+ end
30
+ end
31
+
32
+ describe YandexInflect, "with caching" do
33
+ before(:each) do
34
+ @inflection = mock(:inflection)
35
+ YandexInflect::clear_cache
36
+ end
37
+
38
+ it "should cache successful lookups" do
39
+ sample = ["рубин", "рубина", "рубину", "рубин", "рубином", "рубине"]
40
+ @inflection.stub!(:get).and_return(sample)
41
+ YandexInflect::Inflection.should_receive(:new).once.and_return(@inflection)
42
+
43
+ 2.times { YandexInflect.inflections("рубин") }
44
+ end
45
+
46
+ it "should NOT cache unseccussful lookups" do
47
+ sample = nil
48
+ @inflection.stub!(:get).and_return(sample)
49
+ YandexInflect::Inflection.should_receive(:new).twice.and_return(@inflection)
50
+
51
+ 2.times { YandexInflect.inflections("рубин") }
52
+ end
53
+
54
+ it "should allow to clear cache" do
55
+ sample = "рубин"
56
+ @inflection.stub!(:get).and_return(sample)
57
+ YandexInflect::Inflection.should_receive(:new).twice.and_return(@inflection)
58
+
59
+ YandexInflect.inflections("рубин")
60
+ YandexInflect.clear_cache
61
+ YandexInflect.inflections("рубин")
62
+ end
63
+ end
64
+
65
+ describe YandexInflect::Inflection do
66
+ before(:all) do
67
+ @sample_answer = {
68
+ "inflections"=>{"inflection"=>["рубин", "рубина", "рубину", "рубин", "рубином", "рубине"], "original"=>"рубин"}
69
+ }
70
+ @sample_inflection = ["рубин", "рубина", "рубину", "рубин", "рубином", "рубине"]
71
+ end
72
+
73
+ it "should get inflections for a word" do
74
+ YandexInflect::Inflection.should_receive(:get).and_return(@sample_answer)
75
+ YandexInflect::Inflection.new.get("рубин").should == @sample_inflection
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaroslav-yandex_inflect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Yaroslav Markin
8
+ autorequire: yandex_inflect
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-09 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description: Yandex.Inflect webservice client (Russian language inflection)
25
+ email: yaroslav@markin.net
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README.rdoc
32
+ - LICENSE
33
+ - TODO
34
+ files:
35
+ - LICENSE
36
+ - README.rdoc
37
+ - Rakefile
38
+ - TODO
39
+ - init.rb
40
+ - lib/yandex_inflect
41
+ - lib/yandex_inflect/version.rb
42
+ - lib/yandex_inflect.rb
43
+ - spec/spec.opts
44
+ - spec/spec_helper.rb
45
+ - spec/yandex_inflect_spec.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/yaroslav/yandex_inflect/
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.2.0
69
+ signing_key:
70
+ specification_version: 2
71
+ summary: Yandex.Inflect webservice client (Russian language inflection)
72
+ test_files: []
73
+