review

package
v2.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package review implements business logic for managing flashcard review sessions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAmbiguousAnswers is thrown if a flashcard has contradictory answers.
	ErrAmbiguousAnswers = errors.New("answers are ambiguous")
	// ErrNotFound is thrown if the specified data isn't found.
	ErrNotFound = errors.New("not found")
)

Functions

This section is empty.

Types

type FirestoreStore

type FirestoreStore struct {
	// contains filtered or unexported fields
}

FirestoreStore stores a review session's state in a Cloud Firestore database.

func NewFirestoreStore

func NewFirestoreStore(client *firestore.Client, collection string) *FirestoreStore

NewFirestoreStore returns a new FirestoreStore that stores data in the specified collection.

func (*FirestoreStore) DeleteFlashcards

func (s *FirestoreStore) DeleteFlashcards(ctx context.Context, sessionID string, ids []int64) error

DeleteFlashcards deletes the specified flashcards.

func (*FirestoreStore) GetFlashcard

func (s *FirestoreStore) GetFlashcard(ctx context.Context, sessionID string, flashcardID int64) (*Flashcard, error)

GetFlashcard returns the specified flashcard.

func (*FirestoreStore) GetFlashcards

func (s *FirestoreStore) GetFlashcards(ctx context.Context, sessionID string) ([]*Flashcard, error)

GetFlashcards returns all flashcards.

func (*FirestoreStore) GetSession

func (s *FirestoreStore) GetSession(ctx context.Context, sessionID string) (*Session, error)

GetSession returns the current session metadata.

func (*FirestoreStore) GetSessions

func (s *FirestoreStore) GetSessions(ctx context.Context) ([]*Session, error)

GetSessions returns the metadata for all existing sessions.

func (*FirestoreStore) NextReviewed

func (s *FirestoreStore) NextReviewed(ctx context.Context, sessionID string, round int) (*Flashcard, error)

NextReviewed returns a flashcard that is due to be reviewed again.

func (*FirestoreStore) NextUnreviewed

func (s *FirestoreStore) NextUnreviewed(ctx context.Context, sessionID string) (*Flashcard, error)

NextUnreviewed returns a flashcard that has never been reviewed before.

func (*FirestoreStore) SetFlashcardStats

func (s *FirestoreStore) SetFlashcardStats(ctx context.Context, sessionID string, flashcardID int64, stats *FlashcardStats) error

SetFlashcardStats updates a flashcard's stats.

func (*FirestoreStore) SetFlashcards

func (s *FirestoreStore) SetFlashcards(ctx context.Context, sessionID string, metadata []*FlashcardMetadata) error

SetFlashcards upserts the specified flashcards, clearing any existing stats.

func (*FirestoreStore) SetSession

func (s *FirestoreStore) SetSession(ctx context.Context, sessionID string, session *Session) error

SetSession updates the session metadata.

type Flashcard

type Flashcard struct {
	// Metadata stores immutable data like the prompt and answer.
	Metadata FlashcardMetadata `firestore:"metadata" json:"metadata"`
	// Stats stores mutable data like the view count.
	Stats FlashcardStats `firestore:"stats" json:"stats"`
}

Flashcard represents the state of a flashcard.

func (*Flashcard) Submit

func (f *Flashcard) Submit(submission *Submission, round int) bool

Submit updates the flashcard's stats after being reviewed. Returns true if and only if the answer is correct.

type FlashcardMetadata

type FlashcardMetadata struct {
	// ID uniquely identifies the flashcard.
	ID int64 `firestore:"id" json:"id"`
	// Prompt is the text to be shown to the user.
	Prompt string `firestore:"prompt" json:"prompt"`
	// Context helps narrow down possible answers.
	Context string `firestore:"context,omitempty" json:"context,omitempty"`
	// Answer is the accepted answer.
	Answer string `firestore:"answer" json:"answer"`
}

FlashcardMetadata stores immutable flashcard data like the prompt.

type FlashcardMetadataSource

type FlashcardMetadataSource interface {
	// GetAll returns the metadata for all flashcards.
	GetAll(ctx context.Context) ([]*FlashcardMetadata, error)
}

FlashcardMetadataSource is the source of truth for flashcard metadata.

type FlashcardStats

