feat(parser/keyword): show occurrences summary
All checks were successful
build / build (push) Successful in 1m48s
build / trigger-build-image (push) Has been skipped
build / build (pull_request) Successful in 1m26s
build / trigger-build-image (pull_request) Has been skipped

This commit is contained in:
张泊明518370910136 2024-11-03 20:39:56 -05:00
parent 5569703dfb
commit 8aa4d6d70b
GPG Key ID: D47306D7062CDA9D

View File

@ -2,6 +2,7 @@ package keyword
import (
"fmt"
"sort"
"strings"
"github.com/joint-online-judge/JOJ3/internal/stage"
@ -27,24 +28,48 @@ type Keyword struct{}
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
score := conf.Score
comment := ""
matchCount := make(map[string]int)
scoreChange := make(map[string]int)
for _, match := range conf.Matches {
for _, keyword := range match.Keywords {
keywordMatchCount := 0
for _, file := range conf.Files {
content := executorResult.Files[file]
keywordMatchCount += strings.Count(content, keyword)
matchCount[keyword] += strings.Count(content, keyword)
}
if match.MaxMatchCount > 0 {
keywordMatchCount = min(keywordMatchCount, match.MaxMatchCount)
}
if keywordMatchCount > 0 {
score -= keywordMatchCount * match.Score
comment += fmt.Sprintf(
"Matched keyword %d time(s): %s\n",
keywordMatchCount, keyword)
matchCount[keyword] = min(
matchCount[keyword], match.MaxMatchCount)
}
score += -match.Score * matchCount[keyword]
scoreChange[keyword] = -match.Score * matchCount[keyword]
}
}
type Result struct {
Keyword string
Count int
ScoreChange int
}
var results []Result
for keyword, count := range matchCount {
results = append(results, Result{
Keyword: keyword,
Count: count,
ScoreChange: scoreChange[keyword],
})
}
sort.Slice(results, func(i, j int) bool {
if results[i].ScoreChange != results[j].ScoreChange {
return results[i].ScoreChange < results[j].ScoreChange
}
if results[i].Count != results[j].Count {
return results[i].Count > results[j].Count
}
return results[i].Keyword < results[j].Keyword
})
for i, result := range results {
comment += fmt.Sprintf("%d. `%s`: %d occurrence(s), %d point(s)\n",
i+1, result.Keyword, result.Count, result.ScoreChange)
}
return stage.ParserResult{
Score: score,
Comment: comment,