pub struct SemanticPersonalizer {
pub profiles: HashMap<u64, UserProfile>,
pub decay_rate: f64,
}Expand description
Manages per-user interest profiles and applies personalization to search results.
§Example
use ipfrs_semantic::personalizer::{
InteractionRecord, InteractionType, SemanticPersonalizer,
};
let mut personalizer = SemanticPersonalizer::new(0.99);
personalizer.record_interaction(
1,
InteractionRecord {
result_id: 42,
interaction: InteractionType::Like,
category: "rust".to_string(),
timestamp_secs: 1_700_000_000,
},
);
let results = vec![(42_u64, 0.8_f64, "rust".to_string())];
let biased = personalizer.apply_bias(1, &results);
assert!(!biased.is_empty());Fields§
§profiles: HashMap<u64, UserProfile>Map from user ID to that user’s interest profile.
decay_rate: f64Multiplicative decay applied to existing category scores on each new interaction.
A value of 0.99 means each prior interaction contributes slightly less over time.
Implementations§
Source§impl SemanticPersonalizer
impl SemanticPersonalizer
Sourcepub fn new(decay_rate: f64) -> Self
pub fn new(decay_rate: f64) -> Self
Create a new SemanticPersonalizer with the given decay rate.
decay_rate should be in (0.0, 1.0]. A typical value is 0.99.
Sourcepub fn record_interaction(
&mut self,
user_id: u64,
interaction: InteractionRecord,
)
pub fn record_interaction( &mut self, user_id: u64, interaction: InteractionRecord, )
Record a user interaction and update the corresponding interest profile.
Steps performed:
- Upsert the user profile.
- Apply decay to all existing
category_scores. - Add the interaction weight to
category_scores[category]. - Update
result_scoresand synchroniseliked_ids/disliked_ids.
Sourcepub fn compute_bias(&self, user_id: u64) -> Option<PersonalizationBias>
pub fn compute_bias(&self, user_id: u64) -> Option<PersonalizationBias>
Compute personalization bias for a user.
Returns None if the user has no profile.
Sourcepub fn apply_bias(
&self,
user_id: u64,
results: &[(u64, f64, String)],
) -> Vec<(u64, f64)>
pub fn apply_bias( &self, user_id: u64, results: &[(u64, f64, String)], ) -> Vec<(u64, f64)>
Apply personalization bias to a slice of (result_id, score, category) tuples.
Each result’s score is multiplied by its category boost and its ID boost. Results are returned sorted by adjusted score descending.
If the user has no profile, results are returned sorted by the original scores.
Sourcepub fn profile(&self, user_id: u64) -> Option<&UserProfile>
pub fn profile(&self, user_id: u64) -> Option<&UserProfile>
Retrieve an immutable reference to a user’s profile.
Auto Trait Implementations§
impl Freeze for SemanticPersonalizer
impl RefUnwindSafe for SemanticPersonalizer
impl Send for SemanticPersonalizer
impl Sync for SemanticPersonalizer
impl Unpin for SemanticPersonalizer
impl UnsafeUnpin for SemanticPersonalizer
impl UnwindSafe for SemanticPersonalizer
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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> 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> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
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§fn to_subset_unchecked(&self) -> SS
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.