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        Ok(PrefixDictionary::load(
32            da_data,
33            vals_data,
34            words_idx_data,
35            words_data,
36            true,
37        ))
38    }
39
40    /// Load prefix dictionary using memory-mapped files.
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 da_data = mmap_file(input_dir.join("dict.da").as_path())?;
52        let vals_data = mmap_file(input_dir.join("dict.vals").as_path())?;
53        let words_idx_data = mmap_file(input_dir.join("dict.wordsidx").as_path())?;
54        let words_data = mmap_file(input_dir.join("dict.words").as_path())?;
55
56        Ok(PrefixDictionary::load(
57            da_data,
58            vals_data,
59            words_idx_data,
60            words_data,
61            true,
62        ))
63    }
64}