Skip to content

Commit

Permalink
feat(dict)!: break system dict loader to load base and extra separately
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Dec 27, 2024
1 parent 33c4144 commit 95f6eeb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
Binary file modified capi/data/mini.dat
Binary file not shown.
4 changes: 3 additions & 1 deletion capi/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub unsafe extern "C" fn chewing_new2(
vec![Box::new(builtin.unwrap()) as Box<dyn Dictionary>]
}
};
let extra_dicts = sys_loader.load_extra().unwrap_or_default();
let abbrev = sys_loader.load_abbrev();
let abbrev = match abbrev {
Ok(abbr) => abbr,
Expand Down Expand Up @@ -166,7 +167,8 @@ pub unsafe extern "C" fn chewing_new2(

let estimate = LaxUserFreqEstimate::max_from(user_dictionary.as_ref());

let dict = Layered::new(dictionaries, user_dictionary);
let system_dicts = Vec::from_iter(dictionaries.into_iter().chain(extra_dicts.into_iter()));
let dict = Layered::new(system_dicts, user_dictionary);
let conversion_engine = Box::new(ChewingEngine::new());
let kb_compat = KeyboardLayoutCompat::Default;
let keyboard = AnyKeyboardLayout::Qwerty(Qwerty);
Expand Down
10 changes: 10 additions & 0 deletions src/dictionary/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,17 @@ impl SystemDictionaryLoader {
let tsi_dict = Trie::open(tsi_dict_path).map_err(io_err)?;
results.push(Box::new(tsi_dict));

Ok(results)
}
/// Searches and loads the extra dictionaries.
pub fn load_extra(&self) -> Result<Vec<Box<dyn Dictionary>>, LoadDictionaryError> {
let search_path = if let Some(sys_path) = &self.sys_path {
sys_path.to_owned()
} else {
sys_path_from_env_var()
};
let extra_files = find_extra_dat_by_path(&search_path);
let mut results: Vec<Box<dyn Dictionary>> = vec![];
for path in extra_files {
info!("Loading {}", path.display());
match Trie::open(&path) {
Expand Down
9 changes: 6 additions & 3 deletions src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,16 @@ pub(crate) struct SharedState {

impl Editor {
pub fn chewing() -> Result<Editor, Box<dyn Error>> {
let system_dict = SystemDictionaryLoader::new().load()?;
let sys_loader = SystemDictionaryLoader::new();
let base_dict = sys_loader.load()?;
let extra_dict = sys_loader.load_extra()?;
let system_dict = Vec::from_iter(base_dict.into_iter().chain(extra_dict.into_iter()));

Check warning on line 197 in src/editor/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/editor/mod.rs#L194-L197

Added lines #L194 - L197 were not covered by tests
let user_dict = UserDictionaryLoader::new().load()?;
let estimate = LaxUserFreqEstimate::max_from(user_dict.as_ref());
let dict = Layered::new(system_dict, user_dict);
let conversion_engine = Box::new(ChewingEngine::new());
let abbrev = SystemDictionaryLoader::new().load_abbrev()?;
let sym_sel = SystemDictionaryLoader::new().load_symbol_selector()?;
let abbrev = sys_loader.load_abbrev()?;
let sym_sel = sys_loader.load_symbol_selector()?;

Check warning on line 203 in src/editor/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/editor/mod.rs#L202-L203

Added lines #L202 - L203 were not covered by tests
let editor = Editor::new(conversion_engine, dict, estimate, abbrev, sym_sel);
Ok(editor)
}
Expand Down
43 changes: 30 additions & 13 deletions tools/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,35 @@ use chewing::dictionary::{Dictionary, SystemDictionaryLoader, UserDictionaryLoad
use crate::flags;

pub(crate) fn run(args: flags::Info) -> Result<()> {
let mut dictionaries = vec![];
if args.user {
dictionaries.push(UserDictionaryLoader::new().load()?);
}
if args.system {
dictionaries.extend(SystemDictionaryLoader::new().load()?);
let dictionaries = SystemDictionaryLoader::new().load()?;
if args.json {
print_json_info(&dictionaries, "base");
} else {
print_info(&dictionaries, "base");
}
let extra = SystemDictionaryLoader::new().load_extra()?;
if args.json {
print_json_info(&extra, "extra");
} else {
print_info(&extra, "extra");
}
}
if let Some(path) = args.path {
dictionaries.push(UserDictionaryLoader::new().userphrase_path(path).load()?);
if args.user {
let dict = UserDictionaryLoader::new().load()?;
if args.json {
print_json_info(&[dict], "user");
} else {
print_info(&[dict], "user");
}
}
if args.json {
print_json_info(&dictionaries);
} else {
print_info(&dictionaries);
if let Some(path) = args.path {
let dict = UserDictionaryLoader::new().userphrase_path(path).load()?;
if args.json {
print_json_info(&[dict], "input");
} else {
print_info(&[dict], "input");
}
}
Ok(())
}
Expand All @@ -39,7 +54,7 @@ fn escape_json(str: String) -> String {
out
}

fn print_json_info(dictionaries: &[Box<dyn Dictionary>]) {
fn print_json_info(dictionaries: &[Box<dyn Dictionary>], from: &str) {
let mut iter = dictionaries.iter().peekable();
println!("[");
while let Some(dict) = iter.next() {
Expand All @@ -49,6 +64,7 @@ fn print_json_info(dictionaries: &[Box<dyn Dictionary>]) {
.unwrap_or(String::new());
let info = dict.about();
println!(" {{");
println!(r#" "from": "{from}","#);
println!(r#" "path": "{}","#, escape_json(path));
println!(r#" "name": "{}","#, escape_json(info.name));
println!(r#" "version": "{}","#, escape_json(info.version));
Expand All @@ -60,14 +76,15 @@ fn print_json_info(dictionaries: &[Box<dyn Dictionary>]) {
println!("]");
}

fn print_info(dictionaries: &[Box<dyn Dictionary>]) {
fn print_info(dictionaries: &[Box<dyn Dictionary>], from: &str) {
for dict in dictionaries {
let path = dict
.path()
.map(|p| p.display().to_string())
.unwrap_or(String::new());
let info = dict.about();
println!("---");
println!("From : {from}");
println!("Path : {}", path);
println!("Name : {}", info.name);
println!("Version : {}", info.version);
Expand Down

0 comments on commit 95f6eeb

Please sign in to comment.