Struct ngrammatic::CorpusBuilder
source · [−]pub struct CorpusBuilder { /* private fields */ }
Expand description
Build an Ngram Corpus, one setting at a time.
Implementations
sourceimpl CorpusBuilder
impl CorpusBuilder
sourcepub fn new() -> Self
pub fn new() -> Self
Initialize a new instance of an CorpusBuilder
, with a default arity
of 2, padding set to Auto
, for the given texts
. The default key_trans
function is a pass-through, leaving the keys unmodified.
let mut corpus = CorpusBuilder::new().finish();
corpus.add_text("tomato");
let results = corpus.search("tomacco", 0.40);
if let Some(result) = results.first() {
println!("Closest match to 'tomacco' in the corpus was {}", result.text);
} else {
println!("The corpus contained no words similar to 'tomacco'.");
}
sourcepub fn pad_full(self, pad: Pad) -> Self
pub fn pad_full(self, pad: Pad) -> Self
Set both the left and right padding to build into the Corpus
.
sourcepub fn arity(self, arity: usize) -> Self
pub fn arity(self, arity: usize) -> Self
Set arity
(the n in ngram) to use for the resulting Corpus
.
sourcepub fn fill<It>(self, iterable: It) -> Self where
It: IntoIterator,
It::Item: Into<String>,
pub fn fill<It>(self, iterable: It) -> Self where
It: IntoIterator,
It::Item: Into<String>,
Provide an iterator that will yield strings to be added to the
Corpus
.
sourcepub fn key_trans(
self,
key_trans: Box<dyn Fn(&str) -> String + Send + Sync>
) -> Self
pub fn key_trans(
self,
key_trans: Box<dyn Fn(&str) -> String + Send + Sync>
) -> Self
A key transformation function, supplied as a boxed Fn that takes a
&str and returns a String, applied to all strings that will be added
to the Corpus
. Searches on the Corpus
will be similarly
transformed.
use ngrammatic::CorpusBuilder;
let mut corpus = CorpusBuilder::new().key_trans(Box::new(|x| x.to_lowercase())).finish();
corpus.add_text("tomato");
let results = corpus.search("ToMaTo", 0.90);
if let Some(result) = results.first() {
println!("Closest match to 'ToMaTo' in the corpus was {}", result.text);
} else {
println!("The corpus contained no words similar to 'ToMaTo'.");
}
sourcepub fn case_insensitive(self) -> Self
pub fn case_insensitive(self) -> Self
Convenience function that calls key_trans
with a closure that
lowercases all keys added to the Corpus
.
let mut corpus = CorpusBuilder::new().case_insensitive().finish();
corpus.add_text("tomato");
let results = corpus.search("ToMaTo", 0.90);
if let Some(result) = results.first() {
println!("Closest match to 'ToMaTo' in the corpus was {}", result.text);
} else {
println!("The corpus contained no words similar to 'ToMaTo'.");
}
Trait Implementations
sourceimpl Debug for CorpusBuilder
impl Debug for CorpusBuilder
sourceimpl Default for CorpusBuilder
impl Default for CorpusBuilder
Auto Trait Implementations
impl !RefUnwindSafe for CorpusBuilder
impl Send for CorpusBuilder
impl Sync for CorpusBuilder
impl Unpin for CorpusBuilder
impl !UnwindSafe for CorpusBuilder
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more