Struct ngrammatic::Ngram
source · [−]pub struct Ngram {
pub arity: usize,
pub text: String,
pub text_padded: String,
pub grams: HashMap<String, usize>,
}
Expand description
Stores a “word”, with all its n-grams. The “arity” member determines the value of “n” used in generating the n-grams.
Fields
arity: usize
The “symbol size” for the ngrams
text: String
The text for which ngrams were generated
text_padded: String
The text for which ngrams were generated, with the padding used for generating the ngrams
grams: HashMap<String, usize>
A collection of all generated ngrams for the text, with a count of how many times that ngram appears in the text
Implementations
sourceimpl Ngram
impl Ngram
sourcepub fn similarity_to(&self, other: &Ngram, warp: f32) -> f32
pub fn similarity_to(&self, other: &Ngram, warp: f32) -> f32
Calculate the similarity of this Ngram
and an other
, for a given warp
factor (clamped to the range 1.0 to 3.0).
let a = NgramBuilder::new("tomato").finish();
let b = NgramBuilder::new("tomacco").finish();
println!("Similarity factor for {} and {}: {:.0}%", a.text, b.text, a.similarity_to(&b, 2.0) *
100.0);
sourcepub fn matches(&self, other: &Ngram, threshold: f32) -> Option<SearchResult>
pub fn matches(&self, other: &Ngram, threshold: f32) -> Option<SearchResult>
Determines if this Ngram
matches a given other
Ngram
, for a given
threshold
of certainty. This is equivalent to matches_with_warp
and a warp
of 2.0.
let a = NgramBuilder::new("tomato").finish();
let b = NgramBuilder::new("tomacco").finish();
if let Some(word_match) = a.matches(&b, 0.40) {
println!("{} matches {} with {:.0}% certainty", a.text, b.text, word_match.similarity *
100.0);
} else {
println!("{} doesn't look anything like {}.", a.text, b.text);
}
sourcepub fn matches_with_warp(
&self,
other: &Ngram,
warp: f32,
threshold: f32
) -> Option<SearchResult>
pub fn matches_with_warp(
&self,
other: &Ngram,
warp: f32,
threshold: f32
) -> Option<SearchResult>
Determines if this Ngram
matches a given other
Ngram
, with the specified warp
(clamped to the range 1.0 to 3.0), and for a given threshold
of certainty.
let a = NgramBuilder::new("tomato").finish();
let b = NgramBuilder::new("tomacco").finish();
if let Some(word_match) = a.matches_with_warp(&b, 2.0, 0.40) {
println!("{} matches {} with {:.0}% certainty", a.text, b.text, word_match.similarity *
100.0);
} else {
println!("{} doesn't look anything like {}.", a.text, b.text);
}
sourcepub fn count_gram(&self, gram: &str) -> usize
pub fn count_gram(&self, gram: &str) -> usize
Return the number of times a particular gram
appears in the Ngram
text.
let a = NgramBuilder::new("tomato").arity(2).finish();
println!("Number of times the 'to' bigram appears in {}: {}", a.text, a.count_gram("to"));
sourcepub fn count_grams(&self) -> usize
pub fn count_grams(&self) -> usize
Return the total number of grams generated for the Ngram
text.
Trait Implementations
impl Eq for Ngram
Auto Trait Implementations
impl RefUnwindSafe for Ngram
impl Send for Ngram
impl Sync for Ngram
impl Unpin for Ngram
impl UnwindSafe for Ngram
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
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more