Skip to main content

lindera_dictionary/loader/
user_dictionary.rs

1use std::path::Path;
2
3use crate::LinderaResult;
4use crate::builder::DictionaryBuilder;
5use crate::dictionary::UserDictionary;
6use crate::util::read_file;
7
8/// Loader for user dictionaries with support for different input formats
9pub struct UserDictionaryLoader;
10
11impl UserDictionaryLoader {
12    /// Load user dictionary from a binary (.bin) file
13    pub fn load_from_bin<P: AsRef<Path>>(path: P) -> LinderaResult<UserDictionary> {
14        let data = read_file(path.as_ref())?;
15        UserDictionary::load(&data)
16    }
17
18    /// Load user dictionary from a CSV file
19    /// Requires a DictionaryBuilder to build the user dictionary from CSV format
20    pub fn load_from_csv<P: AsRef<Path>>(
21        builder: DictionaryBuilder,
22        path: P,
23    ) -> LinderaResult<UserDictionary> {
24        builder.build_user_dict(path.as_ref())
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use std::path::PathBuf;
31
32    use super::UserDictionaryLoader;
33
34    /// Every checked-in prebuilt user dictionary fixture must stay loadable.
35    /// A serialization format change in a dependency (e.g. the daachorse 4.0
36    /// automaton format change) invalidates these binaries; this test makes
37    /// such breakage visible for all fixtures, not just the ones used by
38    /// other tests.
39    #[test]
40    fn test_load_all_prebuilt_bin_fixtures() {
41        let user_dict_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
42            .join("../resources")
43            .join("user_dict");
44
45        for name in [
46            "ipadic_simple_userdic.bin",
47            "unidic_simple_userdic.bin",
48            "cc-cedict_simple_userdic.bin",
49            "jieba_simple_userdic.bin",
50            "ko-dic_simple_userdic.bin",
51        ] {
52            let result = UserDictionaryLoader::load_from_bin(user_dict_dir.join(name));
53            assert!(
54                result.is_ok(),
55                "failed to load prebuilt user dictionary fixture {name}: {:?}",
56                result.err()
57            );
58        }
59    }
60}