Embedding as a vector of numbers (PHP)

Minimal example: embedding as a vector of numbers (PHP)

Below is a minimal pure PHP example where we emulate embeddings with fixed vectors and compare a query against documents using cosine similarity. This is the core semantic-search mechanic without external libraries.

 
<?php

include 'code-en.php';

$query 'speed up email sending';
$documents = [
    
'email newsletter optimization',
    
'how to prepare coffee',
];

$queryEmbedding embed($query);

foreach (
$documents as $doc) {
    
$score cosineSimilarity(
        
$queryEmbedding,
        
embed($doc)
    );

    echo 
$doc ': ' round($score3) . PHP_EOL;
}

Documents:

Query: speed up email sending

email newsletter optimization
how to prepare coffee
Result: Memory: 0.005 Mb Time running: < 0.001 sec.
email newsletter optimization: 0.998
how to prepare coffee: 0.202