pub struct CountVectorizer { /* private fields */ }Expand description
Builds vocabulary and transforms texts to sparse term-frequency matrices.
Builds vocabulary from training texts (with min_df/max_df filtering), then transforms
texts to sparse CSR matrices where each cell is the count of an n-gram in a document.
Vocabulary is sorted alphabetically after filtering to ensure deterministic feature indices.
Implementations§
Source§impl CountVectorizer
impl CountVectorizer
Sourcepub fn from_vocab(
vocab: HashMap<NgramKey, usize>,
params: VectorizerParams,
) -> Self
pub fn from_vocab( vocab: HashMap<NgramKey, usize>, params: VectorizerParams, ) -> Self
Create vectorizer from pre-built vocabulary.
Used by TfidfVectorizerBuilder after incremental vocabulary construction.
§Arguments
vocab- Pre-built vocabulary mapping n-grams to feature indicesparams- Vectorizer parameters
Sourcepub fn fit<T: AsRef<str> + Sync>(texts: &[T], params: VectorizerParams) -> Self
pub fn fit<T: AsRef<str> + Sync>(texts: &[T], params: VectorizerParams) -> Self
Fit vectorizer on training texts.
§Arguments
texts- Training documentsparams- Configuration for n-gram extraction and vocabulary filtering
Sourcepub fn fit_from_tokenized(
tokenized_texts: &[Vec<u32>],
params: VectorizerParams,
precomputed_ngrams: Option<&[HashMap<NgramKey, usize>]>,
) -> Self
pub fn fit_from_tokenized( tokenized_texts: &[Vec<u32>], params: VectorizerParams, precomputed_ngrams: Option<&[HashMap<NgramKey, usize>]>, ) -> Self
Fit vectorizer from pre-tokenized texts.
Public method to support memory-efficient workflows that manage tokenization separately.
§Arguments
tokenized_texts- Pre-tokenized documentsparams- Vectorizer parametersprecomputed_ngrams- Optional pre-computed n-grams to avoid recomputation
Sourcepub fn transform<T: AsRef<str> + Sync>(&self, texts: &[T]) -> CsMat<f32>
pub fn transform<T: AsRef<str> + Sync>(&self, texts: &[T]) -> CsMat<f32>
Transform texts to sparse term-frequency matrix.
§Returns
Sparse CSR matrix of shape (n_texts, n_features) with term counts
Sourcepub fn fit_transform<T: AsRef<str> + Sync>(
texts: &[T],
params: VectorizerParams,
) -> (Self, CsMat<f32>)
pub fn fit_transform<T: AsRef<str> + Sync>( texts: &[T], params: VectorizerParams, ) -> (Self, CsMat<f32>)
Fit and transform in a single pass.
Optimized to compute n-grams only once, achieving ~2x speedup over
separate fit() + transform() calls.
§Returns
Tuple of (fitted vectorizer, term-frequency matrix)
Sourcepub fn num_features(&self) -> usize
pub fn num_features(&self) -> usize
Number of features (vocabulary size).
Sourcepub fn vocabulary(&self) -> HashMap<String, usize>
pub fn vocabulary(&self) -> HashMap<String, usize>
Get vocabulary with human-readable text.
Requires reverse tokenization - can be slow for large vocabularies.
Sourcepub fn params(&self) -> &VectorizerParams
pub fn params(&self) -> &VectorizerParams
Get the vectorizer parameters.
Trait Implementations§
Source§impl Archive for CountVectorizer
impl Archive for CountVectorizer
Source§const COPY_OPTIMIZATION: CopyOptimization<Self>
const COPY_OPTIMIZATION: CopyOptimization<Self>
serialize. Read moreSource§impl Clone for CountVectorizer
impl Clone for CountVectorizer
Source§impl Debug for CountVectorizer
impl Debug for CountVectorizer
Source§impl<__D: Fallible + ?Sized> Deserialize<CountVectorizer, __D> for Archived<CountVectorizer>where
VectorizerParams: Archive,
<VectorizerParams as Archive>::Archived: Deserialize<VectorizerParams, __D>,
HashMap<NgramKey, usize>: Archive,
<HashMap<NgramKey, usize> as Archive>::Archived: Deserialize<HashMap<NgramKey, usize>, __D>,
impl<__D: Fallible + ?Sized> Deserialize<CountVectorizer, __D> for Archived<CountVectorizer>where
VectorizerParams: Archive,
<VectorizerParams as Archive>::Archived: Deserialize<VectorizerParams, __D>,
HashMap<NgramKey, usize>: Archive,
<HashMap<NgramKey, usize> as Archive>::Archived: Deserialize<HashMap<NgramKey, usize>, __D>,
Source§fn deserialize(
&self,
deserializer: &mut __D,
) -> Result<CountVectorizer, <__D as Fallible>::Error>
fn deserialize( &self, deserializer: &mut __D, ) -> Result<CountVectorizer, <__D as Fallible>::Error>
Auto Trait Implementations§
impl Freeze for CountVectorizer
impl RefUnwindSafe for CountVectorizer
impl Send for CountVectorizer
impl Sync for CountVectorizer
impl Unpin for CountVectorizer
impl UnsafeUnpin for CountVectorizer
impl UnwindSafe for CountVectorizer
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> ArchiveUnsized for Twhere
T: Archive,
impl<T> ArchiveUnsized for Twhere
T: Archive,
Source§type Archived = <T as Archive>::Archived
type Archived = <T as Archive>::Archived
Archive, it may be
unsized. Read moreSource§fn archived_metadata(
&self,
) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
fn archived_metadata( &self, ) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T, S> SerializeUnsized<S> for T
impl<T, S> SerializeUnsized<S> for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§unsafe fn to_subset_unchecked(&self) -> SS
unsafe fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.