Class: FsrsRuby::FSRSInstance

Inherits:
Algorithm show all
Defined in:
lib/fsrs_ruby/fsrs_instance.rb

Overview

Main FSRS class with public API

Instance Attribute Summary

Attributes inherited from Algorithm

#interval_modifier, #parameters, #seed

Instance Method Summary collapse

Methods inherited from Algorithm

#apply_fuzz, #calculate_interval_modifier, #compute_decay_factor, #forgetting_curve, #init_difficulty, #init_stability, #linear_damping, #mean_reversion, #next_difficulty, #next_forget_stability, #next_interval, #next_recall_stability, #next_short_term_stability, #next_state

Constructor Details

#initialize(params = {}) ⇒ FSRSInstance

Returns a new instance of FSRSInstance.



6
7
8
9
10
# File 'lib/fsrs_ruby/fsrs_instance.rb', line 6

def initialize(params = {})
  super(params)
  @strategy_handlers = {}
  @scheduler_class = @parameters.enable_short_term ? Schedulers::BasicScheduler : Schedulers::LongTermScheduler
end

Instance Method Details

#clear_strategy(mode = nil) ⇒ self

Clear strategy handler(s)

Parameters:

  • mode (Symbol, nil) (defaults to: nil)

    Strategy mode to clear, or nil to clear all

Returns:

  • (self)


26
27
28
29
# File 'lib/fsrs_ruby/fsrs_instance.rb', line 26

def clear_strategy(mode = nil)
  mode ? @strategy_handlers.delete(mode) : @strategy_handlers.clear
  self
end

#forget(card, now, reset_count: false) ⇒ RecordLogItem

Reset card to NEW state

Parameters:

  • card (Card)

    Card to reset

  • now (Time, Integer, String)

    Current time

  • reset_count (Boolean) (defaults to: false)

    Whether to reset reps and lapses

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fsrs_ruby/fsrs_instance.rb', line 89

def forget(card, now, reset_count: false)
  card = card.is_a?(Card) ? card : TypeConverter.card(card)
  now = now.is_a?(Time) ? now : TypeConverter.time(now)

  new_card = ParameterUtils.create_empty_card(now)
  new_card.reps = reset_count ? 0 : card.reps
  new_card.lapses = reset_count ? 0 : card.lapses

  log = ReviewLog.new(
    rating: Rating::MANUAL,
    state: card.state,
    due: card.due,
    stability: card.stability,
    difficulty: card.difficulty,
    elapsed_days: 0,
    last_elapsed_days: card.scheduled_days,
    scheduled_days: 0,
    learning_steps: 0,
    review: now
  )

  RecordLogItem.new(card: new_card, log: log)
end

#get_retrievability(card, now = nil, format: true) ⇒ String, Float

Get retrievability (probability of recall) for a card

Parameters:

  • card (Card, Hash)

    Card

  • now (Time, Integer, String, nil) (defaults to: nil)

    Current time (defaults to Time.now)

  • format (Boolean) (defaults to: true)

    If true, return percentage string; if false, return decimal

Returns:

  • (String, Float)

    Retrievability



55
56
57
58
59
60
61
62
63
# File 'lib/fsrs_ruby/fsrs_instance.rb', line 55

def get_retrievability(card, now = nil, format: true)
  card = card.is_a?(Card) ? card : TypeConverter.card(card)
  now = now ? TypeConverter.time(now) : Time.now

  elapsed_days = Helpers.date_diff(now, card.last_review || card.due, :days)
  retrievability = forgetting_curve(@parameters.w, elapsed_days, card.stability)

  format ? "#{(retrievability * 100).round(2)}%" : retrievability
end

#next(card, now, grade) ⇒ RecordLogItem

Apply a specific rating to a card

Parameters:

  • card (Card, Hash)

    Card to review

  • now (Time, Integer, String)

    Review time

  • grade (Integer)

    Rating (1=Again, 2=Hard, 3=Good, 4=Easy)

Returns:



45
46
47
48
# File 'lib/fsrs_ruby/fsrs_instance.rb', line 45

def next(card, now, grade)
  scheduler = get_scheduler(card, now)
  scheduler.review(grade)
end

#repeat(card, now) ⇒ Hash

Preview all possible ratings for a card

Parameters:

  • card (Card, Hash)

    Card to review

  • now (Time, Integer, String)

    Review time

Returns:

  • (Hash)

    { Rating::AGAIN =>, Rating::HARD =>, Rating::GOOD =>, Rating::EASY => }



35
36
37
38
# File 'lib/fsrs_ruby/fsrs_instance.rb', line 35

def repeat(card, now)
  scheduler = get_scheduler(card, now)
  scheduler.preview
end

#rollback(card, log) ⇒ Card

Rollback a review

Parameters:

  • card (Card)

    Card after review

  • log (ReviewLog)

    Review log

Returns:

  • (Card)

    Card before the review



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/fsrs_ruby/fsrs_instance.rb', line 69

def rollback(card, log)
  Card.new(
    due: log.due,
    stability: log.stability,
    difficulty: log.difficulty,
    elapsed_days: log.elapsed_days,
    scheduled_days: log.last_elapsed_days,
    learning_steps: log.learning_steps,
    reps: card.reps - 1,
    lapses: log.rating == Rating::AGAIN ? card.lapses - 1 : card.lapses,
    state: log.state,
    last_review: card.last_review
  )
end

#use_strategy(mode, handler) ⇒ self

Register a strategy handler

Parameters:

  • mode (Symbol)

    Strategy mode (:scheduler, :learning_steps, :seed)

  • handler (Proc, Method)

    Strategy handler

Returns:

  • (self)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/fsrs_ruby/fsrs_instance.rb', line 16

def use_strategy(mode, handler)
  raise ArgumentError, 'Handler must respond to :call' unless handler.respond_to?(:call)

  @strategy_handlers[mode] = handler
  self
end