File size: 2,994 Bytes
147b2e1
2db0a88
 
 
 
 
 
 
 
 
 
 
 
 
 
bbd9ae1
 
 
2db0a88
147b2e1
 
 
 
 
 
 
 
2db0a88
 
 
 
 
 
 
 
 
 
147b2e1
 
 
 
 
 
 
 
 
2db0a88
 
 
 
 
 
 
 
 
 
 
 
147b2e1
2db0a88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbd9ae1
147b2e1
2db0a88
147b2e1
2db0a88
147b2e1
 
2db0a88
 
 
88c262b
2db0a88
 
 
 
 
147b2e1
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import math
import re

from app.models import GradeResult, TaskDefinition


_IRRELEVANT_MARKERS = {
    "weather",
    "recipe",
    "movie",
    "travel",
    "football",
    "politics",
}

_MIN_SCORE = 0.01
_MAX_SCORE = 0.99


def normalize_score(score: float) -> float:
    """Clamp reward to strict open interval (0, 1) for hub / Phase-2 validators."""
    x = float(score)
    if not math.isfinite(x):
        return _MIN_SCORE
    return max(_MIN_SCORE, min(_MAX_SCORE, x))


def _normalize(text: str) -> str:
    return " ".join(re.findall(r"[a-z0-9_+.#-]+", text.lower()))


def _contains_phrase(text: str, phrase: str) -> bool:
    return phrase.lower() in text


def grade_review(task: TaskDefinition, review: str) -> GradeResult:
    normalized_review = _normalize(review)
    n_groups = len(task.criteria)
    if n_groups == 0:
        return GradeResult(
            score=normalize_score(0.5),
            matched_criteria=[],
            missed_criteria=[],
            penalties={"no_criteria": 0.5},
            rationale="No grading criteria configured; neutral partial score.",
        )

    matched_criteria: list[str] = []
    missed_criteria: list[str] = []

    for criterion_group in task.criteria:
        matched = any(_contains_phrase(normalized_review, _normalize(phrase)) for phrase in criterion_group)
        canonical = criterion_group[0]
        if matched:
            matched_criteria.append(canonical)
        else:
            missed_criteria.append(canonical)

    coverage_score = len(matched_criteria) / n_groups

    penalties: dict[str, float] = {}

    if len(review.split()) < 12:
        penalties["too_short"] = 0.15

    if len(matched_criteria) == 0:
        penalties["no_relevant_content"] = 0.35

    if re.search(r"\b(no issues|looks good|perfect as is)\b", review.lower()):
        penalties["incorrect_no_issues_claim"] = 0.30

    irrelevant_hits = sum(1 for marker in _IRRELEVANT_MARKERS if marker in normalized_review)
    if irrelevant_hits >= 2:
        penalties["irrelevant_content"] = min(0.10 * irrelevant_hits, 0.30)

    if task.id == "hard" and not re.search(r"\b(score|rating|overall)\b", review.lower()):
        penalties["missing_overall_score"] = 0.10

    total_penalty = sum(penalties.values())
    raw_score = coverage_score - total_penalty
    score = normalize_score(round(raw_score, 4))

    cov_display = min(coverage_score, _MAX_SCORE)
    rationale = (
        f"Coverage={cov_display:.2f}; Penalty={total_penalty:.2f}; "
        f"Matched={len(matched_criteria)}/{n_groups}"
    )

    return GradeResult(
        score=score,
        matched_criteria=matched_criteria,
        missed_criteria=missed_criteria,
        penalties=penalties,
        rationale=rationale,
    )


def grader(output: str, expected: TaskDefinition) -> float:
    """OpenEnv-style entry: ``output`` is the review; ``expected`` is the task rubric."""
    result = grade_review(expected, output)
    return normalize_score(result.score)