stenodojo

stenodojo
git clone git@git.zachrice.app:repos/stenodojo.git
Log | Files | Refs

spaced_repetition.py (1840B)


      1 """SM-2 spaced repetition algorithm for stenography practice."""
      2 
      3 
      4 def sm2(quality, ease_factor, interval, repetitions):
      5     """
      6     SM-2 spaced repetition algorithm.
      7 
      8     quality: 0-5 (0=blackout, 5=perfect recall)
      9     ease_factor: current ease factor (starts at 2.5)
     10     interval: current interval in days
     11     repetitions: number of consecutive correct reviews
     12 
     13     Returns: (new_ease_factor, new_interval, new_repetitions)
     14     """
     15     # Calculate new ease factor
     16     new_ef = ease_factor + (0.1 - (5 - quality) * (0.08 + (5 - quality) * 0.02))
     17     new_ef = max(1.3, new_ef)
     18 
     19     if quality >= 3:
     20         # Correct response
     21         new_repetitions = repetitions + 1
     22         if repetitions == 0:
     23             new_interval = 1.0
     24         elif repetitions == 1:
     25             new_interval = 6.0
     26         else:
     27             new_interval = interval * new_ef
     28     else:
     29         # Incorrect response — reset
     30         new_repetitions = 0
     31         new_interval = 1.0
     32         new_ef = ease_factor  # Don't change ease factor on failure
     33 
     34     return (new_ef, new_interval, new_repetitions)
     35 
     36 
     37 def calculate_review(correct, time_ms, ease_factor, interval, repetitions):
     38     """
     39     Map practice result to SM-2 quality score and calculate next review.
     40 
     41     correct: whether the answer was correct
     42     time_ms: time taken in milliseconds
     43     ease_factor: current ease factor
     44     interval: current interval in days
     45     repetitions: consecutive correct count
     46 
     47     Returns: (new_ease_factor, new_interval, new_repetitions)
     48     """
     49     if correct:
     50         if time_ms < 2000:
     51             quality = 5  # Perfect, fast recall
     52         elif time_ms < 5000:
     53             quality = 4  # Correct with hesitation
     54         else:
     55             quality = 3  # Correct but slow
     56     else:
     57         quality = 1  # Incorrect
     58 
     59     return sm2(quality, ease_factor, interval, repetitions)