1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
use daachorse::DoubleArrayAhoCorasick;
use rkyv::rancor::{Fallible, Source};
use rkyv::with::{ArchiveWith, DeserializeWith, SerializeWith};
use rkyv::{Archive, Deserialize as RkyvDeserialize, Place, Serialize as RkyvSerialize};
use serde::{Deserialize, Serialize};
use crate::{LinderaResult, error::LinderaErrorKind, util::Data, viterbi::WordEntry};
/// Match structure for common prefix iterator compatibility
#[derive(Debug, Clone)]
pub struct Match {
pub word_idx: WordIdx,
pub end_char: usize,
}
#[derive(Debug, Clone, Copy)]
pub struct WordIdx {
pub word_id: u32,
}
impl WordIdx {
pub fn new(word_id: u32) -> Self {
Self { word_id }
}
}
pub struct DoubleArrayArchiver;
impl ArchiveWith<DoubleArrayAhoCorasick<u32>> for DoubleArrayArchiver {
type Archived = rkyv::vec::ArchivedVec<u8>;
type Resolver = rkyv::vec::VecResolver;
fn resolve_with(
field: &DoubleArrayAhoCorasick<u32>,
resolver: Self::Resolver,
out: Place<Self::Archived>,
) {
let bytes = field.serialize();
rkyv::vec::ArchivedVec::resolve_from_slice(&bytes, resolver, out);
}
}
impl<S: Fallible + rkyv::ser::Writer + rkyv::ser::Allocator + ?Sized>
SerializeWith<DoubleArrayAhoCorasick<u32>, S> for DoubleArrayArchiver
{
fn serialize_with(
field: &DoubleArrayAhoCorasick<u32>,
serializer: &mut S,
) -> Result<Self::Resolver, S::Error> {
let bytes = field.serialize();
rkyv::vec::ArchivedVec::serialize_from_slice(&bytes, serializer)
}
}
impl<D: Fallible<Error: Source> + ?Sized>
DeserializeWith<rkyv::vec::ArchivedVec<u8>, DoubleArrayAhoCorasick<u32>, D>
for DoubleArrayArchiver
{
/// Deserialize the archived byte vector into a `DoubleArrayAhoCorasick`.
///
/// # Returns
///
/// The deserialized `DoubleArrayAhoCorasick`, or an error if deserialization fails.
fn deserialize_with(
archived: &rkyv::vec::ArchivedVec<u8>,
_deserializer: &mut D,
) -> Result<DoubleArrayAhoCorasick<u32>, D::Error> {
let (da, _) = DoubleArrayAhoCorasick::deserialize(archived.as_slice()).map_err(|err| {
D::Error::new(std::io::Error::new(
std::io::ErrorKind::InvalidData,
err.to_string(),
))
})?;
Ok(da)
}
}
mod double_array_serde {
use daachorse::DoubleArrayAhoCorasick;
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S>(da: &DoubleArrayAhoCorasick<u32>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let bytes = da.serialize();
serializer.serialize_bytes(&bytes)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<DoubleArrayAhoCorasick<u32>, D::Error>
where
D: Deserializer<'de>,
{
let bytes: Vec<u8> = Deserialize::deserialize(deserializer)?;
let (da, _) = DoubleArrayAhoCorasick::deserialize(&bytes)
.map_err(|err| serde::de::Error::custom(err.to_string()))?;
Ok(da)
}
}
#[derive(Clone, Serialize, Deserialize, Archive, RkyvSerialize, RkyvDeserialize)]
pub struct PrefixDictionary {
#[serde(with = "self::double_array_serde")]
#[rkyv(with = DoubleArrayArchiver)]
pub da: DoubleArrayAhoCorasick<u32>,
pub vals_data: Data,
pub words_idx_data: Data,
pub words_data: Data,
pub is_system: bool,
}
impl PrefixDictionary {
/// Decode the `(offset, count)` pair stored in the double-array value.
///
/// System dictionaries use 8-bit count (supports up to 255 variants per
/// surface), while user dictionaries retain the legacy 5-bit count
/// encoding (max 31) for binary backward compatibility with pre-built
/// `.bin` user dictionary files.
#[inline]
pub(crate) fn decode_val(&self, val: u32) -> (u32, u32) {
if self.is_system {
(val >> 8u32, val & ((1u32 << 8) - 1u32))
} else {
(val >> 5u32, val & ((1u32 << 5) - 1u32))
}
}
/// Load a `PrefixDictionary` from raw binary data.
///
/// # Arguments
///
/// * `da_data` - Double-array data bytes.
/// * `vals_data` - Values data bytes.
/// * `words_idx_data` - Word index data bytes.
/// * `words_data` - Words data bytes.
/// * `is_system` - Whether this is a system dictionary.
///
/// # Returns
///
/// A `PrefixDictionary`, or an error if deserialization fails.
pub fn load(
da_data: impl Into<Data>,
vals_data: impl Into<Data>,
words_idx_data: impl Into<Data>,
words_data: impl Into<Data>,
is_system: bool,
) -> LinderaResult<PrefixDictionary> {
let da_bytes = da_data.into();
let (da, _) = DoubleArrayAhoCorasick::deserialize(&da_bytes[..]).map_err(|err| {
LinderaErrorKind::Deserialize.with_error(anyhow::anyhow!(err.to_string()))
})?;
Ok(PrefixDictionary {
da,
vals_data: vals_data.into(),
words_idx_data: words_idx_data.into(),
words_data: words_data.into(),
is_system,
})
}
pub fn prefix<'a>(&'a self, s: &'a str) -> impl Iterator<Item = (usize, WordEntry)> + 'a {
self.da
.find_overlapping_iter(s)
.filter(|m| m.start() == 0)
.flat_map(move |m| {
let (offset, len) = self.decode_val(m.value());
let offset_bytes = (offset as usize) * WordEntry::SERIALIZED_LEN;
let data: &[u8] = &self.vals_data[offset_bytes..];
(0..len as usize).map(move |i| {
(
m.end(),
WordEntry::deserialize(
&data[WordEntry::SERIALIZED_LEN * i..],
self.is_system,
),
)
})
})
}
/// Find `WordEntry`s with surface
pub fn find_surface(&self, surface: &str) -> Vec<WordEntry> {
self.find_surface_iter(surface).collect()
}
/// Find `WordEntry`s with surface using lazy evaluation
/// This iterator-based approach reduces memory allocations
pub fn find_surface_iter<'a>(
&'a self,
surface: &'a str,
) -> impl Iterator<Item = WordEntry> + 'a {
self.da
.find_overlapping_iter(surface)
.filter(|m| m.start() == 0 && m.end() == surface.len())
.flat_map(move |m| {
let (offset, len) = self.decode_val(m.value());
let offset_bytes = (offset as usize) * WordEntry::SERIALIZED_LEN;
let data = &self.vals_data[offset_bytes..];
(0..len as usize).map(move |i| {
WordEntry::deserialize(&data[WordEntry::SERIALIZED_LEN * i..], self.is_system)
})
})
}
/// Common prefix iterator using character array input
pub fn common_prefix_iterator(&self, suffix: &[char]) -> Vec<Match> {
// Warning: This method takes &[char], but daachorse works on bytes (str).
// Converting char slice to string is costly but necessary if we use daachorse standard API.
if self.vals_data.is_empty() {
return Vec::new();
}
let suffix_str: String = suffix.iter().collect();
self.da
.find_overlapping_iter(&suffix_str)
.filter(|m| m.start() == 0)
.flat_map(|m| {
let (offset, len) = self.decode_val(m.value());
let offset_bytes = (offset as usize) * WordEntry::SERIALIZED_LEN;
// 範囲チェックを追加
if offset_bytes >= self.vals_data.len() {
return vec![].into_iter();
}
let data: &[u8] = &self.vals_data[offset_bytes..];
(0..len as usize)
.filter_map(move |i| {
let required_bytes = WordEntry::SERIALIZED_LEN * (i + 1);
if required_bytes <= data.len() {
let word_entry = WordEntry::deserialize(
&data[WordEntry::SERIALIZED_LEN * i..],
self.is_system,
);
Some(Match {
word_idx: WordIdx::new(word_entry.word_id.id),
end_char: m.end(), // prefix_len in bytes? No, m.end() is byte index.
// Match expects char length?
// Original code: end_char: prefix_len
// prefix_len was number of bytes or chars?
// yada::common_prefix_search returns (val, len) where len is length in bytes?
// yada common_prefix_search(str) returns length in bytes.
// But common_prefix_iterator takes &[char].
// Match.end_char usually implies character index if used for Viterbi on chars.
// But Viterbi usually works on bytes in Lindera?
// Let's check typical usage.
// NOTE: daachorse returns byte indices.
// If input was chars converted to String, byte index != char index.
// We need to map back to char index?
// This function common_prefix_iterator might be inefficient or deprecated given we move to byte-based Viterbi.
// For now, let's assume we return byte length.
// But wait, suffix is &[char].
// The caller likely expects char length?
// Yes. if suffix is &[char], end_char 3 means 3 chars.
// We have byte length from daachorse.
// We need to count chars in suffix_str[..m.end()].
// This is inefficient.
})
} else {
None
}
})
.collect::<Vec<_>>()
.into_iter()
})
.collect()
}
}
impl ArchivedPrefixDictionary {
/// Decode the `(offset, count)` pair. See [`PrefixDictionary::decode_val`].
#[inline]
fn decode_val(&self, val: u32) -> (u32, u32) {
if self.is_system {
(val >> 8u32, val & ((1u32 << 8) - 1u32))
} else {
(val >> 5u32, val & ((1u32 << 5) - 1u32))
}
}
/// Find all prefix matches for the given string using the archived dictionary.
///
/// # Arguments
///
/// * `s` - The input string to search for prefix matches.
///
/// # Returns
///
/// An iterator of `(end_position, WordEntry)` pairs, or an error if deserialization fails.
pub fn prefix<'a>(
&'a self,
s: &'a str,
) -> LinderaResult<impl Iterator<Item = (usize, WordEntry)> + 'a> {
// Deserialize on the fly. Performance warning: this is slow.
let (da, _) =
DoubleArrayAhoCorasick::<u32>::deserialize(self.da.as_slice()).map_err(|err| {
LinderaErrorKind::Deserialize.with_error(anyhow::anyhow!(err.to_string()))
})?;
let matches: Vec<_> = da
.find_overlapping_iter(s)
.filter(|m| m.start() == 0)
.map(|m| (m.end(), m.value()))
.collect();
Ok(matches.into_iter().flat_map(move |(end, offset_len)| {
let (offset, len) = self.decode_val(offset_len);
let offset_bytes = (offset as usize) * WordEntry::SERIALIZED_LEN;
let vals = self.vals_data.as_slice();
if offset_bytes >= vals.len() {
return vec![].into_iter();
}
let data = &vals[offset_bytes..];
(0..len as usize)
.map(move |i| {
(
end,
WordEntry::deserialize(
&data[WordEntry::SERIALIZED_LEN * i..],
self.is_system,
),
)
})
.collect::<Vec<_>>()
.into_iter()
}))
}
/// Find `WordEntry`s matching the exact surface in the archived dictionary.
///
/// # Arguments
///
/// * `surface` - The surface string to search for.
///
/// # Returns
///
/// A vector of matching `WordEntry`s, or an error if deserialization fails.
pub fn find_surface(&self, surface: &str) -> LinderaResult<Vec<WordEntry>> {
let (da, _) =
DoubleArrayAhoCorasick::<u32>::deserialize(self.da.as_slice()).map_err(|err| {
LinderaErrorKind::Deserialize.with_error(anyhow::anyhow!(err.to_string()))
})?;
let matches: Vec<_> = da
.find_overlapping_iter(surface)
.filter(|m| m.start() == 0 && m.end() == surface.len())
.map(|m| m.value())
.collect();
Ok(matches
.into_iter()
.flat_map(|offset_len| {
let (offset, len) = self.decode_val(offset_len);
let offset_bytes = (offset as usize) * WordEntry::SERIALIZED_LEN;
let vals = self.vals_data.as_slice();
if offset_bytes >= vals.len() {
return Vec::new().into_iter();
}
let data = &vals[offset_bytes..];
(0..len as usize)
.map(|i| {
WordEntry::deserialize(
&data[WordEntry::SERIALIZED_LEN * i..],
self.is_system,
)
})
.collect::<Vec<_>>()
.into_iter()
})
.collect())
}
}