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 +4 -4
- data/README.md +16 -8
- data/lib/rudachi/configurable.rb +1 -1
- data/lib/rudachi/dependencies.rb +29 -10
- data/lib/rudachi/file_parser.rb +16 -13
- data/lib/rudachi/lazy_load.rb +18 -0
- data/lib/rudachi/loader.rb +11 -9
- data/lib/rudachi/option/boolean_option.rb +2 -2
- data/lib/rudachi/option/string_option.rb +3 -3
- data/lib/rudachi/stream_parser.rb +10 -0
- data/lib/rudachi/text_parser.rb +13 -9
- data/lib/rudachi/version.rb +1 -1
- data/lib/rudachi.rb +1 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b76fd7addfd5f3bd28c0a037a2b3953ddb05a1f83af90aa154e64f54bdd4487
|
4
|
+
data.tar.gz: 57475d77337ee0e058672827ea83ccca6d2713543431deb8625ae6cce4e88abf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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)
|
2
|
+
JRuby wrapper for [Sudachi](https://github.com/WorksApplications/Sudachi).
|
3
3
|
|
4
|
-
####
|
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
|
-
####
|
10
|
+
#### File
|
11
11
|
```rb
|
12
|
-
File.open('
|
13
|
-
Rudachi::FileParser.parse('
|
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: '
|
20
|
-
File.read('
|
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
|
|
data/lib/rudachi/configurable.rb
CHANGED
data/lib/rudachi/dependencies.rb
CHANGED
@@ -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
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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
|
data/lib/rudachi/file_parser.rb
CHANGED
@@ -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
|
-
|
19
|
-
|
20
|
-
|
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
|
data/lib/rudachi/loader.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
module Rudachi
|
2
|
-
|
2
|
+
module Loader
|
3
|
+
class UnavailableError < StandardError; end;
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
class << self
|
6
|
+
def load!
|
7
|
+
raise UnavailableError, 'jruby_required' unless jruby?
|
8
|
+
require 'rudachi/dependencies'
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
+
private
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
def jruby?
|
14
|
+
RUBY_PLATFORM == 'java'
|
15
|
+
end
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
data/lib/rudachi/text_parser.rb
CHANGED
@@ -3,25 +3,29 @@ require 'rudachi/file_parser'
|
|
3
3
|
module Rudachi
|
4
4
|
class TextParser < FileParser
|
5
5
|
def parse(text)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
data/lib/rudachi/version.rb
CHANGED
data/lib/rudachi.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2022-04-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
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:
|
55
|
+
summary: A JRuby wrapper for Sudachi
|
54
56
|
test_files: []
|