keinos / mb_levenshtein
Levenshtein function with UTF-8 support.
Installs: 12 962
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 0
Open Issues: 2
Requires
- php: >=5.3.0
- ext-mbstring: *
This package is auto-updated.
Last update: 2024-11-09 08:08:21 UTC
README
mb_levenshtein PHP function
Levenshtein PHP function with UTF-8 support. This function finds similarity distance between two strings.
- Source Code @ GitHub
- Composer available @ Packagist
Functions
-
Returns in Levenshtein Distance. (The smaller, the closer)
mb_levenshtein ( string $str1 , string $str2 ) : int
mb_levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del ) : int
-
Returns in Levenshtein Ratio between 0 to 1. (The bigger, the closer)
mb_levenshtein_ratio ( string $str1 , string $str2 ) : float
mb_levenshtein_ratio ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del ) : float
Usage
<?php include_once('./mb_levenshtein.php'); $query = 'cafe'; $comps = [ 'coffee', 'café', 'tea', 'sake', ]; echo "Query word: ${query}" . PHP_EOL; foreach ($comps as $comp) { $sim = mb_levenshtein($query, $comp); echo " ${comp}: ${sim}" . PHP_EOL; }
Results:
$ # The smaller, the closer $ php ./sample.php Query word: cafe coffee: 3 café: 1 tea: 4 sake: 2
Composer
-
To use released version:
composer require keinos/mb_levenshtein
-
To use latest version:
composer require keinos/mb_levenshtein:dev-master
<?php require_once('vendor/autoload.php'); $query = 'cafe'; $comps = [ 'coffee', 'café', 'tea', 'sake', ]; echo "Query word: ${query}" . PHP_EOL; foreach ($comps as $comp) { $sim = mb_levenshtein($query, $comp); echo " ${comp}: ${sim}" . PHP_EOL; }
Results:
$ ls sample.php $ composer require keinos/mb_levenshtein Using version ^1.0 for keinos/mb_levenshtein ./composer.json has been created Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing keinos/mb_levenshtein (1.0.0): Downloading (100%) Writing lock file Generating autoload files $ # The smaller, the closer $ php ./sample.php Query word: cafe coffee: 3 café: 1 tea: 4 sake: 2 $ ls composer.json composer.lock sample.php vendor $ cat composer.json { "require": { "keinos/mb_levenshtein": "^1.0" } }