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.da, dict.vals, dict.wordsidx, and dict.words files
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 da_data = read_file(input_dir.join("dict.da").as_path())?;
27        let vals_data = read_file(input_dir.join("dict.vals").as_path())?;
28        let words_idx_data = read_file(input_dir.join("dict.wordsidx").as_path())?;
29        let words_data = read_file(input_dir.join("dict.words").as_path())?;
30
31        PrefixDictionary::load(da_data, vals_data, words_idx_data, words_data, true)
32    }
33
34    /// Load prefix dictionary using memory-mapped files.
35    ///
36    /// # Arguments
37    ///
38    /// * `input_dir` - Path to the directory containing dictionary files.
39    ///
40    /// # Returns
41    ///
42    /// A `PrefixDictionary` loaded via memory mapping.
43    #[cfg(feature = "mmap")]
44    pub fn load_mmap(input_dir: &Path) -> LinderaResult<PrefixDictionary> {
45        let da_data = mmap_file(input_dir.join("dict.da").as_path())?;
46        let vals_data = mmap_file(input_dir.join("dict.vals").as_path())?;
47        let words_idx_data = mmap_file(input_dir.join("dict.wordsidx").as_path())?;
48        let words_data = mmap_file(input_dir.join("dict.words").as_path())?;
49
50        PrefixDictionary::load(da_data, vals_data, words_idx_data, words_data, true)
51    }
52}