Coder Social home page Coder Social logo

maxvien / supermemo Goto Github PK

View Code? Open in Web Editor NEW
275.0 3.0 25.0 501 KB

๐Ÿง‘โ€๐ŸŽ“ A JavaScript/TypeScript implementation of the SuperMemo 2 (SM2) algorithm for spaced based repetition flashcards.

Home Page: https://npm.im/supermemo

License: MIT License

TypeScript 100.00%
supermemo supermemo2 sm2 spaced-repetition flashcards javascript typescript webpack nodejs deno

supermemo's Introduction

SuperMemo

A JavaScript/TypeScript implementation of the SuperMemo 2 (SM2) algorithm for spaced based repetition flashcards.

Hit the Star button if you love this project โญ๏ธ

Explanation

type SuperMemoItem = {
  interval: number;
  repetition: number;
  efactor: number;
};

type SuperMemoGrade = 0 | 1 | 2 | 3 | 4 | 5;

supermemo(item: SuperMemoItem, grade: SuperMemoGrade): SuperMemoItem
  • item
    • repetition: the number of continous correct responses. The initial repetition value should be 0.
    • interval: inter-repetition interval after the repetitions (in days). The initial interval value should be 0.
    • efactor: easiness factor reflecting the easiness of memorizing and retaining a given item in memory. The initial efactor value should be 2.5.
  • grade:
    • 5: perfect response.
    • 4: correct response after a hesitation.
    • 3: correct response recalled with serious difficulty.
    • 2: incorrect response; where the correct one seemed easy to recall.
    • 1: incorrect response; the correct one remembered.
    • 0: complete blackout.

Installation

For Webpack

npm install --save supermemo
import { supermemo, SuperMemoItem, SuperMemoGrade } from 'supermemo';

For Node

npm install --save supermemo
const { supermemo } = require('supermemo');

For Deno

import { supermemo, SuperMemoItem, SuperMemoGrade } from 'https://deno.land/x/supermemo/mod.ts';

Usage

let item: SuperMemoItem = {
  interval: 0,
  repetition: 0,
  efactor: 2.5,
};

console.log(item);

item = supermemo(item, 5);
console.log(item);

item = supermemo(item, 4);
console.log(item);

How to implement SuperMemo with DayJS?

import dayjs from 'dayjs';
import { supermemo, SuperMemoItem, SuperMemoGrade } from 'supermemo';

interface Flashcard extends SuperMemoItem {
  front: string;
  back: string;
  dueDate: string;
}

function practice(flashcard: Flashcard, grade: SuperMemoGrade): Flashcard {
  const { interval, repetition, efactor } = supermemo(flashcard, grade);

  const dueDate = dayjs(Date.now()).add(interval, 'day').toISOString();

  return { ...flashcard, interval, repetition, efactor, dueDate };
}

let flashcard: Flashcard = {
  front: 'programer',
  back: 'an organism that turns caffeine in software',
  interval: 0,
  repetition: 0,
  efactor: 2.5,
  dueDate: dayjs(Date.now()).toISOString(),
};

console.log(flashcard);

flashcard = practice(flashcard, 5);
console.log(flashcard);

flashcard = practice(flashcard, 3);
console.log(flashcard);

References

supermemo's People

Contributors

dependabot[bot] avatar justin-calleja avatar maxvien 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

supermemo's Issues

It doesn't work with Deno

Hi, thank you for making it publicly available! I tried to use it with Deno, but even only import doesn't work. Am I missing something?

$ deno -V
deno 1.16.2

$ cat index.ts
import { supermemo, SuperMemoItem, SuperMemoGrade } from 'https://deno.land/x/supermemo/mod.ts';

$ deno run -A index.ts
Check file:///Users/kyanny/workspace/sm2/index.ts
thread 'main' panicked at 'internal error: entered unreachable code: unexpected missing emit: https://deno.land/x/[email protected]/mod.ts', cli/proc_state.rs:544:15
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

$ RUST_BACKTRACE=full deno run -A index.ts
Check file:///Users/kyanny/workspace/sm2/index.ts
thread 'main' panicked at 'internal error: entered unreachable code: unexpected missing emit: https://deno.land/x/[email protected]/mod.ts', cli/proc_state.rs:544:15
stack backtrace:
   0:        0x10ba63d3f - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf1ee81583766da09
   1:        0x10b3299fb - core::fmt::write::h8dc218a6eb2426a8
   2:        0x10ba6221a - std::io::Write::write_fmt::heef3e26d448f8c80
   3:        0x10ba62e95 - std::panicking::default_hook::{{closure}}::h4b7de73863cf8582
   4:        0x10ba625f7 - std::panicking::default_hook::h778da672ce56a579
   5:        0x10b2e5106 - deno::setup_exit_process_panic_hook::{{closure}}::hfe9a9204138a8142
   6:        0x10ba620c9 - std::panicking::rust_panic_with_hook::ha5146542f697c8af
   7:        0x10ba830ea - std::panicking::begin_panic_handler::{{closure}}::hd8bf1442c82bf304
   8:        0x10ba83069 - std::sys_common::backtrace::__rust_end_short_backtrace::h73a3d0f5ab35b08b
   9:        0x10ba83025 - _rust_begin_unwind
  10:        0x10cc46f5f - core::panicking::panic_fmt::h16f19dd104182e80
  11:        0x10b09de07 - deno::proc_state::ProcState::prepare_module_load::{{closure}}::h7ff3d5c5a5f7f040
  12:        0x10b09b6be - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h95f4f81560e11b4e
  13:        0x10b09b4ad - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hba28f970eba75693
  14:        0x10afb5426 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h011315fb8be13289
  15:        0x10afb41a9 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h1d99aa03600e7c07
  16:        0x10b0c1591 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hac80c5777a31e8a2
  17:        0x10b0bef04 - deno::run_command::{{closure}}::h26541ce94df71d31
  18:        0x10b0bdb61 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::h95b64f207b752c8f
  19:        0x10af8b2e7 - <core::future::from_generator::GenFuture<T> as core::future::future::Future>::poll::hcd0e8c40af4c0e1c
  20:        0x10af7c929 - deno::main::heb40db0691ea69e2
  21:        0x10af7824c - std::sys_common::backtrace::__rust_begin_short_backtrace::h88e6039fee8e0e1b
  22:        0x10af784a0 - _main

Add previous flashcard interval in practice function

Hi :)

I haven't read the SM-2 spec or anything, but shouldn't it be interval + flashcard.interval in the How to implement SuperMemo with DayJS? section of the README?

  const dueDate = dayjs(Date.now())
    .add(interval + flashcard.interval, 'day')
    .toISOString();

Otherwise, you're only ever adding the last interval to today's date.

Thanks

Shouldnt calling supermemo with a poor grade keep the interval as 0?

Hey all
In this example

let flashcard: Flashcard = {
  front: 'programer',
  back: 'an organism that turns caffeine in software',
  interval: 0,
  repetition: 0,
  efactor: 2.5,
  dueDate: dayjs(Date.now()).toISOString(),
};

console.log(flashcard);

flashcard = practice(flashcard, 1);
console.log(flashcard);

I would expect the dueDate for the flashcard to continue to be today's date, since the grade was 1 (incorrect response). In other words, I'd expect the card to remain due until the user selects 3/4/5 as the grade. At least thats is my expectation as an Anki user. Am I mistaken about how Supermemo algorithm works or am I misunderstanding something?

Thank you

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.