Neural network-based string similarity using squared Euclidean approximation to Levenshtein distance
Project description
megashtein
In their paper "Deep Squared Euclidean Approximation to the Levenshtein Distance for DNA Storage", Guo et al. explore techniques for using a neural network to embed sequences in such a way that the squared Euclidean distance between embeddings approximates the Levenshtein distance between the original sequences. This implementation also takes techniques from "Levenshtein Distance Embeddings with Poisson Regression for DNA Storage" by Wei et al. (2023).
This is valuable because there are excellent libraries for doing fast GPU accelerated searches for the K nearest neighbors of vectors, like faiss. Algorithms like HNSW allow us to do these searches in logarithmic time, where a brute force levenshtein distance based fuzzy search would need to run in exponential time.
This repo contains a PyTorch implementation of the core ideas from Guo's paper, adapted for ASCII sequences rather than DNA sequences. The implementation includes:
- A convolutional neural network architecture for sequence embedding
- Training using Poisson regression loss (PNLL) as described in the paper
- Synthetic data generation with controlled edit distance relationships
- Model saving and loading functionality
The trained model learns to embed ASCII strings such that the squared Euclidean distance between embeddings approximates the true Levenshtein distance between the strings.
Model Architecture
The model uses a 5-layer CNN with average pooling followed by fully connected layers to produce fixed-size embeddings from variable-length ASCII sequences.
Installation
pip install megashtein
Usage
import torch
from megashtein import load_model, embed_string
# Load the pre-trained model
model = load_model()
# Example usage
text1 = "hello world"
text2 = "hello word"
# Embed strings
emb1 = embed_string(model, text1)
emb2 = embed_string(model, text2)
# Compute approximate edit distance
approx_distance = torch.sum((emb1 - emb2) ** 2).item()
print(f"Approximate edit distance: {approx_distance}")
Use Cases
- Fuzzy String Search: Find similar strings in large text collections
- Text Clustering: Group similar texts based on edit distance
- Data Deduplication: Identify near-duplicate text entries
- Approximate String Matching: Fast similarity search with controllable accuracy
Limitations
- Fixed Length: Input sequences must be exactly 80 characters (padded/truncated)
- ASCII Only: Limited to ASCII character set (0-127)
- Approximation: Provides approximate rather than exact edit distances
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file megashtein-0.1.5.tar.gz.
File metadata
- Download URL: megashtein-0.1.5.tar.gz
- Upload date:
- Size: 543.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3348eb4d685d4900a86cde59b154cb8e837746ce1beea906b4687e6a46b43873
|
|
| MD5 |
1feeabe0b111d483e9fd95b3308e8fbd
|
|
| BLAKE2b-256 |
ef6b09b56213cf144371bc7c759e426dfa8192e6eb89710493c110d1f2fd4134
|
Provenance
The following attestation bundles were made for megashtein-0.1.5.tar.gz:
Publisher:
publish.yml on Sawyer-Powell/megashtein
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
megashtein-0.1.5.tar.gz -
Subject digest:
3348eb4d685d4900a86cde59b154cb8e837746ce1beea906b4687e6a46b43873 - Sigstore transparency entry: 264969622
- Sigstore integration time:
-
Permalink:
Sawyer-Powell/megashtein@1b751b2aa7548be07d01d0a57554f1f7410292d8 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Sawyer-Powell
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1b751b2aa7548be07d01d0a57554f1f7410292d8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file megashtein-0.1.5-py3-none-any.whl.
File metadata
- Download URL: megashtein-0.1.5-py3-none-any.whl
- Upload date:
- Size: 514.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c11a160c5545293998d94cc49a2ec3f6df95e888dd7822ff26477cbba9a8792
|
|
| MD5 |
f75feffe1b6c732d7870ecf19f40e622
|
|
| BLAKE2b-256 |
c64897093104457223f0073e8bb3560af9f2bb7b5c8688e8e22a9617a33a861a
|
Provenance
The following attestation bundles were made for megashtein-0.1.5-py3-none-any.whl:
Publisher:
publish.yml on Sawyer-Powell/megashtein
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
megashtein-0.1.5-py3-none-any.whl -
Subject digest:
0c11a160c5545293998d94cc49a2ec3f6df95e888dd7822ff26477cbba9a8792 - Sigstore transparency entry: 264969629
- Sigstore integration time:
-
Permalink:
Sawyer-Powell/megashtein@1b751b2aa7548be07d01d0a57554f1f7410292d8 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Sawyer-Powell
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@1b751b2aa7548be07d01d0a57554f1f7410292d8 -
Trigger Event:
push
-
Statement type: