pub struct SpellChecker { /* private fields */ }Expand description
Spell Checker
Checks the spelling of a line.
§Example
use ispell::SpellLauncher;
let mut checker = SpellLauncher::new().launch().unwrap();
let errors = checker.check("This should not contain any error").unwrap();
assert!(errors.is_empty());Implementations§
Source§impl SpellChecker
impl SpellChecker
Sourcepub fn add_word_to_dictionary(&mut self, word: &str) -> Result<()>
pub fn add_word_to_dictionary(&mut self, word: &str) -> Result<()>
Adds a word to your personal dictionary
The word will be saved to your words file (e.g. ~/.ispell_LANG, ~/.hunspell_LANG, or ~/.aspell.LANG.pws), so it will be memorized next time you use i/a/hun/spell. If you only want to add the word to this current session, use add_word`.
§Returns
An error if connection to ispell failed, or word contains multiple words (i.e., spaces).
Note that a successful return of this method doesn’t mean that the word was successfully added, as it is possible that it contains invalid character and ispell will reject it.
§Examples
Adding a valid word
use ispell::SpellLauncher;
fn main() {
let mut checker = SpellLauncher::new()
.launch()
.unwrap();
// "rustacean" is not a valid word...
// (unless you already ran this code example)
let errors = checker.check("rustacean").unwrap();
assert_eq!(errors.len(), 1);
// let's add it to our personal dictionary
checker.add_word_to_dictionary("rustacean").unwrap();
// now it is a valid word
let errors = checker.check("rustacean").unwrap();
assert!(errors.is_empty());
}Invalid word: contains spaces
let mut checker = SpellLauncher::new()
.launch()
.unwrap();
let res = checker.add_word_to_dictionary("multiple words");
assert!(res.is_err());Sourcepub fn add_word(&mut self, word: &str) -> Result<()>
pub fn add_word(&mut self, word: &str) -> Result<()>
Add a word to current session.
Similar to add_word_to_dictionary, except word won’t be memorized the next time you use i/a/hun/spell.
use ispell::SpellLauncher;
fn main() {
let mut checker = SpellLauncher::new()
.launch()
.unwrap();
// "rustaholic" is not a valid word...
// (even if you already ran this code example)
let errors = checker.check("rustaholic").unwrap();
assert_eq!(errors.len(), 1);
// let's add it to this session
checker.add_word("rustaholic").unwrap();
// now it is a valid word
let errors = checker.check("rustaholic").unwrap();
assert!(errors.is_empty());
}Examples found in repository?
4fn main() {
5 let mut checker = SpellLauncher::new()
6 .launch()
7 .unwrap();
8
9 // "foobar" is not a valid word...
10 let errors = checker.check("foobar").unwrap();
11 println!("errors: {:?}", errors);
12 assert_eq!(errors.len(), 1);
13
14 // let's add it
15 checker.add_word("foobar").unwrap();
16 let errors = checker.check("foobar").unwrap();
17 println!("errors: {:?}", errors);
18 assert!(errors.is_empty());
19}Sourcepub fn check(&mut self, text: &str) -> Result<Vec<IspellError>>
pub fn check(&mut self, text: &str) -> Result<Vec<IspellError>>
Checks the spelling of a line.
This method only returns the errors that ispell detects. Since the position returned in those errors is the number of characters since the beginning of the line, this method needs to be called line by line and not on a full document.
Examples found in repository?
4fn main() {
5 let mut checker = SpellLauncher::new()
6 .launch()
7 .unwrap();
8
9 // "foobar" is not a valid word...
10 let errors = checker.check("foobar").unwrap();
11 println!("errors: {:?}", errors);
12 assert_eq!(errors.len(), 1);
13
14 // let's add it
15 checker.add_word("foobar").unwrap();
16 let errors = checker.check("foobar").unwrap();
17 println!("errors: {:?}", errors);
18 assert!(errors.is_empty());
19}More examples
4fn main() {
5 let mut checker = SpellLauncher::new()
6 .aspell()
7 .command("aspell")
8 .dictionary("en")
9 .launch()
10 .unwrap();
11 let errors = checker.check("A simpel test to to see if it detetcs typing errors").unwrap();
12 for e in errors {
13 println!("'{}' (pos: {}) is misspelled!", &e.misspelled, e.position);
14 if !e.suggestions.is_empty() {
15 println!("Maybe you meant '{}'?", &e.suggestions[0]);
16 }
17 }
18}22fn main() {
23 let checker = SpellLauncher::new()
24 .dictionary("en_GB")
25 .command("hunspell")
26 .launch();
27 match checker {
28 Ok(mut checker) => {
29 let res = checker.check("test of a msitake").unwrap();
30 display(&res);
31 let res = checker.check("test without mistake (?)").unwrap();
32 display(&res);
33 let res = checker.check("Another test wiht a mistake").unwrap();
34 display(&res);
35 },
36 Err(err) => println!("Error: {}", err)
37 }
38}