natto_wrap 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4a6d57d93ef344bd7361fd322055809d4202a82ac9a05158e4ad59d31dd3d50a
4
+ data.tar.gz: 271049ae9debb7a04fe2b631f2882c6d8feb85082eb20ba5c5d93c2c8ac9e4a6
5
+ SHA512:
6
+ metadata.gz: 634c676e25da1d923fe9e429cf27ad2ebb69a327800aca9c69c9b9a394ab7de2f6888efa7d742510572e53b9e04f10153aaa9f8c5d682ecfa307a81e33588843
7
+ data.tar.gz: ea50e184e0bcdaa37e93897e5e0807f4a3e73a878a936874d32643178a28979d0eaafb18e02ed9ba88aace5f0c6ed27e0a7fa52d5f35908bd195cf63ac05c8c3
@@ -0,0 +1,25 @@
1
+ name: Ruby
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+ - name: Set up Ruby
16
+ uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
17
+ with:
18
+ ruby-version: 2.6
19
+ - name: Install dependency libraries
20
+ run: |
21
+ sudo apt-get install mecab libmecab-dev mecab-ipadic-utf8
22
+ - name: Install dependencies
23
+ run: bundle install
24
+ - name: Run tests
25
+ run: bundle exec rake spec
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,2 @@
1
+ ## v0.0.1, 2 October 2020.
2
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ natto_wrap (0.0.1)
5
+ natto (~> 1.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.4.4)
11
+ ffi (1.13.1)
12
+ natto (1.2.0)
13
+ ffi (>= 1.9.0)
14
+ rake (10.5.0)
15
+ rspec (3.9.0)
16
+ rspec-core (~> 3.9.0)
17
+ rspec-expectations (~> 3.9.0)
18
+ rspec-mocks (~> 3.9.0)
19
+ rspec-core (3.9.3)
20
+ rspec-support (~> 3.9.3)
21
+ rspec-expectations (3.9.2)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.9.0)
24
+ rspec-mocks (3.9.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.9.0)
27
+ rspec-support (3.9.3)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.17)
34
+ natto_wrap!
35
+ rake (~> 10.0)
36
+ rspec (~> 3.0)
37
+
38
+ BUNDLED WITH
39
+ 1.17.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 ksugawara61
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,62 @@
1
+ # NattoWrap
2
+
3
+ NattoWrap is lightweight wrapper library for using **natto**.
4
+ It is useful for extracting nouns, converting to MeCabModels and so on from sentences.
5
+ To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ original natto library is at https://github.com/buruzaemon/natto
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'natto_wrap'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install natto_wrap
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require 'natto_wrap'
29
+
30
+ puts NattoWrap::MeCabModel.to_reading('今日は晴です')
31
+
32
+ キョウハハレデス
33
+
34
+
35
+ puts NattoWrap::MeCabModel.extract_nouns('今日は晴です').map(&:word)
36
+
37
+ 今日
38
+
39
+
40
+
41
+ models = NattoWrap::MeCabModel.convert_model('今日は晴です')
42
+ puts models.map { |model| model.to_csv.join(',') }
43
+
44
+ 今日,0,0,0,名詞,副詞可能,*,*,*,*,今日,キョウ,キョー
45
+ は,0,0,0,助詞,係助詞,*,*,*,*,は,ハ,ワ
46
+ 晴,0,0,0,名詞,一般,*,*,*,*,晴,ハレ,ハレ
47
+ です,0,0,0,助動詞,*,*,*,特殊・デス,基本形,です,デス,デス
48
+ ```
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ksugawara61/natto_wrap. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "natto_wrap"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "natto_wrap"
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'csv'
4
+ require 'natto'
5
+ require 'natto_wrap/version'
6
+ require 'natto_wrap/me_cab_model'
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NattoWrap
4
+ # NOTE: https://taku910.github.io/mecab/
5
+ class MeCabModel
6
+ attr_accessor :word, :word_class,
7
+ :word_subclass1, :word_subclass2, :word_subclass3,
8
+ :conjugation1, :conjugation2,
9
+ :prototype, :reading, :pronunciation
10
+
11
+ def initialize(word: nil, word_class: nil,
12
+ word_subclass1: nil, word_subclass2: nil, word_subclass3: nil,
13
+ conjugation1: nil, conjugation2: nil,
14
+ prototype: nil, reading: nil, pronunciation: nil)
15
+ @word = word
16
+ @word_class = get_object(word_class)
17
+ @word_subclass1 = get_object(word_subclass1)
18
+ @word_subclass2 = get_object(word_subclass2)
19
+ @word_subclass3 = get_object(word_subclass3)
20
+ @conjugation1 = get_object(conjugation1)
21
+ @conjugation2 = get_object(conjugation2)
22
+ @prototype = get_object(prototype) || word
23
+ @reading = get_object(reading) || @prototype
24
+ @pronunciation = get_object(pronunciation) || @reading
25
+ end
26
+
27
+ def noun?
28
+ @word_class == '名詞'
29
+ end
30
+
31
+ def verb?
32
+ @word_class == '動詞'
33
+ end
34
+
35
+ def adjective?
36
+ @word_class == '形容詞'
37
+ end
38
+
39
+ def adverb?
40
+ @word_class == '副詞'
41
+ end
42
+
43
+ def postpositional_particle?
44
+ @word_class == '助詞'
45
+ end
46
+
47
+ def conjunction?
48
+ @word_class == '接続詞'
49
+ end
50
+
51
+ def auxiliary_verb?
52
+ @word_class == '助動詞'
53
+ end
54
+
55
+ def determiner?
56
+ @word_class == '連体詞'
57
+ end
58
+
59
+ def interjection?
60
+ @word_class == '感動詞'
61
+ end
62
+
63
+ def symbol?
64
+ @word_class == '記号'
65
+ end
66
+
67
+ def to_csv
68
+ [
69
+ @word, 0, 0, 0, @word_class,
70
+ get_str(@word_subclass1), get_str(@word_subclass2), get_str(@word_subclass3),
71
+ get_str(@conjugation1), get_str(@conjugation2),
72
+ get_str(@prototype), get_str(@reading), get_str(@pronunciation)
73
+ ]
74
+ end
75
+
76
+ private
77
+
78
+ def get_object(str)
79
+ str != '*' ? str : nil
80
+ end
81
+
82
+ def get_str(object)
83
+ object || '*'
84
+ end
85
+
86
+ class << self
87
+ def to_reading(str, str_threshold = 20)
88
+ # NOTE: 負荷とパフォーマンスが気になるので上限を超えている場合は切り詰める
89
+ mecab_models = convert_model(str.slice(0, str_threshold).tr('ぁ-ん', 'ァ-ン'))
90
+ mecab_models.inject('') { |reading, model| reading + model.reading }
91
+ end
92
+
93
+ def extract_nouns(str)
94
+ convert_model(str).select(&:noun?)
95
+ end
96
+
97
+ def convert_model(str)
98
+ @natto ||= Natto::MeCab.new
99
+ natto_objects = @natto.parse(str)
100
+ natto_objects.delete_suffix!("EOS\n")
101
+ natto_objects.split("\n").map { |natto_object| create_from_natto_object(natto_object) }
102
+ end
103
+
104
+ def create_as_proper_noun(word, reading = nil, pronunciation = nil)
105
+ new(
106
+ word: word, word_class: '名詞',
107
+ word_subclass1: '固有名詞', word_subclass2: '一般', word_subclass3: nil,
108
+ conjugation1: nil, conjugation2: nil,
109
+ prototype: word,
110
+ reading: reading || word,
111
+ pronunciation: pronunciation || reading || word
112
+ )
113
+ end
114
+
115
+ def load_from_csv(filename)
116
+ CSV.open(filename, 'r') do |rows|
117
+ rows.map do |row|
118
+ new(
119
+ word: row[0], word_class: row[4],
120
+ word_subclass1: row[5], word_subclass2: row[6], word_subclass3: row[7],
121
+ conjugation1: row[8], conjugation2: row[9],
122
+ prototype: row[10], reading: row[11], pronunciation: row[12]
123
+ )
124
+ end
125
+ end
126
+ end
127
+
128
+ private
129
+
130
+ def create_from_natto_object(natto_object)
131
+ objects = natto_object.split("\t")
132
+ word = objects.first
133
+ word_class_objects = objects.last.split(',')
134
+ new(
135
+ word: word, word_class: word_class_objects[0],
136
+ word_subclass1: word_class_objects[1],
137
+ word_subclass2: word_class_objects[2],
138
+ word_subclass3: word_class_objects[3],
139
+ conjugation1: word_class_objects[4],
140
+ conjugation2: word_class_objects[5],
141
+ prototype: word_class_objects[6],
142
+ reading: word_class_objects[7],
143
+ pronunciation: word_class_objects[8]
144
+ )
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NattoWrap
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'natto_wrap/version'
6
+
7
+ GITHUB_URL = 'https://github.com/ksugawara61/natto_wrap'
8
+
9
+ Gem::Specification.new do |spec|
10
+ spec.name = 'natto_wrap'
11
+ spec.version = NattoWrap::VERSION
12
+ spec.authors = ['ksugawara61']
13
+ spec.email = ['listadoko@gmail.com']
14
+
15
+ spec.summary = 'A lightweight wrapper library for natto'
16
+ spec.description = spec.summary
17
+ spec.homepage = GITHUB_URL
18
+ spec.license = 'MIT'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.required_ruby_version = '>= 2.4'
30
+
31
+ spec.add_runtime_dependency 'natto', '~> 1.2'
32
+
33
+ spec.add_development_dependency 'bundler', '~> 1.17'
34
+ spec.add_development_dependency 'rake', '~> 10.0'
35
+ spec.add_development_dependency 'rspec', '~> 3.0'
36
+
37
+ spec.metadata = {
38
+ 'bug_tracker_uri' => "#{GITHUB_URL}/issues",
39
+ 'changelog_uri' => "#{GITHUB_URL}/blob/master/CHANGELOG.md",
40
+ 'documentation_uri' => "#{GITHUB_URL}/blob/master/README.md",
41
+ 'homepage_uri' => GITHUB_URL,
42
+ 'source_code_uri' => GITHUB_URL
43
+ }
44
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: natto_wrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ksugawara61
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: natto
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
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.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: A lightweight wrapper library for natto
70
+ email:
71
+ - listadoko@gmail.com
72
+ executables:
73
+ - natto_wrap
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".github/workflows/ruby.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - CHANGELOG.md
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - exe/natto_wrap
89
+ - lib/natto_wrap.rb
90
+ - lib/natto_wrap/me_cab_model.rb
91
+ - lib/natto_wrap/version.rb
92
+ - natto_wrap.gemspec
93
+ homepage: https://github.com/ksugawara61/natto_wrap
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ bug_tracker_uri: https://github.com/ksugawara61/natto_wrap/issues
98
+ changelog_uri: https://github.com/ksugawara61/natto_wrap/blob/master/CHANGELOG.md
99
+ documentation_uri: https://github.com/ksugawara61/natto_wrap/blob/master/README.md
100
+ homepage_uri: https://github.com/ksugawara61/natto_wrap
101
+ source_code_uri: https://github.com/ksugawara61/natto_wrap
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '2.4'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubygems_version: 3.0.3
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: A lightweight wrapper library for natto
121
+ test_files: []