kabosu 0.1.1 → 0.6.10.dev.20260225.4c46cc6
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 +4 -4
- data/LICENSE +191 -0
- data/README.md +169 -42
- data/ext/kabosu/Cargo.toml +2 -2
- data/ext/kabosu/src/dictionary.rs +104 -0
- data/ext/kabosu/src/errors.rs +12 -0
- data/ext/kabosu/src/lib.rs +53 -451
- data/ext/kabosu/src/morpheme.rs +364 -0
- data/ext/kabosu/src/nogvl.rs +27 -0
- data/ext/kabosu/src/parsing.rs +132 -0
- data/ext/kabosu/src/splitter.rs +135 -0
- data/ext/kabosu/src/token_batch.rs +53 -0
- data/ext/kabosu/src/tokenizer.rs +245 -0
- data/lib/kabosu/morpheme_list.rb +69 -9
- data/lib/kabosu/release.rake +10 -10
- data/lib/kabosu/version.rb +1 -1
- data/lib/kabosu.rb +228 -91
- metadata +25 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 777e090671b61cde329346c6179b4fb89373d52f93f89dff9cacbf3fe31806b7
|
|
4
|
+
data.tar.gz: 17f5680cbcf45ad4cbcdac0c7c8e28827fd86ec480d999827eb2da1a192e75fd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ca0311c2bc431ffb2cfb4a144933db1f59530624aa29f87106ed8f97ed87f5bc858debe4168ab6a5af4b693627a2eb72b25060aaa2be5778f367b7830b8a9614
|
|
7
|
+
data.tar.gz: e31111ad3f7ce865d667702b977db999dc4519c8f894ef879ab9191ccf6bc62dc6c65d6233f79e380f76b697c9f23451ec248a01bbc8568e38fdd7c5b79956ae
|
data/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding any notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
Copyright 2025 davafons
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
data/README.md
CHANGED
|
@@ -1,49 +1,98 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="logo.png" alt="Kabosu" width="150">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">Kabosu</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://rubygems.org/gems/kabosu"><img src="https://img.shields.io/gem/v/kabosu" alt="Gem Version"></a>
|
|
9
|
+
<a href="https://github.com/davafons/kabosu/actions/workflows/edge.yml"><img src="https://github.com/davafons/kabosu/actions/workflows/edge.yml/badge.svg" alt="CI"></a>
|
|
10
|
+
<a href="https://github.com/davafons/kabosu/blob/main/LICENSE"><img src="https://img.shields.io/github/license/davafons/kabosu" alt="License"></a>
|
|
11
|
+
<a href="https://rubygems.org/gems/kabosu"><img src="https://img.shields.io/gem/dt/kabosu" alt="Downloads"></a>
|
|
12
|
+
</p>
|
|
2
13
|
|
|
3
14
|
Ruby bindings for [sudachi.rs](https://github.com/WorksApplications/sudachi.rs), a Rust implementation of the [Sudachi](https://github.com/WorksApplications/Sudachi) Japanese morphological analyzer.
|
|
4
15
|
|
|
5
|
-
##
|
|
16
|
+
## Usage
|
|
6
17
|
|
|
7
|
-
|
|
8
|
-
|
|
18
|
+
```ruby
|
|
19
|
+
require "kabosu"
|
|
20
|
+
|
|
21
|
+
# Explicit dictionary + tokenizer lifecycle
|
|
22
|
+
dict = Kabosu::Dictionary.new(system_dict: Kabosu::Dictionary.path)
|
|
23
|
+
tokenizer = dict.create(mode: :c)
|
|
24
|
+
|
|
25
|
+
# Tokenize Japanese text
|
|
26
|
+
morphemes = Kabosu.tokenize("東京都に住んでいる", tokenizer: tokenizer)
|
|
27
|
+
|
|
28
|
+
# Bulk accessors for quick extraction
|
|
29
|
+
morphemes.surfaces # => ["東京都", "に", "住ん", "で", "いる"]
|
|
30
|
+
morphemes.readings # => ["トウキョウト", "ニ", "スン", "デ", "イル"]
|
|
31
|
+
morphemes.dictionary_forms # => ["東京都", "に", "住む", "で", "居る"]
|
|
32
|
+
|
|
33
|
+
# Each morpheme exposes rich linguistic detail
|
|
34
|
+
morpheme = morphemes.first
|
|
35
|
+
morpheme.surface # => "東京都" - surface form (as it appears in text)
|
|
36
|
+
morpheme.part_of_speech # => ["名詞", "固有名詞", "地名", "一般"] — part-of-speech tags
|
|
37
|
+
morpheme.part_of_speech_id # => 5 - numeric POS id
|
|
38
|
+
morpheme.dictionary_form # => "東京都" - base/dictionary form
|
|
39
|
+
morpheme.normalized_form # => "東京都" - normalized form
|
|
40
|
+
morpheme.reading_form # => "トウキョウト" - phonetic reading
|
|
41
|
+
morpheme.oov? # => false - out-of-vocabulary?
|
|
42
|
+
morpheme.dictionary_id # => 0 - source dictionary id
|
|
43
|
+
morpheme.word_id # => 544373 - internal word id
|
|
44
|
+
morpheme.synonym_group_ids # => [] - synonym group ids
|
|
45
|
+
morpheme.dictionary_form_word_id # => -1 - dictionary-form word id
|
|
46
|
+
morpheme.head_word_length # => 3 - head word length in codepoints
|
|
47
|
+
morpheme.a_unit_split # => [123, 456] - split-A word ids
|
|
48
|
+
morpheme.b_unit_split # => [] - split-B word ids
|
|
49
|
+
morpheme.word_structure # => [123, 456] - word-structure ids
|
|
50
|
+
morpheme.total_cost # => 5765 - morphological analysis cost
|
|
51
|
+
morpheme.begin # => 0 - start byte offset
|
|
52
|
+
morpheme.end # => 9 - end byte offset
|
|
53
|
+
morpheme.begin_c # => 0 - start character offset
|
|
54
|
+
morpheme.end_c # => 3 - end character offset
|
|
55
|
+
morpheme.system? # => true - from system dictionary?
|
|
56
|
+
morpheme.user? # => false - from user dictionary?
|
|
57
|
+
```
|
|
9
58
|
|
|
10
59
|
## Installation
|
|
11
60
|
|
|
61
|
+
- Ruby >= 3.1
|
|
62
|
+
- Rust toolchain (for compiling the native extension)
|
|
63
|
+
|
|
12
64
|
Add to your Gemfile:
|
|
13
65
|
|
|
14
66
|
```ruby
|
|
15
67
|
gem "kabosu"
|
|
16
68
|
```
|
|
17
69
|
|
|
18
|
-
Then install and download a Sudachi dictionary:
|
|
70
|
+
Then install and download a [Sudachi dictionary](https://github.com/WorksApplications/SudachiDict):
|
|
19
71
|
|
|
20
72
|
```sh
|
|
21
73
|
bundle install
|
|
22
74
|
bundle exec rake kabosu:install[small] # or core, full
|
|
23
75
|
```
|
|
24
76
|
|
|
25
|
-
Dictionary editions (from smallest to largest): `small`, `core`, `full`.
|
|
26
|
-
|
|
27
|
-
## Usage
|
|
77
|
+
Dictionary editions (from smallest to largest): `small`, `core`, `full`. See the [SudachiDict documentation](https://github.com/WorksApplications/SudachiDict?tab=readme-ov-file#dictionary-types) for details on the differences between editions.
|
|
28
78
|
|
|
29
|
-
|
|
30
|
-
require "kabosu"
|
|
31
|
-
|
|
32
|
-
# Tokenize Japanese text (auto-discovers installed dictionary)
|
|
33
|
-
morphemes = Kabosu.tokenize("東京都に住んでいる")
|
|
79
|
+
## Dictionary management
|
|
34
80
|
|
|
35
|
-
|
|
36
|
-
morphemes.readings # => ["トウキョウト", "ニ", "スン", "デ", "イル"]
|
|
37
|
-
morphemes.dictionary_forms # => ["東京都", "に", "住む", "で", "居る"]
|
|
81
|
+
Rake tasks for managing Sudachi dictionaries:
|
|
38
82
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
83
|
+
```sh
|
|
84
|
+
rake kabosu:install[small] # Install a dictionary (VERSION=YYYYMMDD for a specific version)
|
|
85
|
+
rake kabosu:list # List installed dictionaries
|
|
86
|
+
rake kabosu:versions # Show available versions from GitHub
|
|
87
|
+
rake kabosu:path # Show path to best available dictionary
|
|
88
|
+
rake kabosu:remove[small] # Remove a dictionary (VERSION=YYYYMMDD for a specific version)
|
|
42
89
|
```
|
|
43
90
|
|
|
44
|
-
|
|
91
|
+
Dictionaries are stored in `~/.kabosu/dict/` by default. Set `KABOSU_DICT_DIR` to customize.
|
|
92
|
+
|
|
93
|
+
## Tokenization modes
|
|
45
94
|
|
|
46
|
-
Sudachi provides three split modes:
|
|
95
|
+
Sudachi provides three [split modes](https://github.com/WorksApplications/Sudachi?tab=readme-ov-file#the-modes-of-splitting):
|
|
47
96
|
|
|
48
97
|
| Mode | Description |
|
|
49
98
|
|------|-------------|
|
|
@@ -52,43 +101,121 @@ Sudachi provides three split modes:
|
|
|
52
101
|
| `C` | Named entity units (default) |
|
|
53
102
|
|
|
54
103
|
```ruby
|
|
55
|
-
Kabosu.
|
|
56
|
-
|
|
104
|
+
dict = Kabosu::Dictionary.new(system_dict: Kabosu::Dictionary.path)
|
|
105
|
+
tok_a = dict.create(mode: :a)
|
|
106
|
+
tok_c = dict.create(mode: :c)
|
|
107
|
+
tok_a.tokenize("東京都").surfaces # => ["東京", "都"]
|
|
108
|
+
tok_c.tokenize("東京都").surfaces # => ["東京都"]
|
|
57
109
|
```
|
|
58
110
|
|
|
59
|
-
|
|
111
|
+
Modes are symbols only (`:a`, `:b`, `:c` or `Kabosu::MODE_A/B/C`).
|
|
112
|
+
Invalid modes now raise `ArgumentError` (for example, `"A"`).
|
|
60
113
|
|
|
61
|
-
|
|
114
|
+
## Dictionary and Tokenizer Internal API
|
|
115
|
+
|
|
116
|
+
For more control over dictionary and tokenizer configuration, create them directly:
|
|
62
117
|
|
|
63
118
|
```ruby
|
|
64
|
-
dict = Kabosu::Dictionary.new
|
|
65
|
-
|
|
119
|
+
dict = Kabosu::Dictionary.new(
|
|
120
|
+
system_dict: "/path/to/custom/system.dic",
|
|
121
|
+
user_dicts: ["/path/to/domain.dic", "/path/to/names.dic"]
|
|
122
|
+
)
|
|
123
|
+
tokenizer = dict.create(mode: :c, fields: %i[surface pos_id reading_form])
|
|
124
|
+
|
|
66
125
|
morphemes = tokenizer.tokenize("国会議事堂前駅")
|
|
126
|
+
# MorphemeList is lazy: morphemes are hydrated on first indexed/iterated access.
|
|
127
|
+
# surfaces uses a fast path and does not force full morpheme hydration.
|
|
128
|
+
morphemes.surfaces
|
|
129
|
+
morphemes.first.part_of_speech
|
|
130
|
+
|
|
131
|
+
# Lexicon lookup (prefix matches from position 0), returns MorphemeList
|
|
132
|
+
dict.lookup("東京都").surfaces
|
|
133
|
+
|
|
134
|
+
# Morpheme split returns MorphemeList
|
|
135
|
+
m = tokenizer.tokenize("東京都").first
|
|
136
|
+
m.split(mode: :a).surfaces
|
|
137
|
+
|
|
138
|
+
# Native bulk extraction helpers (fewer Ruby<->Rust crossings)
|
|
139
|
+
tokenizer.tokenize_surfaces("東京都に住んでいる")
|
|
140
|
+
tokenizer.tokenize_readings("東京都に住んでいる")
|
|
141
|
+
tokenizer.tokenize_dictionary_forms("東京都に住んでいる")
|
|
142
|
+
tokenizer.tokenize_normalized_forms("東京都に住んでいる")
|
|
143
|
+
|
|
144
|
+
# Sentence splitting options
|
|
145
|
+
Kabosu.split_sentences("東京都に住んでいる。大阪も好きだ。", ranges: true)
|
|
146
|
+
Kabosu.split_sentences("長い文...", limit: 12, with_checker: true)
|
|
147
|
+
|
|
148
|
+
# ranges: true returns SentenceRange objects
|
|
149
|
+
ranges = Kabosu.split_sentences("東京都に住んでいる。", ranges: true)
|
|
150
|
+
ranges.first.start
|
|
151
|
+
ranges.first.end
|
|
152
|
+
ranges.first.text
|
|
67
153
|
```
|
|
68
154
|
|
|
69
|
-
|
|
155
|
+
Dictionary initialization failures raise typed errors:
|
|
156
|
+
- `Kabosu::ConfigError` for configuration issues
|
|
157
|
+
- `Kabosu::DictionaryError` for dictionary loading issues
|
|
70
158
|
|
|
71
|
-
|
|
159
|
+
Runtime failures in analysis APIs are also typed:
|
|
160
|
+
- `Kabosu::TokenizationError` for tokenization/split failures
|
|
161
|
+
- `Kabosu::SentenceSplitError` for sentence splitter failures
|
|
162
|
+
- `Kabosu::LookupError` for dictionary lookup failures
|
|
163
|
+
|
|
164
|
+
## Public API Contract
|
|
165
|
+
|
|
166
|
+
| API | Parameters | Return | Notes |
|
|
167
|
+
|---|---|---|---|
|
|
168
|
+
| `Kabosu::Dictionary.new` | `config: String?`, `system_dict: String?`, `user_dicts: Array<String>?` | `Kabosu::Dictionary` | One of `config` or `system_dict` is required |
|
|
169
|
+
| `Dictionary#create` | `mode: :a|:b|:c`, `fields: Array<String\|Symbol>?`, `debug: bool`, `projection: nil` | `Kabosu::Tokenizer` | Unknown kwargs raise `ArgumentError`; `projection` currently raises `NotImplementedError` |
|
|
170
|
+
| `Dictionary#lookup` | `text: String` | `Kabosu::MorphemeList` | Prefix lookup from byte offset 0 |
|
|
171
|
+
| `Tokenizer#tokenize` | `text: String` | `Kabosu::MorphemeList` | Lazy morpheme hydration; raises `Kabosu::TokenizationError` on native failures |
|
|
172
|
+
| `Tokenizer#tokenize_surfaces/readings/dictionary_forms/normalized_forms` | `text: String` | `Array<String>` | Raises `Kabosu::TokenizationError` on native failures |
|
|
173
|
+
| `Morpheme#split` | `mode: :a|:b|:c`, `add_single: bool` | `Kabosu::MorphemeList` | Standardized with `tokenize` return type |
|
|
174
|
+
| `Kabosu.split_sentences` | `text: String`, `limit: Integer?`, `with_checker: bool`, `ranges: bool`, `dictionary: String?` | `Array<String>` or `Array<Kabosu::SentenceRange>` | `limit` must be `>= 1` |
|
|
175
|
+
| `Kabosu.tokenize` | `text: String`, `tokenizer: Kabosu::Tokenizer` | `Kabosu::MorphemeList` | No hidden global tokenizer cache |
|
|
176
|
+
|
|
177
|
+
## Benchmarks
|
|
178
|
+
|
|
179
|
+
Kabosu ships with a benchmark suite that measures tokenization throughput and compares the Ruby bindings against raw [sudachi.rs](https://github.com/WorksApplications/sudachi.rs).
|
|
180
|
+
|
|
181
|
+
This benchmark uses [Wagahai wa Neko de Aru](https://www.aozora.gr.jp/cards/000148/card789.html) (I Am a Cat) by Natsume Soseki, sourced from [Aozora Bunko](https://www.aozora.gr.jp/) (public domain) as the source text. ~958 KB of Japanese prose, 2,256 lines as input.
|
|
182
|
+
|
|
183
|
+
### Results
|
|
184
|
+
|
|
185
|
+
Measured on an AMD Ryzen 7 5800X, `full` dictionary edition, Ruby 3.4, Rust 1.84:
|
|
186
|
+
|
|
187
|
+
| Scenario | Rust | Ruby | Ratio |
|
|
188
|
+
|---|---|---|---|
|
|
189
|
+
| split_sentences | 1.597s | 1.677s | 1.0x |
|
|
190
|
+
| tokenize (mode C) | 3.274s | 4.034s | 1.2x |
|
|
191
|
+
| tokenize (mode A) | 3.429s | 4.273s | 1.2x |
|
|
192
|
+
| tokenize (mode B) | 3.465s | 4.297s | 1.2x |
|
|
193
|
+
| **Throughput** | **2.66 MB/s** | **2.18 MB/s** | **1.2x** |
|
|
194
|
+
|
|
195
|
+
The Ruby bindings add ~20% overhead over raw Rust, primarily from FFI boundary crossings and Ruby object allocation for each morpheme.
|
|
196
|
+
|
|
197
|
+
To reproduce these results, run:
|
|
72
198
|
|
|
73
199
|
```sh
|
|
74
|
-
|
|
75
|
-
rake kabosu:list # List installed dictionaries
|
|
76
|
-
rake kabosu:versions # Show available versions from GitHub
|
|
77
|
-
rake kabosu:path # Show path to best available dictionary
|
|
78
|
-
rake kabosu:remove[small] # Remove a dictionary (VERSION=YYYYMMDD for a specific version)
|
|
200
|
+
bundle exec ruby bench/start
|
|
79
201
|
```
|
|
80
202
|
|
|
81
|
-
|
|
203
|
+
To generate flamegraph SVGs alongside the benchmark:
|
|
82
204
|
|
|
83
|
-
|
|
205
|
+
```sh
|
|
206
|
+
bundle exec ruby bench/start --profile
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
This records both the Rust and Ruby runs with `perf` and produces interactive SVGs (`bench/flamegraph-rust.svg`, `bench/flamegraph-ruby.svg`). Open them in a browser to explore.
|
|
210
|
+
|
|
211
|
+
## Contributing
|
|
84
212
|
|
|
85
213
|
```sh
|
|
86
214
|
bundle install
|
|
87
|
-
bundle exec rake compile # Build the native extension
|
|
88
|
-
bundle exec rake test # Run tests
|
|
89
|
-
bundle exec rake # Compile + test
|
|
90
|
-
```
|
|
91
215
|
|
|
92
|
-
|
|
216
|
+
bundle exec rake kabosu:install # Install Sudachi dictionary
|
|
93
217
|
|
|
94
|
-
|
|
218
|
+
bundle exec rake compile # Build the native extension
|
|
219
|
+
bundle exec rake test # Run tests
|
|
220
|
+
bench/start # Run benchmarks
|
|
221
|
+
```
|
data/ext/kabosu/Cargo.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "kabosu"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.6.10"
|
|
4
4
|
edition = "2021"
|
|
5
5
|
publish = false
|
|
6
6
|
|
|
@@ -9,4 +9,4 @@ crate-type = ["cdylib"]
|
|
|
9
9
|
|
|
10
10
|
[dependencies]
|
|
11
11
|
magnus = { version = "0.8", features = ["rb-sys"] }
|
|
12
|
-
sudachi = { git = "https://github.com/WorksApplications/sudachi.rs",
|
|
12
|
+
sudachi = { git = "https://github.com/WorksApplications/sudachi.rs", tag = "v0.6.10" }
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
use magnus::{Error, RArray, Ruby, Value};
|
|
2
|
+
use std::path::Path;
|
|
3
|
+
use std::sync::Arc;
|
|
4
|
+
use sudachi::config::{Config, ConfigBuilder};
|
|
5
|
+
use sudachi::dic::dictionary::JapaneseDictionary;
|
|
6
|
+
|
|
7
|
+
use crate::errors::sudachi_error;
|
|
8
|
+
use crate::morpheme::{rb_morpheme_from_data, MorphemeData};
|
|
9
|
+
use crate::parsing::{parse_mode, parse_subset, parse_user_dicts};
|
|
10
|
+
use crate::tokenizer::RbTokenizer;
|
|
11
|
+
|
|
12
|
+
fn build_dictionary_config(
|
|
13
|
+
config_path: Option<String>,
|
|
14
|
+
system_dict_path: Option<String>,
|
|
15
|
+
user_dicts: Vec<String>,
|
|
16
|
+
) -> Result<Config, Error> {
|
|
17
|
+
let mut builder = ConfigBuilder::from_opt_file(config_path.as_deref().map(Path::new))
|
|
18
|
+
.map_err(sudachi_error)?;
|
|
19
|
+
|
|
20
|
+
if let Some(system_dict_path) = system_dict_path {
|
|
21
|
+
builder = builder.system_dict(system_dict_path);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
for user_dict in user_dicts {
|
|
25
|
+
builder = builder.user_dict(user_dict);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
Ok(builder.build())
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
#[magnus::wrap(class = "Kabosu::Dictionary")]
|
|
32
|
+
pub(crate) struct RbDictionary {
|
|
33
|
+
inner: Arc<JapaneseDictionary>,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
impl RbDictionary {
|
|
37
|
+
pub(crate) fn new(
|
|
38
|
+
config_path: Option<String>,
|
|
39
|
+
system_dict_path: Option<String>,
|
|
40
|
+
user_dicts_value: Option<Value>,
|
|
41
|
+
) -> Result<Self, Error> {
|
|
42
|
+
let user_dicts = parse_user_dicts(user_dicts_value)?;
|
|
43
|
+
let cfg = build_dictionary_config(config_path, system_dict_path, user_dicts)?;
|
|
44
|
+
let dict = JapaneseDictionary::from_cfg(&cfg).map_err(sudachi_error)?;
|
|
45
|
+
|
|
46
|
+
Ok(Self {
|
|
47
|
+
inner: Arc::new(dict),
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
pub(crate) fn lookup(&self, text: String) -> Result<RArray, Error> {
|
|
52
|
+
let ruby = Ruby::get().unwrap();
|
|
53
|
+
let ary = ruby.ary_new();
|
|
54
|
+
let lexicon = self.inner.lexicon();
|
|
55
|
+
|
|
56
|
+
for entry in lexicon.lookup(text.as_bytes(), 0) {
|
|
57
|
+
let info = lexicon.get_word_info(entry.word_id).map_err(sudachi_error)?;
|
|
58
|
+
let surface_slice = text.get(..entry.end).ok_or_else(|| {
|
|
59
|
+
sudachi_error(format!(
|
|
60
|
+
"lookup returned invalid UTF-8 boundary: end={}",
|
|
61
|
+
entry.end
|
|
62
|
+
))
|
|
63
|
+
})?;
|
|
64
|
+
|
|
65
|
+
let data = MorphemeData {
|
|
66
|
+
surface: surface_slice.to_string(),
|
|
67
|
+
pos_id: info.pos_id(),
|
|
68
|
+
word_id_raw: entry.word_id.as_raw(),
|
|
69
|
+
is_oov: entry.word_id.is_oov(),
|
|
70
|
+
dictionary_id: if entry.word_id.is_oov() {
|
|
71
|
+
-1
|
|
72
|
+
} else {
|
|
73
|
+
entry.word_id.dic() as i32
|
|
74
|
+
},
|
|
75
|
+
is_system: entry.word_id.is_system(),
|
|
76
|
+
is_user: entry.word_id.is_user(),
|
|
77
|
+
begin: 0,
|
|
78
|
+
end: entry.end,
|
|
79
|
+
begin_c: 0,
|
|
80
|
+
end_c: surface_slice.chars().count(),
|
|
81
|
+
total_cost: 0,
|
|
82
|
+
};
|
|
83
|
+
ary.push(rb_morpheme_from_data(data, self.inner.clone(), false))?;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
Ok(ary)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
pub(crate) fn create(
|
|
90
|
+
&self,
|
|
91
|
+
mode_str: Option<String>,
|
|
92
|
+
fields: Option<Value>,
|
|
93
|
+
debug: Option<bool>,
|
|
94
|
+
) -> Result<RbTokenizer, Error> {
|
|
95
|
+
let subset = parse_subset(fields)?;
|
|
96
|
+
let mode = parse_mode(mode_str.as_deref())?;
|
|
97
|
+
Ok(RbTokenizer::new(
|
|
98
|
+
self.inner.clone(),
|
|
99
|
+
mode,
|
|
100
|
+
subset,
|
|
101
|
+
debug.unwrap_or(false),
|
|
102
|
+
))
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
use magnus::{Error, Ruby};
|
|
2
|
+
|
|
3
|
+
pub(crate) fn arg_error(msg: impl Into<String>) -> Error {
|
|
4
|
+
Error::new(Ruby::get().unwrap().exception_arg_error(), msg.into())
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
pub(crate) fn sudachi_error(e: impl std::fmt::Display) -> Error {
|
|
8
|
+
Error::new(
|
|
9
|
+
Ruby::get().unwrap().exception_runtime_error(),
|
|
10
|
+
e.to_string(),
|
|
11
|
+
)
|
|
12
|
+
}
|