Skip to content

Commit

Permalink
to esm (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
asukaminato0721 authored Jan 24, 2025
1 parent e5bbbb6 commit 4c3b18c
Show file tree
Hide file tree
Showing 8 changed files with 1,942 additions and 944 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"


yarn lint-staged
65 changes: 28 additions & 37 deletions examples/migrate.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,68 @@
const { FSRS, FSRSItem, FSRSReview } = require('../index')
import { FSRS, FSRSItem, FSRSReview } from '../index'

function migrateWithFullHistory() {
// Create a new FSRS model
const fsrs = new FSRS();
const fsrs = new FSRS()

// Simulate a full review history for a card
const reviews = [
new FSRSReview(3, 0),
new FSRSReview(3, 1),
new FSRSReview(4, 3),
new FSRSReview(3, 7),
];
const item = new FSRSItem(reviews);
const reviews = [new FSRSReview(3, 0), new FSRSReview(3, 1), new FSRSReview(4, 3), new FSRSReview(3, 7)]
const item = new FSRSItem(reviews)

// Calculate the current memory state
const memoryState = fsrs.memoryState(item);
console.log("Migrated memory state:", JSON.stringify(memoryState));
const memoryState = fsrs.memoryState(item)
console.log('Migrated memory state:', JSON.stringify(memoryState))
}

function migrateWithPartialHistory() {
// Create a new FSRS model
const fsrs = new FSRS();
const fsrs = new FSRS()

// Set the true retention of the original algorithm
const sm2Retention = 0.9;
const sm2Retention = 0.9

// Simulate the earliest card state from the first review log of Anki's card
// - ease_factor: the ratio of the interval to the previous interval
// - interval: the interval of the first review
const easeFactor = 2.0;
const interval = 5.0;
const easeFactor = 2.0
const interval = 5.0

// Calculate the earliest memory state
const initialState = fsrs.memoryStateFromSm2(easeFactor, interval, sm2Retention);
const initialState = fsrs.memoryStateFromSm2(easeFactor, interval, sm2Retention)

// Simulate partial review history
const reviews = [
new FSRSReview(3, 5),
new FSRSReview(4, 10),
new FSRSReview(3, 20),
];
const item = new FSRSItem(reviews);
const reviews = [new FSRSReview(3, 5), new FSRSReview(4, 10), new FSRSReview(3, 20)]
const item = new FSRSItem(reviews)

// Calculate the current memory state, passing the initial state
const memoryState = fsrs.memoryState(item, initialState);
console.log("Migrated memory state:", JSON.stringify(memoryState));
const memoryState = fsrs.memoryState(item, initialState)
console.log('Migrated memory state:', JSON.stringify(memoryState))
}

function migrateWithLatestState() {
// Create a new FSRS model
const fsrs = new FSRS();
const fsrs = new FSRS()

// Set the true retention of the original algorithm
const sm2Retention = 0.9;
const sm2Retention = 0.9

// Simulate the latest card state from Anki's card
// - ease_factor: the ratio of the interval to the previous interval
// - interval: the interval of the last review
const easeFactor = 2.5;
const interval = 10.0;
const easeFactor = 2.5
const interval = 10.0

// Calculate the memory state
const memoryState = fsrs.memoryStateFromSm2(easeFactor, interval, sm2Retention);
console.log("Migrated memory state:", JSON.stringify(memoryState));
const memoryState = fsrs.memoryStateFromSm2(easeFactor, interval, sm2Retention)
console.log('Migrated memory state:', JSON.stringify(memoryState))
}

function main() {
console.log("Migrating with full history:");
migrateWithFullHistory();
console.log("\nMigrating with partial history:");
migrateWithPartialHistory();
console.log("\nMigrating with latest state only:");
migrateWithLatestState();
console.log('Migrating with full history:')
migrateWithFullHistory()
console.log('\nMigrating with partial history:')
migrateWithPartialHistory()
console.log('\nMigrating with latest state only:')
migrateWithLatestState()
}

main();
main()
191 changes: 95 additions & 96 deletions examples/optimize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { FSRSItem, FSRSReview, DEFAULT_PARAMETERS, FSRS } = require('../index')
import { FSRSItem, FSRSReview, DEFAULT_PARAMETERS, FSRS } from '../index'

function main() {
// Create review histories for cards
Expand Down Expand Up @@ -38,123 +38,123 @@ function createReviewHistoriesForCards() {

const reviewHistories = [
[
[new Date("2023-01-01"), 3],
[new Date("2023-01-02"), 4],
[new Date("2023-01-05"), 3],
[new Date("2023-01-15"), 4],
[new Date("2023-02-01"), 3],
[new Date("2023-02-20"), 4],
[new Date('2023-01-01'), 3],
[new Date('2023-01-02'), 4],
[new Date('2023-01-05'), 3],
[new Date('2023-01-15'), 4],
[new Date('2023-02-01'), 3],
[new Date('2023-02-20'), 4],
],
[
[new Date("2023-01-01"), 2],
[new Date("2023-01-02"), 3],
[new Date("2023-01-04"), 4],
[new Date("2023-01-12"), 3],
[new Date("2023-01-28"), 4],
[new Date("2023-02-15"), 3],
[new Date("2023-03-05"), 4],
[new Date('2023-01-01'), 2],
[new Date('2023-01-02'), 3],
[new Date('2023-01-04'), 4],
[new Date('2023-01-12'), 3],
[new Date('2023-01-28'), 4],
[new Date('2023-02-15'), 3],
[new Date('2023-03-05'), 4],
],
[
[new Date("2023-01-01"), 4],
[new Date("2023-01-08"), 4],
[new Date("2023-01-24"), 3],
[new Date("2023-02-10"), 4],
[new Date("2023-03-01"), 3],
[new Date('2023-01-01'), 4],
[new Date('2023-01-08'), 4],
[new Date('2023-01-24'), 3],
[new Date('2023-02-10'), 4],
[new Date('2023-03-01'), 3],
],
[
[new Date("2023-01-01"), 1],
[new Date("2023-01-02"), 1],
[new Date("2023-01-03"), 3],
[new Date("2023-01-06"), 4],
[new Date("2023-01-16"), 4],
[new Date("2023-02-01"), 3],
[new Date("2023-02-20"), 4],
[new Date('2023-01-01'), 1],
[new Date('2023-01-02'), 1],
[new Date('2023-01-03'), 3],
[new Date('2023-01-06'), 4],
[new Date('2023-01-16'), 4],
[new Date('2023-02-01'), 3],
[new Date('2023-02-20'), 4],
],
[
[new Date("2023-01-01"), 3],
[new Date("2023-01-03"), 3],
[new Date("2023-01-08"), 2],
[new Date("2023-01-10"), 4],
[new Date("2023-01-22"), 3],
[new Date("2023-02-05"), 4],
[new Date("2023-02-25"), 3],
[new Date('2023-01-01'), 3],
[new Date('2023-01-03'), 3],
[new Date('2023-01-08'), 2],
[new Date('2023-01-10'), 4],
[new Date('2023-01-22'), 3],
[new Date('2023-02-05'), 4],
[new Date('2023-02-25'), 3],
],
[
[new Date("2023-01-01"), 4],
[new Date("2023-01-09"), 3],
[new Date("2023-01-19"), 4],
[new Date("2023-02-05"), 3],
[new Date("2023-02-25"), 4],
[new Date('2023-01-01'), 4],
[new Date('2023-01-09'), 3],
[new Date('2023-01-19'), 4],
[new Date('2023-02-05'), 3],
[new Date('2023-02-25'), 4],
],
[
[new Date("2023-01-01"), 2],
[new Date("2023-01-02"), 3],
[new Date("2023-01-05"), 4],
[new Date("2023-01-15"), 3],
[new Date("2023-01-30"), 4],
[new Date("2023-02-15"), 3],
[new Date("2023-03-05"), 4],
[new Date('2023-01-01'), 2],
[new Date('2023-01-02'), 3],
[new Date('2023-01-05'), 4],
[new Date('2023-01-15'), 3],
[new Date('2023-01-30'), 4],
[new Date('2023-02-15'), 3],
[new Date('2023-03-05'), 4],
],
[
[new Date("2023-01-01"), 3],
[new Date("2023-01-04"), 4],
[new Date("2023-01-14"), 4],
[new Date("2023-02-01"), 3],
[new Date("2023-02-20"), 4],
[new Date('2023-01-01'), 3],
[new Date('2023-01-04'), 4],
[new Date('2023-01-14'), 4],
[new Date('2023-02-01'), 3],
[new Date('2023-02-20'), 4],
],
[
[new Date("2023-01-01"), 1],
[new Date("2023-01-01"), 3],
[new Date("2023-01-02"), 1],
[new Date("2023-01-02"), 3],
[new Date("2023-01-03"), 3],
[new Date("2023-01-07"), 3],
[new Date("2023-01-15"), 4],
[new Date("2023-01-31"), 3],
[new Date("2023-02-15"), 4],
[new Date("2023-03-05"), 3],
[new Date('2023-01-01'), 1],
[new Date('2023-01-01'), 3],
[new Date('2023-01-02'), 1],
[new Date('2023-01-02'), 3],
[new Date('2023-01-03'), 3],
[new Date('2023-01-07'), 3],
[new Date('2023-01-15'), 4],
[new Date('2023-01-31'), 3],
[new Date('2023-02-15'), 4],
[new Date('2023-03-05'), 3],
],
[
[new Date("2023-01-01"), 4],
[new Date("2023-01-10"), 3],
[new Date("2023-01-20"), 4],
[new Date("2023-02-05"), 4],
[new Date("2023-02-25"), 3],
[new Date("2023-03-15"), 4],
[new Date('2023-01-01'), 4],
[new Date('2023-01-10'), 3],
[new Date('2023-01-20'), 4],
[new Date('2023-02-05'), 4],
[new Date('2023-02-25'), 3],
[new Date('2023-03-15'), 4],
],
[
[new Date("2023-01-01"), 1],
[new Date("2023-01-02"), 2],
[new Date("2023-01-03"), 3],
[new Date("2023-01-04"), 4],
[new Date("2023-01-10"), 3],
[new Date("2023-01-20"), 4],
[new Date("2023-02-05"), 3],
[new Date("2023-02-25"), 4],
[new Date('2023-01-01'), 1],
[new Date('2023-01-02'), 2],
[new Date('2023-01-03'), 3],
[new Date('2023-01-04'), 4],
[new Date('2023-01-10'), 3],
[new Date('2023-01-20'), 4],
[new Date('2023-02-05'), 3],
[new Date('2023-02-25'), 4],
],
[
[new Date("2023-01-01"), 3],
[new Date("2023-01-05"), 4],
[new Date("2023-01-15"), 3],
[new Date("2023-01-30"), 4],
[new Date("2023-02-15"), 3],
[new Date("2023-03-05"), 4],
[new Date('2023-01-01'), 3],
[new Date('2023-01-05'), 4],
[new Date('2023-01-15'), 3],
[new Date('2023-01-30'), 4],
[new Date('2023-02-15'), 3],
[new Date('2023-03-05'), 4],
],
[
[new Date("2023-01-01"), 2],
[new Date("2023-01-03"), 3],
[new Date("2023-01-07"), 4],
[new Date("2023-01-17"), 3],
[new Date("2023-02-01"), 4],
[new Date("2023-02-20"), 3],
[new Date("2023-03-10"), 4],
[new Date('2023-01-01'), 2],
[new Date('2023-01-03'), 3],
[new Date('2023-01-07'), 4],
[new Date('2023-01-17'), 3],
[new Date('2023-02-01'), 4],
[new Date('2023-02-20'), 3],
[new Date('2023-03-10'), 4],
],
[
[new Date("2023-01-01"), 4],
[new Date("2023-01-12"), 3],
[new Date("2023-01-25"), 4],
[new Date("2023-02-10"), 3],
[new Date("2023-03-01"), 4],
[new Date('2023-01-01'), 4],
[new Date('2023-01-12'), 3],
[new Date('2023-01-25'), 4],
[new Date('2023-02-10'), 3],
[new Date('2023-03-01'), 4],
],
]
// Cycle and repeat the array to create a larger dataset of 100 cards
Expand All @@ -177,7 +177,6 @@ function convertToFSRSItem(history) {
let lastDate = history[0][0]
const items = []


for (const [date, rating] of history) {
const deltaT = dateDiffInDays(lastDate, date)
reviews.push(new FSRSReview(rating, deltaT))
Expand All @@ -196,10 +195,10 @@ function convertToFSRSItem(history) {
* @returns {number} The number of days between the two dates.
*/
function dateDiffInDays(a, b) {
const _MS_PER_DAY = 1000 * 60 * 60 * 24;
const _MS_PER_DAY = 1000 * 60 * 60 * 24
// Discard the time and time-zone information.
const utc1 = Date.UTC(a.getUTCFullYear(), a.getUTCMonth(), a.getUTCDate());
const utc2 = Date.UTC(b.getUTCFullYear(), b.getUTCMonth(), b.getUTCDate());
const utc1 = Date.UTC(a.getUTCFullYear(), a.getUTCMonth(), a.getUTCDate())
const utc2 = Date.UTC(b.getUTCFullYear(), b.getUTCMonth(), b.getUTCDate())

return Math.floor((utc2 - utc1) / _MS_PER_DAY)
}
Expand Down
Loading

0 comments on commit 4c3b18c

Please sign in to comment.