feat(parser/keyword): force quit on deduct #75

Merged
张泊明518370910136 merged 1 commits from keyword-forcequit into master 2024-11-02 15:53:37 +08:00

View File

@ -17,18 +17,15 @@ type Conf struct {
Score int
FullScore int // TODO: remove me
Files []string
ForceQuitOnMatch bool
ForceQuitOnDeduct bool `default:"false"`
Matches []Match
}
type Keyword struct{}
func Parse(executorResult stage.ExecutorResult, conf Conf) (
stage.ParserResult, bool,
) {
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
score := conf.Score
comment := ""
matched := false
for _, file := range conf.Files {
content := executorResult.Files[file]
for _, match := range conf.Matches {
@ -37,7 +34,6 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) (
count = min(count, match.MaxMatchCount)
}
if count > 0 {
matched = true
score -= count * match.Score
comment += fmt.Sprintf(
"Matched keyword %d time(s): %s\n",
@ -48,7 +44,7 @@ func Parse(executorResult stage.ExecutorResult, conf Conf) (
return stage.ParserResult{
Score: score,
Comment: comment,
}, matched
}
}
func (*Keyword) Run(results []stage.ExecutorResult, confAny any) (
@ -65,11 +61,11 @@ func (*Keyword) Run(results []stage.ExecutorResult, confAny any) (
var res []stage.ParserResult
forceQuit := false
for _, result := range results {
tmp, matched := Parse(result, *conf)
if matched && conf.ForceQuitOnMatch {
parseRes := Parse(result, *conf)
if conf.ForceQuitOnDeduct && parseRes.Score < conf.Score {
forceQuit = true
}
res = append(res, tmp)
res = append(res, parseRes)
}
return res, forceQuit, nil
}