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-rs

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-rs

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.1.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.1-cp310-abi3-win_amd64.whl (130.9 kB view details)

Uploaded CPython 3.10+Windows x86-64

weighted_levenshtein_rs-0.3.1-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.1-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.1-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.1-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.1.tar.gz.

File metadata

  • Download URL: weighted_levenshtein_rs-0.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 c2c8e340f951c42a40d80e0638dd8e45cd8c8f1378bde1bcf05ec26874366286
MD5 f350c5e6b0c5317a13c7898245600f1f
BLAKE2b-256 c3a07415e387555ec8013175530fb5dd1c54ff8ef6e24b804b65c7e6b8f9f80d

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.1.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.1-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.1-pp310-pypy310_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5128fb43a2c2b98d11784147bfe907e02ec3ec13f122c86dd52935d6ce685f13
MD5 2b9870ffd5e9bda54ae264e374cc242e
BLAKE2b-256 306ba17f868e3b4f1eed24309edceb297db8ae8967e80bdacc1d57dae2a730c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.1-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.1-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.1-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2cd38719873589810dda3ce808451288de01704f5dd969dd6f4d50392b8b7cc5
MD5 816d0e3bc25eee9af5a6289a272367f1
BLAKE2b-256 a32cae920f47f9f0a381be03392a882b69ce202eede3a80d2cf1ab5d4dcee8da

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.1-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.1-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f85a227290549a5ac058049491b559c19ce5ef7cdd24bfd9f26a6413612e9437
MD5 4da07a5225778bf61a9babede7d33cda
BLAKE2b-256 ba04a19568d62c716f5360a1b6c639559850f8dafc50d5f3c65031a0705be48c

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.1-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.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 1273c9be71135ceaf95ccd7f2f67a2e16c8debd44d8c0bcd56ca7d5e3c98540a
MD5 5da40d26aa0dbd3a556ffd569e4b7b8e
BLAKE2b-256 828cd381af312c570a2f00378087fdb5710ace7f0e72be64962d0de30d05cc0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.1-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.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 62d94ab2162182564868fbdda51b9454a8573507e61f4d40c8835c76e1216b0f
MD5 1c7b57f862374e7cc25a0eda08725247
BLAKE2b-256 c9459a33391d9e733f8f93315a50f44e0888f378088cf4bf2455d605b1377f78

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.1-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.1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3ece0f074c237b7174c0ee9ea993794c76885533a53714d695fded6eb33846e7
MD5 94c16bf31c00e0c5feb6aa5d56c786e6
BLAKE2b-256 b9148908219cd10b270a1036bdfa1a3a5f85c486b410ddc242d4afd05fde960f

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.1-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.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for weighted_levenshtein_rs-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6d7843ff495bde1096f9f86cd743c9cf729478eae1e7fdd65076ab53f0e5ae23
MD5 436a747e113eff462bedae6353e37058
BLAKE2b-256 aaa175f369d39532b4d7144e8c14665b2d48bd11787a96f5e36892b4e8ce6df9

See more details on using hashes here.

Provenance

The following attestation bundles were made for weighted_levenshtein_rs-0.3.1-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