mirror of
https://github.com/xai-org/x-algorithm.git
synced 2026-02-13 03:05:06 +01:00
40 lines
1.4 KiB
Rust
40 lines
1.4 KiB
Rust
use crate::util;
|
|
use std::any::type_name_of_val;
|
|
use tonic::async_trait;
|
|
|
|
/// Scorers update candidate fields (like a score field) and run sequentially
|
|
#[async_trait]
|
|
pub trait Scorer<Q, C>: Send + Sync
|
|
where
|
|
Q: Clone + Send + Sync + 'static,
|
|
C: Clone + Send + Sync + 'static,
|
|
{
|
|
/// Decide if this scorer should run for the given query
|
|
fn enable(&self, _query: &Q) -> bool {
|
|
true
|
|
}
|
|
|
|
/// Score candidates by performing async operations.
|
|
/// Returns candidates with this scorer's fields populated.
|
|
///
|
|
/// IMPORTANT: The returned vector must have the same candidates in the same order as the input.
|
|
/// Dropping candidates in a scorer is not allowed - use a filter stage instead.
|
|
async fn score(&self, query: &Q, candidates: &[C]) -> Result<Vec<C>, String>;
|
|
|
|
/// Update a single candidate with the scored fields.
|
|
/// Only the fields this scorer is responsible for should be copied.
|
|
fn update(&self, candidate: &mut C, scored: C);
|
|
|
|
/// Update all candidates with the scored fields from `scored`.
|
|
/// Default implementation iterates and calls `update` for each pair.
|
|
fn update_all(&self, candidates: &mut [C], scored: Vec<C>) {
|
|
for (c, s) in candidates.iter_mut().zip(scored) {
|
|
self.update(c, s);
|
|
}
|
|
}
|
|
|
|
fn name(&self) -> &'static str {
|
|
util::short_type_name(type_name_of_val(self))
|
|
}
|
|
}
|