DKL9 GitList
Repositories
DKL9 home
memoire
Code
Commits
Branches
Tags
Search
Tree:
6975b4b
Branches
Tags
master
memoire
prompt.lua
Fix bug in 9b023b3
John Smith
commited
6975b4b
at 2022-206 08:50:59
prompt.lua
Blame
History
Raw
--[[ a prompt consists of prompt text (with an answer-insertion point), a correct answer, the review timestamp, the review delay, counts of successful and failed reviews, a creation timestamp, and an ease measurement timestamps are seconds from the Unix epoch review delay is in seconds ]] local Prompt = {} Prompt.__index = Prompt --[[ the answer-insertion point is indicated with a "%A" in the prompt text ]] function Prompt:new(pt, ca, rts, rd, sc, fc, ct, ez) local o = {} setmetatable(o, self) o.text = pt o.ans = ca o.time = tonumber(rts) o.delay = tonumber(rd) o.succ = tonumber(sc) o.fail = tonumber(fc) o.created = ct o.ease = tonumber(ez) return o end function Prompt:__tostring() return string.format("\"%s\" (\"%s\") %d -> %d+%d*%d %d/%d", self.text, self.ans, self.created, self.time, self.delay, self.ease, self.succ, self.fail) end --[[ derive the `PromptText` used to create this `Prompt` ]] function Prompt:spt() return self.text:gsub("%%A", "{" .. self.ans:gsub("%%", "%%%%") .. "}") 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], (rec.LastDelay or {})[ctr], (rec.Successes or {})[ctr], (rec.Failures or {})[ctr], (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