File size: 2,558 Bytes
8fb7841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import re
from math import prod
from typing import List

def extract_json(response: str) -> dict:
    """
    Extract JSON content from a string response.

    Args:
        response (str): String containing JSON content, possibly within code blocks.

    Returns:
        dict: Extracted and parsed JSON content.

    Raises:
        ValueError: If no valid JSON content could be extracted.
    """
    try:
        evaluation_json = json.loads(response)
    except json.JSONDecodeError:
        # If JSON parsing fails, try to extract the content between ```json and ```
        match = re.search(r'```json\n(.*?)\n```', response, re.DOTALL)
        if not match:
            # If no match for ```json, try to extract content between ``` and ```
            match = re.search(r'```\n(.*?)\n```', response, re.DOTALL)
        
        if match:
            evaluation_content = match.group(1)
            evaluation_json = json.loads(evaluation_content)
        else:
            raise ValueError("Failed to extract valid JSON content")
    return evaluation_json


def convert_score_fields(data: dict) -> dict:
    """
    Convert score fields in a dictionary to integers recursively.

    Args:
        data (dict): Dictionary containing score fields to convert.

    Returns:
        dict: Dictionary with score fields converted to integers.

    Raises:
        ValueError: If a score value cannot be converted to integer.
    """
    # Create a new dictionary with the converted values
    converted_data = {}
    for key, value in data.items():
        if key == "score":
            if isinstance(value, int):
                converted_data[key] = value
            elif isinstance(value, str) and value.isdigit():
                converted_data[key] = int(value)
            else:
                raise ValueError(f"Invalid score value: {value!r}")
        elif isinstance(value, dict):
            converted_data[key] = convert_score_fields(value)
        else:
            converted_data[key] = value
    return converted_data


def calculate_geometric_mean(scores: List[int]) -> float:
    """
    Calculate the geometric mean of a list of scores.

    Args:
        scores (List[int]): List of integer scores, may contain None values.

    Returns:
        float: Geometric mean of non-None scores. Returns 0.0 if list is empty
            or contains only None values.
    """
    scores = [s for s in scores if s is not None]
    if not scores:
        return 0.0
    product = prod(scores)
    return product ** (1 / len(scores))