100XZX001 commited on
Commit
26fd0f9
·
verified ·
1 Parent(s): 2807113

Create grader.py

Browse files
Files changed (1) hide show
  1. grader.py +25 -0
grader.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def grade_comment(comment: str, expected_keywords: list, expert_comment: str) -> float:
2
+ """
3
+ Returns a score in [0, 1] based on keyword coverage and some simple heuristics.
4
+ """
5
+ comment_lower = comment.lower()
6
+ # 1. Keyword coverage (primary)
7
+ matched = sum(1 for kw in expected_keywords if kw in comment_lower)
8
+ kw_score = min(1.0, matched / max(1, len(expected_keywords) // 2)) # partial if at least half
9
+
10
+ # 2. Bonus for reasonable length (≥ 15 words)
11
+ words = comment.split()
12
+ length_bonus = 0.1 if len(words) >= 15 else 0.0
13
+
14
+ # 3. Penalty if the comment contains "skip" or "done" (agent didn't really comment)
15
+ if any(x in comment_lower for x in ["skip", "done", "no comment"]):
16
+ return 0.0
17
+
18
+ # 4. Penalty for extremely short or generic comments
19
+ if len(words) < 5 or comment_lower in ["lgtm", "looks good", "good"]:
20
+ penalty = 0.2
21
+ else:
22
+ penalty = 0.0
23
+
24
+ final_score = kw_score + length_bonus - penalty
25
+ return max(0.0, min(1.0, final_score))