Fix compression bug
This commit is contained in:
1 parent
1d24fe2631
commit
dddac01bdf
4 files changed
+61
-76
No files matched your search
+10
-16
@@ -58,30 +58,24 @@ impl DeckData {
|
||||
}
|
||||
|
||||
pub fn next(&mut self) -> (WordData, CardStatistics) {
|
||||
let index = match self.order_module.clone() {
|
||||
OrderModule::SemiRandomSRS(mut module) => {
|
||||
let index = match self.order_module {
|
||||
OrderModule::SemiRandomSRS(ref mut module) => {
|
||||
if !module.initialized {
|
||||
module.init(self)
|
||||
module.init(self.set.as_slice())
|
||||
}
|
||||
let index = module.next(self);
|
||||
self.order_module = OrderModule::SemiRandomSRS(module);
|
||||
index
|
||||
module.next(self.set.as_slice())
|
||||
}
|
||||
OrderModule::RandomSRS(mut module) => {
|
||||
OrderModule::RandomSRS(ref mut module) => {
|
||||
if !module.initialized {
|
||||
module.init(self)
|
||||
module.init(self.set.as_slice())
|
||||
}
|
||||
let index = module.next(self);
|
||||
self.order_module = OrderModule::RandomSRS(module);
|
||||
index
|
||||
module.next(self.set.as_slice())
|
||||
}
|
||||
OrderModule::WorstWordsSRS(mut module) => {
|
||||
OrderModule::WorstWordsSRS(ref mut module) => {
|
||||
if !module.initialized {
|
||||
module.init(self)
|
||||
module.init(self.set.as_slice())
|
||||
}
|
||||
let index = module.next(self);
|
||||
self.order_module = OrderModule::WorstWordsSRS(module);
|
||||
index
|
||||
module.next(self.set.as_slice())
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
+16
-22
@@ -1,9 +1,8 @@
|
||||
use crate::core::deck::DeckData;
|
||||
use crate::data::entities::CardStatistics;
|
||||
use rand::distr::weighted::WeightedIndex;
|
||||
use rand::prelude::ThreadRng;
|
||||
use rand::prelude::{Distribution, SliceRandom};
|
||||
use rand::rng;
|
||||
use crate::data::entities::CardStatistics;
|
||||
|
||||
const MAX_HISTORY_LEN: usize = 20;
|
||||
const MAX_HISTORY_LEN_PART: f32 = 0.33;
|
||||
@@ -29,9 +28,9 @@ pub enum OrderModule {
|
||||
WorstWordsSRS(WorstWordsSRSModule),
|
||||
}
|
||||
pub trait SRSModule {
|
||||
fn next(&mut self, set: &mut DeckData) -> usize;
|
||||
fn next(&mut self, set: &[CardStatistics]) -> usize;
|
||||
fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &CardStatistics);
|
||||
fn init(&mut self, set: &mut DeckData);
|
||||
fn init(&mut self, set: &[CardStatistics]);
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct RandomSRSModule {
|
||||
@@ -47,11 +46,11 @@ impl RandomSRSModule {
|
||||
}
|
||||
}
|
||||
impl SRSModule for RandomSRSModule {
|
||||
fn next(&mut self, set: &mut DeckData) -> usize {
|
||||
fn next(&mut self, set: &[CardStatistics]) -> usize {
|
||||
match self.basket.pop() {
|
||||
Some(index) => index,
|
||||
None => {
|
||||
self.basket = (0..set.words.len()).collect::<Vec<usize>>();
|
||||
self.basket = (0..set.len()).collect::<Vec<usize>>();
|
||||
self.basket.shuffle(&mut rand::rng());
|
||||
self.basket.pop().expect("empty basket")
|
||||
}
|
||||
@@ -60,9 +59,9 @@ impl SRSModule for RandomSRSModule {
|
||||
|
||||
fn open(&mut self, _: WordOpenMode, _: usize, _: &CardStatistics) {}
|
||||
|
||||
fn init(&mut self, set: &mut DeckData) {
|
||||
fn init(&mut self, set: &[CardStatistics]) {
|
||||
self.initialized = true;
|
||||
self.basket = (0..set.set.len()).collect::<Vec<usize>>();
|
||||
self.basket = (0..set.len()).collect::<Vec<usize>>();
|
||||
self.basket.shuffle(&mut rand::rng())
|
||||
}
|
||||
}
|
||||
@@ -84,7 +83,7 @@ impl SemiRandomSRSModule {
|
||||
}
|
||||
}
|
||||
impl SRSModule for SemiRandomSRSModule {
|
||||
fn next(&mut self, set: &mut DeckData) -> usize {
|
||||
fn next(&mut self, set: &[CardStatistics]) -> usize {
|
||||
let index = self.last_weights.sample(&mut self.generator);
|
||||
|
||||
if self.history.contains(&index) && self.history.len() != 1 {
|
||||
@@ -104,10 +103,9 @@ impl SRSModule for SemiRandomSRSModule {
|
||||
.update_weights(&[(index, &new_weight)])
|
||||
.expect("update weights error");
|
||||
}
|
||||
fn init(&mut self, set: &mut DeckData) {
|
||||
fn init(&mut self, set: &[CardStatistics]) {
|
||||
self.initialized = true;
|
||||
let weights = set
|
||||
.set
|
||||
.iter()
|
||||
.map(|s| (100.0 / s.calculated_score()).powf(2.0) * 2.0)
|
||||
.collect::<Vec<f32>>();
|
||||
@@ -115,7 +113,7 @@ impl SRSModule for SemiRandomSRSModule {
|
||||
}
|
||||
}
|
||||
impl SemiRandomSRSModule {
|
||||
fn history_len(&self, set: &DeckData) -> usize {
|
||||
fn history_len(&self, set: &[CardStatistics]) -> usize {
|
||||
((set.len() as f32 * MAX_HISTORY_LEN_PART) as usize).clamp(1, MAX_HISTORY_LEN)
|
||||
}
|
||||
}
|
||||
@@ -129,7 +127,7 @@ pub struct WorstWordsSRSModule {
|
||||
rounds_count: u8,
|
||||
}
|
||||
impl SRSModule for WorstWordsSRSModule {
|
||||
fn next(&mut self, set: &mut DeckData) -> usize {
|
||||
fn next(&mut self, set: &[CardStatistics]) -> usize {
|
||||
if self.rounds_remaining == 0 {
|
||||
self.fill_pool(set);
|
||||
self.rounds_remaining = self.rounds_count;
|
||||
@@ -147,7 +145,7 @@ impl SRSModule for WorstWordsSRSModule {
|
||||
|
||||
fn open(&mut self, _: WordOpenMode, _: usize, _: &CardStatistics) {}
|
||||
|
||||
fn init(&mut self, _: &mut DeckData) {
|
||||
fn init(&mut self, _: &[CardStatistics]) {
|
||||
self.initialized = true;
|
||||
}
|
||||
}
|
||||
@@ -162,17 +160,13 @@ impl WorstWordsSRSModule {
|
||||
queue: vec![],
|
||||
}
|
||||
}
|
||||
fn fill_pool(&mut self, set: &DeckData) {
|
||||
let mut sorted = set
|
||||
.set
|
||||
.clone()
|
||||
.into_iter()
|
||||
.zip(0..set.set.len())
|
||||
fn fill_pool(&mut self, set: &[CardStatistics]) {
|
||||
let mut sorted = set.iter().enumerate()
|
||||
.collect::<Vec<_>>();
|
||||
sorted.sort_by_key(|c| c.0.score);
|
||||
sorted.sort_by_key(|c| c.1.score);
|
||||
let mut worst = sorted[0..self.pool_size]
|
||||
.iter()
|
||||
.map(|(_, index)| *index)
|
||||
.map(|(index, _)| *index)
|
||||
.collect::<Vec<usize>>();
|
||||
worst.shuffle(&mut rand::rng());
|
||||
self.pool = worst;
|
||||
|
||||
Reference in new issue
Block a user