Class: Nagori::FSRS

Inherits:
Object
  • Object
show all
Defined in:
lib/nagori.rb

Overview

A configured FSRS-6 scheduler. Build once per parameter set and reuse.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(parameters = nil) ⇒ Object

nil/empty parameters use DEFAULT_PARAMETERS; 17/19/21-length vectors are accepted (padded internally). Invalid input raises ArgumentError.



149
150
151
# File 'lib/nagori.rb', line 149

def self.new(parameters = nil)
  _new(Nagori.params_array(parameters))
end

Instance Method Details

#evaluate(items) ⇒ Object

Model fit over items (array of review arrays). Returns { log_loss:, rmse_bins: }. Releases the GVL.



212
213
214
215
# File 'lib/nagori.rb', line 212

def evaluate(items)
  log_loss, rmse_bins = _evaluate(Nagori.flatten_items(items))
  { log_loss: log_loss, rmse_bins: rmse_bins }
end

#memory_state(reviews, starting_state = nil) ⇒ Object

Memory state after replaying reviews (array of { rating:, delta_t: }). starting_state seeds a truncated history (e.g. from SM-2). Returns { stability:, difficulty: }.



179
180
181
182
# File 'lib/nagori.rb', line 179

def memory_state(reviews, starting_state = nil)
  pair = _memory_state(Nagori.flatten_reviews(reviews), Nagori.memory_pair_or_empty(starting_state))
  { stability: pair[0], difficulty: pair[1] }
end

#memory_state_batch(items, starting_states = nil) ⇒ Object

Batch form of #memory_state. items is an array of review arrays; starting_states is a matching array of { stability:, difficulty: } or nil (defaults to all-new). Returns an array of { stability:, difficulty: }.



187
188
189
190
191
192
193
# File 'lib/nagori.rb', line 187

def memory_state_batch(items, starting_states = nil)
  flat_items = Nagori.flatten_items(items)
  starts = normalize_starting_states(starting_states, flat_items.length)
  _memory_state_batch(flat_items, starts).each_slice(2).map do |stability, difficulty|
    { stability: stability, difficulty: difficulty }
  end
end

#memory_state_from_sm2(ease_factor, interval, sm2_retention) ⇒ Object

Approximate a memory state from SM-2 values for cards imported without a full revlog. Returns { stability:, difficulty: }.



197
198
199
200
# File 'lib/nagori.rb', line 197

def memory_state_from_sm2(ease_factor, interval, sm2_retention)
  pair = _memory_state_from_sm2(Float(ease_factor), Float(interval), Float(sm2_retention))
  { stability: pair[0], difficulty: pair[1] }
end

#next_interval(stability, desired_retention, rating) ⇒ Object

Interval (fractional days) for a memory state at the desired retention. stability is nil for a new card (initial stability for rating is used; rating is otherwise ignored).



205
206
207
208
# File 'lib/nagori.rb', line 205

def next_interval(stability, desired_retention, rating)
  stab = stability.nil? ? [] : [Float(stability)]
  _next_interval(stab, Float(desired_retention), Nagori.rating_int(rating))
end

#next_states(memory_state, desired_retention, days_elapsed) ⇒ Object

Memory states and intervals for each answer button. memory_state is nil for a new card or { stability:, difficulty: }. Returns { again:, hard:, good:, easy: }, each { stability:, difficulty:, interval: } with interval in fractional, unrounded days.



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/nagori.rb', line 162

def next_states(memory_state, desired_retention, days_elapsed)
  raw = _next_states(
    Nagori.memory_pair_or_empty(memory_state),
    Float(desired_retention),
    Integer(days_elapsed)
  )
  {
    again: state_at(raw, 0),
    hard: state_at(raw, 3),
    good: state_at(raw, 6),
    easy: state_at(raw, 9)
  }
end

#parametersObject

The 21-float parameter vector (input padded to FSRS-6 length).



154
155
156
# File 'lib/nagori.rb', line 154

def parameters
  _parameters
end