Skip to main content

Library providing functions to calculate Levenshtein distance, Optimal String Alignment distance, and Damerau-Levenshtein distance, where the cost of each operation can be weighted by letter.

Project description

weighted-levenshtein

Use Cases

Most Levenshtein libraries assign a cost of 1 to every edit operation. This library lets you assign different costs to insertions, deletions, substitutions, and transpositions for any combination of characters. The core algorithms are implemented in Rust via PyO3 for performance.

For example, in OCR correction, substituting 0 for O should cost less than substituting X for O. In typo correction, substituting X for Z should cost less since they are adjacent on a QWERTY keyboard.

Levenshtein supports per-character insertion, deletion, and substitution costs.

Damerau-Levenshtein additionally supports transposition costs.

Installation

pip install weighted-levenshtein

Usage

import numpy as np
from weighted_levenshtein import lev, osa, dam_lev

insert_costs = np.ones(128, dtype=np.float64)
insert_costs[ord('D')] = 1.5  # inserting 'D' costs 1.5

print(lev('BANANAS', 'BANDANAS', insert_costs=insert_costs))  # 1.5

delete_costs = np.ones(128, dtype=np.float64)
delete_costs[ord('S')] = 0.5  # deleting 'S' costs 0.5

print(lev('BANANAS', 'BANANA', delete_costs=delete_costs))  # 0.5

substitute_costs = np.ones((128, 128), dtype=np.float64)
substitute_costs[ord('H'), ord('B')] = 1.25  # substituting 'H' for 'B' costs 1.25

print(lev('HANANA', 'BANANA', substitute_costs=substitute_costs))  # 1.25
print(lev('BANANA', 'HANANA', substitute_costs=substitute_costs))  # 1.0 (not symmetrical!)

transpose_costs = np.ones((128, 128), dtype=np.float64)
transpose_costs[ord('A'), ord('B')] = 0.75

# lev does not support transpositions; use osa or dam_lev
print(dam_lev('ABNANA', 'BANANA', transpose_costs=transpose_costs))  # 0.75

lev, osa, and dam_lev are aliases for levenshtein, optimal_string_alignment, and damerau_levenshtein, respectively.

Limitations

  • All string lookups are case sensitive.
  • Only the first 128 ASCII characters are supported. Cost arrays are indexed by ord() value.
  • Cost arrays must be numpy.float64 arrays of shape (128,) for insert/delete costs and (128, 128) for substitute/transpose costs. Defaults to all-ones if not provided.

Attribution

Originally created by David Su at InfoScout. Rewritten in Rust by Vikram Saraph.

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

weighted_levenshtein_rs-0.3.0.tar.gz (39.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

weighted_levenshtein_rs-0.3.0-cp310-abi3-win_amd64.whl (130.9 kB view details)

Uploaded CPython 3.10+Windows x86-64

weighted_levenshtein_rs-0.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (265.9 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

weighted_levenshtein_rs-0.3.0-cp310-abi3-macosx_11_0_arm64.whl (238.4 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

weighted_levenshtein_rs-0.3.0-cp310-abi3-macosx_10_12_x86_64.whl (243.3 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

weighted_levenshtein_rs-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (276.1 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file weighted_levenshtein_rs-0.3.0.tar.gz.

File metadata

  • Download URL: weighted_levenshtein_rs-0.3.0.tar.gz
  • Upload date:
  • Size: 39.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for weighted_levenshtein_rs-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8d0699d035ce149672eeb15c5528f4ff491eec66521c1f83c9b0dbbfa2f8afd2
MD5 060f7130a099c8f8a167fc10504f6992
BLAKE2b-256 437be4558799f86ad5b2789c7369b44d7a9b1f346e55835251a8d24778497a66

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.0.tar.gz:

Publisher: release.yml on vhxs/weighted-levenshtein-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file weighted_levenshtein_rs-0.3.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 7c1244430456f9f36769e412e606854171d87ba0afde998a157674464ff6adff
MD5 981f17e4f9808c85e3d41d9a2a3f6ffa
BLAKE2b-256 04e676f21d092f648664107b39ff507d4d1a87753deced50e02be442c5f4746a

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.0-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on vhxs/weighted-levenshtein-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file weighted_levenshtein_rs-0.3.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c078b08d00d4b551e2bee172f4e48e2ac005c9da604aa63e97832bed7a14ea89
MD5 9e1cdddf111cdf8de7368b2d427179a9
BLAKE2b-256 19b0df8ba3e888758fa4ef9d359f2e873695e7035b0fca5dea9a9c9bc9223060

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.0-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on vhxs/weighted-levenshtein-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file weighted_levenshtein_rs-0.3.0-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9b2051918fd15b5d3f4c48ac77f0b1bca344f199bcc3c30810415cfba80328ff
MD5 eaa5eeeb955e47e5cb3ea36db46f5263
BLAKE2b-256 5dda3820ff87cc27e5ae60735839ce91bcef1b72651b1969457a2101ce00cd6c

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.0-cp310-abi3-win_amd64.whl:

Publisher: release.yml on vhxs/weighted-levenshtein-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file weighted_levenshtein_rs-0.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 dc4df92c7cdad25186ffa2af07146c90779a98b98f763a63b00968d0cf414325
MD5 7a43e5023ae924126998c7beaef316bb
BLAKE2b-256 693037c3fe1b7d6f44e198e98824941d7bf8564924206ab8ad5f3dbae80ffde7

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on vhxs/weighted-levenshtein-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file weighted_levenshtein_rs-0.3.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 7195b96c8461b49c28b0962444944ae571110439d3776e12a98e296e00f510f1
MD5 7fd419146c9cd2f4054c68c4d546ded5
BLAKE2b-256 c2ca614a1c0358b33fd618b507c49b7c0e42846c15fa06b553f36c35beb31d8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.0-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on vhxs/weighted-levenshtein-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file weighted_levenshtein_rs-0.3.0-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 85635968b0efe3cb06900f4aedfad27fe230e6e6d793808e73875cb5a0da2de6
MD5 8308e76b20ce3b36aa92c783274c38ec
BLAKE2b-256 784cbfaf577c1f7726247cb397179a1b1de77ac1995a65b089e78157e9e7da44

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.0-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on vhxs/weighted-levenshtein-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file weighted_levenshtein_rs-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 dd8323433c3ebc3a0a5482fc4ac377963fdec05a66de56c356d21925b7a0a61e
MD5 9dcd136ffd47b3f7e82e39e455b59100
BLAKE2b-256 cb0f1c1a75479dd81e6c33ffaeedcfbf6644fc36eb388c9b37a66e929423536d

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on vhxs/weighted-levenshtein-rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page