lindera-dictionary 6.1.0

A morphological dictionary library.
Documentation
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use std::collections::BTreeMap;
use std::fs;
use std::fs::File;
use std::io;
use std::io::Read;
use std::io::Write;
use std::path::Path;

use byteorder::{LittleEndian, WriteBytesExt};
use csv::StringRecord;
use daachorse::DoubleArrayAhoCorasickBuilder;
use log::debug;

use crate::LinderaResult;
use crate::dictionary::UserDictionary;
use crate::dictionary::prefix_dictionary::UserPrefixDictionary;
use crate::error::LinderaErrorKind;
use crate::viterbi::WordEntry;

type StringRecordProcessor = Option<Box<dyn Fn(&StringRecord) -> LinderaResult<Vec<String>>>>;

pub struct UserDictionaryBuilder {
    user_dictionary_fields_num: usize,
    dictionary_fields_num: usize,
    default_word_cost: i16,
    default_left_context_id: u16,
    default_right_context_id: u16,
    flexible_csv: bool,
    user_dictionary_handler: StringRecordProcessor,
}

/// Options for [`UserDictionaryBuilder`]. Every field has a default, so
/// [`Self::builder`] is infallible. Setters take `self` by value to keep the
/// original owned-builder calling style.
#[derive(Default)]
pub struct UserDictionaryBuilderOptions {
    user_dictionary_fields_num: Option<usize>,
    dictionary_fields_num: Option<usize>,
    default_word_cost: Option<i16>,
    default_left_context_id: Option<u16>,
    default_right_context_id: Option<u16>,
    flexible_csv: Option<bool>,
    user_dictionary_handler: StringRecordProcessor,
}

impl UserDictionaryBuilderOptions {
    pub fn user_dictionary_fields_num(mut self, value: usize) -> Self {
        self.user_dictionary_fields_num = Some(value);
        self
    }

    pub fn dictionary_fields_num(mut self, value: usize) -> Self {
        self.dictionary_fields_num = Some(value);
        self
    }

    pub fn default_word_cost(mut self, value: i16) -> Self {
        self.default_word_cost = Some(value);
        self
    }

    pub fn default_left_context_id(mut self, value: u16) -> Self {
        self.default_left_context_id = Some(value);
        self
    }

    pub fn default_right_context_id(mut self, value: u16) -> Self {
        self.default_right_context_id = Some(value);
        self
    }

    pub fn flexible_csv(mut self, value: bool) -> Self {
        self.flexible_csv = Some(value);
        self
    }

    pub fn user_dictionary_handler(mut self, value: StringRecordProcessor) -> Self {
        self.user_dictionary_handler = value;
        self
    }

    pub fn builder(self) -> UserDictionaryBuilder {
        UserDictionaryBuilder {
            user_dictionary_fields_num: self.user_dictionary_fields_num.unwrap_or(3),
            dictionary_fields_num: self.dictionary_fields_num.unwrap_or(12),
            default_word_cost: self.default_word_cost.unwrap_or(-10000),
            default_left_context_id: self.default_left_context_id.unwrap_or(0),
            default_right_context_id: self.default_right_context_id.unwrap_or(0),
            flexible_csv: self.flexible_csv.unwrap_or(true),
            user_dictionary_handler: self.user_dictionary_handler,
        }
    }
}

impl UserDictionaryBuilder {
    /// Builds a user dictionary from a CSV file on disk.
    ///
    /// # 引数
    ///
    /// * `input_file` - Path of the user dictionary CSV file.
    ///
    /// # 戻り値
    ///
    /// The built user dictionary.
    pub fn build(&self, input_file: &Path) -> LinderaResult<UserDictionary> {
        debug!("reading {input_file:?}");

        let file = File::open(input_file).map_err(|err| {
            LinderaErrorKind::Io
                .with_error(anyhow::anyhow!(err))
                .add_context(format!(
                    "Failed to open user dictionary CSV file: {input_file:?}"
                ))
        })?;
        self.build_from_reader(file)
    }

