feat(parser/keyword): use Keywords in Matches
All checks were successful
submodules sync / sync (push) Successful in 40s
build / build (push) Successful in 1m17s
build / trigger-build-image (push) Successful in 7s

This commit is contained in:
张泊明518370910136 2024-11-02 04:08:21 -04:00
parent f23d8932ea
commit cc3b4b0b13
GPG Key ID: D47306D7062CDA9D

View File

@ -8,7 +8,8 @@ import (
)
type Match struct {
Keyword string
Keywords []string
Keyword string // TODO: remove me
Score int
MaxMatchCount int
}
@ -26,18 +27,21 @@ type Keyword struct{}
func Parse(executorResult stage.ExecutorResult, conf Conf) stage.ParserResult {
score := conf.Score
comment := ""
for _, match := range conf.Matches {
for _, keyword := range match.Keywords {
keywordMatchCount := 0
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)
keywordMatchCount += strings.Count(content, keyword)
}
if count > 0 {
score -= count * match.Score
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",
count, match.Keyword)
keywordMatchCount, keyword)
}
}
}
@ -54,6 +58,13 @@ func (*Keyword) Run(results []stage.ExecutorResult, confAny any) (
if err != nil {
return nil, true, err
}
// TODO: remove me on Matches.Keyword field removed
for i := range conf.Matches {
match := &conf.Matches[i]
if match.Keyword != "" && len(match.Keywords) == 0 {
match.Keywords = []string{match.Keyword}
}
}
// TODO: remove me on FullScore field removed
if conf.FullScore != 0 && conf.Score == 0 {
conf.Score = conf.FullScore