PHP code example of x-laravel / fuzzy-similarity

1. Go to this page and download the library: Download x-laravel/fuzzy-similarity library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

x-laravel / fuzzy-similarity example snippets


use Illuminate\Database\Eloquent\Model;
use XLaravel\FuzzySimilarity\Attributes\FuzzyOn;
use XLaravel\FuzzySimilarity\Concerns\FuzzyComparable;
use XLaravel\FuzzySimilarity\Contracts\HasFuzzySimilarity;

#[FuzzyOn('name')]
#[FuzzyOn(['name', 'city'], slot: 'location')]
class Company extends Model implements HasFuzzySimilarity
{
    use FuzzyComparable;
}

#[FuzzyOn('title')]
class Article extends Model implements HasFuzzySimilarity
{
    use FuzzyComparable;
}

$company->fuzzySimilarityTo($otherCompany);             // default slot
$company->fuzzySimilarityTo($otherCompany, slot: 'location');
$company->fuzzySimilarityTo($article);                  // Company name vs Article title
$company->fuzzySimilarityTo('Acme Corporation');        // plain string
$company->fuzzySimilarityTo('Corporation Acme', algorithm: 'token_set');

$ranked = Company::rankByFuzzy($candidates, 'Acme Corporation', limit: 20, threshold: 40.0);

$ranked->first()->fuzzy_score; // 0.0 – 100.0

use XLaravel\FuzzySimilarity\FuzzySimilarity;

app(FuzzySimilarity::class)->compare('São Paulo', 'sao-paulo'); // 100.0
bash
php artisan vendor:publish --tag=fuzzy-similarity-config