| |
| |
| |
| |
|
|
| use std::collections::HashMap; |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct MemoryStrength { |
| pub topic: String, |
| |
| pub strength: f64, |
| pub access_count: usize, |
| pub last_accessed: i64, |
| } |
|
|
| |
| #[derive(Debug, Clone)] |
| pub struct UserMemoryModel { |
| |
| strengths: HashMap<String, MemoryStrength>, |
| |
| introduced_topics: Vec<String>, |
| } |
|
|
| impl UserMemoryModel { |
| pub fn new() -> Self { |
| Self { |
| strengths: HashMap::new(), |
| introduced_topics: Vec::new(), |
| } |
| } |
|
|
| |
| pub fn record_introduction(&mut self, topic: &str) { |
| if !self.introduced_topics.contains(&topic.to_lowercase()) { |
| self.introduced_topics.push(topic.to_lowercase()); |
| } |
| } |
|
|
| |
| pub fn record_memory_access(&mut self, topic: &str, success: bool) { |
| let key = topic.to_lowercase(); |
|
|
| if success { |
| if let Some(strength) = self.strengths.get_mut(&key) { |
| strength.strength = (strength.strength * 0.9 + 0.1).min(1.0); |
| strength.access_count += 1; |
| strength.last_accessed = crate::now_timestamp(); |
| } else { |
| self.strengths.insert( |
| key, |
| MemoryStrength { |
| topic: topic.to_string(), |
| strength: 0.6, |
| access_count: 1, |
| last_accessed: crate::now_timestamp(), |
| }, |
| ); |
| } |
| } else if let Some(strength) = self.strengths.get_mut(&key) { |
| strength.strength *= 0.8; |
| } |
| } |
|
|
| |
| pub fn get_strength(&self, topic: &str) -> Option<f64> { |
| self.strengths |
| .get(&topic.to_lowercase()) |
| .map(|s| s.strength) |
| } |
|
|
| |
| pub fn did_introduce(&self, topic: &str) -> bool { |
| self.introduced_topics.contains(&topic.to_lowercase()) |
| } |
|
|
| |
| pub fn potentially_forgotten(&self) -> Vec<&str> { |
| self.strengths |
| .iter() |
| .filter(|(_, s)| s.strength < 0.3) |
| .map(|(_, s)| s.topic.as_str()) |
| .collect() |
| } |
|
|
| |
| pub fn strong_topics(&self) -> Vec<&str> { |
| self.strengths |
| .iter() |
| .filter(|(_, s)| s.strength > 0.7) |
| .map(|(_, s)| s.topic.as_str()) |
| .collect() |
| } |
| } |
|
|
| impl Default for UserMemoryModel { |
| fn default() -> Self { |
| Self::new() |
| } |
| } |
|
|