Struct SpellChecker

Source
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

Source

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());
Source

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?
examples/dict.rs (line 15)
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}
Source

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?
examples/dict.rs (line 10)
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
Hide additional examples
examples/readme.rs (line 11)
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}
examples/simple.rs (line 29)
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}
Source

pub fn check_raw(&mut self, text: &str) -> Result<Vec<IspellResult>>

Checks the spelling of a string

This method returns a vector of all ispell answers, even when there is no errors. Usually, the check method, which only returns errors, will be more useful.

Trait Implementations§

Source§

impl Drop for SpellChecker

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.