The quickest way to see the library in action is one small script. Every class used here is available from the package root.
Run the examples
Create a file named example.py:
from strsimpy import (
Jaccard,
JaroWinkler,
Levenshtein,
NeedlemanWunsch,
NormalizedCompressionDistance,
)
print(Levenshtein().distance("kitten", "sitting"))
print(JaroWinkler().similarity("MARTHA", "MARHTA"))
print(Jaccard(2).similarity("night", "nacht"))
print(NeedlemanWunsch().score("GATTACA", "GCATGCU"))
print(NormalizedCompressionDistance().distance("aaaa", "aaab"))Run it with:
python example.pyOn strsimpy 0.5.0 the output is exactly:
3
0.9611111111111111
0.14285714285714285
0.0
0.3333333333333333What each result means
| Measure | Result | Interpretation |
|---|---|---|
Levenshtein().distance |
3 |
Three single-character edits transform "kitten" into "sitting". The result is an int here. |
JaroWinkler().similarity |
0.9611111111111111 |
A normalized similarity, where 1.0 means identical. |
Jaccard(2).similarity |
0.14285714285714285 |
Similarity between the sets of character bigrams, where 1.0 means identical sets. |
NeedlemanWunsch().score |
0.0 |
A raw global alignment score, not a distance. It can be zero or negative. |
NormalizedCompressionDistance().distance |
0.3333333333333333 |
A compression-based distance with the RLE compressor. Lower values are more similar. |
Similarity, distance or score?
| Method | Meaning | Typical range | Classes |
|---|---|---|---|
similarity() |
Higher is more similar; 1.0 usually means identical. |
Usually 0.0 to 1.0 |
JaroWinkler, Jaccard, Cosine |
distance() |
Lower is more similar; 0.0 usually means identical. |
Depends on the measure | Levenshtein, NormalizedCompressionDistance |
score() |
Raw alignment score under the configured match, mismatch and gap parameters. | Any finite value; Smith-Waterman is non-negative | NeedlemanWunsch, SmithWaterman, Gotoh |
The sequence-alignment classes deliberately expose scores rather than distances. Use alignment() on those classes, or on OptimalStringAlignment, when you need the edit operations behind a result.
Where to go next
- Read the algorithms section for the definition, range and caveats of each measure.
- Browse the API reference for constructors and method signatures.
- Use the Rust core directly if you need the algorithms outside Python.