def generate_summeval_groundedness_golden_set(
    file_path: str, max_samples_per_bucket: int = 200
):
    """
    Generate a balanced groundedness golden set from the Summeval dataset.

    Args:
        file_path (str): Path to the JSON file.
        max_samples_per_bucket (int): Maximum number of samples per score bucket.

    Yields:
        dict: A dictionary containing query, expected_response, expected_score, and human_score.
    """

    def calculate_expected_score(
        normalized_metrics_lst: List[Any], weights_lst: List[float]
    ):
        """Calculate the expected score using normalized metrics and weights."""
        assert len(normalized_metrics_lst) == len(weights_lst)
        return round(
            sum(
                normalized_metrics_lst[i] * weights_lst[i]
                for i in range(len(normalized_metrics_lst))
            )
            / sum(weights_lst),
            2,
        )

    # Track the number of samples in each bucket
    buckets = {
        "low": 0,  # Scores between 0.0 and 0.3
        "medium": 0,  # Scores between 0.3 and 0.7
        "high": 0,  # Scores between 0.7 and 1.0
    }

    with open(file_path, "r", encoding="utf-8") as f:
        for line in f:
            # Parse each line as a JSON object
            try:
                data = json.loads(line)

                # Ensure the expected keys exist in the JSON
                try:
                    row = data
                    assert (
                        len(row["machine_summaries"]) == len(row["consistency"])
                    ), "Mismatch in lengths of machine_summaries and consistency"

                    # Iterate over the summaries and create the desired dictionary structure
                    for i in range(len(row["machine_summaries"])):
                        query = row.get("text", "")
                        expected_response = row["machine_summaries"][i]

                        # Calculate the expected score based on normalized consistency score
                        expected_score = calculate_expected_score(
                            [
                                (row["consistency"][i] - 1)
                                / 4,  # Normalize from [1, 5] to [0, 1]
                            ],
                            [1.0],
                        )

                        # Determine the bucket based on the score range
                        if expected_score <= 0.3:
                            bucket = "low"
                        elif expected_score <= 0.7:
                            bucket = "medium"
                        else:
                            bucket = "high"

                        # Yield the record only if the bucket has not reached the limit
                        if buckets[bucket] < max_samples_per_bucket:
                            buckets[bucket] += 1
                            yield {
                                "query": query,
                                "expected_response": expected_response,
                                "expected_score": expected_score,
                                "human_score": row["consistency"][i],
                            }

                except KeyError as e:
                    print(
                        f"Key error: {e}. Please check if the keys exist in the JSON file."
                    )
                except AssertionError as e:
                    print(
                        f"Assertion error: {e}. The lengths of 'machine_summaries' and 'consistency' do not match."
                    )

            except json.JSONDecodeError as e:
                print(f"JSON decode error: {e}. Check the line format.")

    Generate a balanced groundedness golden set from the Summeval dataset.

    Args:
        file_path (str): Path to the JSON file.
        max_samples_per_bucket (int): Maximum number of samples per score bucket.

    Yields:
        dict: A dictionary containing query, expected_response, expected_score, and human_score.
    
src/benchmark/trulens/benchmark/benchmark_frameworks/experiments/dataset_preprocessing.py