-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Pgvector; | ||
|
||
class SparseVector | ||
{ | ||
protected $dimensions; | ||
protected $indices; | ||
protected $values; | ||
|
||
public function __construct($dimensions, $indices, $values) | ||
{ | ||
$this->dimensions = $dimensions; | ||
$this->indices = $indices; | ||
$this->values = $values; | ||
} | ||
|
||
public static function fromDense($value) | ||
{ | ||
$dimensions = count($value); | ||
$indices = []; | ||
$values = []; | ||
foreach ($value as $i => $v) { | ||
if ($v != 0) { | ||
$indices[] = $i; | ||
$values[] = floatval($v); | ||
} | ||
} | ||
return new SparseVector($dimensions, $indices, $values); | ||
} | ||
|
||
public static function fromString($value) | ||
{ | ||
$parts = explode('/', $value, 2); | ||
$dimensions = intval($parts[1]); | ||
$indices = []; | ||
$values = []; | ||
$elements = explode(',', substr($parts[0], 1, -1)); | ||
foreach ($elements as $e) { | ||
$ep = explode(':', $e, 2); | ||
$indices[] = intval($ep[0]) - 1; | ||
$values[] = floatval($ep[1]); | ||
} | ||
return new SparseVector($dimensions, $indices, $values); | ||
} | ||
|
||
public function __toString() | ||
{ | ||
$elements = []; | ||
for ($i = 0; $i < count($this->indices); $i++) { | ||
$elements[] = ($this->indices[$i] + 1) . ':' . $this->values[$i]; | ||
} | ||
return '{' . implode(',', $elements) . '}/' . $this->dimensions; | ||
} | ||
|
||
public function toArray() | ||
{ | ||
$result = array_fill(0, $this->dimensions, 0.0); | ||
for ($i = 0; $i < count($this->indices); $i++) { | ||
$result[$this->indices[$i]] = $this->values[$i]; | ||
} | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Pgvector\Laravel; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\Castable; | ||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class SparseVector extends \Pgvector\SparseVector implements Castable | ||
{ | ||
public static function castUsing(array $arguments): CastsAttributes | ||
{ | ||
return new class ($arguments) implements CastsAttributes { | ||
public function __construct(array $arguments) | ||
{ | ||
// no need for dimensions | ||
} | ||
|
||
public function get(mixed $model, string $key, mixed $value, array $attributes): ?\Pgvector\SparseVector | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
return SparseVector::fromString($value); | ||
} | ||
|
||
public function set(mixed $model, string $key, mixed $value, array $attributes): ?string | ||
{ | ||
if (is_null($value)) { | ||
return null; | ||
} | ||
|
||
if (is_array($value)) { | ||
$value = SparseVector::fromDense($value); | ||
} | ||
|
||
return (string) $value; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters