DKL9 GitList
Repositories
DKL9 home
memoire
Code
Commits
Branches
Tags
Search
Tree:
b39812d
Branches
Tags
master
memoire
src
prompt.lua
Reformulate data structures
John Smith
commited
b39812d
at 2023-177 12:35:37
prompt.lua
Blame
History
Raw
--[[ a prompt consists of a parent card, an index into the note, the review timestamp, the review delay, counts of successful and failed reviews, and an ease measurement timestamps are seconds from the Unix epoch review delay is in seconds ]] local Prompt = {} Prompt.__index = Prompt function Prompt:new(prtable) local o = { card = {}, ind = 0, time = os.time(), delay = 30, succ = 0, fail = 0, ease = 2.5 } setmetatable(o, self) for k, v in pairs(prtable) do if o[k] then o[k] = v end end return o end function Prompt:__tostring() return string.format( "\"%s\" (\"%s\") %d -> %d+%d*%f %d/%d", self:text(), self:ans(), self.card.created or 0, self.time, math.floor(self.delay), self.ease, self.succ, self.fail ) end --[[ the answer-insertion point is indicated with a "%A" in the prompt text ]] function Prompt:text() if self.ind == 0 then return "error: unpopulated ind" end if self.card.ct == "bmc" then local _, eoa = self.card.note:find("{[^}]+}", self.ind) return self.card.note:sub(1, self.ind - 1) .. "%A" .. self.card.note:sub(eoa + 1) else return "error: unrecognised card type " .. self.card.ct end end function Prompt:ans() if self.ind == 0 then return "error: unpopulated ind" end if self.card.ct == "bmc" then return self.card.note:match("{([^}]+)}", self.ind) else return "error: unrecognised card type " .. self.card.ct end end function Prompt:retention() return self.succ / (self.succ + self.fail) end --[[ extracts `Prompt`s from a record-based deck returns a table of tables of `Prompt`s ]] function Prompt:fromrecs(recs, conf) local ret = {} for i, rec in ipairs(recs) do -- only bother with records with `PromptText` if rec.PromptText then ret[i] = {} local opt = rec.PromptText[1] local m, n = opt:find "%{[^%}]+%}" local ctr = 1 while m do local mpt = opt:sub(1, m - 1) .. "%A" .. opt:sub(n + 1) table.insert(ret[i], Prompt:new( mpt, opt:sub(m + 1, n - 1), (rec.LastReview or {})[ctr] or (rec.LastReview or {})[1], (rec.LastDelay or {})[ctr] or 15, (rec.Successes or {})[ctr] or 0, (rec.Failures or {})[ctr] or 0, (rec.Created or {})[1], (rec.Ease or {})[ctr] or conf:get("StartingEase") or "2.5" )) m, n = opt:find("%{[^%}]+%}", n) ctr = ctr + 1 end end end return ret end --[[ serialises an ordered table of `Prompt`s derived from the same `PromptText` to a record returns (on success) a table mapping labels to value-lists returns (on failure) nil ]] function Prompt:torec(prompts) if prompts[1].deleted then return {} end local fspt = prompts[1]:spt() local ret = { PromptText = { fspt }, LastReview = {}, LastDelay = {}, Successes = {}, Failures = {}, Ease = {} } if prompts[1].created then ret.Created = { prompts[1].created } end for i, pr in ipairs(prompts) do if pr:spt() ~= fspt then -- not from the same `PromptText` return nil end ret.LastReview[i] = pr.time ret.LastDelay[i] = pr.delay ret.Successes[i] = pr.succ ret.Failures[i] = pr.fail ret.Ease[i] = pr.ease end return ret end return Prompt