pub struct CorpusWorkspace<T> { /* private fields */ }Expand description
A corpus prepared once for repeated scoring under a fixed Metric.
Build it with CorpusWorkspace::build, then score many queries with
query_corpus_scores /
query_corpus_scores_into or rerank a single query
with rerank_with. The corpus and metric are fixed for the life
of the workspace.
Implementations§
Source§impl<T: NabledReal> CorpusWorkspace<T>
impl<T: NabledReal> CorpusWorkspace<T>
Sourcepub fn build(
corpus: &ArrayView2<'_, T>,
metric: Metric,
) -> Result<Self, EmbeddingError>
pub fn build( corpus: &ArrayView2<'_, T>, metric: Metric, ) -> Result<Self, EmbeddingError>
Precompute reusable corpus state for metric.
For Metric::Cosine this computes and caches the corpus row norms (rejecting zero-norm
rows up front, exactly as the stateless cosine path would).
§Errors
Returns EmbeddingError::EmptyInput when corpus is empty and
EmbeddingError::ZeroNorm when a cosine corpus contains a zero-norm row.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the corpus has no rows. Always false for a successfully built workspace.
Sourcepub fn query_corpus_scores(
&self,
queries: &ArrayView2<'_, T>,
) -> Result<Array2<T>, EmbeddingError>
pub fn query_corpus_scores( &self, queries: &ArrayView2<'_, T>, ) -> Result<Array2<T>, EmbeddingError>
Score every queries row against the cached corpus, reusing precomputed corpus state.
Returns a (queries.nrows(), corpus_len) matrix identical to the stateless
query_corpus_scores result.
§Errors
Returns EmbeddingError::EmptyInput for empty queries,
EmbeddingError::DimensionMismatch when the feature dimension differs, and
EmbeddingError::ZeroNorm for cosine queries with zero-norm rows.
Sourcepub fn query_corpus_scores_into(
&self,
queries: &ArrayView2<'_, T>,
out: &mut Array2<T>,
) -> Result<(), EmbeddingError>
pub fn query_corpus_scores_into( &self, queries: &ArrayView2<'_, T>, out: &mut Array2<T>, ) -> Result<(), EmbeddingError>
Score every queries row against the cached corpus into a caller-provided out buffer.
out must be shaped (queries.nrows(), corpus_len).
§Errors
See query_corpus_scores; also returns
EmbeddingError::DimensionMismatch when out is mis-sized.
Sourcepub fn rerank_with(
&self,
query: &ArrayView1<'_, T>,
k: usize,
) -> Result<Vec<Neighbor<T>>, EmbeddingError>
pub fn rerank_with( &self, query: &ArrayView1<'_, T>, k: usize, ) -> Result<Vec<Neighbor<T>>, EmbeddingError>
Rerank the cached corpus against a single query, returning the best k neighbors.
Equivalent to rerank against this workspace’s corpus and metric,
but reusing the precomputed corpus state.
§Errors
See query_corpus_scores.
Sourcepub fn knn_with(
&self,
queries: &ArrayView2<'_, T>,
k: usize,
) -> Result<Vec<Vec<Neighbor<T>>>, EmbeddingError>
pub fn knn_with( &self, queries: &ArrayView2<'_, T>, k: usize, ) -> Result<Vec<Vec<Neighbor<T>>>, EmbeddingError>
Exact brute-force kNN over the cached corpus for every queries row.
Per-query best-first neighbor lists, reusing the precomputed corpus state.
§Errors
See query_corpus_scores.
Trait Implementations§
Source§impl<T: Clone> Clone for CorpusWorkspace<T>
impl<T: Clone> Clone for CorpusWorkspace<T>
Source§fn clone(&self) -> CorpusWorkspace<T>
fn clone(&self) -> CorpusWorkspace<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more