VLMs Utils¶
supervision.detection.utils.vlms.edit_distance(string_1, string_2, case_sensitive=True)
¶
Calculates the minimum number of single-character edits required to transform one string into another. Allowed operations are insertion, deletion, and substitution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The source string to be transformed. |
required |
|
str
|
The target string to transform into. |
required |
|
bool
|
Whether comparison should be case-sensitive. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The minimum number of edits required to convert |
int
|
into |
Examples:
import supervision as sv
sv.edit_distance("hello", "hello")
# 0
sv.edit_distance("Test", "test", case_sensitive=True)
# 1
sv.edit_distance("abc", "xyz")
# 3
sv.edit_distance("hello", "")
# 5
sv.edit_distance("", "")
# 0
sv.edit_distance("hello world", "helloworld")
# 1
Source code in supervision/detection/utils/vlms.py
supervision.detection.utils.vlms.fuzzy_match_index(candidates, query, threshold, case_sensitive=True)
¶
Searches for the first string in candidates whose edit distance
to query is less than or equal to threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[str]
|
List of strings to search. |
required |
|
str
|
String to compare against the candidates. |
required |
|
int
|
Maximum allowed edit distance for a match. |
required |
|
bool
|
Whether matching should be case-sensitive. |
True
|
Returns:
| Type | Description |
|---|---|
int | None
|
Optional[int]: Index of the first matching string in candidates, or None if no match is found. |
Examples:
fuzzy_match_index(["cat", "dog", "rat"], "dat", threshold=1)
# 0
fuzzy_match_index(["alpha", "beta", "gamma"], "bata", threshold=1)
# 1
fuzzy_match_index(["one", "two", "three"], "ten", threshold=2)
# None