use std::path::Path;
use crate::LinderaResult;
use crate::builder::DictionaryBuilder;
use crate::dictionary::UserDictionary;
use crate::util::read_file;
pub struct UserDictionaryLoader;
impl UserDictionaryLoader {
pub fn load_from_bin<P: AsRef<Path>>(path: P) -> LinderaResult<UserDictionary> {
let data = read_file(path.as_ref())?;
UserDictionary::load(&data)
}
pub fn load_from_csv<P: AsRef<Path>>(
builder: DictionaryBuilder,
path: P,
) -> LinderaResult<UserDictionary> {
builder.build_user_dict(path.as_ref())
}
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::UserDictionaryLoader;
#[test]
fn test_load_all_prebuilt_bin_fixtures() {
let user_dict_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("../resources")
.join("user_dict");
for name in [
"ipadic_simple_userdic.bin",
"unidic_simple_userdic.bin",
"cc-cedict_simple_userdic.bin",
"jieba_simple_userdic.bin",
"ko-dic_simple_userdic.bin",
] {
let result = UserDictionaryLoader::load_from_bin(user_dict_dir.join(name));
assert!(
result.is_ok(),
"failed to load prebuilt user dictionary fixture {name}: {:?}",
result.err()
);
}
}
}