Skip to main content

lindera_dictionary/loader/
prefix_dictionary.rs

1use std::path::Path;
2
3use crate::LinderaResult;
4use crate::dictionary::prefix_dictionary::PrefixDictionary;
5#[cfg(feature = "mmap")]
6use crate::util::mmap_file;
7use crate::util::read_file;
8
9/// Loader for prefix dictionary data from disk files.
10pub struct PrefixDictionaryLoader {}
11
12impl PrefixDictionaryLoader {
13    /// Load prefix dictionary from files in the specified directory.
14    ///
15    /// Reads dict.trie, dict.valsidx, dict.vals, dict.wordsidx and dict.words
16    /// and constructs a PrefixDictionary.
17    ///
18    /// # Arguments
19    ///
20    /// * `input_dir` - Path to the directory containing dictionary files.
21    ///
22    /// # Returns
23    ///
24    /// A `PrefixDictionary` loaded from the files.
25    pub fn load(input_dir: &Path) -> LinderaResult<PrefixDictionary> {
26        let trie_data = read_file(input_dir.join("dict.trie").as_path())?;
27        let vals_idx = read_file(input_dir.join("dict.valsidx").as_path())?;
28        let vals_data = read_file(input_dir.join("dict.vals").as_path())?;
29        let words_idx_data = read_file(input_dir.join("dict.wordsidx").as_path())?;
30        let words_data = read_file(input_dir.join("dict.words").as_path())?;
31
32        PrefixDictionary::load(trie_data, vals_idx, vals_data, words_idx_data, words_data)
33    }
34
35    /// Load prefix dictionary using memory-mapped files.
36    ///
37    /// Every component stays genuinely mmap-backed: the trie is walked in
38    /// place over its serialized bytes, and the values/word files are read
39    /// lazily at lookup time via `Data::Map`. Loading costs an O(1) header
40    /// check, no decode and no anonymous memory.
41    ///
42    /// # Arguments
43    ///
44    /// * `input_dir` - Path to the directory containing dictionary files.
45    ///
46    /// # Returns
47    ///
48    /// A `PrefixDictionary` loaded via memory mapping.
49    #[cfg(feature = "mmap")]
50    pub fn load_mmap(input_dir: &Path) -> LinderaResult<PrefixDictionary> {
51        let trie_data = mmap_file(input_dir.join("dict.trie").as_path())?;
52        let vals_idx = mmap_file(input_dir.join("dict.valsidx").as_path())?;
53        let vals_data = mmap_file(input_dir.join("dict.vals").as_path())?;
54        let words_idx_data = mmap_file(input_dir.join("dict.wordsidx").as_path())?;
55        let words_data = mmap_file(input_dir.join("dict.words").as_path())?;
56
57        PrefixDictionary::load(trie_data, vals_idx, vals_data, words_idx_data, words_data)
58    }
59}