type FlashcardStats struct {
	// ViewCount is the number of times the flashcard has been reviewed.
	ViewCount int `firestore:"viewCount" json:"viewCount"`
	// Repetitions is the number of successful reviews in a row.
	Repetitions int `firestore:"repetitions,omitempty" json:"repetitions"`
	// NextReview is the round in which the card is due to be reviewed next.
	NextReview int `firestore:"nextReview,omitempty"`
}

FlashcardStats stores mutable flashcard data like the view count.

type MemorySource

type MemorySource struct {
	// contains filtered or unexported fields
}

MemorySource stores flashcard metadata in memory.

func NewMemorySource

func NewMemorySource(metadata []*FlashcardMetadata) *MemorySource

NewMemorySource returns a new MemorySource with the specified flashcards.

func (*MemorySource) GetAll

func (s *MemorySource) GetAll(_ context.Context) ([]*FlashcardMetadata, error)

GetAll returns the metadata for all flashcards.

type MemoryStore

type MemoryStore struct {
	// contains filtered or unexported fields
}

MemoryStore stores a review session's state in memory. It's unoptimized and mainly intended for use in tests.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore returns a new empty MemoryStore.

func (*MemoryStore) DeleteFlashcards

func (s *MemoryStore) DeleteFlashcards(_ context.Context, sessionID string, ids []int64) error

DeleteFlashcards deletes the specified flashcards.

func (*MemoryStore) GetFlashcard

func (s *MemoryStore) GetFlashcard(_ context.Context, sessionID string, flashcardID int64) (*Flashcard, error)

GetFlashcard returns the specified flashcard.

func (*MemoryStore) GetFlashcards

func (s *MemoryStore) GetFlashcards(_ context.Context, sessionID string) ([]*Flashcard, error)

GetFlashcards returns all flashcards.

func (*MemoryStore) GetSession

func (s *MemoryStore) GetSession(_ context.Context, sessionID string) (*Session, error)

GetSession returns the current session metadata.

func (*MemoryStore) GetSessions

func (s *MemoryStore) GetSessions(_ context.Context) ([]*Session, error)

GetSessions returns the metadata for all existing sessions.

func (*MemoryStore) NextReviewed

func (s *MemoryStore) NextReviewed(_ context.Context, sessionID string, round int) (*Flashcard, error)

NextReviewed returns a flashcard that is due to be reviewed again.

func (*MemoryStore) NextUnreviewed

func (s *MemoryStore) NextUnreviewed(_ context.Context, sessionID string) (*Flashcard, error)

NextUnreviewed returns a flashcard that has never been reviewed before.

func (*MemoryStore) SetFlashcardStats

func (s *MemoryStore) SetFlashcardStats(_ context.Context, sessionID string, flashcardID int64, stats *FlashcardStats) error

SetFlashcardStats updates a flashcard's stats.

func (*MemoryStore) SetFlashcards

func (s *MemoryStore) SetFlashcards(_ context.Context, sessionID string, metadata []*FlashcardMetadata) error

SetFlashcards upserts the specified flashcards, clearing any existing stats.

func (*MemoryStore) SetSession

func (s *MemoryStore) SetSession(_ context.Context, sessionID string, session *Session) error

SetSession updates the session metadata.

type Reviewer

type Reviewer struct {
	// contains filtered or unexported fields
}

Reviewer manages flashcard review sessions.

func NewReviewer

func NewReviewer(store SessionStore) *Reviewer

NewReviewer returns a new flashcard reviewer.

func (*Reviewer) CreateSession

func (r *Reviewer) CreateSession(ctx context.Context, source FlashcardMetadataSource, numProficiencyLevels int) (*Session, error)

CreateSession creates a new session with all flashcards marked as unreviewed.

func (*Reviewer) GetFlashcards

func (r *Reviewer) GetFlashcards(ctx context.Context, sessionID string) ([]*Flashcard, error)

GetFlashcards returns all flashcards.

func (*Reviewer) GetSession

func (r *Reviewer) GetSession(ctx context.Context, sessionID string) (*Session, error)

GetSession returns an existing session.

func (*Reviewer) GetSessions

func (r *Reviewer) GetSessions(ctx context.Context) ([]*Session, error)

GetSessions returns all existing sessions.

func (*Reviewer) NextFlashcard

func (r *Reviewer) NextFlashcard(ctx context.Context, sessionID string) (*Flashcard, error)

NextFlashcard returns the next flashcard to be reviewed.

func (*Reviewer) Submit

