-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example for checking from a given dictionary
- Loading branch information
1 parent
404a584
commit 1d48f16
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
editor.workspace-lsp-roots = ["examples"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::time::Instant; | ||
|
||
use spellbook::Dictionary; | ||
use xdg::BaseDirectories; | ||
|
||
fn main() { | ||
let mut args = std::env::args().skip(1); | ||
let arg1 = match args.next() { | ||
Some(arg) => arg, | ||
None => { | ||
eprintln!("Usage: check [LANG] WORD"); | ||
std::process::exit(1); | ||
} | ||
}; | ||
let (lang, word) = match args.next() { | ||
Some(arg2) => (arg1, arg2), | ||
None => ("en_US".to_string(), arg1), | ||
}; | ||
|
||
let base = BaseDirectories::new().expect("Could not determine XDG directories"); | ||
let (dic_path, aff_path) = match base.get_data_dirs().iter().find_map(|dir| { | ||
let subdir = dir.join("hunspell"); | ||
if !subdir.is_dir() { | ||
return None; | ||
} | ||
|
||
let dic = subdir.join(format!("{lang}.dic")); | ||
let aff = subdir.join(format!("{lang}.aff")); | ||
if dic.is_file() && aff.is_file() { | ||
Some((dic, aff)) | ||
} else { | ||
None | ||
} | ||
}) { | ||
Some((dic, aff)) => (dic, aff), | ||
None => { | ||
eprintln!("Could not find the {lang} dictionary"); | ||
std::process::exit(1); | ||
} | ||
}; | ||
let dic_text = std::fs::read_to_string(dic_path).unwrap(); | ||
let aff_text = std::fs::read_to_string(aff_path).unwrap(); | ||
|
||
let now = Instant::now(); | ||
let dict = Dictionary::compile(&aff_text, &dic_text).unwrap(); | ||
println!( | ||
"Compiled the {lang} dictionary in {}ms", | ||
now.elapsed().as_millis() | ||
); | ||
|
||
let now = Instant::now(); | ||
if dict.check(&word) { | ||
println!( | ||
"\"{word}\" is in the dictionary (checked in {}µs)", | ||
now.elapsed().as_micros() | ||
); | ||
} else { | ||
eprintln!( | ||
"\"{word}\" is NOT in the dictionary (checked in {}µs)", | ||
now.elapsed().as_micros() | ||
); | ||
std::process::exit(1); | ||
} | ||
} |