The library lists SIFT4 under Experimental. It was developed through experimental testing and without a theoretical foundation, so treat its scores as heuristic and validate them against your own data, especially when you customize tokenization.
SIFT4
SIFT4 is a general-purpose string distance inspired by Jaro-Winkler and longest common subsequence. It accounts for character substitutions, the distance between matching characters, and transpositions. Lower values indicate more similar strings; with the default character tokenizer and options, identical strings return 0.
SIFT4().distance(s1, s2, maxoffset=5, options=None) returns an integer-valued distance. maxoffset controls how far ahead the algorithm searches for a matching token. The default is 5; larger values let it bridge larger shifts or edits. The second example below uses maxoffset=10.
from strsimpy import SIFT4
s = SIFT4()
print(s.distance("This is the first string", "And this is another string"))
a = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
b = "Amet Lorm ispum dolor sit amet, consetetur adixxxpiscing elit."
print(s.distance(a, b, maxoffset=10))will produce:
11
12Options
options accepts a dictionary. SIFT4Options validates the same dictionary shape and exposes the built-in tokenizers and evaluators as static methods. Pass the dictionary to distance(), not a SIFT4Options instance; the binding rejects a non-dictionary with ValueError. If you have built an instance, its .options attribute returns a dictionary that distance() accepts.
| Key | Value | Named presets |
|---|---|---|
maxdistance |
int; 0 (default) disables the cutoff |
none |
tokenizer |
callable taking a string and returning an iterable of tokens | "ngram", "wordsplit", "characterfrequency" |
tokenmatcher |
callable (token1, token2) -> bool |
"sift4tokenmatcher" |
matchingevaluator |
callable (token1, token2) -> float |
"sift4matchingevaluator" |
locallengthevaluator |
callable taking a length | "rewardlengthevaluator", "rewardlengthevaluator2" |
transpositioncostevaluator |
callable (c1, c2) -> float |
"longertranspositionsaremorecostly" |
transpositionsevaluator |
callable (lcss, transpositions) -> float |
none; callable only |
A nonzero maxdistance makes the algorithm stop the scan once its running distance reaches that value. For example:
from strsimpy import SIFT4
print(SIFT4().distance("abcdef", "uvwxyz", options={"maxdistance": 2}))will produce:
2Unknown keys, a non-integer maxdistance, a non-dictionary options value, and unknown preset names raise ValueError.
Named tokenizers
"ngram"splits a string into overlapping character n-grams throughSIFT4Options.ngramtokenizer, with a defaultnof2."wordsplit"splits on Unicode whitespace throughSIFT4Options.wordsplittokenizer."characterfrequency"builds a 26-element vector of case-insensitive counts for the ASCII lettersathroughzthroughSIFT4Options.characterfrequencytokenizer.
from functools import partial
from strsimpy import SIFT4, SIFT4Options
print(SIFT4Options.ngramtokenizer("abcd"))
print(SIFT4Options.ngramtokenizer("abcdef", 3))
print(SIFT4Options.wordsplittokenizer(" hello\tworld\n"))
print(SIFT4().distance("abcd", "abXY", options={"tokenizer": "ngram"}))
tokenizer = partial(SIFT4Options.ngramtokenizer, n=3)
print(SIFT4().distance("abcdef", "abcXYZ", options={"tokenizer": tokenizer}))will produce:
['ab', 'bc', 'cd']
['abc', 'bcd', 'cde', 'def']
['hello', 'world']
2
3The named "ngram" preset uses bigrams. To choose another n-gram size, wrap SIFT4Options.ngramtokenizer with functools.partial as shown above; the tokenizer is called with the string as its first argument.
The SIFT4Options API
SIFT4Options(options=None) validates a dictionary or loads the defaults. The resolved settings are available as attributes: maxdistance, tokenizer, tokenmatcher, matchingevaluator, locallengthevaluator, transpositioncostevaluator, and transpositionsevaluator. Because distance() takes the dictionary directly, constructing an instance is optional; its static methods are convenient when you build custom configurations.
| Method | Purpose |
|---|---|
SIFT4Options.ngramtokenizer(s, n=2) |
Overlapping character n-grams. |
SIFT4Options.wordsplittokenizer(s) |
Whitespace-separated tokens. |
SIFT4Options.characterfrequencytokenizer(s) |
26 case-insensitive ASCII letter counts. |
SIFT4Options.sift4tokenmatcher(token1, token2) |
Boolean token matcher based on SIFT4 similarity. |
SIFT4Options.sift4matchingevaluator(token1, token2) |
Float token similarity derived from SIFT4 distance. |
SIFT4Options.rewardlengthevaluator(length) |
Reward for local match length. |
SIFT4Options.rewardlengthevaluator2(length) |
Alternative local-length reward. |
SIFT4Options.longertranspositionsaremorecostly(c1, c2) |
Transposition cost that grows with distance. |
The default tokenizer treats each Unicode code point as a token, so the default distance is character-based.
Experimental status
SIFT4 is not a normalized measure. Its result is a raw distance whose scale depends on tokenization, maxoffset, and the evaluators you choose. The library’s README groups it with experimental algorithms because it was tuned by experiment rather than derived from a theoretical model. Prefer a normalized, well-understood measure when you need interpretable scores, and benchmark SIFT4 on representative inputs when you use it in production.
Related
- Sequence alignment provides raw alignment scores for comparing two sequences.
- Normalized compression distance is a compressor-based distance.
- Documentation home