From ddfc1cbd66c88c8612d5f652aeb474cb0971d9c8 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Wed, 22 Oct 2025 22:08:38 +0800 Subject: [PATCH] :art: Improve FSRS weight initialization https://github.com/siyuan-note/siyuan/issues/16181 Signed-off-by: Daniel <845765@qq.com> --- kernel/conf/flashcard.go | 22 +++++++++++++--------- kernel/go.mod | 4 ++-- kernel/go.sum | 8 ++++---- kernel/model/conf.go | 27 +++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/kernel/conf/flashcard.go b/kernel/conf/flashcard.go index 869ba7cea..596e06acc 100644 --- a/kernel/conf/flashcard.go +++ b/kernel/conf/flashcard.go @@ -41,14 +41,6 @@ type Flashcard struct { func NewFlashcard() *Flashcard { param := fsrs.DefaultParam() - weightsBuilder := bytes.Buffer{} - for i, w := range param.W { - weightsBuilder.WriteString(fmt.Sprintf("%.2f", w)) - if i < len(param.W)-1 { - weightsBuilder.WriteString(", ") - } - } - return &Flashcard{ NewCardLimit: 20, ReviewCardLimit: 200, @@ -60,6 +52,18 @@ func NewFlashcard() *Flashcard { ReviewMode: 0, RequestRetention: param.RequestRetention, MaximumInterval: int(param.MaximumInterval), - Weights: weightsBuilder.String(), + Weights: DefaultFSRSWeights(), } } + +func DefaultFSRSWeights() string { + buf := bytes.Buffer{} + defaultWs := fsrs.DefaultWeights() + for i, w := range defaultWs { + buf.WriteString(fmt.Sprintf("%v", w)) + if i < len(defaultWs)-1 { + buf.WriteString(", ") + } + } + return buf.String() +} diff --git a/kernel/go.mod b/kernel/go.mod index d234d18f8..5bf6abb7d 100644 --- a/kernel/go.mod +++ b/kernel/go.mod @@ -64,7 +64,7 @@ require ( github.com/siyuan-note/filelock v0.0.0-20251010020544-2603449ff16d github.com/siyuan-note/httpclient v0.0.0-20251016024153-048ea2b2ebfd github.com/siyuan-note/logging v0.0.0-20250425042449-b96c40249b54 - github.com/siyuan-note/riff v0.0.0-20250424035337-0a1e91809f6d + github.com/siyuan-note/riff v0.0.0-20251022131846-228528e70754 github.com/spf13/cast v1.10.0 github.com/steambap/captcha v1.4.1 github.com/studio-b12/gowebdav v0.11.0 @@ -145,7 +145,7 @@ require ( github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 // indirect github.com/jolestar/go-commons-pool/v2 v2.1.2 // indirect github.com/juju/errors v1.0.0 // indirect - github.com/klauspost/compress v1.18.0 // indirect + github.com/klauspost/compress v1.18.1 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/levigross/exp-html v0.0.0-20120902181939-8df60c69a8f5 // indirect diff --git a/kernel/go.sum b/kernel/go.sum index c7a748216..8175adc24 100644 --- a/kernel/go.sum +++ b/kernel/go.sum @@ -258,8 +258,8 @@ github.com/juju/errors v1.0.0/go.mod h1:B5x9thDqx0wIMH3+aLIMP9HjItInYWObRovoCFM5 github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U= github.com/juju/testing v0.0.0-20191001232224-ce9dec17d28b/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA= github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= -github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= -github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co= +github.com/klauspost/compress v1.18.1/go.mod h1:ZQFFVG+MdnR0P+l6wpXgIL4NTtwiKIdBnrBd8Nrxr+0= github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= @@ -386,8 +386,8 @@ github.com/siyuan-note/httpclient v0.0.0-20251016024153-048ea2b2ebfd h1:g67edlPa github.com/siyuan-note/httpclient v0.0.0-20251016024153-048ea2b2ebfd/go.mod h1:xbFmjardDUhZL5+nKVwIr3dEbyhXC2eK3bbSYVR1GRE= github.com/siyuan-note/logging v0.0.0-20250425042449-b96c40249b54 h1:bNfLBxasCNYNIiE8YwIDDq0DxFjQ3aDbp25w8F5aShM= github.com/siyuan-note/logging v0.0.0-20250425042449-b96c40249b54/go.mod h1:9lb3q2RCdpjoQk7nDQwLSArNtCLafHwWtl8+kNkSISM= -github.com/siyuan-note/riff v0.0.0-20250424035337-0a1e91809f6d h1:VJ/DuLIx4FE7MZtlX2Gahx2m9tr4YCXPxZxUXZIWRx4= -github.com/siyuan-note/riff v0.0.0-20250424035337-0a1e91809f6d/go.mod h1:oz+GggNPu23prmW5/8aqpu9Zs5eQBgN+YQJzFLxbT4g= +github.com/siyuan-note/riff v0.0.0-20251022131846-228528e70754 h1:6QYpy7s5HlRSge09TyM/mT0vz1RDcWYZdkxEh7hmbH4= +github.com/siyuan-note/riff v0.0.0-20251022131846-228528e70754/go.mod h1:/N7+N2CsZ0nleNPpP3b+06Bzqvuhy6GUmLY7Kug/zT0= github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY= github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo= github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo= diff --git a/kernel/model/conf.go b/kernel/model/conf.go index 76897e7e7..1043a69b7 100644 --- a/kernel/model/conf.go +++ b/kernel/model/conf.go @@ -458,6 +458,33 @@ func InitConf() { if "" == Conf.Flashcard.Weights { Conf.Flashcard.Weights = conf.NewFlashcard().Weights } + if 19 != len(strings.Split(Conf.Flashcard.Weights, ",")) { + defaultWeights := conf.DefaultFSRSWeights() + msg := "fsrs store weights length must be [19]" + logging.LogWarnf("%s , given [%s], reset to default weights [%s]", msg, Conf.Flashcard.Weights, defaultWeights) + Conf.Flashcard.Weights = defaultWeights + go func() { + util.WaitForUILoaded() + task.AppendAsyncTaskWithDelay(task.PushMsg, 2*time.Second, util.PushErrMsg, msg, 15000) + }() + } + isInvalidFlashcardWeights := false + for _, w := range strings.Split(Conf.Flashcard.Weights, ",") { + if _, err := strconv.ParseFloat(strings.TrimSpace(w), 64); err != nil { + isInvalidFlashcardWeights = true + break + } + } + if isInvalidFlashcardWeights { + defaultWeights := conf.DefaultFSRSWeights() + msg := "fsrs store weights contain invalid number" + logging.LogWarnf("%s, given [%s], reset to default weights [%s]", msg, Conf.Flashcard.Weights, defaultWeights) + Conf.Flashcard.Weights = defaultWeights + go func() { + util.WaitForUILoaded() + task.AppendAsyncTaskWithDelay(task.PushMsg, 2*time.Second, util.PushErrMsg, msg, 15000) + }() + } if nil == Conf.AI { Conf.AI = conf.NewAI()