mecab-ext 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 86a39f9280e431a9e303a48b944241abd455bafa
4
+ data.tar.gz: 7e35368ddb085a3043299f68ea15ce15d331053a
5
+ SHA512:
6
+ metadata.gz: 9a9b8065e323666f937788134a60e292974e825ccebc8ceb8a94f798e31aeb215ec38210a9e45809a949c9710426429cf41053a66b9f4a4f6333c27121385e51
7
+ data.tar.gz: 52a6dae9f4b862290fc6fc2d9d05e378c9b66884b1ff9e009955ca3a7359380fa32af4a04a461da054420d88d4a5e17c3035195c304fe54261cdbc76d5fda394
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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ branches:
6
+ only:
7
+ - master
8
+ script:
9
+ - bundle exec rake spec
10
+ notifications:
11
+ email: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mecab-ext.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # mecab-ext [![Build Status](https://travis-ci.org/taiki45/mecab-ext.png?branch=master)](https://travis-ci.org/taiki45/mecab-ext)
2
+ A handy extensions for mecab-ruby.
3
+
4
+ ## Installation
5
+ At first install `Mecab` and `mecab-ruby`.
6
+ [See more detail](https://code.google.com/p/mecab/).
7
+
8
+ Then this line to your application's Gemfile:
9
+
10
+ gem 'mecab-ext'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install mecab-ext
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ # require with original ruby-mecab
24
+ require "mecab/ext"
25
+
26
+ # "Mecab" for this gem. Original takes "MeCab"
27
+ Mecab #=> for this gem
28
+ MeCab #=> for original ruby-mecab
29
+
30
+ # Parse japanese text and get extented node instance
31
+ nodes = Mecab::Ext::Parser.parse("テスト文章")
32
+ nodes.class #=> Mecab::Ext::Node
33
+
34
+ # Call Mecab::Ext::Node#each to get each MeCab::Node object
35
+ nodes.each {|node| p node }
36
+
37
+ # Extented node class has Enumerable methods
38
+ nodes.map {|node| node.surface }
39
+ nodes.select {|node| node.surface == "テスト" }
40
+
41
+ # If you need only surfaces, call Mecab::Ext::Node#each_surface
42
+ nodes.each_surface {|surface| p surface }
43
+
44
+ # mecab-ext cuts beginning of line node and end of line node for handiness
45
+ nodes.size #=> 2
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
55
+
56
+ ## License
57
+ Copyright (c) 2013 Taiki ONO
58
+
59
+ MIT License
60
+
61
+ Permission is hereby granted, free of charge, to any person obtaining
62
+ a copy of this software and associated documentation files (the
63
+ "Software"), to deal in the Software without restriction, including
64
+ without limitation the rights to use, copy, modify, merge, publish,
65
+ distribute, sublicense, and/or sell copies of the Software, and to
66
+ permit persons to whom the Software is furnished to do so, subject to
67
+ the following conditions:
68
+
69
+ The above copyright notice and this permission notice shall be
70
+ included in all copies or substantial portions of the Software.
71
+
72
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
73
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
74
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
75
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
76
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
77
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
78
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:spec) do |task|
5
+ task.rspec_opts = ["-c", "-fs"]
6
+ end
7
+
8
+ task :default => :spec
9
+ task :test => :spec
@@ -0,0 +1,33 @@
1
+ module Mecab
2
+ module Ext
3
+ class Node
4
+
5
+ delegate *Enumerable.instance_methods, :each, to: :__enum__
6
+ attr_reader :__enum__
7
+
8
+ def initialize(generator)
9
+ @generator = generator
10
+ @__enum__ = to_enum
11
+ end
12
+
13
+ def to_enum
14
+ Enumerator.new do |y|
15
+ node = @generator.call
16
+ while node
17
+ node = node.next
18
+ y << node if node.respond_to?(:surface) && !node.surface.empty?
19
+ end
20
+ self
21
+ end
22
+ end
23
+
24
+ def each_surface
25
+ each {|node| yield node.surface }
26
+ end
27
+
28
+ def each_feature
29
+ each {|node| yield node.feature }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,15 @@
1
+ module Mecab
2
+ module Ext
3
+ module Parser
4
+ class << self
5
+ def parse(str)
6
+ generator = lambda { ::MeCab::Tagger.new.parseToNode(str) }
7
+ Node.new(generator)
8
+ end
9
+
10
+ alias :parseToNode :parse
11
+ alias :parse_to_node :parse
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Mecab
2
+ module Ext
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/mecab/ext.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "active_support/core_ext/module/delegation"
2
+ require "MeCab"
3
+
4
+ require "mecab/ext/parser"
5
+ require "mecab/ext/node"
6
+
7
+ module Mecab
8
+ module Ext
9
+ end
10
+ end
data/mecab-ext.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mecab/ext/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.add_dependency "activesupport", "~> 3.2.13"
8
+
9
+ spec.name = "mecab-ext"
10
+ spec.version = Mecab::Ext::VERSION
11
+ spec.authors = ["Taiki ONO"]
12
+ spec.email = ["taiks.4559@gmail.com"]
13
+ spec.description = %q{Make mecab-ruby more handy for most of rubyist.}
14
+ spec.summary = %q{extensions for mecab-ruby}
15
+ spec.homepage = "https://github.com/taiki45/mecab-ext"
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec", "~> 2.13.0"
26
+ end
data/spec/MeCab.rb ADDED
@@ -0,0 +1,2 @@
1
+ # File to fake loading original mecab-ruby gem
2
+ module MeCab; end
data/spec/node_spec.rb ADDED
@@ -0,0 +1,115 @@
1
+ require "spec_helper"
2
+
3
+ describe Mecab::Ext::Node do
4
+
5
+ shared_context %{with MeCab::Node like mock which given "test string"}, mecab: :nodes do
6
+ let(:first_node) do
7
+ n = mock("node").tap {|o| o.stub(:surface).and_return("") }
8
+ n.stub(:feature).and_return("")
9
+ n.tap {|o| o.stub(:next).and_return(second_node) }
10
+ end
11
+ let(:second_node) do
12
+ n = mock("node").tap {|o| o.stub(:surface).and_return("test") }
13
+ n.stub(:feature).and_return("test feature")
14
+ n.tap {|o| o.stub(:next).and_return(third_node) }
15
+ end
16
+ let(:third_node) do
17
+ n = mock("node").tap {|o| o.stub(:surface).and_return("string") }
18
+ n.stub(:feature).and_return("string feature")
19
+ n.tap {|o| o.stub(:next).and_return(fourth_node) }
20
+ end
21
+ let(:fourth_node) do
22
+ n = mock("node").tap {|o| o.stub(:surface).and_return("") }
23
+ n.stub(:feature).and_return("")
24
+ n.tap {|o| o.stub(:next).and_return(nil) }
25
+ end
26
+ let(:generator) { double("generator", call: first_node) }
27
+ let(:tests) { Array.new }
28
+
29
+ subject { described_class.new(generator) }
30
+ end
31
+
32
+
33
+ describe "#each" do
34
+ context "with generator mock" do
35
+ let(:generator) do
36
+ mock("generator").tap {|o| o.should_receive(:call).at_least(:once).and_return(nil) }
37
+ end
38
+ subject { described_class.new(generator) }
39
+
40
+ it "calls given generator's :call" do
41
+ subject.each {}
42
+ end
43
+
44
+ it "returns self" do
45
+ expect(subject.each {}).to equal subject
46
+ end
47
+
48
+ it "returns enumerable" do
49
+ Enumerable.instance_methods.each do |method_name|
50
+ expect(subject.each {}).to be_respond_to method_name
51
+ end
52
+ end
53
+ end
54
+
55
+ context "with node mocks" do
56
+ let(:node) { mock("node").tap {|o| o.stub(:next).and_return(nil) } }
57
+ let(:parent_node) { mock("node").tap {|o| o.should_receive(:next).and_return(node) } }
58
+ let(:generator) { double("generator").tap {|o| o.stub(:call).and_return(parent_node) } }
59
+ subject { described_class.new(generator) }
60
+
61
+ it "calls node#next" do
62
+ subject.each {}
63
+ end
64
+
65
+ it "yields sub node" do
66
+ subject.each {|test| expect(test).to equal node }
67
+ end
68
+ end
69
+
70
+ context %(with mecab nodes which given "test string"), mecab: :nodes do
71
+ it "yields nodes" do
72
+ subject.each {|node| expect(node).to be_instance_of RSpec::Mocks::Mock }
73
+ end
74
+
75
+ it "yields 2 nodes" do
76
+ subject.each {|node| tests.push node }
77
+ expect(tests).to have(2).nodes
78
+ end
79
+
80
+ it "yields nodes which respond to :surface" do
81
+ subject.each {|node| expect(node).to be_respond_to :surface }
82
+ end
83
+ end
84
+ end
85
+
86
+
87
+ describe "#each_surface" do
88
+ context %(with mecab nodes which given "test string"), mecab: :nodes do
89
+ it "yields 2 surfaces" do
90
+ subject.each_surface {|surface| tests.push surface }
91
+ expect(tests).to have(2).surfaces
92
+ end
93
+
94
+ it "yields each surface" do
95
+ subject.each_surface {|surface| tests.push surface }
96
+ expect(tests).to eq ["test", "string"]
97
+ end
98
+ end
99
+ end
100
+
101
+ describe "#each_feature" do
102
+ context %(with mecab nodes which given "test string"), mecab: :nodes do
103
+ it "yields 2 features" do
104
+ subject.each_feature {|feature| tests.push feature }
105
+ expect(tests).to have(2).features
106
+ end
107
+
108
+ it "yields each features" do
109
+ subject.each_feature {|feature| tests.push feature }
110
+ expect(tests).to eq ["test feature", "string feature"]
111
+ end
112
+ end
113
+ end
114
+
115
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mecab::Ext::Parser do
4
+
5
+ describe ".parse" do
6
+ subject { described_class.parse("test string") }
7
+
8
+ it "should return Node instance" do
9
+ expect(subject).to be_instance_of Mecab::Ext::Node
10
+ end
11
+
12
+ it "should call Node.new" do
13
+ Mecab::Ext::Node.stub(:new).and_return("called")
14
+ expect(subject).to eq "called"
15
+ end
16
+
17
+ it "should pass proc to Node#initialize" do
18
+ Mecab::Ext::Node.should_receive(:new) do |arg|
19
+ expect(arg).to be_instance_of Proc
20
+ end
21
+ subject
22
+ end
23
+
24
+ it "should pass callable obj" do
25
+ Mecab::Ext::Node.should_receive(:new) do |arg|
26
+ expect(arg).to be_respond_to :call
27
+ end
28
+ subject
29
+ end
30
+ end
31
+
32
+ describe ".parseToNode" do
33
+ subject { described_class.method(:parseToNode) }
34
+ it { should eq described_class.method(:parse) }
35
+ end
36
+
37
+ describe ".parse_to_node" do
38
+ subject { described_class.method(:parse_to_node) }
39
+ it { should eq described_class.method(:parse_to_node) }
40
+ end
41
+
42
+ end
@@ -0,0 +1,12 @@
1
+ # Do not require original mecab-ruby gem for testing
2
+ require File.expand_path('../MeCab', __FILE__)
3
+
4
+ module MeCab
5
+ class Tagger
6
+ end
7
+ end
8
+
9
+ $LOAD_PATH.unshift File.expand_path("../", __FILE__)
10
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
11
+
12
+ require 'mecab/ext'
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mecab-ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Taiki ONO
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.13
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.13
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 2.13.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 2.13.0
69
+ description: Make mecab-ruby more handy for most of rubyist.
70
+ email:
71
+ - taiks.4559@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - lib/mecab/ext.rb
83
+ - lib/mecab/ext/node.rb
84
+ - lib/mecab/ext/parser.rb
85
+ - lib/mecab/ext/version.rb
86
+ - mecab-ext.gemspec
87
+ - spec/MeCab.rb
88
+ - spec/node_spec.rb
89
+ - spec/parser_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: https://github.com/taiki45/mecab-ext
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.0.0
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: extensions for mecab-ruby
115
+ test_files:
116
+ - spec/MeCab.rb
117
+ - spec/node_spec.rb
118
+ - spec/parser_spec.rb
119
+ - spec/spec_helper.rb
120
+ has_rdoc: