File size: 2,260 Bytes
1856027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-

# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import Literal
from vertexai.evaluation import constants
from vertexai.evaluation.metrics import _base


class Rouge(_base._AutomaticMetric):  # pylint: disable=protected-access
    """The ROUGE Metric.

    Calculates the recall of n-grams in prediction as compared to reference and
    returns a score ranging between 0 and 1. Supported rouge types are
    rougen[1-9], rougeL, and rougeLsum.
    """

    _metric_name = constants.Metric.ROUGE

    def __init__(
        self,
        *,
        rouge_type: Literal[
            "rouge1",
            "rouge2",
            "rouge3",
            "rouge4",
            "rouge5",
            "rouge6",
            "rouge7",
            "rouge8",
            "rouge9",
            "rougeL",
            "rougeLsum",
        ],
        use_stemmer: bool = False,
        split_summaries: bool = False
    ):
        """Initializes the ROUGE metric.

        Args:
          rouge_type: Supported rouge types are rougen[1-9], rougeL, and rougeLsum.
          use_stemmer: Whether to use stemmer to compute rouge score.
          split_summaries: Whether to split summaries while using 'rougeLsum' to
            compute rouge score.
        """
        self._rouge_type = rouge_type
        self._use_stemmer = use_stemmer
        self._split_summaries = split_summaries

        super().__init__(
            metric=Rouge._metric_name,
        )

    @property
    def rouge_type(self) -> str:
        return self._rouge_type

    @property
    def use_stemmer(self) -> bool:
        return self._use_stemmer

    @property
    def split_summaries(self) -> bool:
        return self._split_summaries