kabosu 0.7.0.1 → 0.7.0.2
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/CHANGELOG.md +118 -0
- data/README.md +151 -65
- data/ext/kabosu/src/dictionary.rs +200 -93
- data/ext/kabosu/src/errors.rs +114 -8
- data/ext/kabosu/src/grouping.rs +44 -92
- data/ext/kabosu/src/lib.rs +73 -23
- data/ext/kabosu/src/morpheme.rs +289 -572
- data/ext/kabosu/src/morpheme_list.rs +204 -0
- data/ext/kabosu/src/nogvl.rs +108 -14
- data/ext/kabosu/src/parsing.rs +104 -118
- data/ext/kabosu/src/splitter.rs +62 -102
- data/ext/kabosu/src/tokenizer.rs +298 -140
- data/ext/kabosu/src/user_dict.rs +47 -22
- data/lib/kabosu/morpheme_list.rb +129 -115
- data/lib/kabosu/pos_matcher.rb +119 -28
- data/lib/kabosu/release.rake +7 -10
- data/lib/kabosu/version.rb +1 -1
- data/lib/kabosu.rb +207 -158
- metadata +12 -5
- data/ext/kabosu/src/token_batch.rs +0 -60
data/lib/kabosu.rb
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require_relative "kabosu/version"
|
|
2
4
|
|
|
3
5
|
# Load the native extension. Precompiled gems place it under a per-Ruby-version
|
|
@@ -16,13 +18,25 @@ require_relative "kabosu/pos_matcher"
|
|
|
16
18
|
require_relative "kabosu/morpheme_list"
|
|
17
19
|
require_relative "kabosu/railtie" if defined?(Rails::Railtie)
|
|
18
20
|
|
|
21
|
+
# The native extension defines the classes below and the native halves of
|
|
22
|
+
# their methods (named with a leading underscore, private). This file adds the
|
|
23
|
+
# public, keyword-argument API on top.
|
|
24
|
+
#
|
|
25
|
+
# The error classes are defined natively too, so the extension raises them
|
|
26
|
+
# directly (ext/kabosu/src/errors.rs):
|
|
27
|
+
#
|
|
28
|
+
# Kabosu::Error < StandardError anything else that goes wrong natively
|
|
29
|
+
# Kabosu::ConfigError a config file is missing, unreadable or invalid
|
|
30
|
+
# Kabosu::DictionaryError a dictionary cannot be loaded, read or built
|
|
31
|
+
# Kabosu::LookupError Dictionary#lookup / #lookup_all_entries failed
|
|
32
|
+
# Kabosu::TokenizationError Tokenizer#tokenize / Morpheme#split failed
|
|
33
|
+
# Kabosu::SentenceSplitError Kabosu.split_sentences failed
|
|
19
34
|
module Kabosu
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
class SentenceSplitError < Error; end
|
|
35
|
+
MODE_A = :a
|
|
36
|
+
MODE_B = :b
|
|
37
|
+
MODE_C = :c
|
|
38
|
+
|
|
39
|
+
private_constant :NativeMorphemeList
|
|
26
40
|
|
|
27
41
|
class SentenceRange
|
|
28
42
|
attr_reader :start, :end, :text
|
|
@@ -38,55 +52,60 @@ module Kabosu
|
|
|
38
52
|
end
|
|
39
53
|
end
|
|
40
54
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
case mode
|
|
51
|
-
when :a
|
|
52
|
-
"A"
|
|
53
|
-
when :b
|
|
54
|
-
"B"
|
|
55
|
-
when :c
|
|
56
|
-
"C"
|
|
57
|
-
else
|
|
58
|
-
raise ArgumentError, "invalid #{param_name} #{mode.inspect}; expected :a, :b, or :c"
|
|
55
|
+
# Argument checks shared by the public entry points.
|
|
56
|
+
module Arguments
|
|
57
|
+
module_function
|
|
58
|
+
|
|
59
|
+
# A filesystem path from a String or anything with #to_path (Pathname).
|
|
60
|
+
def path(value, name)
|
|
61
|
+
File.path(value)
|
|
62
|
+
rescue TypeError
|
|
63
|
+
raise ArgumentError, "#{name} must be a String or Pathname, got #{value.class}"
|
|
59
64
|
end
|
|
60
|
-
end
|
|
61
|
-
private_class_method :normalize_mode
|
|
62
65
|
|
|
63
|
-
|
|
66
|
+
def optional_path(value, name)
|
|
67
|
+
value.nil? ? nil : path(value, name)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def string(value, name)
|
|
71
|
+
raise ArgumentError, "#{name} must be a String, got #{value.class}" unless value.is_a?(String)
|
|
72
|
+
|
|
73
|
+
value
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def boolean(value, name)
|
|
77
|
+
raise ArgumentError, "#{name} must be true or false" unless [true, false].include?(value)
|
|
78
|
+
|
|
79
|
+
value
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
private_constant :Arguments
|
|
64
83
|
|
|
65
84
|
class Dictionary
|
|
66
85
|
DEFAULT_CONFIG_PATH = File.expand_path("kabosu/resources/sudachi.json", __dir__).freeze
|
|
67
86
|
|
|
68
87
|
class << self
|
|
69
|
-
|
|
70
|
-
|
|
88
|
+
# Loads a dictionary. Give the system dictionary explicitly, or a config
|
|
89
|
+
# file that names one; paths may be Strings or Pathnames.
|
|
90
|
+
#
|
|
91
|
+
# Kabosu::Dictionary.new(system_dict: Kabosu::Dictionary.path)
|
|
92
|
+
# Kabosu::Dictionary.new(system_dict: path, user_dicts: ["names.dic"])
|
|
93
|
+
# Kabosu::Dictionary.new(config: "sudachi.json")
|
|
71
94
|
def new(config: nil, system_dict: nil, user_dicts: nil)
|
|
72
|
-
|
|
73
|
-
|
|
95
|
+
config = Arguments.optional_path(config, "config")
|
|
96
|
+
system_dict = Arguments.optional_path(system_dict, "system_dict")
|
|
74
97
|
unless user_dicts.nil? || user_dicts.is_a?(Array)
|
|
75
|
-
raise ArgumentError, "user_dicts must be an Array
|
|
98
|
+
raise ArgumentError, "user_dicts must be an Array of paths or nil"
|
|
76
99
|
end
|
|
77
|
-
raise ArgumentError, "user_dicts must contain only String values" if user_dicts&.any? { !_1.is_a?(String) }
|
|
78
|
-
|
|
79
100
|
raise ArgumentError, "either config or system_dict is required" if config.nil? && system_dict.nil?
|
|
80
101
|
|
|
102
|
+
user_dicts = user_dicts&.map { Arguments.path(_1, "user_dicts") }
|
|
103
|
+
|
|
81
104
|
# Default to the sudachi.json bundled with this gem when only
|
|
82
105
|
# system_dict is given. sudachi.rs's own default config path is
|
|
83
106
|
# captured via env!("CARGO_MANIFEST_DIR") and doesn't exist on
|
|
84
107
|
# consumers' machines once the gem is precompiled and shipped.
|
|
85
|
-
config
|
|
86
|
-
|
|
87
|
-
_new(config, system_dict, user_dicts)
|
|
88
|
-
rescue RuntimeError => e
|
|
89
|
-
raise map_dictionary_init_error(e, config: config, system_dict: system_dict)
|
|
108
|
+
_new(config || DEFAULT_CONFIG_PATH, system_dict, user_dicts)
|
|
90
109
|
end
|
|
91
110
|
|
|
92
111
|
def install(edition = "core", version: nil)
|
|
@@ -101,60 +120,37 @@ module Kabosu
|
|
|
101
120
|
dict_manager.installed
|
|
102
121
|
end
|
|
103
122
|
|
|
104
|
-
private
|
|
105
|
-
|
|
106
|
-
def map_dictionary_init_error(error, config:, system_dict:)
|
|
107
|
-
message = error.message
|
|
108
|
-
if message.include?("V0 version")
|
|
109
|
-
return DictionaryError.new(
|
|
110
|
-
"#{message}: a system or user dictionary is in the pre-V1 format, which sudachi.rs 0.7+ cannot read. " \
|
|
111
|
-
"Install SudachiDict #{DictManager::MIN_VERSION} or later (rake kabosu:install) " \
|
|
112
|
-
"and rebuild user dictionaries against it."
|
|
113
|
-
)
|
|
114
|
-
end
|
|
123
|
+
private :_new
|
|
115
124
|
|
|
116
|
-
|
|
117
|
-
ConfigError.new(message)
|
|
118
|
-
else
|
|
119
|
-
DictionaryError.new(message)
|
|
120
|
-
end
|
|
121
|
-
end
|
|
125
|
+
private
|
|
122
126
|
|
|
123
127
|
def dict_manager
|
|
124
128
|
@dict_manager ||= DictManager.new
|
|
125
129
|
end
|
|
126
130
|
end
|
|
127
131
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
projection = options.fetch(:projection, nil)
|
|
139
|
-
|
|
140
|
-
raise ArgumentError, "fields must be an Array<String|Symbol> or nil" unless fields.nil? || fields.is_a?(Array)
|
|
141
|
-
if fields&.any? { !(_1.is_a?(String) || _1.is_a?(Symbol)) }
|
|
142
|
-
raise ArgumentError, "fields must contain only String or Symbol values"
|
|
143
|
-
end
|
|
144
|
-
raise ArgumentError, "debug must be true or false" unless [true, false].include?(debug)
|
|
145
|
-
|
|
132
|
+
# Creates a tokenizer. One tokenizer can be shared by any number of
|
|
133
|
+
# threads.
|
|
134
|
+
#
|
|
135
|
+
# mode:: :a, :b or :c (default), the unit the text is split into.
|
|
136
|
+
# fields:: the word-info fields to load, e.g. %i[surface pos_id]; nil
|
|
137
|
+
# loads all of them. Accessors for fields that were left out
|
|
138
|
+
# return empty values, and Morpheme#split needs split_a/split_b.
|
|
139
|
+
# debug:: have sudachi.rs print the lattice while tokenizing.
|
|
140
|
+
def tokenizer(mode: MODE_C, fields: nil, debug: false, projection: nil)
|
|
141
|
+
Arguments.boolean(debug, "debug")
|
|
146
142
|
raise NotImplementedError, "projection is not supported yet" unless projection.nil?
|
|
147
143
|
|
|
148
|
-
|
|
149
|
-
|
|
144
|
+
fields = fields.to_a if fields.is_a?(Enumerable)
|
|
145
|
+
_tokenizer(mode, fields, debug)
|
|
150
146
|
end
|
|
147
|
+
# The name this method had before SudachiPy renamed it. Not deprecated.
|
|
148
|
+
alias create tokenizer
|
|
151
149
|
|
|
150
|
+
# Every dictionary entry that is a prefix of text, as a MorphemeList over
|
|
151
|
+
# text. The query is normalized first, as tokenization does.
|
|
152
152
|
def lookup(text)
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
MorphemeList.new(_lookup(text))
|
|
156
|
-
rescue RuntimeError => e
|
|
157
|
-
raise LookupError.new(e.message), cause: e
|
|
153
|
+
MorphemeList.new(_lookup(Arguments.string(text, "text")))
|
|
158
154
|
end
|
|
159
155
|
|
|
160
156
|
# Every public dictionary entry as a standalone Morpheme (offsets cover its
|
|
@@ -170,13 +166,10 @@ module Kabosu
|
|
|
170
166
|
end
|
|
171
167
|
|
|
172
168
|
# Like #lookup, but exact matches only and found by scanning every entry,
|
|
173
|
-
# so it also finds entries that are not indexed for lookup. Slow
|
|
169
|
+
# so it also finds entries that are not indexed for lookup. Slow; other
|
|
170
|
+
# threads keep running during the scan.
|
|
174
171
|
def lookup_all_entries(surface)
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
MorphemeList.new(_lookup_all_entries(surface))
|
|
178
|
-
rescue RuntimeError => e
|
|
179
|
-
raise LookupError.new(e.message), cause: e
|
|
172
|
+
MorphemeList.new(_lookup_all_entries(Arguments.string(surface, "surface")))
|
|
180
173
|
end
|
|
181
174
|
|
|
182
175
|
# Builds an out-of-vocabulary Morpheme by hand. The forms default to the
|
|
@@ -185,98 +178,154 @@ module Kabosu
|
|
|
185
178
|
# dict.oov_morpheme(pos_id, "ズィーガンボルト", reading: "ズィーガンボルト")
|
|
186
179
|
def oov_morpheme(pos_id, surface, reading: nil, normalized_form: nil, dictionary_form: nil)
|
|
187
180
|
raise ArgumentError, "pos_id must be an Integer" unless pos_id.is_a?(Integer)
|
|
188
|
-
raise ArgumentError, "
|
|
181
|
+
raise ArgumentError, "invalid part-of-speech id #{pos_id}" unless pos_id.between?(0, 0xFFFF)
|
|
189
182
|
|
|
190
|
-
_oov_morpheme(pos_id, surface, reading, normalized_form, dictionary_form)
|
|
191
|
-
rescue RangeError, RuntimeError => e
|
|
192
|
-
raise ArgumentError, "invalid part-of-speech id #{pos_id}: #{e.message}"
|
|
183
|
+
_oov_morpheme(pos_id, Arguments.string(surface, "surface"), reading, normalized_form, dictionary_form)
|
|
193
184
|
end
|
|
194
|
-
end
|
|
195
185
|
|
|
196
|
-
|
|
186
|
+
# A PosMatcher that knows this dictionary's parts of speech, so matching a
|
|
187
|
+
# morpheme from it is a table lookup rather than a string comparison.
|
|
188
|
+
#
|
|
189
|
+
# nouns = dict.pos_matcher(["名詞"])
|
|
190
|
+
# dict.pos_matcher(["名詞", "固有名詞"], ["動詞"])
|
|
191
|
+
# dict.pos_matcher { |pos| pos[5] == "終止形-一般" }
|
|
192
|
+
#
|
|
193
|
+
# A pattern that matches no part of speech in this dictionary is most
|
|
194
|
+
# likely a typo, and raises ArgumentError.
|
|
195
|
+
def pos_matcher(*patterns, &)
|
|
196
|
+
# Also accept SudachiPy's shape: one list of patterns.
|
|
197
|
+
first = patterns.first
|
|
198
|
+
patterns = first if patterns.size == 1 && first.is_a?(Array) && !first.empty? && first.all?(Array)
|
|
197
199
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
+
patterns.each do |pattern|
|
|
201
|
+
next unless PosMatcher.new(pattern, dictionary: self).empty?
|
|
200
202
|
|
|
203
|
+
raise ArgumentError, "POS pattern #{pattern.inspect} does not match any part of speech in this dictionary"
|
|
204
|
+
end
|
|
205
|
+
PosMatcher.new(*patterns, dictionary: self, &)
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
private :_tokenizer, :_lookup, :_each_entry, :_lookup_all_entries, :_oov_morpheme
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
class Tokenizer
|
|
212
|
+
# Tokenizes text into a MorphemeList. Raises TokenizationError for input
|
|
213
|
+
# over sudachi.rs's limit of 49,149 bytes; split longer text first, e.g.
|
|
214
|
+
# with Kabosu.split_sentences.
|
|
201
215
|
def tokenize(text)
|
|
202
|
-
|
|
216
|
+
MorphemeList.new(_tokenize(Arguments.string(text, "text")))
|
|
217
|
+
end
|
|
203
218
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
219
|
+
# Tokenizes each text of an Array, returning one MorphemeList per text.
|
|
220
|
+
#
|
|
221
|
+
# The same as `texts.map { tokenize(_1) }`, but the GVL is released once
|
|
222
|
+
# per 64 KB of text rather than once per text. With several threads
|
|
223
|
+
# tokenizing short texts, that hand-off is what they spend their time
|
|
224
|
+
# waiting for, so batching sentences is noticeably faster there. Every
|
|
225
|
+
# text is checked before any is analyzed: one bad text fails the batch.
|
|
226
|
+
def tokenize_batch(texts)
|
|
227
|
+
raise ArgumentError, "texts must be an Array of Strings" unless texts.is_a?(Array) && texts.all?(String)
|
|
228
|
+
|
|
229
|
+
_tokenize_batch(texts).map! { MorphemeList.new(_1) }
|
|
209
230
|
end
|
|
231
|
+
|
|
232
|
+
private :_tokenize, :_tokenize_batch
|
|
210
233
|
end
|
|
211
234
|
|
|
212
235
|
class Morpheme
|
|
213
|
-
|
|
236
|
+
# The sub-units of this morpheme in a finer split mode, as a MorphemeList.
|
|
237
|
+
#
|
|
238
|
+
# A morpheme with no finer split gives an empty list, as in SudachiPy. Pass
|
|
239
|
+
# add_single: true to get a list holding the morpheme itself instead, so
|
|
240
|
+
# that `list.flat_map { _1.split(mode: :a, add_single: true).to_a }` is the
|
|
241
|
+
# mode-A tokenization.
|
|
242
|
+
def split(mode:, add_single: false)
|
|
243
|
+
MorphemeList.new(_split(mode, Arguments.boolean(add_single, "add_single")))
|
|
244
|
+
end
|
|
214
245
|
|
|
215
|
-
|
|
216
|
-
|
|
246
|
+
# Where this morpheme sits in the tokenized text, in characters:
|
|
247
|
+
# `text[m.char_range] == m.surface`.
|
|
248
|
+
def char_range
|
|
249
|
+
begin_c...end_c
|
|
250
|
+
end
|
|
217
251
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
252
|
+
# The same in bytes: `text.byteslice(m.byte_range) == m.surface`.
|
|
253
|
+
# #begin and #end are byte offsets too.
|
|
254
|
+
def byte_range
|
|
255
|
+
self.begin...self.end
|
|
222
256
|
end
|
|
223
|
-
end
|
|
224
257
|
|
|
225
|
-
|
|
226
|
-
# loads only next to the system dictionary it was built against, so rebuild
|
|
227
|
-
# whenever that changes (including the move to sudachi.rs 0.7).
|
|
228
|
-
#
|
|
229
|
-
# Kabosu.build_user_dict("names.csv", output: "names.dic")
|
|
230
|
-
# Kabosu::Dictionary.new(system_dict: Kabosu::Dictionary.path, user_dicts: ["names.dic"])
|
|
231
|
-
def self.build_user_dict(inputs, output:, system_dict: Dictionary.path, description: nil)
|
|
232
|
-
inputs = Array(inputs)
|
|
233
|
-
raise ArgumentError, "inputs must be String paths" unless inputs.all?(String)
|
|
234
|
-
raise ArgumentError, "output must be a String" unless output.is_a?(String)
|
|
235
|
-
|
|
236
|
-
_build_user_dict(inputs, system_dict, output, description)
|
|
237
|
-
output
|
|
238
|
-
rescue RuntimeError => e
|
|
239
|
-
raise DictionaryError.new(e.message), cause: e
|
|
240
|
-
end
|
|
258
|
+
alias size length
|
|
241
259
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
raise ArgumentError, "limit must be an Integer or nil" unless limit.nil? || limit.is_a?(Integer)
|
|
245
|
-
raise ArgumentError, "limit must be greater than 0" if limit && limit < 1
|
|
246
|
-
raise ArgumentError, "with_checker must be true or false" unless [true, false].include?(with_checker)
|
|
247
|
-
raise ArgumentError, "ranges must be true or false" unless [true, false].include?(ranges)
|
|
248
|
-
raise ArgumentError, "dictionary must be a String path or nil" unless dictionary.nil? || dictionary.is_a?(String)
|
|
260
|
+
FIELDS = %i[surface part_of_speech part_of_speech_id dictionary_form normalized_form reading_form
|
|
261
|
+
begin_c end_c begin end oov? dictionary_id word_id synonym_group_ids user_data].freeze
|
|
249
262
|
|
|
250
|
-
|
|
251
|
-
|
|
263
|
+
# The morpheme as a Hash, keyed by reader name (`oov?` is `:oov`).
|
|
264
|
+
def to_h
|
|
265
|
+
deconstruct_keys(nil)
|
|
266
|
+
end
|
|
252
267
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
268
|
+
# For pattern matching:
|
|
269
|
+
#
|
|
270
|
+
# case morpheme
|
|
271
|
+
# in { part_of_speech: ["名詞", *], surface: } then …
|
|
272
|
+
# end
|
|
273
|
+
def deconstruct_keys(keys)
|
|
274
|
+
(keys ? FIELDS.select { keys.include?(_1.to_s.delete_suffix("?").to_sym) } : FIELDS).to_h do |reader|
|
|
275
|
+
[reader.to_s.delete_suffix("?").to_sym, public_send(reader)]
|
|
256
276
|
end
|
|
257
|
-
else
|
|
258
|
-
_split_sentences(text, limit, dict_path)
|
|
259
277
|
end
|
|
260
|
-
|
|
261
|
-
|
|
278
|
+
|
|
279
|
+
private :_split
|
|
262
280
|
end
|
|
263
281
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
282
|
+
class << self
|
|
283
|
+
# Compiles user-dictionary CSV(s) into a binary user dictionary. The result
|
|
284
|
+
# loads only next to the system dictionary it was built against, so rebuild
|
|
285
|
+
# whenever that changes (including the move to sudachi.rs 0.7).
|
|
286
|
+
#
|
|
287
|
+
# Kabosu.build_user_dict("names.csv", output: "names.dic")
|
|
288
|
+
# Kabosu::Dictionary.new(system_dict: Kabosu::Dictionary.path, user_dicts: ["names.dic"])
|
|
289
|
+
def build_user_dict(inputs, output:, system_dict: Dictionary.path, description: nil)
|
|
290
|
+
inputs = Array(inputs).map { Arguments.path(_1, "inputs") }
|
|
291
|
+
output = Arguments.path(output, "output")
|
|
292
|
+
|
|
293
|
+
_build_user_dict(inputs, Arguments.path(system_dict, "system_dict"), output, description)
|
|
294
|
+
output
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# Splits text into sentences.
|
|
298
|
+
#
|
|
299
|
+
# limit:: how many characters ahead to look for the end of a
|
|
300
|
+
# sentence (default 4096). When there is none that close,
|
|
301
|
+
# the rest of the text is returned as one sentence.
|
|
302
|
+
# ranges:: return SentenceRange objects (byte offsets plus text).
|
|
303
|
+
# with_checker:: consult a dictionary so that a boundary does not fall
|
|
304
|
+
# inside a word (the 。 of モーニング娘。). Known bug in
|
|
305
|
+
# sudachi.rs 0.7.0: this also suppresses every boundary
|
|
306
|
+
# whose punctuation mark is a dictionary word, as 。 is,
|
|
307
|
+
# unless it ends the text. In practice most text comes back
|
|
308
|
+
# unsplit; leave the checker off until sudachi.rs ships the
|
|
309
|
+
# fix (its PR #369).
|
|
310
|
+
# dictionary:: that dictionary: a Kabosu::Dictionary, which is reused, or
|
|
311
|
+
# the path of a system dictionary, which is loaded on every
|
|
312
|
+
# call (default: Dictionary.path). Implies with_checker.
|
|
313
|
+
def split_sentences(text, limit: nil, with_checker: false, ranges: false, dictionary: nil)
|
|
314
|
+
Arguments.string(text, "text")
|
|
315
|
+
raise ArgumentError, "limit must be an Integer or nil" unless limit.nil? || limit.is_a?(Integer)
|
|
316
|
+
raise ArgumentError, "limit must be greater than 0" if limit && limit < 1
|
|
317
|
+
|
|
318
|
+
# No text is this long, and anything larger does not fit a native integer.
|
|
319
|
+
limit = [limit, 0xFFFF_FFFF].min if limit
|
|
320
|
+
|
|
321
|
+
Arguments.boolean(ranges, "ranges")
|
|
322
|
+
dictionary = Arguments.path(dictionary, "dictionary") unless dictionary.nil? || dictionary.is_a?(Dictionary)
|
|
323
|
+
checker = dictionary || (Dictionary.path if Arguments.boolean(with_checker, "with_checker"))
|
|
324
|
+
|
|
325
|
+
sentences = _split_sentences(text, limit, checker, ranges)
|
|
326
|
+
ranges ? sentences.map! { |start, finish, sentence| SentenceRange.new(start, finish, sentence) } : sentences
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
private :_build_user_dict, :_split_sentences
|
|
281
330
|
end
|
|
282
331
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: kabosu
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.7.0.
|
|
4
|
+
version: 0.7.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- davafons
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rb_sys
|
|
@@ -56,16 +56,22 @@ dependencies:
|
|
|
56
56
|
name: minitest
|
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
|
58
58
|
requirements:
|
|
59
|
-
- - "
|
|
59
|
+
- - ">="
|
|
60
60
|
- !ruby/object:Gem::Version
|
|
61
61
|
version: '5.0'
|
|
62
|
+
- - "<"
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
version: '7'
|
|
62
65
|
type: :development
|
|
63
66
|
prerelease: false
|
|
64
67
|
version_requirements: !ruby/object:Gem::Requirement
|
|
65
68
|
requirements:
|
|
66
|
-
- - "
|
|
69
|
+
- - ">="
|
|
67
70
|
- !ruby/object:Gem::Version
|
|
68
71
|
version: '5.0'
|
|
72
|
+
- - "<"
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '7'
|
|
69
75
|
- !ruby/object:Gem::Dependency
|
|
70
76
|
name: rake
|
|
71
77
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -116,6 +122,7 @@ extensions:
|
|
|
116
122
|
- ext/kabosu/extconf.rb
|
|
117
123
|
extra_rdoc_files: []
|
|
118
124
|
files:
|
|
125
|
+
- CHANGELOG.md
|
|
119
126
|
- Cargo.lock
|
|
120
127
|
- Cargo.toml
|
|
121
128
|
- LICENSE
|
|
@@ -127,10 +134,10 @@ files:
|
|
|
127
134
|
- ext/kabosu/src/grouping.rs
|
|
128
135
|
- ext/kabosu/src/lib.rs
|
|
129
136
|
- ext/kabosu/src/morpheme.rs
|
|
137
|
+
- ext/kabosu/src/morpheme_list.rs
|
|
130
138
|
- ext/kabosu/src/nogvl.rs
|
|
131
139
|
- ext/kabosu/src/parsing.rs
|
|
132
140
|
- ext/kabosu/src/splitter.rs
|
|
133
|
-
- ext/kabosu/src/token_batch.rs
|
|
134
141
|
- ext/kabosu/src/tokenizer.rs
|
|
135
142
|
- ext/kabosu/src/user_dict.rs
|
|
136
143
|
- lib/kabosu.rb
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
use magnus::{Error, RArray, Ruby};
|
|
2
|
-
use std::sync::Arc;
|
|
3
|
-
use sudachi::dic::dictionary::JapaneseDictionary;
|
|
4
|
-
|
|
5
|
-
use crate::grouping::group_morphemes_rust;
|
|
6
|
-
use crate::morpheme::{rb_morpheme_from_data, MorphemeData, RbMorpheme};
|
|
7
|
-
|
|
8
|
-
#[magnus::wrap(class = "Kabosu::TokenBatch")]
|
|
9
|
-
pub(crate) struct RbTokenBatch {
|
|
10
|
-
morphemes: Vec<MorphemeData>,
|
|
11
|
-
dict: Arc<JapaneseDictionary>,
|
|
12
|
-
debug: bool,
|
|
13
|
-
internal_cost: i32,
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
impl RbTokenBatch {
|
|
17
|
-
pub(crate) fn new(
|
|
18
|
-
morphemes: Vec<MorphemeData>,
|
|
19
|
-
dict: Arc<JapaneseDictionary>,
|
|
20
|
-
debug: bool,
|
|
21
|
-
internal_cost: i32,
|
|
22
|
-
) -> Self {
|
|
23
|
-
Self {
|
|
24
|
-
morphemes,
|
|
25
|
-
dict,
|
|
26
|
-
debug,
|
|
27
|
-
internal_cost,
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
pub(crate) fn size(&self) -> usize {
|
|
32
|
-
self.morphemes.len()
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
pub(crate) fn internal_cost(&self) -> i32 {
|
|
36
|
-
self.internal_cost
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
pub(crate) fn morpheme_at(&self, idx: usize) -> Option<RbMorpheme> {
|
|
40
|
-
self.morphemes
|
|
41
|
-
.get(idx)
|
|
42
|
-
.cloned()
|
|
43
|
-
.map(|data| rb_morpheme_from_data(data, self.dict.clone(), self.debug))
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
pub(crate) fn surfaces(&self) -> Result<RArray, Error> {
|
|
47
|
-
let ruby = Ruby::get().unwrap();
|
|
48
|
-
let ary = ruby.ary_new_capa(self.morphemes.len());
|
|
49
|
-
for m in &self.morphemes {
|
|
50
|
-
ary.push(ruby.str_new(&m.surface))?;
|
|
51
|
-
}
|
|
52
|
-
Ok(ary)
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/// jpdb-style grouping performed natively in Rust.
|
|
56
|
-
/// Returns an Array-of-Array of Kabosu::Morpheme.
|
|
57
|
-
pub(crate) fn group_morphemes(&self) -> Result<RArray, Error> {
|
|
58
|
-
group_morphemes_rust(&self.morphemes, &self.dict, self.debug)
|
|
59
|
-
}
|
|
60
|
-
}
|