Download the PHP package zbmowrey/weighted-levenshtein without Composer

On this page you can find all versions of the php package zbmowrey/weighted-levenshtein. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package weighted-levenshtein

weighted-levenshtein

CI

Weighted Levenshtein, Optimal String Alignment, and Damerau-Levenshtein edit-distance algorithms for PHP 8.4+. Pure PHP, strict types, no extensions.

PHP port of weighted-levenshtein by David Su / InfoScout. The algorithms, default behavior, and asymmetric cost semantics are preserved exactly; only the API has been reshaped to feel native in modern PHP.

Why weighted distance?

Most edit-distance libraries treat every insertion, deletion, substitution, or transposition as cost 1. That's fine for generic fuzzy-matching but often not what you actually want.

This library lets you specify a cost per character (for insert/delete) and per ordered pair (for substitute/transpose), then runs the appropriate dynamic-programming algorithm.

Installation

Requires PHP 8.4 or newer.

Quick start

Cost maps are immutable value objects. Build them with withCost():

Full examples

These are the same examples as the original Python README, translated 1:1. Every snippet has a corresponding test in tests/Readme/.

Short aliases are available as free functions:

Built-in cost map presets

Two opinionated presets ship under Zbmowrey\WeightedLevenshtein\Presets\ for the two most common weighted-distance use cases. Both return immutable CharPairCostMap instances, so you can layer your own overrides with withCost().

OCR output (OcrConfusions)

OcrConfusions::common(float $cost = 0.25) covers a curated list of high-confidence OCR confusions in both directions. The full set:

The list is intentionally conservative — only pairs that confuse across most fonts are included. Font-specific or low-resolution-only confusions (e.g. 4↔A, 0↔6, n↔u) and multi-character confusions (rn↔m, cl↔d, vv↔w) are not registered. Layer them on with withCost() if your data needs them.

Pairs not in the list keep the default cost of 1.0. Layer your domain-specific tweaks with withCost():

Human typos (QwertyKeyboard)

Costs are derived from the Euclidean distance between keys on a standard staggered US QWERTY layout. Orthogonal and close-diagonal neighbors get the adjacent cost (default 0.5); one-key-removed pairs get the near cost (default 0.75); everything else stays at 1.0. Both lowercase and uppercase letters are populated. Mixed-case and mixed-shift-state pairs (e.g. q/W, Q/1) are left at default — those errors are rare in practice.

Override the thresholds if 0.5/0.75 don't fit your data:

API reference

Zbmowrey\WeightedLevenshtein\Distance

Method Description
Distance::levenshtein(string $a, string $b, ?CharCostMap $insertCosts = null, ?CharCostMap $deleteCosts = null, ?CharPairCostMap $substituteCosts = null): float Wagner-Fischer Levenshtein distance.
Distance::optimalStringAlignment(string $a, string $b, ?CharCostMap $insertCosts = null, ?CharCostMap $deleteCosts = null, ?CharPairCostMap $substituteCosts = null, ?CharPairCostMap $transposeCosts = null): float Wagner-Fischer with a single adjacent transposition check; substrings used in a transposition cannot also be edited.
Distance::damerauLevenshtein(string $a, string $b, ?CharCostMap $insertCosts = null, ?CharCostMap $deleteCosts = null, ?CharPairCostMap $substituteCosts = null, ?CharPairCostMap $transposeCosts = null): float True Damerau-Levenshtein distance with arbitrary non-overlapping adjacent transpositions.

Zbmowrey\WeightedLevenshtein\CharCostMap

Immutable per-character cost map for insert/delete operations.

Method Description
CharCostMap::uniform(float $defaultCost = 1.0): self Construct a map where every character has cost $defaultCost.
withCost(string $char, float $cost): self Return a new map with $cost for the single ASCII byte $char.
cost(string $char): float Look up the cost for $char.

Zbmowrey\WeightedLevenshtein\CharPairCostMap

Immutable per-ordered-pair cost map for substitute/transpose operations.

Method Description
CharPairCostMap::uniform(float $defaultCost = 1.0): self Construct a map where every ordered pair has cost $defaultCost.
withCost(string $from, string $to, float $cost): self Return a new map with $cost for the ordered pair ($from, $to).
cost(string $from, string $to): float Look up the cost for the ordered pair.

Zbmowrey\WeightedLevenshtein\Presets\OcrConfusions

Method Description
OcrConfusions::common(float $cost = 0.25): CharPairCostMap Curated OCR confusion substitutions in both directions.

Zbmowrey\WeightedLevenshtein\Presets\QwertyKeyboard

Method Description
QwertyKeyboard::substituteCosts(float $adjacentCost = 0.5, float $nearCost = 0.75): CharPairCostMap Adjacency-weighted substitution cost map for ASCII letters and digits on a US QWERTY layout.
QwertyKeyboard::transposeCosts(float $adjacentCost = 0.5, float $nearCost = 0.75): CharPairCostMap Adjacency-weighted transposition cost map using the same layout.

Free function aliases

In namespace Zbmowrey\WeightedLevenshtein:

Limitations

Performance

Pure PHP. Suitable for typical fuzzy-matching workloads in the range of low thousands of comparisons per second on strings of ~100 characters. If you need C-speed, use the original Python library, or PHP's built-in levenshtein() for the unweighted case. Two-row rolling buffer is used for plain Levenshtein; full DP matrices are used for OSA and Damerau-Levenshtein because both need O(m × n) state.

Contributing

Contributions welcome. Open an issue or PR. The full QA suite is composer qa (PHPUnit + PHPStan level max + PHP-CS-Fixer dry-run). All three must be green for a PR to land.

License

MIT. See LICENSE. PHP port copyright © 2026 Zach Mowrey. Original Python library copyright © 2016 InfoScout, distributed under the MIT License.


All versions of weighted-levenshtein with dependencies

PHP Build Version
Package Version
Requires php Version ^8.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package zbmowrey/weighted-levenshtein contains the following files

Loading the files please wait ...