Code Monkey home page Code Monkey logo

ts-fsrs's Introduction

Introduction | 简体中文はじめに


About The

ts-fsrs npm version Downloads codecov Build and Publish Deploy

ts-fsrs is a versatile package based on TypeScript that supports ES modules, CommonJS, and UMD. It implements the Free Spaced Repetition Scheduler (FSRS) algorithm, enabling developers to integrate FSRS into their flashcard applications to enhance the user learning experience.

The workflow for TS-FSRS can be referenced from the following resources:

Usage

The [email protected] package requires Node.js version 16.0.0 or higher. Starting with [email protected], the minimum required Node.js version is 18.0.0. From version 3.5.6 onwards, ts-fsrs supports CommonJS, ESM, and UMD module systems.

npm install ts-fsrs
yarn install ts-fsrs
pnpm install ts-fsrs
bun install ts-fsrs

Example

import {createEmptyCard, formatDate, fsrs, generatorParameters, Rating, Grades} from 'ts-fsrs';

const params = generatorParameters({ enable_fuzz: true });
const f = fsrs(params);
const card = createEmptyCard(new Date('2022-2-1 10:00:00'));// createEmptyCard();
const now = new Date('2022-2-2 10:00:00');// new Date();
const scheduling_cards = f.repeat(card, now);

// console.log(scheduling_cards);
Grades.forEach(grade => { // [Rating.Again, Rating.Hard, Rating.Good, Rating.Easy]
    const { log, card } = scheduling_cards[grade];
    console.group(`${Rating[grade]}`);
    console.table({
        [`card_${Rating[grade]}`]: {
            ...card,
            due: formatDate(card.due),
            last_review: formatDate(card.last_review as Date),
        },
    });
    console.table({
        [`log_${Rating[grade]}`]: {
            ...log,
            review: formatDate(log.review),
        },
    });
    console.groupEnd();
    console.log('----------------------------------------------------------------');
});

More refer:

Basic Use

1. Initialization:

To begin, create an empty card instance and set the current date(default: current time from system):

import { Card, createEmptyCard } from "ts-fsrs";
let card: Card = createEmptyCard();
// createEmptyCard(new Date('2022-2-1 10:00:00'));
// createEmptyCard(new Date(Date.UTC(2023, 9, 18, 14, 32, 3, 370)));
// createEmptyCard(new Date('2023-09-18T14:32:03.370Z'));

2. Parameter Configuration:

The library allows for customization of SRS parameters. Use generatorParameters to produce the final set of parameters for the SRS algorithm. Here's an example setting a maximum interval:

import { Card, createEmptyCard, generatorParameters, FSRSParameters } from "ts-fsrs";
let card: Card = createEmptyCard();
const params: FSRSParameters = generatorParameters({ maximum_interval: 1000 });

3. Scheduling with FSRS:

The core functionality lies in the fsrs function. When invoked, it returns a collection of cards scheduled based on different potential user ratings:

import {
  Card,
  createEmptyCard,
  generatorParameters,
  FSRSParameters,
  FSRS,
  RecordLog,
} from "ts-fsrs";

let card: Card = createEmptyCard();
const f: FSRS = new FSRS(); // or const f: FSRS = fsrs(params);
let scheduling_cards: RecordLog = f.repeat(card, new Date());

4. Retrieving Scheduled Cards:

Once you have the scheduling_cards object, you can retrieve cards based on user ratings. For instance, to access the card scheduled for a 'Good' rating:

const good: RecordLogItem = scheduling_cards[Rating.Good];
const newCard: Card = good.card;

Get the new state of card for each rating:

scheduling_cards[Rating.Again].card
scheduling_cards[Rating.Again].log

scheduling_cards[Rating.Hard].card
scheduling_cards[Rating.Hard].log

scheduling_cards[Rating.Good].card
scheduling_cards[Rating.Good].log

scheduling_cards[Rating.Easy].card
scheduling_cards[Rating.Easy].log

