morpher_inflecter 0.0.1

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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDYyNGM0NzQyMTJjMWFkY2FkNGI5MWVhZDNlNWI2NjAyM2I0YWViMw==
5
+ data.tar.gz: !binary |-
6
+ NDliNTkyNzkyNjg1MjZmNzUxYWQzNWY4YmU2ZmEyMjVhNWU3NjA3YQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmViMTczNTc0ZTAyZGRiZTViMDQ4MjhlOGRmN2I2MmI3YjcxMTZmMDhjYmNl
10
+ MTJjM2I4NjQyZWI5MGY0ZmJiMzY0YWMzMzkxMjkyN2RjZWRkYjhkYzk3NjYy
11
+ NmI3OTI2Mjg2MWJiYjU4NWVlMDIwYTMwMzFiYzRmNDczMDA1OGY=
12
+ data.tar.gz: !binary |-
13
+ NmQzYWE1ZTI3NzljNmYyOWRjY2RhYTc0YmVhYTY3MDAyNjNhZGUzZWFjNzg1
14
+ ZGMzMzBhMTMwNDdlYWQ1YTk3NzczOTUxNDY4OWU1NzMyMjI4ZjM1MjZhNGVh
15
+ M2EyNWQwMzNjODM4YzU3NTYxYTg1YmUzNjgwZGI1ODI4OGNiYzA=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in morpher_inflecter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dmitry Penkin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = MorpherInflecter
2
+ {<img src="https://secure.travis-ci.org/dmitryp/morpher_inflecter.png" />}[http://travis-ci.org/dmitryp/morpher_inflecter]
3
+
4
+ Morpher.ru webservice client (provides Russian language pluralization)
5
+
6
+ This gem based on yandex_inflect gem by Yaroslav Markin.
7
+
8
+ == Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'morpher_inflecter'
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install morpher_inflecter
21
+
22
+ == Usage
23
+
24
+ > MorpherInflecter.inflections("рубин")
25
+ => {:singular=>["рубин", "рубина", "рубину", "рубин", "рубином", "рубине"], :plural=>["рубины", "рубинов", "рубинам", "рубины", "рубинами", "рубинах"]}
26
+ > MorpherInflecter.inflections("рубины")
27
+ => {:plural=>["рубины", "рубинов", "рубинам", "рубины", "рубинами", "рубинах"]}
28
+ > MorpherInflecter.inflections("ЭтогоСловаНетВСловаре")
29
+ => {:error => 5}
30
+
31
+ The nil will be returned if occur in connection with Web Services.
32
+
33
+ If you have account on the morpher.ru:
34
+
35
+ > MorpherInflecter.inflections("рубин", yourlogin, your_password)
36
+
37
+
38
+ == Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
11
+ # Put spec opts in a file named .rspec in root
12
+ end
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ # Rails plugin init
2
+ require 'morpher_inflecter'
3
+
@@ -0,0 +1,3 @@
1
+ module MorpherInflecter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,78 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require "morpher_inflecter/version"
4
+ require 'rubygems'
5
+ require 'open-uri'
6
+ require 'nokogiri'
7
+
8
+ module MorpherInflecter
9
+ # Падежи
10
+ CASES = %w(И Р Д В Т П)
11
+
12
+ # Класс для получения данных с веб-сервиса Морфера.
13
+ class Inflection
14
+ def get(text, login = nil, password = nil)
15
+ url = 'http://morpher.ru/WebService.asmx/GetXml?'
16
+ options = { :s => text }
17
+ if login and password
18
+ options[:username] = login
19
+ options[:password] = password
20
+ end
21
+ url = url + URI.encode(options.map{|key, val| "#{key}=#{val}"}.join('&'))
22
+ Nokogiri.XML(open(url)) rescue nil
23
+ end
24
+ end
25
+
26
+ # Кеширование успешных результатов запроса к веб-сервису
27
+ @@cache = {}
28
+
29
+ # Возвращает хэш склонений в следуюшем формате
30
+ # {:singular => [], :plural => []}
31
+ # Если слово не найдено в словаре или произошла ошибка, будет возвращен код ошибки {:error => XXX}
32
+ def self.inflections(word)
33
+ inflections = {}
34
+
35
+ lookup = cache_lookup(word)
36
+ return lookup if lookup
37
+
38
+ doc = Inflection.new.get(word)
39
+
40
+ return nil if doc.nil?
41
+
42
+ unless doc.xpath('error/code').empty?
43
+ inflections[:error] = doc.xpath('error/code').text.to_i
44
+ else
45
+ singular = true unless doc.search('множественное').empty?
46
+
47
+ CASES.each do |_case|
48
+ nodes = doc.search(_case)
49
+ if singular == true
50
+ s = _case == 'И' ? word : nodes.first.text.to_s
51
+ p = nodes.last.text.to_s
52
+ else
53
+ p = _case == 'И' ? word : nodes.last.text.to_s
54
+ end
55
+
56
+ (inflections[:singular]||=[]) << s if singular
57
+ (inflections[:plural]||=[]) << p if p
58
+ end
59
+ cache_store(word, inflections)
60
+ end
61
+
62
+ inflections
63
+ end
64
+
65
+ # Очистить кеш
66
+ def self.clear_cache
67
+ @@cache.clear
68
+ end
69
+
70
+ private
71
+ def self.cache_lookup(word)
72
+ @@cache[word.to_s]
73
+ end
74
+
75
+ def self.cache_store(word, value)
76
+ @@cache[word.to_s] = value
77
+ end
78
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'morpher_inflecter/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "morpher_inflecter"
8
+ gem.version = MorpherInflecter::VERSION
9
+ gem.authors = ["Dmitry Penkin"]
10
+ gem.homepage = "http://github.com/dmitryp/morpher_inflecter/"
11
+ gem.summary = "Morpher.ru webservice client (Russian language inflection)"
12
+ gem.description = "Morpher.ru inflections for russian proper and common nouns. Code inspired by yandex_inflect gem by Yaroslav Markin."
13
+ gem.rubyforge_project = "morpher_inflecter"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.has_rdoc = true
21
+ gem.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
22
+
23
+ gem.add_dependency "nokogiri"
24
+ gem.add_development_dependency "rspec", '2.6'
25
+ gem.add_development_dependency "rake"
26
+ end
@@ -0,0 +1,111 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper.rb'
4
+
5
+ describe MorpherInflecter do
6
+ before(:all) do
7
+ @singular_noun_answer = <<EOS
8
+ <?xml version="1.0" encoding="utf-8"?>
9
+ <xml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://morpher.ru/">
10
+ <Р>рубина</Р>
11
+ <Д>рубину</Д>
12
+ <В>рубин</В>
13
+ <Т>рубином</Т>
14
+ <П>рубине</П>
15
+ <множественное>
16
+ <И>рубины</И>
17
+ <Р>рубинов</Р>
18
+ <Д>рубинам</Д>
19
+ <В>рубины</В>
20
+ <Т>рубинами</Т>
21
+ <П>рубинах</П>
22
+ </множественное>
23
+ </xml>
24
+ EOS
25
+ @singular_noun_inflection = {:singular=>["рубин", "рубина", "рубину", "рубин", "рубином", "рубине"], :plural=>["рубины", "рубинов", "рубинам", "рубины", "рубинами", "рубинах"]}
26
+
27
+ @plural_noun_answer = <<EOS
28
+ <?xml version="1.0" encoding="utf-8"?>
29
+ <xml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://morpher.ru/">
30
+ <Р>рубинов</Р>
31
+ <Д>рубинам</Д>
32
+ <В>рубины</В>
33
+ <Т>рубинами</Т>
34
+ <П>рубинах</П>
35
+ </xml>
36
+ EOS
37
+ @plural_noun_inflection = {:plural=>["рубины", "рубинов", "рубинам", "рубины", "рубинами", "рубинах"]}
38
+
39
+ @error_answer =<<EOS
40
+ <error>
41
+ <code>5</code>
42
+ <message>Не найдено русских слов.</message>
43
+ </error>
44
+ EOS
45
+ @error_code = {:error => 5}
46
+ end
47
+
48
+ before(:each) do
49
+ @inflection = mock(:inflection)
50
+ MorpherInflecter::clear_cache
51
+ end
52
+
53
+ it "should return an hash of inflections for singular noun" do
54
+ @inflection.stub!(:get).and_return(parsed_xml(@singular_noun_answer))
55
+ MorpherInflecter::Inflection.should_receive(:new).and_return(@inflection)
56
+ MorpherInflecter.inflections("рубин").should == @singular_noun_inflection
57
+ end
58
+
59
+ it "should return an hash of inflections for plural noun" do
60
+ @inflection.stub!(:get).and_return(parsed_xml(@plural_noun_answer))
61
+ MorpherInflecter::Inflection.should_receive(:new).and_return(@inflection)
62
+ MorpherInflecter.inflections("рубины").should == @plural_noun_inflection
63
+ end
64
+
65
+ it "should return error when webservice returns error" do
66
+ @inflection.stub!(:get).and_return(parsed_xml(@error_answer))
67
+ MorpherInflecter::Inflection.should_receive(:new).and_return(@inflection)
68
+ MorpherInflecter.inflections("рубин1").should == {:error => 5}
69
+ end
70
+
71
+ it "should return nil when webservice does not return xml or connection failed" do
72
+ @inflection.stub!(:get).and_return(nil)
73
+ MorpherInflecter::Inflection.should_receive(:new).and_return(@inflection)
74
+ MorpherInflecter.inflections("рубин").should == nil
75
+ end
76
+
77
+ context 'Cache' do
78
+ it "should cache successful lookups" do
79
+ @inflection.stub!(:get).and_return(parsed_xml(@singular_noun_answer))
80
+ MorpherInflecter::Inflection.should_receive(:new).once.and_return(@inflection)
81
+
82
+ 2.times { MorpherInflecter.inflections("рубин") }
83
+ end
84
+
85
+ it "should NOT cache unseccussful lookups" do
86
+ sample = nil
87
+ @inflection.stub!(:get).and_return(sample)
88
+ MorpherInflecter::Inflection.should_receive(:new).twice.and_return(@inflection)
89
+
90
+ 2.times { MorpherInflecter.inflections("рубин") }
91
+ end
92
+
93
+ it "should NOT cache unseccussful lookups" do
94
+ sample = parsed_xml(@singular_noun_answer)
95
+ @inflection.stub!(:get).and_return(sample)
96
+ MorpherInflecter::Inflection.should_receive(:new).once.and_return(@inflection)
97
+
98
+ 2.times { MorpherInflecter.inflections("рубин") }
99
+ end
100
+
101
+ it "should allow to clear cache" do
102
+ sample = parsed_xml(@singular_noun_answer)
103
+ @inflection.stub!(:get).and_return(sample)
104
+ MorpherInflecter::Inflection.should_receive(:new).twice.and_return(@inflection)
105
+
106
+ MorpherInflecter.inflections("рубин")
107
+ MorpherInflecter.clear_cache
108
+ MorpherInflecter.inflections("рубин")
109
+ end
110
+ end
111
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format specdoc
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+
3
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
4
+ require 'morpher_inflecter'
5
+
6
+ def parsed_xml(text)
7
+ Nokogiri::XML.parse(text)
8
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: morpher_inflecter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Penkin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
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: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: '2.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: '2.6'
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
+ description: Morpher.ru inflections for russian proper and common nouns. Code inspired
56
+ by yandex_inflect gem by Yaroslav Markin.
57
+ email:
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files:
61
+ - README.rdoc
62
+ - LICENSE.txt
63
+ files:
64
+ - .gitignore
65
+ - .travis.yml
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.rdoc
69
+ - Rakefile
70
+ - init.rb
71
+ - lib/morpher_inflecter.rb
72
+ - lib/morpher_inflecter/version.rb
73
+ - morpher_inflecter.gemspec
74
+ - spec/morpher_inflecter_spec.rb
75
+ - spec/spec.opts
76
+ - spec/spec_helper.rb
77
+ homepage: http://github.com/dmitryp/morpher_inflecter/
78
+ licenses: []
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project: morpher_inflecter
96
+ rubygems_version: 2.1.4
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Morpher.ru webservice client (Russian language inflection)
100
+ test_files:
101
+ - spec/morpher_inflecter_spec.rb
102
+ - spec/spec.opts
103
+ - spec/spec_helper.rb