Cleanup and format
This commit is contained in:
1 parent
af6cf10765
commit
d5a2a575fe
9 files changed
+64
-73
No files matched your search
+11
-15
@@ -1,5 +1,5 @@
|
||||
use crate::app::AppState;
|
||||
use crate::core::srs::OrderModule::{WorstWordsSRS, FSRS, RandomSRS, SemiRandomSRS};
|
||||
use crate::core::srs::OrderModule::{Fsrs, RandomSRS, SemiRandomSRS, WorstWordsSRS};
|
||||
use crate::core::srs::{
|
||||
FsrsModule, OrderMode, OrderModule, RandomSRSModule, SRSModule, SemiRandomSRSModule,
|
||||
WordOpenMode, WorstWordsSRSModule,
|
||||
@@ -48,7 +48,7 @@ impl DeckData {
|
||||
words,
|
||||
current_word_index: None,
|
||||
state: state_for,
|
||||
order_module: FSRS(FsrsModule::new()),
|
||||
order_module: Fsrs(FsrsModule::new()),
|
||||
open_mode: settings.open_mode,
|
||||
settings: settings.clone(),
|
||||
})
|
||||
@@ -75,7 +75,7 @@ impl DeckData {
|
||||
}
|
||||
module.next(self.set.as_slice())
|
||||
}
|
||||
FSRS(ref mut module) => {
|
||||
Fsrs(ref mut module) => {
|
||||
if !module.initialized {
|
||||
module.init(self.set.as_slice())
|
||||
}
|
||||
@@ -103,22 +103,18 @@ impl DeckData {
|
||||
WorstWordsSRS(ref mut module) => {
|
||||
module.open(status, index, word);
|
||||
}
|
||||
FSRS(ref mut module) => {
|
||||
Fsrs(ref mut module) => {
|
||||
module.open(status, index, word);
|
||||
}
|
||||
}
|
||||
|
||||
if let FSRS(ref fsrs) = self.order_module {
|
||||
if fsrs.finished() {
|
||||
self.order_module = match self.open_mode {
|
||||
OrderMode::Default => {
|
||||
SemiRandomSRS(SemiRandomSRSModule::new())
|
||||
}
|
||||
OrderMode::TrainWorstFirst => {
|
||||
WorstWordsSRS(WorstWordsSRSModule::new())
|
||||
}
|
||||
OrderMode::FullRandom => RandomSRS(RandomSRSModule::new()),
|
||||
}
|
||||
if let Fsrs(ref fsrs) = self.order_module
|
||||
&& fsrs.finished()
|
||||
{
|
||||
self.order_module = match self.open_mode {
|
||||
OrderMode::Default => SemiRandomSRS(SemiRandomSRSModule::new()),
|
||||
OrderMode::TrainWorstFirst => WorstWordsSRS(WorstWordsSRSModule::new()),
|
||||
OrderMode::FullRandom => RandomSRS(RandomSRSModule::new()),
|
||||
}
|
||||
}
|
||||
update_stat_score(word, &self.state.lock().connection)?;
|
||||
|
||||
+43
-22
@@ -1,10 +1,10 @@
|
||||
use crate::data::entities::CardStatistics;
|
||||
use chrono::Local;
|
||||
use chrono::{Local, TimeDelta};
|
||||
use fsrs::FSRS;
|
||||
use rand::distr::weighted::WeightedIndex;
|
||||
use rand::prelude::ThreadRng;
|
||||
use rand::prelude::{Distribution, SliceRandom};
|
||||
use rand::rng;
|
||||
use rand::{RngExt, rng};
|
||||
|
||||
const MAX_HISTORY_LEN: usize = 20;
|
||||
const MAX_HISTORY_LEN_PART: f32 = 0.33;
|
||||
@@ -28,7 +28,7 @@ pub enum OrderModule {
|
||||
SemiRandomSRS(SemiRandomSRSModule),
|
||||
RandomSRS(RandomSRSModule),
|
||||
WorstWordsSRS(WorstWordsSRSModule),
|
||||
FSRS(FsrsModule),
|
||||
Fsrs(FsrsModule),
|
||||
}
|
||||
pub trait SRSModule {
|
||||
fn next(&mut self, set: &[CardStatistics]) -> usize;
|
||||
@@ -183,12 +183,12 @@ pub struct FsrsModule {
|
||||
queue: Vec<usize>,
|
||||
rng: ThreadRng,
|
||||
fsrs: FSRS,
|
||||
pub(crate) initialized: bool
|
||||
pub(crate) initialized: bool,
|
||||
}
|
||||
|
||||
impl SRSModule for FsrsModule {
|
||||
fn next(&mut self, set: &[CardStatistics]) -> usize {
|
||||
if self.solved.len() == self.current_group.len() {
|
||||
fn next(&mut self, _: &[CardStatistics]) -> usize {
|
||||
if self.current_group.is_empty() {
|
||||
self.fill_partial_pool();
|
||||
}
|
||||
|
||||
@@ -197,16 +197,39 @@ impl SRSModule for FsrsModule {
|
||||
self.queue.shuffle(&mut self.rng);
|
||||
}
|
||||
|
||||
self.queue.pop().expect("empty queue")
|
||||
self.queue.pop().unwrap_or(0)
|
||||
}
|
||||
|
||||
fn open(&mut self, status: WordOpenMode, index: usize, updated_word: &mut CardStatistics) {
|
||||
let today = Local::now().date_naive();
|
||||
let delta = today - updated_word.fsrs_opened;
|
||||
let delta_days = delta.num_days();
|
||||
|
||||
if updated_word.fsrs_open_day != today{
|
||||
|
||||
let memory = self
|
||||
.fsrs
|
||||
.next_states(updated_word.fsrs_memory, 0.9, delta_days as u32)
|
||||
.expect("Wrong next stage calculation");
|
||||
|
||||
let next = match status {
|
||||
WordOpenMode::Easy => memory.easy,
|
||||
WordOpenMode::Ok => memory.good,
|
||||
WordOpenMode::Hard => memory.hard,
|
||||
WordOpenMode::None => memory.again,
|
||||
};
|
||||
|
||||
let mut next_day = today + TimeDelta::days(next.interval.round() as i64);
|
||||
if (next_day - today).num_days() >= 5 {
|
||||
let offset = ((rng().random::<f32>() - 0.5) * 3.0).round() as i32;
|
||||
next_day += TimeDelta::days(offset as i64);
|
||||
}
|
||||
updated_word.fsrs_memory = Some(next.memory);
|
||||
updated_word.fsrs_open_day = next_day;
|
||||
updated_word.fsrs_opened = updated_word.last_open.date_naive();
|
||||
|
||||
if updated_word.fsrs_open_day != today {
|
||||
self.solved.push(index);
|
||||
let index = self.current_group.iter().position(|x| *x == index).unwrap();
|
||||
self.current_group.remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,8 +249,8 @@ impl SRSModule for FsrsModule {
|
||||
}
|
||||
|
||||
impl FsrsModule {
|
||||
pub fn new() -> Self{
|
||||
Self{
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
today_set: vec![],
|
||||
current_group: vec![],
|
||||
queue: vec![],
|
||||
@@ -238,21 +261,19 @@ impl FsrsModule {
|
||||
}
|
||||
}
|
||||
fn fill_partial_pool(&mut self) {
|
||||
self.current_group.clear();
|
||||
self.current_group
|
||||
.extend(self.today_set.iter().take(PARTIAL_POOL_SIZE));
|
||||
self.current_group.iter().for_each(|_| {
|
||||
self.today_set.remove(0);
|
||||
});
|
||||
self.current_group.extend(
|
||||
self.today_set
|
||||
.iter()
|
||||
.filter(|x| !self.solved.contains(*x))
|
||||
.take(PARTIAL_POOL_SIZE),
|
||||
);
|
||||
|
||||
self.queue.clear();
|
||||
self.queue
|
||||
.extend(self.current_group.clone());
|
||||
self.solved.clear();
|
||||
self.queue.extend(self.current_group.clone());
|
||||
}
|
||||
|
||||
fn fill_queue(&mut self) {
|
||||
self.queue.clear();
|
||||
self.queue.extend(self.current_group.iter().filter(|c| !self.today_set.contains(*c)));
|
||||
self.queue.extend(self.current_group.clone());
|
||||
}
|
||||
|
||||
pub fn finished(&self) -> bool {
|
||||
|
||||
Reference in new issue
Block a user