5. Understanding Card Attributes:

Each Card object consists of various attributes that determine its status, scheduling, and other metrics:

type Card = {
  due: Date;           // Date when the card is next due for review
  stability: number;   // A measure of how well the information is retained
  difficulty: number;  // Reflects the inherent difficulty of the card content
  elapsed_days: number; // Days since the card was last reviewed
  scheduled_days: number; // The interval at which the card is next scheduled
  reps: number;          // Total number of times the card has been reviewed
  lapses: number;        // Times the card was forgotten or remembered incorrectly
  state: State;          // The current state of the card (New, Learning, Review, Relearning)
  last_review?: Date;    // The most recent review date, if applicable
};

6. Understanding Log Attributes:

Each ReviewLog object contains various attributes that determine the review record information associated with the card, used for analysis, undoing the review, and optimization (WIP).

type ReviewLog = {
    rating: Rating; // Rating of the review (Again, Hard, Good, Easy)
    state: State; // State of the review (New, Learning, Review, Relearning)
    due: Date;  // Date of the last scheduling
    stability: number; // Stability of the card before the review
    difficulty: number; // Difficulty of the card before the review
    elapsed_days: number; // Number of days elapsed since the last review
    last_elapsed_days: number; // Number of days between the last two reviews
    scheduled_days: number; // Number of days until the next review
    review: Date; // Date of the review
}

ts-fsrs's People

Contributors

2lavine avatar barrelltech avatar dependabot[bot] avatar ishiko732 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ts-fsrs's Issues

Questions about using cards

First of all huge thanks for making this. I want to ramble about how grateful I am for this library but I’ll just go straight to asking the questions.

I saw the ts-fsrs-demo but I have a really hard time reading code (actually just reading in general) and I tried my best to understand it but I’m still confused about stuff.

If I grade a newly created card “Again” it’s due in 1 minute, is there a way to lower that? For examples to 30 seconds? Because I can’t recall it at all after a minute because I’m actually very dumb.

When am I expected to f.repeat(card, now) the current card, and how do I set the preview times (those things that say 1min, 5min, 1day, 3day)?

I looked into this and I’m currently using f.repeat and getting the next card at the same time when the user presses one of the 4 grade buttons. And I’m setting the preview times before I actually grade the current card by using f.repeat the current card but not setting the card with the results. Am I doing everything correctly?

How do I get a card like anki in a list of cards? Do I get the one with the lowest due date sorted by Review -> Relearning, Learning -> New?

Do I have to store const f = new FSRS() on localStorage with the cards? Does it store any information about the cards? Or do I only have to store the cards?

[Feat] switch of enable/disable short-term schedule

The current implementation imitates the learning step mechanism of Anki, which forces users to do more reviews when they learn a new card or forget a stale card. I'm considering to add a switch to enable/disable it. In disable mode, a new card will enter into long-term review after the first rating, just like users who leave learning steps empty in Anki. Likewise, if the user press again in a stale card, it won't enter into relearning state.

对测试在线ts-fsrs的example例子的疑惑

首先感谢作者的开源库,然后刚看到这个算法,发现有个在线的例子:https://open-spaced-repetition.github.io/ts-fsrs/example 随手测试了一下

我没有改变任何参数,直接选择按钮测试,分别测试如下:
[Hard->Hard->Good->Hard->Hard->Easy->Good->Hard->Hard]

首先我没有使用anki的经验,纯粹是看到了这个库就使用了一下,感觉到有点反个人直觉的就是,我在两次选择困难后,它安排的下次学习时间合理,但是当我继续点击Good之后再接着点击Hard,这个时候安排的下一次学习是2天之后,如果下次学习还是Hard,这个继续安排的学习时间会继续扩大,甚至在后面点击Easy之后安排的下次学习间隔会更大,而且stability是一直保持稳定增长的,哪怕后面一直选择Hard,间隔的时间也是比较长,且stability一直在增加

