feat: keyword parser

This commit is contained in:
张泊明518370910136 2024-04-06 16:41:53 -04:00
parent b9957a9965
commit 355a9f0d00
GPG Key ID: D47306D7062CDA9D
3 changed files with 77 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/cpplint" _ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/cpplint"
_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/diff" _ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/diff"
_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/dummy" _ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/dummy"
_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/keyword"
_ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/resultstatus" _ "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/parsers/resultstatus"
) )

View File

@ -0,0 +1,9 @@
package keyword
import "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
var name = "keyword"
func init() {
stage.RegisterParser(name, &Keyword{})
}

View File

@ -0,0 +1,67 @@
package keyword
import (
"fmt"
"strings"
"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
)
type Match struct {
Keyword string
Score int
}
type Conf struct {
FullScore int
MinScore int
Files []string
EndOnMatch bool
Matches []Match
}
type Keyword struct{}
func Parse(executorResult stage.ExecutorResult, conf Conf) (
stage.ParserResult, bool,
) {
score := conf.FullScore
comment := ""
matched := false
for _, file := range conf.Files {
content := executorResult.Files[file]
for _, match := range conf.Matches {
count := strings.Count(content, match.Keyword)
if count > 0 {
matched = true
score -= count * match.Score
comment += fmt.Sprintf(
"Matched keyword %d time(s): %s\n",
count, match.Keyword)
}
}
}
return stage.ParserResult{
Score: min(score, conf.MinScore),
Comment: comment,
}, matched
}
func (*Keyword) Run(results []stage.ExecutorResult, confAny any) (
[]stage.ParserResult, bool, error,
) {
conf, err := stage.DecodeConf[Conf](confAny)
if err != nil {
return nil, true, err
}
var res []stage.ParserResult
end := false
for _, result := range results {
tmp, matched := Parse(result, *conf)
if matched && conf.EndOnMatch {
end = true
}
res = append(res, tmp)
}
return res, end, nil
}