Skip to content

Commit a498440

Browse files
committed
fix(similarity): handle edge case of unknown symbol class
1 parent f98b32d commit a498440

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

signwriting_evaluation/metrics/similarity.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ def calculate_distance(self, hyp: SignSymbol, ref: SignSymbol) -> float:
6060
hyp_class = self.get_shape_class_index(hyp_veq)
6161
ref_class = self.get_shape_class_index(ref_veq)
6262

63+
if hyp_class is None or ref_class is None:
64+
return self.max_distance
65+
6366
hyp_veq = self.weight_vector(hyp_veq)
6467
ref_veq = self.weight_vector(ref_veq)
6568
distance = (dis.euclidean(hyp_veq, ref_veq) +

signwriting_evaluation/metrics/test_similarity.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ def test_score_swu(self):
6565
self.assertIsInstance(score, float) # Check if the score is a float
6666
self.assertAlmostEqual(score, 0.5509574768254414)
6767

68+
def test_unknown_symbol_class_returns_zero_score(self):
69+
# Test that symbols with shapes outside defined class ranges are handled gracefully
70+
# When a symbol's shape doesn't match any defined symbol class ranges, the metric
71+
# should return maximum distance (resulting in zero similarity score) rather than crashing
72+
hypothesis = "M530x538S38c00508x462" # S38c00 has shape 0x38c, outside all defined ranges
73+
reference = "M530x538S10000508x462" # S10000 has shape 0x100, in hands_shapes range
74+
score = self.metric.score(hypothesis, reference)
75+
self.assertEqual(score, 0)
76+
6877

6978
if __name__ == '__main__':
7079
unittest.main()

0 commit comments

Comments
 (0)