libkkc_ruby 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
+ SHA256:
3
+ metadata.gz: 78e888b0929f386e44db092edda55209946124bcc56a07d01ea23c8262016a45
4
+ data.tar.gz: 448a93c1d5accbf6543ec4441db1b322a1702a6cba9a729d3702862f70ec3d7d
5
+ SHA512:
6
+ metadata.gz: '01196a545202aae2fc5f645cc5682f8815c16a34c2108dab4a34fe6fd133d9095a8139ccc17967f4fca3facb9e0910b79e0328ee69ff4f793300a0500cb4cf48'
7
+ data.tar.gz: e9dbd91654cbfad182d519358dc3dc619736e598f89ccc19fc33bb519ab4658a7d5a882c7a849754aa6617e8c39156aed7ea27e61fd2a749dbe3ef78d3f7b343
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in libkkc_ruby.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 yamasy1549
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # libkkc_ruby
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'libkkc_ruby'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install libkkc_ruby
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ @kkc = LibKKC::Core.new
23
+
24
+ @kkc.convert('にじいろにかがやく')
25
+ #=> '虹色に輝く'
26
+
27
+ @kkc.convert_n_best('おもいのままたのしんじゃえ', n_best: 2, boundary: [0, 3, 6, 9])
28
+ #=> [
29
+ # '思いのまま楽しんじゃ絵',
30
+ # '思いのまま楽しんじゃえ'
31
+ # ]
32
+
33
+ @kkc.segmentalize_n_best('すたーとしよう', n_best: 2, boundary: [0, 4])
34
+ #=> [
35
+ # [
36
+ # #<LibKKC::Segment:0x00005610f9558380 @output="スタート" @input="すたーと">,
37
+ # #<LibKKC::Segment:0x00005610f9558330 @output="しよう" @input="しよう">
38
+ # ],
39
+ # [
40
+ # #<LibKKC::Segment:0x00005610f9558308 @output="スタート" @input="すたーと">,
41
+ # #<LibKKC::Segment:0x00005610f9558290 @output="仕様" @input="しよう">
42
+ # ]
43
+ # ]
44
+ ```
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ 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).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yamasy1549/libkkc_ruby.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "libkkc_ruby"
5
+ require "irb"
6
+
7
+ @kkc = LibKKC::Core.new
8
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
+ require "libkkc_ruby/version"
2
+ require "libkkc_ruby/core"
3
+ require "libkkc_ruby/segment"
@@ -0,0 +1,73 @@
1
+ module LibKKC
2
+ class Core
3
+ #
4
+ # 第1候補を文字列で返す
5
+ #
6
+ # convert(String)
7
+ # #=> String
8
+ #
9
+ def convert(sentence, boundary: nil)
10
+ convert_n_best(sentence, boundary: boundary).first
11
+ end
12
+
13
+ #
14
+ # 第n候補までを文字列の配列で返す
15
+ #
16
+ # convert_n_best(String, Integer)
17
+ # #=> [String, ...]
18
+ #
19
+ def convert_n_best(sentence, n_best: 1, boundary: nil)
20
+ segments = segmentalize_n_best(sentence, n_best: n_best, boundary: boundary)
21
+ segments.map do |segment|
22
+ segment.map(&:output).join
23
+ end
24
+ end
25
+
26
+ #
27
+ # 第1候補をSegmentの配列で返す
28
+ #
29
+ # segmentalize(String)
30
+ # #=> [Segment, ...]
31
+ #
32
+ def segmentalize(sentence, boundary: nil)
33
+ segmentalize_n_best(sentence, boundary: boundary).first
34
+ end
35
+
36
+ #
37
+ # 第n候補までをSegmentの配列の配列で返す
38
+ #
39
+ # segmentalize_n_best(String, Integer)
40
+ # #=> [[Segment, ...], ...]
41
+ #
42
+ def segmentalize_n_best(sentence, n_best: 1, boundary: nil)
43
+ return [] if sentence.empty?
44
+ command = "echo '#{sentence} #{n_best} #{boundary}' | kkc"
45
+
46
+ run_command(command) do |kkc_io|
47
+ kkc_stdout = kkc_io.read
48
+ extract_segments(kkc_stdout)
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def run_command(cmd, &block)
55
+ return IO.popen(cmd, &block) if block_given?
56
+
57
+ `#{cmd}`.chomp
58
+ end
59
+
60
+
61
+ def extract_segments(stdout)
62
+ # e.g.
63
+ # >> 0: <キラキラ/きらきら><ときめ/ときめ><い/い><たら/たら>
64
+ # 1: <きらきら/きらきら><ときめ/ときめ><い/い><たら/たら>
65
+
66
+ stdout.scan(/\d+:\s(?<row_segments>.*)$/).flatten.map do |row_segment|
67
+ row_segment.scan(/<(?<output>.+?)\/(?<input>.+?)>/).map do |(output, input)|
68
+ Segment.new(output, input)
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,10 @@
1
+ module LibKKC
2
+ class Segment
3
+ attr_accessor :output, :input
4
+
5
+ def initialize(output, input)
6
+ @output = output
7
+ @input = input
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module LibkkcRuby
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "libkkc_ruby/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "libkkc_ruby"
7
+ spec.version = LibkkcRuby::VERSION
8
+ spec.authors = ["yamasy1549"]
9
+ spec.email = ["sanae@yamasy.info"]
10
+
11
+ spec.summary = "a Ruby wrapper of libkkc"
12
+ spec.description = "a Ruby wrapper of libkkc"
13
+ spec.homepage = "https://github.com/yamasy1549/libkkc_ruby"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.16"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest"
25
+ spec.add_development_dependency "pry"
26
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: libkkc_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - yamasy1549
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
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: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: a Ruby wrapper of libkkc
70
+ email:
71
+ - sanae@yamasy.info
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - bin/console
82
+ - bin/setup
83
+ - lib/libkkc_ruby.rb
84
+ - lib/libkkc_ruby/core.rb
85
+ - lib/libkkc_ruby/segment.rb
86
+ - lib/libkkc_ruby/version.rb
87
+ - libkkc_ruby.gemspec
88
+ homepage: https://github.com/yamasy1549/libkkc_ruby
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.7.3
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: a Ruby wrapper of libkkc
111
+ test_files: []