Spaces:
Running
Running
github-actions[bot]
commited on
Commit
·
146e626
1
Parent(s):
e8e5833
Sync from GitHub: 2a8cafcdb01ce486f27f41e1614e3212399b4525
Browse files- demos/compare.py +8 -3
demos/compare.py
CHANGED
|
@@ -140,12 +140,17 @@ def dist_to_sim(data, dim=64):
|
|
| 140 |
if k == "instance_match":
|
| 141 |
result[k.split("_")[0].title()] = 1.0 if v is True else -1.0
|
| 142 |
else:
|
| 143 |
-
result[k.split("_")[0].title()] =
|
| 144 |
return result
|
| 145 |
|
| 146 |
|
| 147 |
-
def
|
| 148 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
result = 1 - (2 * hamming_distance) / dim
|
| 150 |
log.debug(f"Hamming distance: {hamming_distance} - Dim: {dim} - Result: {result}")
|
| 151 |
return result
|
|
|
|
| 140 |
if k == "instance_match":
|
| 141 |
result[k.split("_")[0].title()] = 1.0 if v is True else -1.0
|
| 142 |
else:
|
| 143 |
+
result[k.split("_")[0].title()] = hamming_to_similarity(v, dim)
|
| 144 |
return result
|
| 145 |
|
| 146 |
|
| 147 |
+
def hamming_to_similarity(hamming_distance: int, dim: int) -> float:
|
| 148 |
+
"""Convert Hamming distance to a normalized similarity measure in range [-1, +1]"""
|
| 149 |
+
if dim == 0:
|
| 150 |
+
raise ValueError("Dimension must be greater than 0")
|
| 151 |
+
if hamming_distance < 0 or hamming_distance > dim:
|
| 152 |
+
raise ValueError(f"Hamming distance must be between 0 and {dim}")
|
| 153 |
+
|
| 154 |
result = 1 - (2 * hamming_distance) / dim
|
| 155 |
log.debug(f"Hamming distance: {hamming_distance} - Dim: {dim} - Result: {result}")
|
| 156 |
return result
|