Skip to main content

lindera_dictionary/
loader.rs

1pub mod character_definition;
2pub mod connection_cost_matrix;
3pub mod metadata;
4pub mod prefix_dictionary;
5pub mod unknown_dictionary;
6pub mod user_dictionary;
7
8use std::path::Path;
9
10use crate::LinderaResult;
11use crate::dictionary::Dictionary;
12use crate::error::LinderaErrorKind;
13
14/// Common trait for all dictionary loaders (both external and embedded)
15pub trait DictionaryLoader {
16    /// Load dictionary from configured location or embedded data
17    fn load(&self) -> LinderaResult<Dictionary> {
18        Err(LinderaErrorKind::Io.with_error(anyhow::anyhow!(
19            "This loader does not support load function"
20        )))
21    }
22
23    /// Load dictionary from a specific path (optional for embedded loaders)
24    fn load_from_path(&self, path: &Path) -> LinderaResult<Dictionary> {
25        let _ = path;
26        Err(LinderaErrorKind::Io.with_error(anyhow::anyhow!(
27            "This loader does not support load_from_path function"
28        )))
29    }
30}
31
32pub struct FSDictionaryLoader;
33
34impl Default for FSDictionaryLoader {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40impl FSDictionaryLoader {
41    pub fn new() -> Self {
42        Self
43    }
44
45    /// Load a dictionary from a directory, always doing a plain file read.
46    ///
47    /// # Arguments
48    ///
49    /// * `dict_path` - Path to the directory containing dictionary files.
50    ///
51    /// # Returns
52    ///
53    /// A `Dictionary`, or an error if loading fails.
54    pub fn load_from_path<P: AsRef<Path>>(&self, dict_path: P) -> LinderaResult<Dictionary> {
55        Dictionary::load_from_path(dict_path.as_ref())
56    }
57
58    /// Load a dictionary from a directory, optionally via memory-mapped
59    /// reads. See [`Dictionary::load_from_path_with_options`] for exactly
60    /// which components `use_mmap` does and does not make lazy.
61    ///
62    /// # Arguments
63    ///
64    /// * `dict_path` - Path to the directory containing dictionary files.
65    /// * `use_mmap` - Whether to route the connection-cost matrix and
66    ///   prefix dictionary through memory-mapped reads.
67    ///
68    /// # Returns
69    ///
70    /// A `Dictionary`, or an error if loading fails.
71    pub fn load_from_path_with_options<P: AsRef<Path>>(
72        &self,
73        dict_path: P,
74        use_mmap: bool,
75    ) -> LinderaResult<Dictionary> {
76        Dictionary::load_from_path_with_options(dict_path.as_ref(), use_mmap)
77    }
78}
79
80impl DictionaryLoader for FSDictionaryLoader {
81    fn load_from_path(&self, dict_path: &Path) -> LinderaResult<Dictionary> {
82        Dictionary::load_from_path(dict_path)
83    }
84}