rudachi 1.1.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1cd976cea8211f4f452c2cd20a3fb286828d449aa346ea54458e98ecf47819aa
4
- data.tar.gz: 7b8b886b6dc1467d33fdd44878ce1a490c13c7aacd6a40bd80e68ddcea631801
3
+ metadata.gz: 4b76fd7addfd5f3bd28c0a037a2b3953ddb05a1f83af90aa154e64f54bdd4487
4
+ data.tar.gz: 57475d77337ee0e058672827ea83ccca6d2713543431deb8625ae6cce4e88abf
5
5
  SHA512:
6
- metadata.gz: 6c21aaea246963a585ceee7800f13013797ae16f10386c933e01d3f1709ed96a5a7bcfd0d5f0e9e0a43ba17b9be2a7d2db0ce9a82ec446673ff2cf3b48a016fa
7
- data.tar.gz: c06e0239aa4c8bae28f9a4b60d00beb66aea07a7d85760126a2e875e539a46a6716aace54f62f3f193edee31c8e8300c34bf1b2be8908b4acc2285d366bc1b7b
6
+ metadata.gz: a07a1ac681d0999c103ca0f27c12190a8fa95de9ab7a5613d7d6412709ce55b4eb4492c471a1fb10bf5d238b205e51b281b3cd5a6f54237577463a3a8563cbd2
7
+ data.tar.gz: 75c332d969191be48746969cd103716a3a2c02b7bf1222eaa11b293165635466aeddf93deaed367aba86dd2307fc3df8896eb81b255600fe87e8fa06a599b886
data/README.md CHANGED
@@ -1,23 +1,29 @@
1
1
  # Rudachi
