Skip to content

Commit

Permalink
Added Hamming and Jaccard distances
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed May 17, 2024
1 parent 95cc23a commit f6cff70
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 0.2.0 (unreleased)

- Added `L1` distance
- Added `L1`, `Hamming`, and `Jaccard` distances
- Changed `Distance` to enum
- Dropped support for PHP < 8.1
- Dropped support for Laravel < 10
Expand Down
2 changes: 2 additions & 0 deletions src/laravel/Distance.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ enum Distance
case InnerProduct;
case Cosine;
case L1;
case Hamming;
case Jaccard;
}
8 changes: 7 additions & 1 deletion src/laravel/HasNeighbors.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ public function scopeNearestNeighbors(Builder $query, string $column, mixed $val
case Distance::L1:
$op = '<+>';
break;
case Distance::Hamming:
$op = '<~>';
break;
case Distance::Jaccard:
$op = '<%>';
break;
default:
throw new \InvalidArgumentException("Invalid distance");
}
$wrapped = $query->getGrammar()->wrap($column);
$order = "$wrapped $op ?";
$neighborDistance = $distance == Distance::InnerProduct ? "($order) * -1" : $order;
$vector = $value instanceof Vector ? $value : new Vector($value);
$vector = is_array($value) ? new Vector($value) : $value;

// ideally preserve existing select, but does not appear to be a way to get columns
$query->select()
Expand Down

0 comments on commit f6cff70

Please sign in to comment.