40 Zeilen
1.022 B
PHP
40 Zeilen
1.022 B
PHP
|
|
<?php
|
||
|
|
/**
|
||
|
|
* SM-2 Spaced Repetition Algorithm
|
||
|
|
* Ratings: easy=5, medium=3, hard=2, wrong=0
|
||
|
|
*/
|
||
|
|
function sm2_calculate($rating_name, $current_ease, $current_interval, $current_reps) {
|
||
|
|
$rating_map = ['wrong' => 0, 'hard' => 2, 'medium' => 3, 'easy' => 5];
|
||
|
|
$q = $rating_map[$rating_name] ?? 3;
|
||
|
|
|
||
|
|
$ease = $current_ease;
|
||
|
|
$interval = $current_interval;
|
||
|
|
$reps = $current_reps;
|
||
|
|
|
||
|
|
if ($q < 3) {
|
||
|
|
$reps = 0;
|
||
|
|
$interval = 1;
|
||
|
|
} else {
|
||
|
|
if ($reps === 0) {
|
||
|
|
$interval = 1;
|
||
|
|
} elseif ($reps === 1) {
|
||
|
|
$interval = 3;
|
||
|
|
} else {
|
||
|
|
$interval = (int) round($interval * $ease);
|
||
|
|
}
|
||
|
|
$reps++;
|
||
|
|
}
|
||
|
|
|
||
|
|
$ease = $ease + 0.1 - (5 - $q) * (0.08 + (5 - $q) * 0.02);
|
||
|
|
if ($ease < 1.3) $ease = 1.3;
|
||
|
|
|
||
|
|
$next_review = date('Y-m-d', strtotime("+{$interval} days"));
|
||
|
|
|
||
|
|
return [
|
||
|
|
'ease_factor' => round($ease, 2),
|
||
|
|
'interval_days' => $interval,
|
||
|
|
'repetitions' => $reps,
|
||
|
|
'next_review' => $next_review
|
||
|
|
];
|
||
|
|
}
|