PS:我理解的Hard是学习有印象但比较困难,不是完全不知道的状态,哪怕我哪天记忆深刻中间选择了Good,但在接下来的复习里我又遗忘大部分然后选择Hard,那么这个时候直觉应该给我安排一个更近的复习时间。

产生的数据如下:

index due state last_review stability difficulty R elapsed_days scheduled_days reps lapses
1 7/9/2024, 5:00:41 PM 1(Learning) 7/9/2024, 4:55:41 PM 1.44 6.34 / 0 0 1 0
2 7/9/2024, 5:10:41 PM 1(Learning) 7/9/2024, 5:00:41 PM 1.44 6.34 / 0 0 2 0
3 7/10/2024, 5:10:41 PM 2(Review) 7/9/2024, 5:10:41 PM 1.44 6.34 90.00% 0 1 3 0
4 7/12/2024, 5:10:41 PM 2(Review) 7/10/2024, 5:10:41 PM 1.99 7.13 90.00% 1 2 4 0
5 7/15/2024, 5:10:41 PM 2(Review) 7/12/2024, 5:10:41 PM 2.83 7.89 90.00% 2 3 5 0
6 7/30/2024, 5:10:41 PM 2(Review) 7/15/2024, 5:10:41 PM 15.25 6.96 90.00% 3 15 6 0
7 9/6/2024, 5:10:41 PM 2(Review) 7/30/2024, 5:10:41 PM 38.22 6.90 90.00% 15 38 7 0
8 10/26/2024, 5:10:41 PM 2(Review) 9/6/2024, 5:10:41 PM 49.65 7.66 90.00% 38 50 8 0
9 12/26/2024, 5:10:41 PM 2(Review) 10/26/2024, 5:10:41 PM 61.45 8.40 90.00% 50 61 9 0

Update Example

  • Change console.log to console.table
  • get retrievability

invalid date error while using f.repeat function

我在使用ts-fsrs的过程中遇到了invalid date的报错,我试过修改时间的格式,但依旧未能解决这个问题

具体报错如下:
ts-fsrs.js?v=a1e981b4:895 Uncaught (in promise) Error: Invalid date
at x (ts-fsrs.js?v=a1e981b4:895:11)
at Date.diff (ts-fsrs.js?v=a1e981b4:884:10)
at new D (ts-fsrs.js?v=a1e981b4:975:132)
at P.repeat (ts-fsrs.js?v=a1e981b4:1075:15)
at Proxy.getReviewLog (review_mode.vue:110:28)

代码:
import { createEmptyCard, formatDate, generatorParameters, fsrs, Rating, Grades } from "ts-fsrs";

async function getReviewLog(key) {
const now = new Date()
FirstCard.value.due = new Date(formatDate(FirstCard.value.due))
FirstCard.value.lastReview = new Date(formatDate(FirstCard.value.lastReview))
console.log(FirstCard.value)
let scheduling_cards = f.repeat(FirstCard.value, formatDate(now));
let card = scheduling_cards[key].card
let log = scheduling_cards[key].log

传入的参数是:FirstCard.value:
{
"cid": 12,
"nid": 12,
"due": "2024-04-01 21:45:32",
"stability": 0.5701,
"difficulty": 7.5455,
"elapsedDays": 0,
"scheduledDays": 0,
"reps": 1,
"lapses": 0,
"state": 1,
"lastReview": "2024-03-31 17:37:26",
"videoId": "AtChcxeaukQ",
"sid": 35
}

添加显示retrievability数值

我想显示FSRS的DSR数据,通过card.stabilitycard.difficulty可以获得S、D数据。但是R需要使用公式:
$$R(t,S) = 0.9^{\frac{t}{S}}$$
进行计算,对于不熟悉FSRS的人来言可能不知道t,s分别代表什么,希望添加一个函数来获取召回概率。

函数结构希望是:

retrievability = (card: Card, now: dayjs.Dayjs):undefined|string {
    ...
}

又或者提供一个直接显示当前DSR的函数?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.