[][src]Struct spellcheck_toy::Dict

pub struct Dict { /* fields omitted */ }

Dict is a spellchecking dictionar, that returns suggested words with levenstein distance 1 or 2 using it's Dict::correction method. It defaults to using the ASCII_LOWERCASE alphabet for it's edits; see stringedits::Edit for more details on alphabets & edits. All values in the dictionary are nonnegative integers; index access does not panic on a missing key, but instead returns zero. The default alphabet is ASCII_LOWERCASE; that is, 'a..=z'

let mut dict = Dict::default();
dict.insert("foo");
dict.insert("foo");
dict.insert("bar");
assert_eq!(dict["foo"], 2);
assert_eq!(dict["baz"], 0);

Methods

impl Dict[src]

pub fn get<Q: ?Sized>(&self, word: &Q) -> Option<&usize> where
    String: Borrow<Q>,
    Q: Eq + Hash
[src]

Retrieve the number of times a word appears in the dictionary,

let custom_dict: Dict = Dict::from_corpus("the quick brown fox jumped over the lazy dog".split_whitespace(), ASCII_LOWERCASE);
assert_eq!(custom_dict.get("the"), Some(&2));
assert_eq!(custom_dict.get("foobar"), None);

pub fn new(alphabet: impl AsRef<str>) -> Self[src]

pub fn new_ascii_lowercase() -> Self[src]

Create a new, empty dictionary, using ASCII_LOWERCASE as it's alphabet

pub fn correction(&self, word: impl AsRef<str>) -> Option<String>[src]

the most likely correction for a word, if any. If the word exists in the dictionary, chooses that word. Otherwise, prioritizes the most common distance-1 correction, then the most common distance-2 correction, if no distance-1 correction exists.

let custom_dict: Dict = Dict::from_corpus("the quick brown fox jumped over the lazy dog".split_whitespace(), ASCII_LOWERCASE);
assert_eq!(custom_dict.correction("brown"), Some("brown".to_string())); // distance 0
assert_eq!(custom_dict.correction("fxo"), Some("fox".to_string())); // distance 1
assert_eq!(custom_dict.correction("thnn"), Some("the".to_string())); // distance 2
assert_eq!(custom_dict.correction("amklsdmalskdnasklfn"), None); // distance... a lot

pub fn insert(&mut self, word: impl AsRef<str>)[src]

insert a word into the dictionary, incrementing it's count by one.

let mut dict = Dict::default();
dict.insert("foo");
dict.insert("bar");
dict.insert("baz".to_string());
dict.insert("bar");
assert_eq!(dict["bar"], 2);
assert_eq!(dict["baz"], 1);

pub fn from_corpus<I>(corpus: I, alphabet: impl AsRef<str>) -> Self where
    I: IntoIterator,
    I::Item: AsRef<str>, 
[src]

create a new dictionary from a corpus of text. alias for FromIterator::from_iter

let custom_dict = Dict::from_corpus("the quick brown fox jumped over the lazy dog".split_whitespace(), ASCII_LOWERCASE);

pub fn save_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()>[src]

Save a dictionary to the specified file. See Dict::save_to_writer

pub fn save_to_writer(&self, w: impl Write) -> Result<()>[src]

save a dictionary to a writer in the space-separated format 'word count' i.e

const TEST_STR: &str = (
"the 23135851162
of 13151942776
and 12997637966
");
let f = io::BufReader::new(TEST_STR.as_bytes());
let dict = Dict::load_from_reader(f, ASCII_LOWERCASE).unwrap();
let mut buf = io::BufWriter::new(Vec::with_capacity(200));
dict.save_to_writer(&mut buf).unwrap();
let got_str = String::from_utf8(buf.into_inner().unwrap()).unwrap();
assert_eq!(TEST_STR, got_str)

pub fn load_from_file<P: AsRef<Path>>(
    path: P,
    alphabet: impl AsRef<str>
) -> Result<Self>
[src]

Load a dictionary from the specified file. See Dict::load_from_reader

pub fn load_from_reader(
    r: impl BufRead,
    alphabet: impl AsRef<str>
) -> Result<Self>
[src]

Load a dictionary from a reader. See Dict::save_to_writer.

Trait Implementations

impl PartialEq<Dict> for Dict[src]

impl Default for Dict[src]

fn default() -> Self[src]

Create a new, empty dictionary, using ASCII_LOWERCASE as it's alphabet

impl Clone for Dict[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

impl<S: AsRef<str>> Extend<S> for Dict[src]

impl Eq for Dict[src]

impl<'a, Q: ?Sized> Index<&'a Q> for Dict where
    String: Borrow<Q>,
    Q: Eq + Hash
[src]

type Output = usize

The returned type after indexing.

impl Debug for Dict[src]

Auto Trait Implementations

impl Send for Dict

impl Sync for Dict

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.