JOJ3/internal/parser/keyword/parser.go
张泊明518370910136 f23d8932ea
All checks were successful
submodules sync / sync (push) Successful in 37s
build / build (push) Successful in 1m17s
build / trigger-build-image (push) Successful in 7s
feat(parser/keyword): force quit on deduct (#75)
Reviewed-on: #75
Co-authored-by: Boming Zhang <bomingzh@sjtu.edu.cn>
Co-committed-by: Boming Zhang <bomingzh@sjtu.edu.cn>
2024-11-02 15:53:37 +08:00

72 lines
1.5 KiB
Go

package keyword
import (
"fmt"
"strings"
"github.com/joint-online-judge/JOJ3/internal/stage"
)
type Match struct {
Keyword string
Score int
MaxMatchCount int
}
type Conf struct {
Score int
FullScore int // TODO: remove me
Files []string
ForceQuitOnDeduct bool `default:"false"`
Matches []Match
}
type Keyword struct{}
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
score := conf.Score
comment := ""
for _, file := range conf.Files {
content := executorResult.Files[file]
for _, match := range conf.Matches {
count := strings.Count(content, match.Keyword)
if match.MaxMatchCount > 0 {
count = min(count, match.MaxMatchCount)
}
if count > 0 {
score -= count * match.Score
comment += fmt.Sprintf(
"Matched keyword %d time(s): %s\n",
count, match.Keyword)
}
}
}
return stage.ParserResult{
Score: score,
Comment: comment,
}
}
func (*Keyword) Run(results []stage.ExecutorResult, confAny any) (
[]stage.ParserResult, bool, error,
) {
conf, err := stage.DecodeConf[Conf](confAny)
if err != nil {
return nil, true, err
}
// TODO: remove me on FullScore field removed
if conf.FullScore != 0 && conf.Score == 0 {
conf.Score = conf.FullScore
}
var res []stage.ParserResult
forceQuit := false
for _, result := range results {
parseRes := Parse(result, *conf)
if conf.ForceQuitOnDeduct && parseRes.Score < conf.Score {
forceQuit = true
}
res = append(res, parseRes)
}
return res, forceQuit, nil
}