diff --git a/supervision/__init__.py b/supervision/__init__.py index 0a80113d..cc54fe70 100644 --- a/supervision/__init__.py +++ b/supervision/__init__.py @@ -51,7 +51,6 @@ from supervision.detection.tools.inference_slicer import InferenceSlicer from supervision.detection.tools.json_sink import JSONSink from supervision.detection.tools.polygon_zone import PolygonZone, PolygonZoneAnnotator from supervision.detection.tools.smoother import DetectionsSmoother -from supervision.detection.utils.vlms import edit_distance, fuzzy_match_index from supervision.detection.utils.boxes import ( clip_boxes, denormalize_boxes, @@ -93,6 +92,7 @@ from supervision.detection.utils.polygons import ( approximate_polygon, filter_polygons_by_area, ) +from supervision.detection.utils.vlms import edit_distance, fuzzy_match_index from supervision.detection.vlm import LMM, VLM from supervision.draw.color import Color, ColorPalette from supervision.draw.utils import ( diff --git a/supervision/detection/utils/vlms.py b/supervision/detection/utils/vlms.py index 94af7476..9022b061 100644 --- a/supervision/detection/utils/vlms.py +++ b/supervision/detection/utils/vlms.py @@ -62,7 +62,7 @@ def edit_distance(string_1: str, string_2: str, case_sensitive: bool = True) -> distance_matrix[i][j] = min( distance_matrix[i - 1][j] + 1, distance_matrix[i][j - 1] + 1, - distance_matrix[i - 1][j - 1] + substitution_cost + distance_matrix[i - 1][j - 1] + substitution_cost, ) return distance_matrix[length_1][length_2] @@ -103,4 +103,4 @@ def fuzzy_match_index( for idx, candidate in enumerate(candidates): if edit_distance(candidate, query, case_sensitive=case_sensitive) <= threshold: return idx - return None \ No newline at end of file + return None diff --git a/test/detection/utils/test_vlms.py b/test/detection/utils/test_vlms.py index 6365cf8a..76310a22 100644 --- a/test/detection/utils/test_vlms.py +++ b/test/detection/utils/test_vlms.py @@ -2,77 +2,69 @@ import pytest from supervision.detection.utils.vlms import edit_distance, fuzzy_match_index + @pytest.mark.parametrize( "string_1, string_2, case_sensitive, expected_result", [ # identical strings, various cases ("hello", "hello", True, 0), ("hello", "hello", False, 0), - # case sensitive vs insensitive ("Test", "test", True, 1), ("Test", "test", False, 0), ("CASE", "case", True, 4), ("CASE", "case", False, 0), - # completely different ("abc", "xyz", True, 3), ("abc", "xyz", False, 3), - # one string empty ("hello", "", True, 5), ("", "world", True, 5), - # single character cases ("a", "b", True, 1), ("A", "a", True, 1), ("A", "a", False, 0), - # whitespaces ("hello world", "helloworld", True, 1), ("test", " test", True, 1), - # unicode and emoji ("😊", "😊", True, 0), ("😊", "😢", True, 1), - # long string vs empty ("a" * 100, "", True, 100), ("", "b" * 100, True, 100), - # prefix/suffix ("prefix", "prefixes", True, 2), ("suffix", "asuffix", True, 1), - # leading/trailing whitespace (" hello", "hello", True, 1), ("hello", "hello ", True, 1), - # long almost-equal string ( "The quick brown fox jumps over the lazy dog", "The quick brown fox jumps over the lazy cog", True, - 1 + 1, ), ( "The quick brown fox jumps over the lazy dog", "The quick brown fox jumps over the lazy cog", False, - 1 + 1, ), - # both empty ("", "", True, 0), ("", "", False, 0), - # mixed case with symbols ("123ABC!", "123abc!", True, 3), ("123ABC!", "123abc!", False, 0), ], ) def test_edit_distance(string_1, string_2, case_sensitive, expected_result): - assert edit_distance(string_1, string_2, case_sensitive=case_sensitive) == expected_result + assert ( + edit_distance(string_1, string_2, case_sensitive=case_sensitive) + == expected_result + ) @pytest.mark.parametrize( @@ -109,12 +101,17 @@ def test_edit_distance(string_1, string_2, case_sensitive, expected_result): (["short", "words", "only"], "longerword", 2, True, None), # repeated candidates (["a", "a", "a"], "b", 1, True, 0), - ] + ], ) -def test_fuzzy_match_index(candidates, query, threshold, case_sensitive, expected_result): - assert fuzzy_match_index( - candidates=candidates, - query=query, - threshold=threshold, - case_sensitive=case_sensitive - ) == expected_result +def test_fuzzy_match_index( + candidates, query, threshold, case_sensitive, expected_result +): + assert ( + fuzzy_match_index( + candidates=candidates, + query=query, + threshold=threshold, + case_sensitive=case_sensitive, + ) + == expected_result + )