    /// Builds a user dictionary from CSV content supplied by a reader.
    ///
    /// This is the filesystem-free entry point: WebAssembly callers feed it
    /// bytes obtained in JavaScript (fetch, file inputs, OPFS), and
    /// [`Self::build`] delegates here after opening its file (#972).
    /// The content must be UTF-8.
    ///
    /// # 引数
    ///
    /// * `reader` - Read source of the user dictionary CSV content.
    ///
    /// # 戻り値
    ///
    /// The built user dictionary.
    pub fn build_from_reader<R: Read>(&self, reader: R) -> LinderaResult<UserDictionary> {
        let mut rdr = csv::ReaderBuilder::new()
            .has_headers(false)
            .flexible(self.flexible_csv)
            .from_reader(reader);

        let mut rows: Vec<StringRecord> = vec![];
        for (line_num, result) in rdr.records().enumerate() {
            let record = result.map_err(|err| {
                LinderaErrorKind::Content
                    .with_error(anyhow::anyhow!(err))
                    .add_context(format!(
                        "Failed to parse CSV record at line {}",
                        line_num + 1,
                    ))
            })?;
            rows.push(record);
        }

        // Classify row arity before anything indexes into a row: with
        // `flexible` CSV parsing a 1- or 2-field row would otherwise reach
        // the cost/context-id column accesses below and panic out of bounds
        // -- and a panic aborts the module on wasm32, where this builder is
        // fed untrusted browser input (#972). Runs before the sort so the
        // reported row number matches the input order.
        for (row_id, row) in rows.iter().enumerate() {
            if row.len() != self.user_dictionary_fields_num
                && row.len() < self.dictionary_fields_num
            {
                return Err(LinderaErrorKind::Content
                    .with_error(anyhow::anyhow!(
                        "user dictionary should be a CSV with {} or {}+ fields",
                        self.user_dictionary_fields_num,
                        self.dictionary_fields_num
                    ))
                    .add_context(format!(
                        "Row {} has {} fields (surface: '{}')",
                        row_id + 1,
                        row.len(),
                        row.get(0).unwrap_or("<empty>")
                    )));
            }
        }

        // The cached variant computes the allocating surface key once per row
        // instead of once per comparison.
        rows.sort_by_cached_key(|row| row[0].to_string());

        let mut word_entry_map: BTreeMap<String, Vec<WordEntry>> = BTreeMap::new();

        for (row_id, row) in rows.iter().enumerate() {
            let surface = row[0].to_string();
            let word_cost = if row.len() == self.user_dictionary_fields_num {
                self.default_word_cost
            } else {
                row[3].parse::<i16>().map_err(|_err| {
                    LinderaErrorKind::Parse
                        .with_error(anyhow::anyhow!("failed to parse word cost"))
                        .add_context(format!(
                            "Invalid word cost '{}' at row {} (surface: '{}')",
                            &row[3],
                            row_id + 1,
                            &row[0]
                        ))
                })?
            };
            let (left_id, right_id) = if row.len() == self.user_dictionary_fields_num {
                (self.default_left_context_id, self.default_right_context_id)
            } else {
                (
                    row[1].parse::<u16>().map_err(|_err| {
                        LinderaErrorKind::Parse
                            .with_error(anyhow::anyhow!("failed to parse left context id"))
                            .add_context(format!(
                                "Invalid left context ID '{}' at row {} (surface: '{}')",
                                &row[1],
                                row_id + 1,
                                &row[0]
                            ))
                    })?,
                    row[2].parse::<u16>().map_err(|_err| {
                        LinderaErrorKind::Parse
                            .with_error(anyhow::anyhow!("failed to parse right context id"))
                            .add_context(format!(
                                "Invalid right context ID '{}' at row {} (surface: '{}')",
                                &row[2],
                                row_id + 1,
                                &row[0]
                            ))
                    })?,
                )
            };

            word_entry_map
                .entry(surface)
                .or_default()
                .push(WordEntry::new(
                    crate::viterbi::WordId::new(crate::viterbi::LexType::User, row_id as u32),
                    word_cost,
                    left_id,
                    right_id,
                ));
        }

        let mut words_data = Vec::<u8>::new();
        let mut words_idx_data = Vec::<u8>::new();
        for (row_id, row) in rows.iter().enumerate() {
            let word_detail = if row.len() == self.user_dictionary_fields_num {
                if let Some(handler) = &self.user_dictionary_handler {
                    handler(row)?
                } else {
                    row.iter()
                        .skip(1)
                        .map(|s| s.to_string())
                        .collect::<Vec<String>>()
                }
            } else if row.len() >= self.dictionary_fields_num {
                let mut tmp_word_detail = Vec::new();
                for item in row.iter().skip(4) {
                    tmp_word_detail.push(item.to_string());
                }
                tmp_word_detail
            } else {
                return Err(LinderaErrorKind::Content
                    .with_error(anyhow::anyhow!(
                        "user dictionary should be a CSV with {} or {}+ fields",
                        self.user_dictionary_fields_num,
                        self.dictionary_fields_num
                    ))
                    .add_context(format!(
                        "Row {} has {} fields (surface: '{}')",
                        row_id + 1,
                        row.len(),
                        row.get(0).unwrap_or("<empty>")
                    )));
            };

            let offset = words_data.len();
            words_idx_data
                .write_u32::<LittleEndian>(offset as u32)
                .map_err(|err| {
                    LinderaErrorKind::Io
                        .with_error(anyhow::anyhow!(err))
                        .add_context("Failed to write word offset to user dictionary words index")
                })?;

            // Store word details as null-separated string (like main dictionary)
            let joined_details = word_detail.join("\0");
            let joined_details_len = u32::try_from(joined_details.len()).map_err(|err| {
                LinderaErrorKind::Serialize
                    .with_error(anyhow::anyhow!(err))
                    .add_context(format!(
                        "Word details length too large: {} bytes for word '{}'",
                        joined_details.len(),
                        row.get(0).unwrap_or("<unknown>")
                    ))
            })?;

            words_data
                .write_u32::<LittleEndian>(joined_details_len)
                .map_err(|err| {
                    LinderaErrorKind::Serialize
                        .with_error(anyhow::anyhow!(err))
                        .add_context(
                            "Failed to write word details length to user dictionary words data",
                        )
                })?;
            words_data
                .write_all(joined_details.as_bytes())
                .map_err(|err| {
                    LinderaErrorKind::Serialize
                        .with_error(anyhow::anyhow!(err))
                        .add_context("Failed to write word details to user dictionary words data")
                })?;
        }

        let mut id = 0u32;

        // building double array trie
        let mut keyset: Vec<(&[u8], u32)> = vec![];
        for (key, word_entries) in &word_entry_map {
            let len = word_entries.len() as u32;
            // 24bit for word ID, 8bit for variant count (up to 255 per surface),
            // matching the system dictionary encoding. The legacy 5-bit user
            // encoding (max 31 variants) was retired in v4.0.0; user dictionary
            // `.bin` files built with v3 must be rebuilt from their CSV source.
            let val = crate::builder::prefix_dictionary::pack_entry_value(key, id, len)?;
            keyset.push((key.as_bytes(), val));
            id += len;
        }
        let da_bytes = DoubleArrayAhoCorasickBuilder::new()
            .build_with_values(keyset)
            .map_err(|err| {
                LinderaErrorKind::Build
                    .with_error(anyhow::anyhow!(err))
                    .add_context("Failed to build DoubleArray for user dictionary")
            })?
            .serialize();

        // building values
        let mut vals_data = Vec::<u8>::new();
        for word_entries in word_entry_map.values() {
            for word_entry in word_entries {
                word_entry.serialize(&mut vals_data).map_err(|err| {
                    LinderaErrorKind::Serialize
                        .with_error(anyhow::anyhow!(err))
                        .add_context(format!(
                            "Failed to serialize user dictionary word entry (id: {})",
                            word_entry.word_id().id()
                        ))
                })?;
            }
        }

        let dict = UserPrefixDictionary::load(da_bytes, vals_data, words_idx_data, words_data)?;

        Ok(UserDictionary { dict })
    }
}