2
- [Sudachi](https://github.com/WorksApplications/Sudachi) wrapper Gem for JRuby.
2
+ JRuby wrapper for [Sudachi](https://github.com/WorksApplications/Sudachi).
3
3
 
4
- #### For Text
4
+ #### Text
5
5
  ```rb
6
6
  Rudachi::TextParser.parse('東京都へ行く')
7
7
  => "東京都\t名詞,固有名詞,地名,一般,*,*\t東京都\nへ\t助詞,格助詞,*,*,*,*\tへ\n行く\t動詞,非自立可能,*,*,五段-カ行,終止形-一般\t行く\nEOS\n"
8
8
  ```
9
9
 
10
- #### For File
10
+ #### File
11
11
  ```rb
12
- File.open('sample.txt', 'w') { |f| f << '東京都へ行く' }
13
- Rudachi::FileParser.parse('sample.txt')
12
+ File.open('input.txt', 'w') { |f| f << '東京都へ行く' }
13
+ Rudachi::FileParser.parse('input.txt')
14
+ => "東京都\t名詞,固有名詞,地名,一般,*,*\t東京都\nへ\t助詞,格助詞,*,*,*,*\tへ\n行く\t動詞,非自立可能,*,*,五段-カ行,終止形-一般\t行く\nEOS\n"
15
+ ```
16
+
17
+ #### IO
18
+ ```rb
19
+ Rudachi::StreamParser.parse(StringIO.new('東京都へ行く'))
14
20
  => "東京都\t名詞,固有名詞,地名,一般,*,*\t東京都\nへ\t助詞,格助詞,*,*,*,*\tへ\n行く\t動詞,非自立可能,*,*,五段-カ行,終止形-一般\t行く\nEOS\n"
15
21
  ```
16
22
 
17
23
  #### With [some options](https://github.com/WorksApplications/Sudachi#options)
18
24
  ```rb
19
- Rudachi::TextParser.new(o: 'result.txt', m: 'A').parse('東京都へ行く')
20
- File.read('result.txt')
25
+ Rudachi::TextParser.new(o: 'output.txt', m: 'A').parse('東京都へ行く')
26
+ File.read('output.txt')
21
27
  => "東京\t名詞,固有名詞,地名,一般,*,*\t東京\n都\t名詞,普通名詞,一般,*,*,*\t都\nへ\t助詞,格助詞,*,*,*,*\tへ\n行く\t動詞,非自立可能,*,*,五段-カ行,終止形-一般\t行く\nEOS\n"
22
28
  ```
23
29
 
@@ -26,6 +32,8 @@ File.read('result.txt')
26
32
  - [JRuby](https://github.com/jruby/jruby) 9.1.3.0 or later
27
33
  - [Sudachi](https://github.com/WorksApplications/Sudachi)
28
34
 
35
+ For Ruby, please check [rudachi-rb](https://github.com/SongCastle/rudachi-rb).
36
+
29
37
  ## Installation
30
38
 
31
39
  1. Install JAR and dictionary of Sudachi ([Details](https://github.com/WorksApplications/Sudachi/blob/develop/docs/tutorial.md#linux-%E3%81%AE%E5%A0%B4%E5%90%88))
@@ -54,7 +62,7 @@ LEGAL LICENSE-2.0.txt system_core.dic
54
62
  gem 'rudachi'
55
63
  ```
56
64
 
57
- Then run `bundle install` .
65
+ Then run `bundle install`.
58
66
 
59
67
  3. Initialize Rudachi
60
68
 
@@ -11,7 +11,7 @@ module Rudachi
11
11
  def self.#{name}; @@#{name}; end
12
12
  def self.#{name}=(val); @@#{name} = #{klass}.new(val); end
13
13
  EOS
14
- public_send("#{name}=", default)
14
+ send("#{name}=", default)
15
15
  end
16
16
  end
17
17
  end
@@ -1,19 +1,38 @@
1
1
  require 'java'
2
+ require 'rudachi/lazy_load'
3
+ require Rudachi.jar_path
4
+
2
5
  java_import 'java.lang.System'
3
6
  java_import 'java.io.PrintStream'
4
7
  java_import 'java.io.ByteArrayInputStream'
5
8
  java_import 'java.io.ByteArrayOutputStream'
6
9
  java_import 'java.nio.charset.StandardCharsets'
7
-
8
- require Rudachi.jar_path
9
10
  java_import 'com.worksap.nlp.sudachi.SudachiCommandLine'
10
11
 
11
- module Java
12
- String = JavaLang::String
13
- System = JavaLang::System
14
- ByteArrayInputStream = JavaIo::ByteArrayInputStream
15
- ByteArrayOutputStream = JavaIo::ByteArrayOutputStream
16
- PrintStream = JavaIo::PrintStream
17
- UTF_8 = JavaNioCharset::StandardCharsets::UTF_8
18
- SudachiCommandLine = ComWorksapNlpSudachi::SudachiCommandLine
12
+ module Rudachi
13
+ module Java
14
+ String = ::Java::JavaLang::String
15
+ System = ::Java::JavaLang::System
16
+ PrintStream = ::Java::JavaIo::PrintStream
17
+ ByteArrayInputStream = ::Java::JavaIo::ByteArrayInputStream
18
+ ByteArrayOutputStream = ::Java::JavaIo::ByteArrayOutputStream
19
+ UTF_8 = ::Java::JavaNioCharset::StandardCharsets::UTF_8
20
+ SudachiCommandLine = ::Java::ComWorksapNlpSudachi::SudachiCommandLine
21
+ end
22
+
23
+ module StreamProcessor
24
+ class InvalidInclusion < StandardError; end
25
+
26
+ def self.included(base)
27
+ raise InvalidInclusion unless base.ancestors.include?(TextParser)
28
+ end
29
+
30
+ private
31
+
32
+ def input_stream(io)
33
+ io.to_inputstream
34
+ end
35
+
36
+ LazyLoad.run_load_hooks(:stream_processor, self)
37
+ end
19
38
  end
@@ -8,31 +8,34 @@ module Rudachi
8
8
  end
9
9
 
10
10
  def initialize(**opts)
11
- Rudachi.load!
12
-
13
- @output = Java::ByteArrayOutputStream.new
14
- @opts = Option.new(opts)
11
+ Rudachi::Loader.load!
12
+ @opts = Option.new(**opts)
15
13
  end
16
14
 
17
15
  def parse(path)
18
- take_stdout do
19
- Java::SudachiCommandLine.main(
20
- Option.cmds(@opts).push(Java::String.new(path))
21
- )
16
+ output_stream do |output|
17
+ take_stdout(output) do
18
+ Java::SudachiCommandLine.main(
19
+ Option.cmds(@opts).push(path)
20
+ )
21
+ end
22
22
  end
23
- @output.toString
24
23
  end
25
24
 
26
25
  private
27
26
 
28
- def take_stdout
27
+ def take_stdout(output)
29
28
  stdout = Java::System.out
30
- stream = Java::PrintStream.new(@output)
31
- Java::System.setOut(stream)
32
29
 
30
+ Java::System.setOut(output)
33
31
  yield
34
-
35
32
  Java::System.setOut(stdout)
36
33
  end
34
+
35
+ def output_stream
36
+ Java::ByteArrayOutputStream.new.tap do |output|
37
+ yield Java::PrintStream.new(output)
38
+ end.toString
39
+ end
37
40
  end
38
41
  end
@@ -0,0 +1,18 @@
1
+ module Rudachi
2
+ module LazyLoad
3
+ @@hooks = {}
4
+
5
+ class << self
6
+ def on_load(name, &block)
7
+ @@hooks[name] ||= []
8
+ @@hooks[name] << block
9
+ end
10
+
11
+ def run_load_hooks(name, mod)
12
+ @@hooks[name]&.each do |hook|
13
+ hook.call(mod)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,16 +1,18 @@
1
1
  module Rudachi
2
- class UnavailableError < StandardError; end;
2
+ module Loader
3
+ class UnavailableError < StandardError; end;
3
4
 
4
- class << self
5
- def load!
6
- raise UnavailableError, 'jruby_required' unless jruby?
7
- require 'rudachi/dependencies'
8
- end
5
+ class << self
6
+ def load!
7
+ raise UnavailableError, 'jruby_required' unless jruby?
8
+ require 'rudachi/dependencies'
9
+ end
9
10
 
10
- private
11
+ private
11
12
 
12
- def jruby?
13
- RUBY_PLATFORM == 'java'
13
+ def jruby?
14
+ RUBY_PLATFORM == 'java'
15
+ end
14
16
  end
15
17
  end
16
18
  end
@@ -7,8 +7,8 @@ module Rudachi
7
7
  end
8
8
 
9
9
  def __getobj__; @value; end
10
- def enable?; @value; end
11
- def with_arg?; false; end
10
+ def enable? ; @value; end
11
+ def with_arg? ; false ; end
12
12
  end
13
13
  end
14
14
  end
@@ -6,9 +6,9 @@ module Rudachi
6
6
  @value = str
7
7
  end
8
8
 
9
- def __getobj__; @value; end
10
- def enable?; !!@value; end
11
- def with_arg?; true; end
9
+ def __getobj__; @value ; end
10
+ def enable? ; !!@value; end
11
+ def with_arg? ; true ; end
12
12
  end
13
13
  end
14
14
  end
@@ -0,0 +1,10 @@
1
+ require 'rudachi/lazy_load'
2
+ require 'rudachi/text_parser'
3
+
4
+ module Rudachi
5
+ class StreamParser < TextParser
6
+ LazyLoad.on_load(:stream_processor) do |mod|
7
+ include mod
8
+ end
9
+ end
10
+ end
@@ -3,25 +3,29 @@ require 'rudachi/file_parser'
3
3
  module Rudachi
4
4
  class TextParser < FileParser
5
5
  def parse(text)
6
- @input = Java::String.new(text)
7
- take_stdin do
8
- take_stdout do
9
- Java::SudachiCommandLine.main(Option.cmds(@opts))
6
+ output_stream do |output|
7
+ take_stdin(input_stream(text)) do
8
+ take_stdout(output) do
9
+ Java::SudachiCommandLine.main(Option.cmds(@opts))
10
+ end
10
11
  end
11
12
  end
12
- @output.toString
13
13
  end
14
14
 
15
15
  private
16
16
 
17
- def take_stdin
17
+ def take_stdin(input)
18
18
  stdin = Java::System.in
19
- stream = Java::ByteArrayInputStream.new(@input.getBytes(Java::UTF_8))
20
- Java::System.setIn(stream)
21
19
 
20
+ Java::System.setIn(input)
22
21
  yield
23
-
24
22
  Java::System.setIn(stdin)
25
23
  end
24
+
25
+ def input_stream(text)
26
+ Java::ByteArrayInputStream.new(
27
+ Java::String.new(text).getBytes(Java::UTF_8)
28
+ )
29
+ end
26
30
  end
27
31
  end
@@ -1,3 +1,3 @@
1
1
  module Rudachi
2
- VERSION = '1.1.1'
2
+ VERSION = '1.4.0'
3
3
  end
data/lib/rudachi.rb CHANGED
@@ -2,3 +2,4 @@ require 'rudachi/config'
2
2
  require 'rudachi/option/config'
3
3
  require 'rudachi/file_parser'
4
4
  require 'rudachi/text_parser'
5
+ require 'rudachi/stream_parser'
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rudachi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - SongCastle
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-10 00:00:00.000000000 Z
11
+ date: 2022-04-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Sudachi wrapper for JRuby.
13
+ description: A JRuby wrapper for Sudachi.
14
14
  email: "-"
15
15
  executables: []
16
16
  extensions: []
@@ -22,10 +22,12 @@ files:
22
22
  - lib/rudachi/configurable.rb
23
23
  - lib/rudachi/dependencies.rb
24
24
  - lib/rudachi/file_parser.rb
25
+ - lib/rudachi/lazy_load.rb
25
26
  - lib/rudachi/loader.rb
26
27
  - lib/rudachi/option/boolean_option.rb
27
28
  - lib/rudachi/option/config.rb
28
29
  - lib/rudachi/option/string_option.rb
30
+ - lib/rudachi/stream_parser.rb
29
31
  - lib/rudachi/text_parser.rb
30
32
  - lib/rudachi/version.rb
31
33
  homepage: https://github.com/SongCastle/rudachi
@@ -50,5 +52,5 @@ requirements: []
50
52
  rubygems_version: 3.0.3
51
53
  signing_key:
52
54
  specification_version: 4
53
- summary: Sudachi wrapper for JRuby
55
+ summary: A JRuby wrapper for Sudachi
54
56
  test_files: []