File size: 9,988 Bytes
148b631
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
"""
metrics.py - Evaluation metrics for code completion validation.

Implement functions to calculate bracket accuracy, indentation,
and other code-specific metrics.
"""

import re
from typing import List, Tuple, Dict
from dataclasses import dataclass
from collections import Counter


@dataclass
class TestResult:
    """Individual test result."""
    test_name: str
    category: str
    passed: bool
    prompt: str
    generated: str
    expected_patterns: List[str]
    matched_patterns: List[str]
    failed_patterns: List[str]
    forbidden_matches: List[str]
    score: float  # 0.0 to 1.0


@dataclass
class CategoryResult:
    """Aggregated result for a category."""
    category: str
    total_tests: int
    passed_tests: int
    accuracy: float
    test_results: List[TestResult]


@dataclass
class ValidationReport:
    """Complete validation report."""
    model_name: str
    total_tests: int
    total_passed: int
    overall_accuracy: float
    category_results: Dict[str, CategoryResult]
    bracket_accuracy: float
    indentation_accuracy: float
    structure_accuracy: float


def check_brackets_balanced(text: str) -> Tuple[bool, str]:
    """
    Checks if brackets are balanced.
    
    Returns:
        (is_balanced, error_message)
    """
    stack = []
    pairs = {'(': ')', '[': ']', '{': '}'}
    
    for i, char in enumerate(text):
        if char in pairs:
            stack.append((char, i))
        elif char in pairs.values():
            if not stack:
                return False, f"Extra bracket '{char}' at position {i}"
            opening, pos = stack.pop()
            if pairs[opening] != char:
                return False, f"Mismatch: '{opening}' at position {pos} closed with '{char}' at position {i}"
    
    if stack:
        unclosed = [(char, pos) for char, pos in stack]
        return False, f"Unclosed brackets: {unclosed}"
    
    return True, "OK"


def count_bracket_errors(prompt: str, generated: str) -> Dict[str, int]:
    """
    Counts bracket errors in generated code.
    
    Returns:
        Dictionary with error counts by type
    """
    full_code = prompt + generated
    
    errors = {
        'unclosed_parens': 0,
        'unclosed_brackets': 0,
        'unclosed_braces': 0,
        'extra_closing': 0
    }
    
    # Count open and close
    parens = full_code.count('(') - full_code.count(')')
    brackets = full_code.count('[') - full_code.count(']')
    braces = full_code.count('{') - full_code.count('}')
    
    if parens > 0:
        errors['unclosed_parens'] = parens
    elif parens < 0:
        errors['extra_closing'] += abs(parens)
        
    if brackets > 0:
        errors['unclosed_brackets'] = brackets
    elif brackets < 0:
        errors['extra_closing'] += abs(brackets)
        
    if braces > 0:
        errors['unclosed_braces'] = braces
    elif braces < 0:
        errors['extra_closing'] += abs(braces)
    
    return errors


def check_indentation(text: str) -> Dict[str, any]:
    """
    Analyzes indentation quality in code.
    
    Returns:
        Dictionary with indentation metrics
    """
    lines = text.split('\n')
    
    stats = {
        'total_lines': len(lines),
        'indented_lines': 0,
        'consistent_indent': True,
        'indent_style': None,  # 'spaces' or 'tabs'
        'indent_size': None,
        'indent_errors': []
    }
    
    indent_sizes = []
    
    for i, line in enumerate(lines):
        if not line.strip():  # Empty line
            continue
            
        # Count leading whitespace
        stripped = line.lstrip()
        indent = len(line) - len(stripped)
        
        if indent > 0:
            stats['indented_lines'] += 1
            
            # Detect style
            if line.startswith('\t'):
                if stats['indent_style'] is None:
                    stats['indent_style'] = 'tabs'
                elif stats['indent_style'] == 'spaces':
                    stats['consistent_indent'] = False
            else:
                if stats['indent_style'] is None:
                    stats['indent_style'] = 'spaces'
                elif stats['indent_style'] == 'tabs':
                    stats['consistent_indent'] = False
            
            if stats['indent_style'] == 'spaces':
                indent_sizes.append(indent)
    
    # Determine most common indent size
    if indent_sizes:
        # Find GCD of indent sizes
        common_indents = Counter(indent_sizes)
        stats['indent_size'] = min(common_indents.keys()) if common_indents else 4
    
    return stats


