feat(parser): show match occurrences #76

Merged
张泊明518370910136 merged 4 commits from occurrences into master 2024-11-04 09:44:05 +08:00
Showing only changes of commit 8aa4d6d70b - Show all commits

View File

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