kabosu 0.6.11.4-aarch64-linux → 0.7.0.2-aarch64-linux
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 +186 -61
- data/lib/kabosu/3.1/kabosu.so +0 -0
- data/lib/kabosu/3.2/kabosu.so +0 -0
- data/lib/kabosu/3.3/kabosu.so +0 -0
- data/lib/kabosu/3.4/kabosu.so +0 -0
- data/lib/kabosu/4.0/kabosu.so +0 -0
- data/lib/kabosu/dict_manager.rb +45 -5
- 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/resources/char.def +1 -1
- data/lib/kabosu/tasks.rake +15 -1
- data/lib/kabosu/version.rb +1 -1
- data/lib/kabosu.rb +230 -121
- metadata +11 -4
data/lib/kabosu/morpheme_list.rb
CHANGED
|
@@ -1,145 +1,133 @@
|
|
|
1
1
|
module Kabosu
|
|
2
|
+
# An ordered list of morphemes: the result of Tokenizer#tokenize,
|
|
3
|
+
# Dictionary#lookup or Morpheme#split, or any Array of morphemes.
|
|
4
|
+
#
|
|
5
|
+
# A list that comes from the analyzer is lazy. The analysis result lives in
|
|
6
|
+
# native memory and Morpheme objects are only made when asked for: one for
|
|
7
|
+
# `list[i]`, all of them when the list is iterated, and none by the bulk
|
|
8
|
+
# readers such as #surfaces. An index answers with the same object each
|
|
9
|
+
# time; only threads racing to be the first to read a list can each get an
|
|
10
|
+
# object of their own for an index, and those are == and interchangeable.
|
|
2
11
|
class MorphemeList
|
|
3
12
|
include Enumerable
|
|
4
13
|
|
|
5
|
-
|
|
14
|
+
# The lattice path cost of a tokenization; nil for any other list.
|
|
15
|
+
attr_reader :internal_cost
|
|
6
16
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def each(&block)
|
|
14
|
-
return enum_for(:each) unless block
|
|
15
|
-
|
|
16
|
-
if @source
|
|
17
|
-
i = 0
|
|
18
|
-
while i < size
|
|
19
|
-
block.call(fetch(i))
|
|
20
|
-
i += 1
|
|
21
|
-
end
|
|
17
|
+
# source is an Array of morphemes, or a native analysis result.
|
|
18
|
+
def initialize(source, internal_cost: nil)
|
|
19
|
+
if source.respond_to?(:morpheme_at)
|
|
20
|
+
@native = source
|
|
21
|
+
@size = source.size
|
|
22
|
+
@internal_cost = internal_cost || source.internal_cost
|
|
22
23
|
else
|
|
23
|
-
@morphemes.
|
|
24
|
+
@morphemes = source.to_a
|
|
25
|
+
@size = @morphemes.size
|
|
26
|
+
@internal_cost = internal_cost
|
|
24
27
|
end
|
|
25
28
|
end
|
|
26
29
|
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
attr_reader :size
|
|
31
|
+
alias length size
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
fetch(idx)
|
|
33
|
+
def empty?
|
|
34
|
+
@size.zero?
|
|
34
35
|
end
|
|
35
36
|
|
|
36
|
-
def
|
|
37
|
-
return
|
|
38
|
-
|
|
39
|
-
n = Integer(n)
|
|
40
|
-
raise ArgumentError, "negative array size" if n.negative?
|
|
37
|
+
def each
|
|
38
|
+
return enum_for(:each) { @size } unless block_given?
|
|
41
39
|
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
morphemes.each { yield _1 }
|
|
41
|
+
self
|
|
44
42
|
end
|
|
45
43
|
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
# Like Array#[]: `list[i]` is one morpheme (or nil), while `list[range]`
|
|
45
|
+
# and `list[start, length]` are an Array of them.
|
|
46
|
+
def [](index, length = nil)
|
|
47
|
+
return to_a[index, length] if length
|
|
48
|
+
return to_a[index] unless index.is_a?(Integer)
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
index += @size if index.negative?
|
|
51
|
+
morpheme_at(index) if index.between?(0, @size - 1)
|
|
52
|
+
end
|
|
53
|
+
alias slice []
|
|
54
|
+
|
|
55
|
+
def first(count = nil)
|
|
56
|
+
return self[0] unless count
|
|
57
|
+
raise ArgumentError, "negative array size" if count.negative?
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
(start...size).map { fetch(_1) }
|
|
59
|
+
self[0, count]
|
|
54
60
|
end
|
|
55
61
|
|
|
56
|
-
def
|
|
57
|
-
|
|
62
|
+
def last(count = nil)
|
|
63
|
+
return self[-1] unless count
|
|
64
|
+
raise ArgumentError, "negative array size" if count.negative?
|
|
65
|
+
|
|
66
|
+
self[[@size - count, 0].max, count]
|
|
58
67
|
end
|
|
59
68
|
|
|
60
|
-
def
|
|
61
|
-
|
|
69
|
+
def to_a
|
|
70
|
+
morphemes.dup
|
|
62
71
|
end
|
|
63
72
|
|
|
64
|
-
|
|
65
|
-
|
|
73
|
+
# The bulk readers below take one native call for the whole list and make
|
|
74
|
+
# no Morpheme objects: prefer them to `list.map(&:surface)` and the like.
|
|
66
75
|
|
|
67
|
-
|
|
76
|
+
def surfaces
|
|
77
|
+
@native ? @native.surfaces : map(&:surface)
|
|
68
78
|
end
|
|
69
79
|
|
|
70
80
|
def readings
|
|
71
|
-
map(&:reading_form)
|
|
81
|
+
@native ? @native.reading_forms : map(&:reading_form)
|
|
72
82
|
end
|
|
73
83
|
|
|
74
84
|
def dictionary_forms
|
|
75
|
-
map(&:dictionary_form)
|
|
85
|
+
@native ? @native.dictionary_forms : map(&:dictionary_form)
|
|
76
86
|
end
|
|
77
87
|
|
|
78
88
|
def normalized_forms
|
|
79
|
-
map(&:normalized_form)
|
|
89
|
+
@native ? @native.normalized_forms : map(&:normalized_form)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def parts_of_speech
|
|
93
|
+
@native ? @native.parts_of_speech : map(&:part_of_speech)
|
|
80
94
|
end
|
|
81
95
|
|
|
82
96
|
def total_costs
|
|
83
|
-
map(&:total_cost)
|
|
97
|
+
@native ? @native.total_costs : map(&:total_cost)
|
|
84
98
|
end
|
|
85
99
|
|
|
86
100
|
def synonym_group_ids
|
|
87
101
|
map(&:synonym_group_ids)
|
|
88
102
|
end
|
|
89
103
|
|
|
90
|
-
#
|
|
104
|
+
# The surfaces joined back into the text they were cut from.
|
|
91
105
|
def to_text
|
|
92
106
|
surfaces.join
|
|
93
107
|
end
|
|
94
108
|
|
|
95
|
-
#
|
|
96
|
-
|
|
97
|
-
|
|
109
|
+
# The surfaces separated by spaces (wakati-gaki), as SudachiPy prints a list.
|
|
110
|
+
def to_s
|
|
111
|
+
surfaces.join(" ")
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# jpdb-style grouping: each content word together with the inflectional
|
|
115
|
+
# suffixes and auxiliaries that cling to it, as an Array of Arrays of
|
|
116
|
+
# morphemes. Done natively for analyzer results; the Ruby version below
|
|
117
|
+
# gives the same answer for any other list.
|
|
98
118
|
def group_morphemes
|
|
99
|
-
return @
|
|
119
|
+
return @native.group_morphemes if @native
|
|
100
120
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
last << m
|
|
121
|
+
each_with_object([]) do |morpheme, groups|
|
|
122
|
+
group = groups.last
|
|
123
|
+
if group && content_word?(group.first) && extends_group?(morpheme, group.last)
|
|
124
|
+
group << morpheme
|
|
106
125
|
else
|
|
107
|
-
groups << [
|
|
126
|
+
groups << [morpheme]
|
|
108
127
|
end
|
|
109
128
|
end
|
|
110
|
-
groups
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
private
|
|
114
|
-
|
|
115
|
-
def content_word?(morpheme)
|
|
116
|
-
!%w[助詞 助動詞 補助記号 記号 空白].include?(morpheme.part_of_speech.first)
|
|
117
129
|
end
|
|
118
130
|
|
|
119
|
-
def extends_group?(morpheme, prev = nil)
|
|
120
|
-
pos = morpheme.part_of_speech
|
|
121
|
-
pos1 = pos[0]
|
|
122
|
-
pos1 == "助動詞" ||
|
|
123
|
-
(pos1 == "助詞" && !clause_boundary?(morpheme) &&
|
|
124
|
-
(pos[1] == "接続助詞" ||
|
|
125
|
-
(pos[1] == "副助詞" && prev && %w[動詞 形容詞 形状詞].include?(prev.part_of_speech[0])))) ||
|
|
126
|
-
(pos1 == "動詞" && pos[1] == "非自立可能" &&
|
|
127
|
-
prev && prev.part_of_speech[0] == "助詞" && %w[て で].include?(prev.surface))
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def clause_boundary?(morpheme)
|
|
131
|
-
return false unless morpheme
|
|
132
|
-
|
|
133
|
-
pos = morpheme.part_of_speech
|
|
134
|
-
return true if pos[0] == "助詞" &&
|
|
135
|
-
%w[ながら たら ば と のに から ので けれど けど つつ なり や か かどうか とも].include?(morpheme.surface)
|
|
136
|
-
return true if pos[0] == "助詞" && pos[1] == "接続助詞" && morpheme.surface == "が"
|
|
137
|
-
|
|
138
|
-
false
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
public
|
|
142
|
-
|
|
143
131
|
# Filter morphemes by POS. Accepts a PosMatcher or an array pattern.
|
|
144
132
|
# Returns a new MorphemeList with only matching morphemes.
|
|
145
133
|
#
|
|
@@ -147,30 +135,40 @@ module Kabosu
|
|
|
147
135
|
# list.select_pos(["名詞", "固有名詞"])
|
|
148
136
|
#
|
|
149
137
|
def select_pos(matcher_or_pattern)
|
|
150
|
-
|
|
151
|
-
self.class.new(matcher.filter(to_a))
|
|
138
|
+
self.class.new(coerce_to_matcher(matcher_or_pattern).filter(self))
|
|
152
139
|
end
|
|
153
140
|
|
|
154
141
|
# Inverse of select_pos. Returns a new MorphemeList excluding matching morphemes.
|
|
155
142
|
def reject_pos(matcher_or_pattern)
|
|
156
|
-
|
|
157
|
-
self.class.new(matcher.reject(to_a))
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
def to_a
|
|
161
|
-
return @morphemes.dup unless @source
|
|
162
|
-
|
|
163
|
-
(0...size).map { fetch(_1) }
|
|
143
|
+
self.class.new(coerce_to_matcher(matcher_or_pattern).reject(self))
|
|
164
144
|
end
|
|
165
145
|
|
|
166
146
|
def inspect
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
base + ": #{surfaces.join(" | ")}>"
|
|
147
|
+
cost = " cost=#{@internal_cost}" if @internal_cost
|
|
148
|
+
"#<Kabosu::MorphemeList (#{@size} morphemes)#{cost}: #{surfaces.join(" | ")}>"
|
|
170
149
|
end
|
|
171
150
|
|
|
172
151
|
private
|
|
173
152
|
|
|
153
|
+
# index must be within 0...size.
|
|
154
|
+
def morpheme_at(index)
|
|
155
|
+
return @morphemes[index] if @complete || !@native
|
|
156
|
+
|
|
157
|
+
(@morphemes ||= Array.new(@size))[index] ||= @native.morpheme_at(index)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Every morpheme, made in one native call. Morphemes that were already
|
|
161
|
+
# handed out one by one stay the objects they were.
|
|
162
|
+
def morphemes
|
|
163
|
+
return @morphemes if @complete || !@native
|
|
164
|
+
|
|
165
|
+
already_made = @morphemes
|
|
166
|
+
@morphemes = @native.morphemes
|
|
167
|
+
already_made&.each_with_index { |morpheme, index| @morphemes[index] = morpheme if morpheme }
|
|
168
|
+
@complete = true
|
|
169
|
+
@morphemes
|
|
170
|
+
end
|
|
171
|
+
|
|
174
172
|
def coerce_to_matcher(matcher_or_pattern)
|
|
175
173
|
case matcher_or_pattern
|
|
176
174
|
when PosMatcher
|
|
@@ -182,24 +180,40 @@ module Kabosu
|
|
|
182
180
|
end
|
|
183
181
|
end
|
|
184
182
|
|
|
185
|
-
|
|
186
|
-
obj.respond_to?(:morpheme_at) && obj.respond_to?(:size) && obj.respond_to?(:surfaces)
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
def normalize_index(index)
|
|
190
|
-
return nil if size.zero?
|
|
183
|
+
# ── group_morphemes: mirrors ext/kabosu/src/grouping.rs ──
|
|
191
184
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
185
|
+
FUNCTION_WORDS = %w[助詞 助動詞 補助記号 記号 空白].freeze
|
|
186
|
+
INFLECTING_WORDS = %w[動詞 形容詞 形状詞].freeze
|
|
187
|
+
CLAUSE_BOUNDARY_PARTICLES = %w[ながら たら ば と のに から ので けれど けど つつ なり や か かどうか とも].freeze
|
|
188
|
+
private_constant :FUNCTION_WORDS, :INFLECTING_WORDS, :CLAUSE_BOUNDARY_PARTICLES
|
|
195
189
|
|
|
196
|
-
|
|
190
|
+
def content_word?(morpheme)
|
|
191
|
+
!FUNCTION_WORDS.include?(morpheme.part_of_speech[0])
|
|
197
192
|
end
|
|
198
193
|
|
|
199
|
-
def
|
|
200
|
-
|
|
194
|
+
def extends_group?(morpheme, prev)
|
|
195
|
+
pos = morpheme.part_of_speech
|
|
196
|
+
case pos[0]
|
|
197
|
+
when "助動詞"
|
|
198
|
+
true
|
|
199
|
+
when "助詞"
|
|
200
|
+
return false if clause_boundary?(morpheme)
|
|
201
|
+
|
|
202
|
+
# 副助詞 clings to a preceding verb/adjective (e.g. たり/だり)
|
|
203
|
+
pos[1] == "接続助詞" || (pos[1] == "副助詞" && INFLECTING_WORDS.include?(prev.part_of_speech[0]))
|
|
204
|
+
when "動詞"
|
|
205
|
+
# te-form auxiliary chain: て/で + いる/ある/くる/etc.
|
|
206
|
+
pos[1] == "非自立可能" && prev.part_of_speech[0] == "助詞" && %w[て で].include?(prev.surface)
|
|
207
|
+
else
|
|
208
|
+
false
|
|
209
|
+
end
|
|
210
|
+
end
|
|
201
211
|
|
|
202
|
-
|
|
212
|
+
# Whether a particle starts a new clause rather than attaching to the word
|
|
213
|
+
# before it. Contrastive が (接続助詞) does, unlike subject が (格助詞).
|
|
214
|
+
def clause_boundary?(particle)
|
|
215
|
+
CLAUSE_BOUNDARY_PARTICLES.include?(particle.surface) ||
|
|
216
|
+
(particle.part_of_speech[1] == "接続助詞" && particle.surface == "が")
|
|
203
217
|
end
|
|
204
218
|
end
|
|
205
219
|
end
|
data/lib/kabosu/pos_matcher.rb
CHANGED
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
module Kabosu
|
|
2
|
+
# Decides whether a part of speech is one of a chosen set.
|
|
3
|
+
#
|
|
4
|
+
# A matcher is defined by patterns or by a block, and works on anything: a
|
|
5
|
+
# Morpheme, or a raw part-of-speech Array.
|
|
6
|
+
#
|
|
7
|
+
# # Single pattern (array of strings; "*" or nil = match anything)
|
|
8
|
+
# PosMatcher.new(["名詞", "固有名詞", "*", "*"])
|
|
9
|
+
#
|
|
10
|
+
# # Multiple patterns (matches if any pattern matches)
|
|
11
|
+
# PosMatcher.new(["名詞", "固有名詞"], ["動詞", "*", "*", "*"])
|
|
12
|
+
#
|
|
13
|
+
# # From a block
|
|
14
|
+
# PosMatcher.new { |pos| pos[0] == "名詞" }
|
|
15
|
+
#
|
|
16
|
+
# A matcher made by Dictionary#pos_matcher (or #bind) is also bound to that
|
|
17
|
+
# dictionary: it has decided every part of speech the dictionary has up
|
|
18
|
+
# front, so matching one of its morphemes is a single table lookup with no
|
|
19
|
+
# string comparison. Anything else it is asked about (a raw Array, a morpheme
|
|
20
|
+
# of another dictionary) still goes through the patterns or the block.
|
|
2
21
|
class PosMatcher
|
|
3
|
-
#
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
#
|
|
8
|
-
# # Single pattern (array of strings; "*" or nil = match anything)
|
|
9
|
-
# PosMatcher.new(["名詞", "固有名詞", "*", "*"])
|
|
10
|
-
#
|
|
11
|
-
# # Multiple patterns (matches if any pattern matches)
|
|
12
|
-
# PosMatcher.new(["名詞", "固有名詞"], ["動詞", "*", "*", "*"])
|
|
13
|
-
#
|
|
14
|
-
def initialize(*patterns, &block)
|
|
22
|
+
# The dictionary this matcher is bound to, or nil.
|
|
23
|
+
attr_reader :dictionary
|
|
24
|
+
|
|
25
|
+
def initialize(*patterns, dictionary: nil, &block)
|
|
15
26
|
if block
|
|
16
27
|
raise ArgumentError, "cannot supply both patterns and a block" unless patterns.empty?
|
|
17
28
|
|
|
@@ -22,18 +33,39 @@ module Kabosu
|
|
|
22
33
|
@patterns = patterns.map(&:freeze).freeze
|
|
23
34
|
end
|
|
24
35
|
|
|
36
|
+
@dictionary = dictionary
|
|
37
|
+
# Morpheme#part_of_speech returns the dictionary's own frozen Array for
|
|
38
|
+
# that part of speech, the same object every time. Keying the verdicts by
|
|
39
|
+
# identity therefore finds them without comparing a single String, and
|
|
40
|
+
# cannot confuse this dictionary's parts of speech with another's.
|
|
41
|
+
@verdicts = dictionary && decide_all(dictionary.pos_list)
|
|
42
|
+
|
|
25
43
|
freeze
|
|
26
44
|
end
|
|
27
45
|
|
|
46
|
+
# The same matcher, bound to a dictionary.
|
|
47
|
+
def bind(dictionary)
|
|
48
|
+
return self if dictionary.equal?(@dictionary)
|
|
49
|
+
|
|
50
|
+
matcher = self
|
|
51
|
+
PosMatcher.new(dictionary: dictionary) { |pos| matcher.match?(pos) }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def bound?
|
|
55
|
+
!@dictionary.nil?
|
|
56
|
+
end
|
|
57
|
+
|
|
28
58
|
# Returns true if the morpheme (or raw POS array) matches this matcher.
|
|
29
59
|
def match?(morpheme_or_pos)
|
|
30
60
|
pos = extract_pos(morpheme_or_pos)
|
|
61
|
+
verdict = @verdicts && @verdicts[pos]
|
|
62
|
+
verdict.nil? ? decide(pos) : verdict
|
|
63
|
+
end
|
|
64
|
+
alias === match?
|
|
65
|
+
alias call match?
|
|
31
66
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
else
|
|
35
|
-
@patterns.any? { |pattern| pattern_match?(pattern, pos) }
|
|
36
|
-
end
|
|
67
|
+
def to_proc
|
|
68
|
+
method(:match?).to_proc
|
|
37
69
|
end
|
|
38
70
|
|
|
39
71
|
# Return matching morphemes as an Array.
|
|
@@ -48,28 +80,62 @@ module Kabosu
|
|
|
48
80
|
|
|
49
81
|
# Union: matches if either matcher matches.
|
|
50
82
|
def |(other)
|
|
51
|
-
|
|
52
|
-
b = other
|
|
53
|
-
PosMatcher.new { |pos| a.match?(pos) || b.match?(pos) }
|
|
83
|
+
combine(other) { |pos| match?(pos) || other.match?(pos) }
|
|
54
84
|
end
|
|
55
85
|
|
|
56
86
|
# Intersection: matches if both matchers match.
|
|
57
87
|
def &(other)
|
|
58
|
-
|
|
59
|
-
b = other
|
|
60
|
-
PosMatcher.new { |pos| a.match?(pos) && b.match?(pos) }
|
|
88
|
+
combine(other) { |pos| match?(pos) && other.match?(pos) }
|
|
61
89
|
end
|
|
62
90
|
|
|
63
91
|
# Difference: matches self but not other.
|
|
64
92
|
def -(other)
|
|
65
|
-
|
|
93
|
+
combine(other) { |pos| match?(pos) && !other.match?(pos) }
|
|
94
|
+
end
|
|
95
|
+
alias difference -
|
|
96
|
+
|
|
97
|
+
# Complement: matches whatever this matcher does not.
|
|
98
|
+
def ~
|
|
99
|
+
PosMatcher.new(dictionary: @dictionary) { |pos| !match?(pos) }
|
|
100
|
+
end
|
|
101
|
+
alias invert ~
|
|
102
|
+
|
|
103
|
+
# ── Bound matchers only ──
|
|
104
|
+
|
|
105
|
+
# Yields every part of speech of the dictionary that matches, in id order.
|
|
106
|
+
def each(&block)
|
|
107
|
+
return enum_for(:each) { size } unless block
|
|
108
|
+
|
|
109
|
+
verdicts.each { |pos, matched| block.call(pos) if matched }
|
|
110
|
+
self
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# The ids of the matching parts of speech (see Morpheme#part_of_speech_id).
|
|
114
|
+
def ids
|
|
115
|
+
verdicts.each_value.with_index.filter_map { |matched, id| id if matched }
|
|
66
116
|
end
|
|
67
117
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
PosMatcher.new { |pos| a.match?(pos) && !b.match?(pos) }
|
|
118
|
+
# How many of the dictionary's parts of speech match.
|
|
119
|
+
def size
|
|
120
|
+
verdicts.each_value.count(true)
|
|
72
121
|
end
|
|
122
|
+
alias length size
|
|
123
|
+
|
|
124
|
+
def empty?
|
|
125
|
+
verdicts.each_value.none?
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# The matching parts of speech of the dictionary.
|
|
129
|
+
def to_a
|
|
130
|
+
each.to_a
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def inspect
|
|
134
|
+
return "#<Kabosu::PosMatcher #{size} of #{verdicts.size} parts of speech>" if bound?
|
|
135
|
+
|
|
136
|
+
"#<Kabosu::PosMatcher #{@patterns ? @patterns.map(&:inspect).join(" | ") : "block"}>"
|
|
137
|
+
end
|
|
138
|
+
alias to_s inspect
|
|
73
139
|
|
|
74
140
|
# ── Pre-built matchers ──
|
|
75
141
|
|
|
@@ -103,6 +169,31 @@ module Kabosu
|
|
|
103
169
|
|
|
104
170
|
private
|
|
105
171
|
|
|
172
|
+
# The verdict table, in id order. Only bound matchers have one.
|
|
173
|
+
def verdicts
|
|
174
|
+
@verdicts or raise ArgumentError,
|
|
175
|
+
"this PosMatcher is not bound to a dictionary; use Dictionary#pos_matcher or #bind"
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# The result stays bound when both sides know the same dictionary.
|
|
179
|
+
def combine(other, &)
|
|
180
|
+
raise ArgumentError, "expected a PosMatcher, got #{other.class}" unless other.is_a?(PosMatcher)
|
|
181
|
+
|
|
182
|
+
PosMatcher.new(dictionary: (@dictionary if @dictionary.equal?(other.dictionary)), &)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def decide_all(pos_list)
|
|
186
|
+
pos_list.each_with_object({}.compare_by_identity) { |pos, verdicts| verdicts[pos] = decide(pos) }.freeze
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def decide(pos)
|
|
190
|
+
if @proc
|
|
191
|
+
@proc.call(pos) ? true : false
|
|
192
|
+
else
|
|
193
|
+
@patterns.any? { |pattern| pattern_match?(pattern, pos) }
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
106
197
|
def extract_pos(morpheme_or_pos)
|
|
107
198
|
if morpheme_or_pos.is_a?(Array)
|
|
108
199
|
morpheme_or_pos
|
data/lib/kabosu/release.rake
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
namespace :release do
|
|
2
|
-
|
|
2
|
+
# The gem version is the sudachi.rs version plus an optional fourth part of
|
|
3
|
+
# kabosu's own (0.7.0.2). Cargo only takes three, so the crate keeps the
|
|
4
|
+
# sudachi.rs part.
|
|
5
|
+
desc "Bump version. Usage: rake release:bump[0.7.0.2]"
|
|
3
6
|
task :bump, [:version] do |_t, args|
|
|
4
7
|
version = args[:version]
|
|
5
|
-
abort "Usage: rake release:bump[
|
|
8
|
+
abort "Usage: rake release:bump[0.7.0.2]" unless version&.match?(/\A\d+\.\d+\.\d+(\.\d+)?\z/)
|
|
9
|
+
crate_version = version.split(".").first(3).join(".")
|
|
6
10
|
|
|
7
11
|
# Update lib/kabosu/version.rb
|
|
8
12
|
version_file = File.expand_path("version.rb", __dir__)
|
|
@@ -14,7 +18,7 @@ namespace :release do
|
|
|
14
18
|
# Update ext/kabosu/Cargo.toml
|
|
15
19
|
cargo_file = File.expand_path("../../ext/kabosu/Cargo.toml", __dir__)
|
|
16
20
|
content = File.read(cargo_file)
|
|
17
|
-
new_content = content.sub(/^version = ".*"/, %(version = "#{
|
|
21
|
+
new_content = content.sub(/^version = ".*"/, %(version = "#{crate_version}"))
|
|
18
22
|
File.write(cargo_file, new_content)
|
|
19
23
|
puts "Updated #{cargo_file}"
|
|
20
24
|
|
|
@@ -30,10 +34,3 @@ desc "Build the gem"
|
|
|
30
34
|
task :build do
|
|
31
35
|
sh "gem build kabosu.gemspec"
|
|
32
36
|
end
|
|
33
|
-
|
|
34
|
-
desc "Build and push the gem to RubyGems"
|
|
35
|
-
task release: :build do
|
|
36
|
-
gemfile = Dir["kabosu-*.gem"].max_by { |f| File.mtime(f) }
|
|
37
|
-
abort "No .gem file found" unless gemfile
|
|
38
|
-
sh "gem push #{gemfile}"
|
|
39
|
-
end
|
|
@@ -174,6 +174,6 @@ CYRILLIC 1 1 0
|
|
|
174
174
|
0x1F3FB..0x1F3FE ALL NOOOVBOW # emoji skin tone modifiers https://codepoints.net/U+1F3FF
|
|
175
175
|
|
|
176
176
|
# Combination marks
|
|
177
|
-
0x200C..0x200D ALL
|
|
177
|
+
0x200C..0x200D ALL NOOOVBOW NOOOVEOW # Zero Width Non-Joiner/Joiner
|
|
178
178
|
|
|
179
179
|
# END OF TABLE
|
data/lib/kabosu/tasks.rake
CHANGED
|
@@ -56,6 +56,19 @@ namespace :kabosu do
|
|
|
56
56
|
end
|
|
57
57
|
end
|
|
58
58
|
|
|
59
|
+
# ── User dictionaries ──
|
|
60
|
+
|
|
61
|
+
desc "Build a user dictionary: CSV=a.csv[,b.csv] OUTPUT=user.dic [SYSTEM_DICT=path] " \
|
|
62
|
+
"(defaults to the best installed dictionary)"
|
|
63
|
+
task :build_user_dict do
|
|
64
|
+
require "kabosu"
|
|
65
|
+
csv = ENV.fetch("CSV") { abort "CSV=path/to/lexicon.csv is required" }.split(",")
|
|
66
|
+
output = ENV.fetch("OUTPUT") { abort "OUTPUT=path/to/user.dic is required" }
|
|
67
|
+
system_dict = ENV.fetch("SYSTEM_DICT") { Kabosu::Dictionary.path }
|
|
68
|
+
Kabosu.build_user_dict(csv, output: output, system_dict: system_dict)
|
|
69
|
+
puts "Built #{output} against #{system_dict}"
|
|
70
|
+
end
|
|
71
|
+
|
|
59
72
|
# ── Info ──
|
|
60
73
|
|
|
61
74
|
desc "List installed Sudachi dictionaries"
|
|
@@ -70,7 +83,8 @@ namespace :kabosu do
|
|
|
70
83
|
puts
|
|
71
84
|
dicts.each do |d|
|
|
72
85
|
size_mb = (File.size(d[:path]).to_f / 1024 / 1024).round(1)
|
|
73
|
-
|
|
86
|
+
unreadable = manager.v1?(d[:path]) ? "" : " [pre-V1, unreadable by this kabosu]"
|
|
87
|
+
puts " #{d[:version]} / #{d[:edition].ljust(5)} (#{size_mb} MB)#{unreadable}"
|
|
74
88
|
puts " #{d[:path]}"
|
|
75
89
|
end
|
|
76
90
|
end
|
data/lib/kabosu/version.rb
CHANGED