diff --git a/internal/parser/keyword/parser.go b/internal/parser/keyword/parser.go index f7a3913..57c992e 100644 --- a/internal/parser/keyword/parser.go +++ b/internal/parser/keyword/parser.go @@ -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,