feat: Implement SM2
This commit is contained in:
parent
1c244d6f5d
commit
1dfdd2f409
35
src/algorithms/sm2/index.ts
Normal file
35
src/algorithms/sm2/index.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { dayInNDays } from "../../utils/date";
|
||||||
|
|
||||||
|
export interface ISchedulingData {
|
||||||
|
easiness: number;
|
||||||
|
consecutiveCorrectAnswers: number;
|
||||||
|
nextDueDate: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export enum AnswerType {
|
||||||
|
CORRECT,
|
||||||
|
WRONG,
|
||||||
|
};
|
||||||
|
|
||||||
|
function performanceRating(answer: AnswerType): number {
|
||||||
|
switch (answer) {
|
||||||
|
case AnswerType.WRONG:
|
||||||
|
return 1;
|
||||||
|
case AnswerType.CORRECT:
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function updateSchedulingData(data: ISchedulingData, answer: AnswerType): ISchedulingData {
|
||||||
|
const perfRating = performanceRating(answer);
|
||||||
|
|
||||||
|
data.easiness += -0.8 + 0.28 * perfRating + 0.02 * Math.pow(perfRating, 2);
|
||||||
|
data.consecutiveCorrectAnswers = answer === AnswerType.CORRECT ? (
|
||||||
|
data.consecutiveCorrectAnswers + 1
|
||||||
|
) : 0;
|
||||||
|
data.nextDueDate = answer === AnswerType.CORRECT ? (
|
||||||
|
dayInNDays(6 * Math.pow(data.easiness, data.consecutiveCorrectAnswers - 1))
|
||||||
|
) : dayInNDays(1);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
9
src/utils/date.ts
Normal file
9
src/utils/date.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
export const DAY_IN_MILLI = 24 * 60 * 60 * 1000;
|
||||||
|
|
||||||
|
export function daysToMilli(n: number): number {
|
||||||
|
return DAY_IN_MILLI * n;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function dayInNDays(n: number): number {
|
||||||
|
return Date.now() * daysToMilli(n);
|
||||||
|
}
|
Reference in New Issue
Block a user