keinos/mb_levenshtein

Levenshtein function with UTF-8 support.

1.0.1 2022-01-09 01:34 UTC

This package is auto-updated.

Last update: 2022-09-09 03:35:36 UTC


README

Build Status

mb_levenshtein PHP function

Levenshtein PHP function with UTF-8 support. This function finds similarity distance between two strings.

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"
    }
}