-
Notifications
You must be signed in to change notification settings - Fork 0
/
dayplan.go
70 lines (57 loc) · 1.62 KB
/
dayplan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package plan
import (
"context"
"fmt"
"strings"
"time"
"github.com/bonaysoft/engra/apis/client"
"github.com/bonaysoft/engra/apis/graph/model"
"github.com/saltbo/learn-eng/glossaries"
"github.com/samber/lo"
)
type DayPlan struct {
Date time.Time
Words []Word
numsPerDay int
glossaries []string
}
func NewDayPlan() *DayPlan {
return &DayPlan{}
}
func (d *DayPlan) Run() (err error) {
words := make([]string, 0)
for _, glossaryName := range d.glossaries {
words = append(words, glossaries.Get(glossaryName)...)
}
masteredWords := glossaries.GetMasteredWords()
unMasteredWords, _ := lo.Difference(words, masteredWords)
d.Date = time.Now()
d.Words, err = d.findHighPriorityWords(unMasteredWords, masteredWords, d.numsPerDay)
fmt.Println(len(unMasteredWords), len(words))
return
}
func (d *DayPlan) SetNumsPerDay(numsPerDay int) {
d.numsPerDay = numsPerDay
}
func (d *DayPlan) SelectGlossary(glossary ...string) {
d.glossaries = append(d.glossaries, glossary...)
}
func (d *DayPlan) findHighPriorityWords(words []string, masteredWords []string, nums int) ([]Word, error) {
// TODO: 待实现,先按顺序返回
// endpoint := "https://engra.saltbo.cn/query"
endpoint := "http://localhost:8081/query"
ecdict := client.NewClient(endpoint)
resp, err := client.Lookup(ecdict.WithContext(context.Background()), words[:nums])
if err != nil {
return nil, err
}
return lo.Map(resp.Vocabularies, func(v model.Vocabulary, index int) Word {
return Word{
Name: v.Name,
Phonetic: v.Phonetic,
Meaning: strings.ReplaceAll(v.Meaning, "\\n", "\n\n"),
Roots: v.Roots,
RootGraph: v.Mnemonic,
}
}), nil
}