Giter VIP home page Giter VIP logo

php-levenshtein-distance's Introduction

php-levenshtein-distance

GitHub Workflow Status (branch) Packagist Packagist Version Project license GitHub stars Donate to this project using Paypal

Calculate the Levenshtein distance and edit progresses between two strings. Note that if you do not need the edit path, PHP has a built-in levenshtein() function.

Features

  • UTF-8-ready.
  • Full edit progresses information.

Installation

$ composer require jfcherng/php-levenshtein-distance

Example

See demo.php.

<?php

include __DIR__ . '/vendor/autoload.php';

use Jfcherng\Diff\LevenshteinDistance as LD;

$old = '自訂取代詞語模組';
$new = '自订取代词语模组!';

$calculator = new LD(
    true, // calculate edit progresses?
    // progress options
    LD::PROGRESS_OP_AS_STRING | LD::PROGRESS_PATCH_MODE
);

$results = $calculator->calculate($old, $new);

// this is the same but using an internal singleton
$results = LD::staticCalculate(
    $old, // old string
    $new, // new string
    true, // calculate edit progresses?
    // progress options
    LD::PROGRESS_OP_AS_STRING | LD::PROGRESS_PATCH_MODE
);

// [
//     'distance' => 5,
//     'progresses' => [
//         ['ins', 8, '!', 1],
//         ['rep', 7, '组', 1],
//         ['cpy', 6, '模', 1],
//         ['rep', 5, '语', 1],
//         ['rep', 4, '词', 1],
//         ['cpy', 3, '代', 1],
//         ['cpy', 2, '取', 1],
//         ['rep', 1, '订', 1],
//         ['cpy', 0, '自', 1],
//     ],
// ]
var_dump($results);

$results = LD::staticCalculate(
    $old, // old string
    $new, // new string
    true, // calculate edit progresses?
    // progress options
    LD::PROGRESS_OP_AS_STRING | LD::PROGRESS_PATCH_MODE | LD::PROGRESS_MERGE_NEIGHBOR
);

// [
//     'distance' => 5,
//     'progresses' => [
//         ['ins', 8, '!', 1],
//         ['rep', 7, '组', 1],
//         ['cpy', 6, '模', 1],
//         ['rep', 4, '词语', 2],
//         ['cpy', 2, '取代', 2],
//         ['rep', 1, '订', 1],
//         ['cpy', 0, '自', 1],
//     ],
// ]
var_dump($results);

Progress Options

  • LD::PROGRESS_NO_COPY: Do not include COPY operations in the progresses.
  • LD::PROGRESS_MERGE_NEIGHBOR: Merge neighbor progresses if possible.
  • LD::PROGRESS_OP_AS_STRING: Convert the operation in progresses from int to string.
  • LD::PROGRESS_PATCH_MODE: Replace the new edit position with the corresponding string.

Returned progresses

  1. The operation.
  2. The edit position for the new string.
  3. The edit position for the old string. Or the corresponding string if LD::PROGRESS_PATCH_MODE is used.
  4. The edit length.

php-levenshtein-distance's People

Contributors

codacy-badger avatar jfcherng avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

php-levenshtein-distance's Issues

Class LevenshteinDistance not found

Hi !
I tried to test the class after the composer installation
I tried "demo.php" but i got this

Fatal error: Uncaught Error: Class 'Jfcherng\Utility\LevenshteinDistance' not found in C:\wamp64\www\2022\22xxx-faterapp\leven\demo.php on line 12

where 'C:\wamp64\www\2022\22xxx-faterapp\leven' is te directory where i have run composer and copied demo.php

PHP is 7.4.7 with apache2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.