pub fn build_user_dictionary(user_dict: UserDictionary, output_file: &Path) -> LinderaResult<()> {
    let parent_dir = match output_file.parent() {
        Some(parent_dir) => parent_dir,
        None => {
            return Err(LinderaErrorKind::Io
                .with_error(anyhow::anyhow!(
                    "failed to get parent directory of output file"
                ))
                .add_context(format!("Invalid output file path: {output_file:?}")));
        }
    };
    fs::create_dir_all(parent_dir).map_err(|err| {
        LinderaErrorKind::Io
            .with_error(anyhow::anyhow!(err))
            .add_context(format!("Failed to create parent directory: {parent_dir:?}"))
    })?;

    let mut wtr = io::BufWriter::new(File::create(output_file).map_err(|err| {
        LinderaErrorKind::Io
            .with_error(anyhow::anyhow!(err))
            .add_context(format!(
                "Failed to create user dictionary output file: {output_file:?}"
            ))
    })?);
    let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&user_dict).map_err(|err| {
        LinderaErrorKind::Serialize
            .with_error(anyhow::anyhow!(err))
            .add_context(format!(
                "Failed to serialize user dictionary to file: {output_file:?}"
            ))
    })?;
    wtr.write_all(&bytes).map_err(|err| {
        LinderaErrorKind::Io
            .with_error(anyhow::anyhow!(err))
            .add_context(format!(
                "Failed to write user dictionary to file: {output_file:?}"
            ))
    })?;
    wtr.flush().map_err(|err| {
        LinderaErrorKind::Io
            .with_error(anyhow::anyhow!(err))
            .add_context(format!(
                "Failed to flush user dictionary output file: {output_file:?}"
            ))
    })?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use crate::builder::DictionaryBuilder;
    use crate::dictionary::metadata::Metadata;

    fn fixture_path(name: &str) -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("../resources/user_dict")
            .join(name)
    }

    fn flexible_builder() -> DictionaryBuilder {
        let metadata = Metadata {
            flexible_csv: true,
            ..Metadata::default()
        };
        DictionaryBuilder::new(metadata)
    }

    /// The reader-based build must produce a byte-identical dictionary to the
    /// path-based build over the same CSV (#972).
    #[test]
    fn reader_and_path_builds_are_identical() {
        let path = fixture_path("ipadic_simple_userdic.csv");
        let builder = flexible_builder();

        let from_path = match builder.build_user_dict(&path) {
            Ok(dict) => dict,
            Err(err) => panic!("path build failed: {err}"),
        };
        let bytes = match std::fs::read(&path) {
            Ok(bytes) => bytes,
            Err(err) => panic!("failed to read fixture: {err}"),
        };
        let from_reader = match builder.build_user_dict_from_reader(bytes.as_slice()) {
            Ok(dict) => dict,
            Err(err) => panic!("reader build failed: {err}"),
        };

        let path_bytes = match rkyv::to_bytes::<rkyv::rancor::Error>(&from_path) {
            Ok(bytes) => bytes,
            Err(err) => panic!("serialization failed: {err}"),
        };
        let reader_bytes = match rkyv::to_bytes::<rkyv::rancor::Error>(&from_reader) {
            Ok(bytes) => bytes,
            Err(err) => panic!("serialization failed: {err}"),
        };
        assert_eq!(path_bytes.as_slice(), reader_bytes.as_slice());
    }

    /// A CSV mixing simple (3-field) and detailed (13-field) rows builds
    /// under flexible parsing. First test to exercise the mixed fixture.
    #[test]
    fn mixed_simple_and_detailed_rows_build() {
        let bytes = match std::fs::read(fixture_path("ipadic_mixed_userdic.csv")) {
            Ok(bytes) => bytes,
            Err(err) => panic!("failed to read fixture: {err}"),
        };
        let dict = match flexible_builder().build_user_dict_from_reader(bytes.as_slice()) {
            Ok(dict) => dict,
            Err(err) => panic!("mixed build failed: {err}"),
        };
        assert!(!dict.dict.vals_data.is_empty());
    }

    /// Rows shorter than the simple format must produce an error, not an
    /// out-of-bounds panic: with flexible CSV parsing such rows previously
    /// reached the cost-column access before any arity check, and on wasm32
    /// a panic aborts the module (#972).
    #[test]
    fn short_rows_error_instead_of_panicking() {
        for csv in ["東京\n", "東京,1288\n"] {
            let result = flexible_builder().build_user_dict_from_reader(csv.as_bytes());
            let err = match result {
                Ok(_) => panic!("{csv:?} must not build"),
                Err(err) => format!("{err:?}"),
            };
            assert!(
                err.contains("user dictionary should be a CSV with 3 or 13+ fields"),
                "unexpected error for {csv:?}: {err}"
            );
        }
    }
}