func (r *Reviewer) Submit(ctx context.Context, sessionID string, flashcardID int64, submission *Submission) (*Session, bool, error)

Submit updates the session state following the review of a flashcard.

func (*Reviewer) SyncFlashcards

func (r *Reviewer) SyncFlashcards(ctx context.Context, sessionID string, source FlashcardMetadataSource) (*Session, error)

SyncFlashcards ensures that the session data is up to date with the flashcard metadata source.

type Session

type Session struct {
	// ID uniquely identifies a review session.
	ID string `firestore:"id" json:"id"`
	// Round is an incrementing counter that identifies the current round.
	Round int `firestore:"round"`
	// IsNewRound is true if and only if the round has just started.
	IsNewRound bool `firestore:"isNewRound"`
	// ProficiencyCounts is the number of flashcards at each proficiency level.
	ProficiencyCounts []int `firestore:"proficiencyCounts" json:"proficiencyCounts"`
	// UnreviewedCount is the number of flashcards that haven't been reviewed yet.
	UnreviewedCount int `firestore:"unreviewedCount" json:"unreviewedCount"`
}

Session represents review session metadata.

func NewSession

func NewSession(sessionID string, numProficiencyLevels int) *Session

NewSession initializes session metadata for the case where no flashcards have been added yet.

func (*Session) IncrementProficiency

func (session *Session) IncrementProficiency(repetitions int, increment int)

IncrementProficiency adjusts the proficiency count. Specifically, it adjusts the count of flashcards for the proficiency level corresponding to the number of repetitions (correct answers in a row) by the specified increment (positive or negative).

type SessionStore

type SessionStore interface {
	// DeleteFlashcards deletes the specified flashcards.
	DeleteFlashcards(ctx context.Context, sessionID string, ids []int64) error
	// GetFlashcard returns the specified flashcard.
	GetFlashcard(ctx context.Context, sessionID string, flashcardID int64) (*Flashcard, error)
	// GetFlashcards returns all flashcards.
	GetFlashcards(ctx context.Context, sessionID string) ([]*Flashcard, error)
	// SetFlashcards upserts the specified flashcards, clearing any existing stats.
	SetFlashcards(ctx context.Context, sessionID string, metadata []*FlashcardMetadata) error
	// SetFlashcardStats updates a flashcard's stats.
	SetFlashcardStats(ctx context.Context, sessionID string, flashcardID int64, stats *FlashcardStats) error
	// NextReviewed returns a flashcard that is due to be reviewed again.
	NextReviewed(ctx context.Context, sessionID string, round int) (*Flashcard, error)
	// NextUnreviewed returns a flashcard that has never been reviewed before.
	NextUnreviewed(ctx context.Context, sessionID string) (*Flashcard, error)
	// GetSession returns the current session metadata.
	GetSession(ctx context.Context, sessionID string) (*Session, error)
	// GetSessions returns the metadata for all existing sessions.
	GetSessions(ctx context.Context) ([]*Session, error)
	// SetSession updates the session metadata.
	SetSession(ctx context.Context, sessionID string, session *Session) error
}

SessionStore stores the state of a review session.

type SheetSource

type SheetSource struct {
	// SpreadsheetID uniquely identifies the spreadsheet.
	SpreadsheetID string `json:"spreadsheetId"`
	// CellRange is the range of cells containing the data.
	CellRange string `json:"cellRange"`
	// IDHeader is the name of the column containing unique IDs.
	IDHeader string `json:"idHeader"`
	// PromptHeader is the name of the column containing the prompts.
	PromptHeader string `json:"promptHeader"`
	// ContextHeader is the name of the column containing the context (if any).
	ContextHeader string `json:"contextHeader"`
	// AnswerHeader is the name of the column containing the answers.
	AnswerHeader string `json:"answerHeader"`
}

SheetSource stores flashcard metadata in a Google Sheets spreadsheet.

func (*SheetSource) GetAll

func (s *SheetSource) GetAll(ctx context.Context) ([]*FlashcardMetadata, error)

GetAll returns the metadata for all flashcards.

type Submission

type Submission struct {
	// Answer is the submitted answer.
	Answer string `json:"answer"`
	// IsFirstGuess is true if and only if this is the user's first guess.
	IsFirstGuess bool `firestore:"isFirstGuess"`
}

Submission represents a user's answer to a flashcard prompt.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL