File size: 6,481 Bytes
64ec34e
 
 
 
 
 
 
 
 
 
 
 
 
361b7e8
64ec34e
 
 
361b7e8
64ec34e
 
361b7e8
 
 
 
 
 
 
 
64ec34e
 
 
 
361b7e8
64ec34e
 
 
361b7e8
 
64ec34e
361b7e8
 
 
 
64ec34e
361b7e8
64ec34e
361b7e8
 
 
 
 
 
 
64ec34e
361b7e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64ec34e
361b7e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64ec34e
 
 
 
361b7e8
64ec34e
 
 
 
 
 
 
361b7e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64ec34e
 
 
db2ce77
 
c6e9951
de3203d
361b7e8
 
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
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# 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.
"""SignWriting Similarity metric from the signwriting-evaluation package"""

import evaluate
import datasets
from signwriting_evaluation.metrics.similarity import SignWritingSimilarityMetric

_CITATION = """\
@misc{moryossef2024signwritingevaluationeffectivesignlanguage,
  title={signwriting-evaluation: Effective Sign Language Evaluation via SignWriting}, 
  author={Amit Moryossef and Rotem Zilberman and Ohad Langer},
  year={2024},
  eprint={2410.13668},
  archivePrefix={arXiv},
  primaryClass={cs.CL},
  url={https://arxiv.org/abs/2410.13668}, 
}
"""

_DESCRIPTION = """\
SignWriting Similarity metric from the signwriting-evaluation package
"""

_KWARGS_DESCRIPTION = """
Produces similarity scores for hypotheses given reference translations.

Args:
    predictions (list of str): 
        The predicted sentences.
    references (list of list of str): 
        The references. There should be one reference sub-list for each prediction sentence.
Returns:
    score (float): The similarity score between 0 and 1
Examples:
    Example 1 -- basic similarity score:
        >>> predictions = ["M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517"]
        >>> references = [["M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513"]]
        >>> metric = evaluate.load("signwriting_similarity")
        >>> results = metric.compute(predictions=predictions, references=references)
        >>> print(results)
        {'score': 0.5509574768254414}

    Example 2 -- identical signs in different order:
        >>> predictions = ["M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517"]
        >>> references = [["M530x538S22f03469x517S37602508x462S20e00488x510S15a11493x494"]]
        >>> metric = evaluate.load("signwriting_similarity")
        >>> results = metric.compute(predictions=predictions, references=references)
        >>> print(results)
        {'score': 1.0}

    Example 3 -- slightly different symbols:
        >>> predictions = ["M530x538S17600508x462S15a11493x494S20e00488x510S22f03469x517"]
        >>> references = [["M530x538S17600508x462S12a11493x494S20e00488x510S22f13469x517"]]
        >>> metric = evaluate.load("signwriting_similarity")
        >>> results = metric.compute(predictions=predictions, references=references)
        >>> print(results)
        {'score': 0.8326259781509948}

    Example 4 -- multiple references, one good and one bad:
        >>> predictions = ["M530x538S17600508x462S15a11493x494S20e00488x510S22f03469x517"]
        >>> references = [["M530x538S17600508x462S12a11493x494S20e00488x510S22f13469x517"], ["M530x538S17600508x462"]]
        >>> metric = evaluate.load("signwriting_similarity")
        >>> results = metric.compute(predictions=predictions, references=references)
        >>> print(results)
        {'score': 0.8326259781509948}

    Example 5 -- multiple signs in hypothesis:
        >>> predictions = ["M530x538S17600508x462S15a11493x494S20e00488x510S22f03469x517 M530x538S17600508x462S15a11493x494S20e00488x510S22f03469x517"]
        >>> references = [["M530x538S17600508x462S12a11493x494S20e00488x510S22f13469x517"]]
        >>> metric = evaluate.load("signwriting_similarity")
        >>> results = metric.compute(predictions=predictions, references=references)
        >>> print(results)
        {'score': 0.4163129890754974}

    Example 6 -- sign order does not affect similarity:
        >>> predictions = ["M530x538S17600508x462S15a11493x494S20e00488x510S22f03469x517 M530x538S17600508x462S12a11493x494S20e00488x510S22f13469x517"]
        >>> references = [["M530x538S17600508x462S12a11493x494S20e00488x510S22f13469x517 M530x538S17600508x462S15a11493x494S20e00488x510S22f03469x517"]]
        >>> metric = evaluate.load("signwriting_similarity")
        >>> results = metric.compute(predictions=predictions, references=references)
        >>> print(results)
        {'score': 1.0}

    Example 7 -- invalid FSW input should result in 0 score:
        >>> predictions = ["M<s><s>M<s>p483"]
        >>> references = [["M<s><s>M<s>p483"]]
        >>> metric = evaluate.load("signwriting_similarity")
        >>> results = metric.compute(predictions=predictions, references=references)
        >>> print(results)
        {'score': 0.0}
"""


@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
class SignWritingSimilarity(evaluate.Metric):
    metric = SignWritingSimilarityMetric()

    def _info(self):
        return evaluate.MetricInfo(
            module_type="metric",
            description=_DESCRIPTION,
            citation=_CITATION,
            inputs_description=_KWARGS_DESCRIPTION,
            homepage="https://github.com/sign-language-processing/signwriting-evaluation",
            features=[
                datasets.Features(
                    {
                        "predictions": datasets.Value("string", id="sequence"),
                        "references": datasets.Sequence(datasets.Value("string", id="sequence"), id="references"),
                    }
                ),
                datasets.Features(
                    {
                        "predictions": datasets.Value("string", id="sequence"),
                        "references": datasets.Value("string", id="sequence"),
                    }
                ),
            ],
            codebase_urls=["https://github.com/sign-language-processing/signwriting-evaluation"],
            reference_urls=[
                "https://github.com/sign-language-processing/signwriting-evaluation",
            ],
        )

    def _compute(self, predictions, references):
        # the internal array is as long as the predictions, the external one is for multiple references.
        references = list(zip(*references))

        score = self.metric.corpus_score(predictions, references)

        return {"score": score}