mirror of
https://github.com/fumiama/paper-manager.git
synced 2026-06-23 12:00:35 +08:00
finish 试卷库
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"encoding/json"
|
||||
"math"
|
||||
"strconv"
|
||||
|
||||
"github.com/fumiama/paper-manager/backend/global"
|
||||
)
|
||||
@@ -17,21 +20,78 @@ type duplication struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func parseFileQuestions(qb []byte) ([]question, []duplication, error) {
|
||||
type duplications []duplication
|
||||
|
||||
func (d *duplications) Len() int {
|
||||
return len(*d)
|
||||
}
|
||||
|
||||
// Less is actually more for a big-top heap
|
||||
func (d *duplications) Less(i, j int) bool {
|
||||
return (*d)[i].Percent > (*d)[j].Percent
|
||||
}
|
||||
|
||||
func (d *duplications) Swap(i, j int) {
|
||||
(*d)[i], (*d)[j] = (*d)[j], (*d)[i]
|
||||
}
|
||||
|
||||
func (d *duplications) Push(x any) {
|
||||
*d = append(*d, x.(duplication))
|
||||
}
|
||||
|
||||
func (d *duplications) Pop() any {
|
||||
if d.Len() == 0 {
|
||||
return nil
|
||||
}
|
||||
i := d.Len() - 1
|
||||
x := (*d)[i]
|
||||
*d = (*d)[:i]
|
||||
return x
|
||||
}
|
||||
|
||||
func parseFileQuestions(qb []byte, istemp bool) ([]question, []duplication, float64, error) {
|
||||
ques := make([]global.QuestionJSON, 0, 16)
|
||||
qs := make([]question, 0, 16)
|
||||
ds := make([]duplication, 0, 16)
|
||||
err := json.Unmarshal(qb, &qs)
|
||||
err := json.Unmarshal(qb, &ques)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, nil, 0, err
|
||||
}
|
||||
dh := make(duplications, 0, 16)
|
||||
heap.Init(&dh)
|
||||
sum := 0.0
|
||||
cnt := 0
|
||||
for _, q := range ques {
|
||||
qs = append(qs, question{
|
||||
Count: len(q.Sub),
|
||||
Point: q.Points,
|
||||
Name: q.Name,
|
||||
})
|
||||
// TODO: use heap to get top 10 ds
|
||||
for i, subq := range q.Sub {
|
||||
qstruct, err := global.FileDB.GetQuestionHex(subq.Name, istemp)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
p := qstruct.MaxDuplicateRate()
|
||||
heap.Push(&dh, duplication{
|
||||
Percent: int(math.Round(p * 100)),
|
||||
Name: q.Name + "." + strconv.Itoa(i+1),
|
||||
})
|
||||
sum += p
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
return nil, nil, nil
|
||||
i := dh.Len()
|
||||
ds := make([]duplication, 10)
|
||||
if i > 10 {
|
||||
i = 10
|
||||
} else {
|
||||
for j := i; j < 10; j++ {
|
||||
ds[j] = duplication{Name: "N/A"}
|
||||
}
|
||||
}
|
||||
for i--; i >= 0; i-- {
|
||||
ds[i] = heap.Pop(&dh).(duplication)
|
||||
}
|
||||
|
||||
return qs, ds, sum / float64(cnt), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user