def evaluate_test_case(
    prompt: str,
    generated: str,
    expected_patterns: List[str],
    forbidden_patterns: List[str] = None
) -> Tuple[bool, float, List[str], List[str], List[str]]:
    """
    Evaluates a test case.
    
    Returns:
        (passed, score, matched_patterns, failed_patterns, forbidden_matches)
    """
    if forbidden_patterns is None:
        forbidden_patterns = []
    
    matched = []
    failed = []
    forbidden_found = []
    
    # Check expected patterns
    for pattern in expected_patterns:
        try:
            if re.search(pattern, generated, re.MULTILINE):
                matched.append(pattern)
            else:
                failed.append(pattern)
        except re.error:
            # Invalid pattern, treat as literal
            if pattern in generated:
                matched.append(pattern)
            else:
                failed.append(pattern)
    
    # Check forbidden patterns
    for pattern in forbidden_patterns:
        try:
            if re.search(pattern, generated, re.MULTILINE):
                forbidden_found.append(pattern)
        except re.error:
            if pattern in generated:
                forbidden_found.append(pattern)
    
    # Calculate score
    if expected_patterns:
        score = len(matched) / len(expected_patterns)
    else:
        score = 1.0
    
    # Penalize forbidden patterns
    if forbidden_found:
        score *= 0.5
    
    passed = len(matched) > 0 and len(forbidden_found) == 0
    
    return passed, score, matched, failed, forbidden_found


def calculate_bracket_accuracy(results: List[TestResult]) -> float:
    """Calculates accuracy specific to brackets."""
    bracket_tests = [r for r in results if r.category == 'brackets']
    if not bracket_tests:
        return 0.0
    return sum(1 for t in bracket_tests if t.passed) / len(bracket_tests)


def calculate_indentation_accuracy(results: List[TestResult]) -> float:
    """Calculates accuracy specific to indentation."""
    indent_tests = [r for r in results if r.category == 'indentation']
    if not indent_tests:
        return 0.0
    return sum(1 for t in indent_tests if t.passed) / len(indent_tests)


def generate_report(
    model_name: str,
    results: List[TestResult]
) -> ValidationReport:
    """
    Generates complete validation report.
    """
    # Group by category
    categories = {}
    for result in results:
        if result.category not in categories:
            categories[result.category] = []
        categories[result.category].append(result)
    
    # Calculate results per category
    category_results = {}
    for cat, cat_results in categories.items():
        passed = sum(1 for r in cat_results if r.passed)
        category_results[cat] = CategoryResult(
            category=cat,
            total_tests=len(cat_results),
            passed_tests=passed,
            accuracy=passed / len(cat_results) if cat_results else 0,
            test_results=cat_results
        )
    
    # Calculate general metrics
    total = len(results)
    passed = sum(1 for r in results if r.passed)
    
    return ValidationReport(
        model_name=model_name,
        total_tests=total,
        total_passed=passed,
        overall_accuracy=passed / total if total > 0 else 0,
        category_results=category_results,
        bracket_accuracy=calculate_bracket_accuracy(results),
        indentation_accuracy=calculate_indentation_accuracy(results),
        structure_accuracy=sum(1 for r in results if r.category == 'structure' and r.passed) / 
                          max(1, len([r for r in results if r.category == 'structure']))
    )


def format_report(report: ValidationReport) -> str:
    """Formats report for printing."""
    lines = [
        "=" * 60,
        f"πŸ“Š VALIDATION REPORT: {report.model_name}",
        "=" * 60,
        "",
        f"πŸ“ˆ OVERALL RESULTS",
        f"   Total tests:  {report.total_tests}",
        f"   Passed tests: {report.total_passed}",
        f"   Overall Accuracy:  {report.overall_accuracy:.1%}",
        "",
        "πŸ“‹ SPECIFIC METRICS",
        f"   Bracket Accuracy:     {report.bracket_accuracy:.1%}",
        f"   Indentation Accuracy: {report.indentation_accuracy:.1%}",
        f"   Structure Accuracy:   {report.structure_accuracy:.1%}",
        "",
        "πŸ“ RESULTS BY CATEGORY",
    ]
    
    for cat_name, cat_result in report.category_results.items():
        status = "βœ…" if cat_result.accuracy >= 0.7 else "⚠️" if cat_result.accuracy >= 0.5 else "❌"
        lines.append(f"   {status} {cat_name}: {cat_result.passed_tests}/{cat_result.total_tests} ({cat_result.accuracy:.1%})")
    
    lines.extend([
        "",
        "=" * 60
    ])
    
    return "\n".join(lines)


if __name__ == '__main__':
    # Function tests
    print("πŸ§ͺ Testing metrics...")
    
    # Bracket test
    is_bal, msg = check_brackets_balanced("def foo(a, b):")
    print(f"Balanced '(a, b)': {is_bal} - {msg}")
    
    is_bal, msg = check_brackets_balanced("def foo(a, b:")
    print(f"Balanced '(a, b:': {is_bal} - {msg}")
    
    # Evaluation test
    passed, score, matched, failed, forbidden = evaluate_test_case(
        prompt="def hello(",
        generated="name):\n    print(name)",
        expected_patterns=[r"\)", r":"]
    )
    print(f"Test result: passed={passed}, score={score}, matched={matched}")