Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-19137-review-rule-classifier-noise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The review-rule classifier now ignores quoted lines, fenced code and questions, falls back to the whole comment when the rule sentence is too short to stand alone, and also recognises "for the future", "moving forward" and "for future reviews" as going-forward phrases ([#19137](https://github.com/hivecommons/hive/issues/19137)).
54 changes: 50 additions & 4 deletions src/pkg/reviewrules/classify.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var ruleSignals = []signal{
{SignalAuthoritative, regexp.MustCompile(`(?i)\bauthoritative\b`)},
{SignalAlways, regexp.MustCompile(`(?i)\b(?:should|must|needs? to|has to|have to) always\b`)},
{SignalNever, regexp.MustCompile(`(?i)\b(?:should|must) never\b`)},
{SignalGoingForward, regexp.MustCompile(`(?i)\b(?:going forward|from now on|in (?:the )?future|next time|for future (?:analys[ie]s|assessments|runs|prs|work))\b`)},
{SignalGoingForward, regexp.MustCompile(`(?i)\b(?:going forward|from now on|in (?:the )?future|for the future|moving forward|next time|for future (?:analys[ie]s|assessments?|reviews?|runs|prs|work))\b`)},
{SignalCaptureRequest, regexp.MustCompile(`(?i)\b(?:capture|record|document|write down|remember)\s+(?:this|that|it)\b`)},
{SignalCriteria, regexp.MustCompile(`(?i)\bassessment (?:criteria|rubric|knowledge|guidance)\b`)},
{SignalKnowledgeBase, regexp.MustCompile(`(?i)\bknowledge[- ]?base\b`)},
Expand All @@ -85,6 +85,47 @@ var metaClause = regexp.MustCompile(`(?i)^\s*(?:please\s+|and\s+)?(?:capture|rec

var sentenceSplit = regexp.MustCompile(`[.!?]+\s+|\n+`)

// minRuleRunes is the shortest rule sentence that can stand alone. A shorter
// one ("Going forward.") is meaningless without the comment around it, so the
// whole comment is kept instead.
const minRuleRunes = 20

// stripNoise removes what is not the reviewer's own statement: quoted lines
// (a leading ">"), fenced code blocks and questions. A question asks, it does
// not state, so a phrase inside one is not a rule.
func stripNoise(body string) string {
var lines []string
fenced := false
for _, line := range strings.Split(body, "\n") {
t := strings.TrimSpace(line)
if strings.HasPrefix(t, "```") || strings.HasPrefix(t, "~~~") {
fenced = !fenced
continue
}
if fenced || strings.HasPrefix(t, ">") {
continue
}
lines = append(lines, line)
}
text := strings.Join(lines, "\n")
var out strings.Builder
last := 0
keep := func(seg string) {
if !strings.HasSuffix(strings.TrimSpace(seg), "?") {
out.WriteString(seg)
}
}
for _, loc := range sentenceSplit.FindAllStringIndex(text, -1) {
seg := text[last:loc[1]]
if !strings.Contains(text[loc[0]:loc[1]], "?") {
out.WriteString(seg)
}
last = loc[1]
}
keep(text[last:])
return out.String()
}

// Classification is Classify's verdict.
type Classification struct {
Kind Kind
Expand All @@ -95,6 +136,7 @@ type Classification struct {
// Classify says whether body states a rule for future work. It is a pure
// function of the text: every signal is an explicit phrase from ruleSignals.
func Classify(body string) Classification {
body = stripNoise(body)
var hit []string
for _, s := range ruleSignals {
if s.re.MatchString(body) {
Expand All @@ -115,17 +157,21 @@ func ForeignHomeHint(body string) bool { return foreignHomeHint.MatchString(body
// signal, without the clauses that only ask for the capture, normalised and
// bounded. A comment with no signal yields its whole body, bounded.
func ExtractRuleText(body string) string {
clean := stripNoise(body)
var kept []string
for _, sent := range sentenceSplit.Split(body, -1) {
for _, sent := range sentenceSplit.Split(clean, -1) {
if strings.TrimSpace(sent) == "" {
continue
}
if Classify(sent).Kind == KindRule {
kept = append(kept, sent)
}
}
if len(kept) == 0 {
kept = []string{body}
if len(kept) == 0 || utf8.RuneCountInString(NormalizeText(strings.Join(kept, " "))) < minRuleRunes {
kept = []string{clean}
if strings.TrimSpace(clean) == "" {
kept = []string{body}
}
}
var clauses []string
for _, sent := range kept {
Expand Down
12 changes: 12 additions & 0 deletions src/pkg/reviewrules/classify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ func TestClassify(t *testing.T) {
{"typo", "typo in line 4", KindCorrection, nil},
{"always without a modal", "He is always late with reviews.", KindCorrection, nil},
{"empty", "", KindCorrection, nil},
{"for the future", "Keep this in mind for the future.", KindRule, []string{SignalGoingForward}},
{"moving forward", "Moving forward, cite the commit.", KindRule, []string{SignalGoingForward}},
{"for future reviews", "Apply this for future reviews.", KindRule, []string{SignalGoingForward}},
{"for future analyses and work", "Use it for future analyses and for future work.", KindRule, []string{SignalGoingForward}},
{"quoted line ignored", "> Going forward, use the maintainers file.\nThe score is too high.", KindCorrection, nil},
{"fenced code ignored", "Fix this:\n```\n// going forward use X\n```\nThanks", KindCorrection, nil},
{"question ignored", "Should we do this from now on?", KindCorrection, nil},
{"question ignored, statement kept", "Why is this low? Going forward, cite the commit.", KindRule, []string{SignalGoingForward}},
{"upper case", "SOURCE OF TRUTH is the lockfile", KindRule, []string{SignalSourceOfTruth}},
}
for _, tc := range tests {
Expand Down Expand Up @@ -59,6 +67,10 @@ func TestExtractRuleText(t *testing.T) {
{"only a capture request keeps it", "Please capture this.", "Please capture this."},
{"no signal returns the body", "just a remark", "just a remark"},
{"control characters and comment delimiters", "source of truth <!-- x --> is\tthe lockfile\x00", "source of truth x is the lockfile"},
{"quoted rule is not extracted", "> Going forward, cite everything.\nThe maintainers file is the source of truth.", "The maintainers file is the source of truth."},
{"fenced code is not extracted", "```\nalways cite\n```\nGoing forward, cite the commit.", "Going forward, cite the commit."},
{"question is not extracted", "Is it the source of truth? Going forward, cite the commit.", "Going forward, cite the commit."},
{"short rule sentence falls back to the whole comment", "Cite the commit for scores. Going forward.", "Cite the commit for scores. Going forward."},
{"empty clause list falls back", "capture this; record it", "capture this; record it"},
}
for _, tc := range tests {
Expand Down
Loading