Skip to content

Commit f76634e

Browse files
authored
Merge pull request #8 from sign-language-processing/copilot/ensure-float-return-values
2 parents caed1f0 + d935534 commit f76634e

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

signwriting_evaluation/metrics/similarity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def length_acc(self, hyp: Sign, ref: Sign) -> float:
129129
def error_rate(self, hyp: Sign, ref: Sign) -> float:
130130
# Calculate the evaluate score for a given hypothesis and ref.
131131
if not hyp["symbols"] or not ref["symbols"]:
132-
return 1
132+
return 1.0
133133

134134
cost_matrix = np.array(
135135
[self.symbols_score(first, second) for first in hyp["symbols"] for second in ref["symbols"]])
@@ -150,7 +150,7 @@ def score_single_sign(self, hypothesis: str, reference: str) -> float:
150150

151151
def score(self, hypothesis: Optional[str], reference: Optional[str]) -> float:
152152
if hypothesis is None or reference is None:
153-
return 0
153+
return 0.0
154154

155155
# Here, hypothesis and reference are both FSW strings of potentially different number of signs
156156
hypothesis_signs = text_to_signs(hypothesis)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import unittest
2+
3+
from signwriting_evaluation.metrics.similarity import SignWritingSimilarityMetric
4+
from signwriting_evaluation.metrics.bleu import SignWritingBLEU
5+
from signwriting_evaluation.metrics.chrf import SignWritingCHRF
6+
7+
8+
class TestMetricsReturnFloat(unittest.TestCase):
9+
"""Test that all metrics return float values, never integers."""
10+
11+
def setUp(self):
12+
self.similarity = SignWritingSimilarityMetric()
13+
self.bleu = SignWritingBLEU()
14+
self.chrf = SignWritingCHRF()
15+
16+
def test_similarity_returns_float(self):
17+
"""Test that similarity metric always returns float."""
18+
# Test with None values (edge case that was returning int)
19+
score = self.similarity.score(None, None)
20+
self.assertIsInstance(score, float, "Similarity score with None inputs should be float")
21+
22+
# Test with valid FSW strings
23+
hypothesis = "M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517"
24+
reference = "M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513"
25+
score = self.similarity.score(hypothesis, reference)
26+
self.assertIsInstance(score, float, "Similarity score should be float")
27+
28+
# Test with None hypothesis
29+
score = self.similarity.score(None, reference)
30+
self.assertIsInstance(score, float, "Similarity score with None hypothesis should be float")
31+
32+
# Test with None reference
33+
score = self.similarity.score(hypothesis, None)
34+
self.assertIsInstance(score, float, "Similarity score with None reference should be float")
35+
36+
def test_bleu_returns_float(self):
37+
"""Test that BLEU metric always returns float."""
38+
hypothesis = "M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517"
39+
reference = "M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513"
40+
score = self.bleu.score(hypothesis, reference)
41+
self.assertIsInstance(score, float, "BLEU score should be float")
42+
43+
def test_chrf_returns_float(self):
44+
"""Test that CHRF metric always returns float."""
45+
hypothesis = "M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517"
46+
reference = "M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513"
47+
score = self.chrf.score(hypothesis, reference)
48+
self.assertIsInstance(score, float, "CHRF score should be float")
49+
50+
def test_similarity_score_all_returns_float(self):
51+
"""Test that score_all returns list of lists of floats."""
52+
hypotheses = ["M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517"]
53+
references = ["M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513"]
54+
scores = self.similarity.score_all(hypotheses, references, progress_bar=False)
55+
for row in scores:
56+
for score in row:
57+
self.assertIsInstance(score, float, "Each score in score_all should be float")
58+
59+
def test_similarity_corpus_score_returns_float(self):
60+
"""Test that corpus_score returns float."""
61+
hypotheses = ["M530x538S37602508x462S15a11493x494S20e00488x510S22f03469x517"]
62+
references = [["M519x534S37900497x466S3770b497x485S15a51491x501S22f03481x513"]]
63+
score = self.similarity.corpus_score(hypotheses, references)
64+
self.assertIsInstance(score, float, "Corpus score should be float")
65+
66+
67+
if __name__ == '__main__':
68+
unittest.main()

0 commit comments

Comments
 (0)