# Super Fast and Accurate string distance algorithm: Sift3

*Update November 2014:*Sift4 is here!! Check out the new improved version of Sift here: Super Fast and Accurate string distance algorithm: Sift4

*Update October 6 2014:*New stuff, compare Levenstein vs Sift here:

Algorithm: Levenstein Sift

String 1: String 2:

Result: *Update June 25th 2013:*I've decided to play a little with the suggestions in the comments and check for validity. This was spurned by the realization that a lot of people use my algorithm. So, in order to celebrate this, here is the "3B" version of the Sift3 algorithm:

It is made in Javascript, this time, as it was easier to test and has the following extra features:

- a maxDistance value that tells the algorithm to stop if the strings are already too different.
- two pointers c1 and c2, rather than a single pointer c and two offsets
- Instead of dividing to 2 the total length of the strings compared, now I divide it with 1.5. Why? Because this way the value is closer to the Levenshtein distance computed per random strings

Happy usage!

Update: the Sift algorithm is now on Github.

A while ago I wrote an entry here about Sift2, an improvement of Sift, the original and silly string distance algorithm. Now I am publishing Sift3, which is way more accurate and even simpler as an algorithm.

I found out that my algorithm is part of a class of algorithms that solve the Longest Common Substring problem, therefore I calculated the LCS, not the distance, then the distance from the LCS. The result is way more robust, easy to understand and closer to the Levenshtein algorithm both on random strings and user databases. Not to mention that there is no

*goto*in this one.

BTW, if you are looking for an algorithm that detects switched words, this is not it :) This just looks for typos and small regional differences between the strings. I mean, you could normalize the strings, so that words are ordered by some mechanism, then it would work because the words wouldn't be switched :)

I promise to work on a word switching algorithm, but not in the near future.

Without further due, here is the code:

The C# code is a method in an object that has a private member maxOffset. As in Sift2 maxOffset should be around 5 and it represents the range in which to try to find a missing character.

public float Distance(string s1, string s2, int maxOffset)

{

if (String.IsNullOrEmpty(s1))

{

return String.IsNullOrEmpty(s2)

? 0

: s2.Length;

}

if (String.IsNullOrEmpty(s2))

{

return s1.Length;

}

int c = 0;

int offset1 = 0;

int offset2 = 0;

int lcs = 0;

while ((c + offset1 < s1.Length)

&&

(c + offset2 < s2.Length))

{

if (s1[c + offset1] ==

s2[c + offset2])

lcs++;

else

{

offset1 = 0;

offset2 = 0;

for (int i = 0;

i < maxOffset;

i++)

{

if ((c + i < s1.Length)

&&

(s1[c + i] == s2[c]))

{

offset1 = i;

break;

}

if ((c + i < s2.Length)

&&

(s1[c] == s2[c + i]))

{

offset2 = i;

break;

}

}

}

c++;

}

return (s1.Length + s2.Length)/2 - lcs;

}

And here is the T-Sql code. This version is actually an improvement of my original source, gracefully provided by Todd Wolf:

CREATE FUNCTION [DBO].[Sift3distance2]

(

@s1 NVARCHAR(3999),@s2 NVARCHAR(3999),@maxOffset INT

)

RETURNS FLOAT

AS

BEGIN

DECLARE @s1LEN INT,@s2LEN INT

SELECT @s1LEN=Len(Isnull(@s1,'')),@s2LEN=Len(Isnull(@s2,''))

IF @s1LEN=0 RETURN @s2LEN

ELSE

IF @s2LEN=0 RETURN @s1LEN

IF Isnull(@maxOffset,0)=0 SET @maxOffset=5

DECLARE @currPos INT,@matchCnt INT,@wrkPos INT,@s1Offset INT,@s1Char VARCHAR,@s1Pos INT,@s1Dist INT,@s2Offset INT,@s2Char VARCHAR,@s2Pos INT,@s2Dist INT

SELECT @s1Offset=0,@s2Offset=0,@matchCnt=0,@currPos=0

WHILE(@currPos+@s1Offset<@s1LEN AND @currPos+@s2Offset<@s2LEN)

BEGIN

SET @wrkPos=@currPos+1

IF(Substring(@s1,@wrkPos+@s1Offset,1)=Substring(@s2,@wrkPos+@s2Offset,1)) SET @matchCnt=@matchCnt+1

ELSE

BEGIN

SET @s1Offset=0

SET @s2Offset=0

SELECT @s1Char=Substring(@s1,@wrkPos,1),@s2Char=Substring(@s2,@wrkPos,1)

SELECT @s1Pos=Charindex(@s2Char,@s1,@wrkPos)-1,@s2Pos=Charindex(@s1Char,@s2,@wrkPos)-1

SELECT @s1Dist=@s1Pos-@currPos,@s2Dist=@s2Pos-@currPos

IF(@s1Pos>0 AND (@s1Dist<=@s2Dist OR @s2Pos<1) AND @s1Dist<@maxOffset) SET @s1Offset=(@s1Pos-@wrkPos)+1

ELSE

IF(@s2Pos>0 AND (@s2Dist<@s1Dist OR @s1Pos<1) AND @s2Dist<@maxOffset) SET @s2Offset=(@s2Pos-@wrkPos)+1

END

SET @currPos=@currPos+1

END

RETURN(@s1LEN+@s2LEN)/2.0-@matchCnt

END

It doesn't give the same exact results as my own code, yet the result is close enough and the speed is about 20% higher.

And thanks to Diogo Nechtan, the version in PHP:

function sift3Plus($s1, $s2, $maxOffset) {

$s1Length = strlen($s1);

$s2Length = strlen($s2);

if (empty($s1)) {

return (empty($s2) ? 0 : $s2Length);

}

if (empty($s2)) {

return $s1Length;

}

$c1 = $c2 = $lcs = 0;

while (($c1 < $s1Length) && ($c2 < $s2Length)) {

if (($d = $s1{$c1}) == $s2{$c2}) {

$lcs++;

} else {

for ($i = 1; $i < $maxOffset; $i++) {

if (($c1 + $i < $s1Length) && (($d = $s1{$c1 + $i}) == $s2{$c2})) {

$c1 += $i;

break;

}

if (($c2 + $i < $s2Length) && (($d = $s1{$c1}) == $s2{$c2 + $i})) {

$c2 += $i;

break;

}

}

}

$c1++;

$c2++;

}

return (($s1Length + $s2Length) / 2 - $lcs);

}

And thanks to Fernando Jorge Mota, the version in Python:

Also, here is the Javascript version, used in Mailcheck, by Derrick Ko and Wei Lu.

function sift3Distance(s1, s2) {

if (s1 == null || s1.length === 0) {

if (s2 == null || s2.length === 0) {

return 0;

} else {

return s2.length;

}

}

if (s2 == null || s2.length === 0) {

return s1.length;

}

var c = 0;

var offset1 = 0;

var offset2 = 0;

var lcs = 0;

var maxOffset = 5;

while ((c + offset1 < s1.length) && (c + offset2 < s2.length)) {

if (s1.charAt(c + offset1) == s2.charAt(c + offset2)) {

lcs++;

} else {

offset1 = 0;

offset2 = 0;

for (var i = 0; i < maxOffset; i++) {

if ((c + i < s1.length) && (s1.charAt(c + i) == s2.charAt(c))) {

offset1 = i;

break;

}

if ((c + i < s2.length) && (s1.charAt(c) == s2.charAt(c + i))) {

offset2 = i;

break;

}

}

}

c++;

}

return (s1.length + s2.length) / 2 - lcs;

}

You might also be interested in a customised version in AutoKey, by Toralf:

Thanks all for your comments and I look forward to more. Just tell me it worked or not and, most important, why. Good luck!

## Comments

<p>First of all, check out Sift4, it is much more well thought of. <br><br>The explanation for your problem is the letter L, found at the beginning of one word and the end of the second. So the algorithm tries for 13 characters to find a match, it matches the end L, and then assumes that the rest of the string (what it jumped) is totally different and adds it to the distance - remember, I traded accuracy for speed, the algorithm is not recursive.<br><br>The more precise Sift4 should take that into consideration and give you a better result.</p>

Siderite<p>Hi to all!<br>I tried every implementation of the Sif3 algorithm that is present in this blog and in the discussion on the strings "mug" against "muggle" and "T EY1 B AH0 L" against "L EY1 B AH0 L".<br><br>Well, for the first pair, the every implementation of the function returns me 1.5, that is what I expect, even if I variate a lot the maxOffset parameter (up to 1000). For the second pair the situation is different, and I really can't get the rid of it: the fucntion(s) returns my 1.0 for values of maxOffset ranging from 0 to 12 (that is the length of the strings-1), that is what I expect. For values from 13 up to 1000 (and maybe more), it returns me 13...that is strange, especially if compared with what happens for the first pair. Any suggestion?<br><br>Thank you!</p>

Emanuele Bastianelli<p>Good news, people, Sift4 is here. Check out the new improvements at <a rel="nofollow">/blog/super-fast-and-accurate-string-distance.html</a></p>

SideriteGood news, people, Sift4 is here. Check out the new improvements at <a rel="nofollow">/blog/super-fast-and-accurate-string-distance.html</a>

Siderite<p>Looked over it and I realize what the problem is. In the for loop, when finding a match and setting offset1 or offset2, I don't increment lcs. If I do that, the result is the same no matter the first or second letter (<b>x</b> or <b>a</b>). However, the resulting lcs value is now a lot more optimistic than it should be. A cost of changing the offsets should be applied.<br><br>I've reached the same formula as you in the first tries, even attempted to use <i>lcs+=1-(i/maxOffset);</i> and divided by 1.75, but I don't think it is really the correct approach and it is certainly not very scientific. I think lcs should be incremented conditionally, but I haven't found the correct condition yet.</p>

Siderite<p>Well, I understand not wanting to decrease the performance, but this doesn't do any noticeable damage.<br>It would only do something extra once it had found a match and was ready to exit the loop.<br><br>I used tiny strings for clarity, and because <b>sift3Distance</b>, as used in the post for testing, limits <b>maxOffset</b> to 5.<br><br>If you try, with <b>sift3Distance</b> or <b>sift3B</b>,<br>("<b>ABCDE</b>", "<b>xxxAxxxBxxxCxxxDxxxE</b>", 20)<br>and<br>("<b>ABCDE</b>", "<b>xxxxxxxxxxxxxxxxxxxx</b>", 20)<br>both result are the same,<br>both are completely different strings.<br>But by increasing <b>lcs</b> a little bit when matches are found by moving the offset the first case would be a little less than totally different.<br><br>I use <i><b>lcs += 1 / (i + 1)</b></i> because the farther away the less it increases <b>lcs</b>, but it always increases something, as opposed to nothing for the first or only character found in another part of the string.<br><br>Anyway, with a little tweaking, I'm liking Sift more than Levenshtein.<br>Thanks.</p>

Carlos Antonio Gil<p>The Sift implementation in the post is normal Sift3. Sift3B is an experiment, really.<br><br>Anyway, Sift is a little fuzzy. The algorithm's focus is performance. For tiny strings like that, performance is not an issue, obviously.<br><br>I will look into that specific case, if I find the time, though.</p>

Siderite<p>Hi. Good work.<br><br>Sift3B gives the same result for<br>("<b>a</b>", "<b>xx</b>")<br>and for<br>("<b>a</b>", "<b>xa</b>")<br>like the strings of the second one have nothing alike,<br>but then it gives a better match for<br>("<b>a</b>", "<b>ax</b>")<br><br>In my implementation I put<br><b><i>lcs += 1 / (i + 1);</i></b><br>before the <b><i>break</i></b>s</p>

Carlos Antonio Gil<p>I've checked the MySQL implementation and it should work as well as the T-Sql one. I checked a few values for that and it seemed OK.<br><br>If you could, give me some test strings and tell me the results you were expecting and the results you got.<br><br>I've updated the post with a sift vs Levenstein calculator.</p>

Siderite<p>It's me again with the MySQL implementation... The results returned are a kind of far from accurate. Could you please look at it and suggest how best I can make it more efficient?. I posted the code in the post I was asking what offset is.<br><br>Thank you for your assistance!</p>

Anonymous<p>Thank you so much Siderite. That was helpful :)</p>

Anonymous<p>MaxOffset is a value that tells Sift how many position to search for a character. In my usual dealing I used the value of 5. You can use whatever you want. The algorithm will be slower as the value is higher. You can consider MaxOffset as the length of what you would call a word in a sentence.<br><br> For example you have strings: abcdef and 12345abcdef. If MaxOffset is 5, then the algorithm will search for 'a' at the first position then advance and look again 5 times. It will find 'a' in the second string and conclude that the strings are different only in '12345'. If MaxOffset is 4, it will stop before finding 'a' and conclude the strings are very different.</p>

Siderite<p>Hie Siderite.... I am thinking of using the MySQL implementation of your algorithm for my Honours project. However I don't understand what max offset is and thus I can't make use of it....<br><br>The algorithms is as follows:<br>DELIMITER $$<br><br>CREATE DEFINER=`root`@`localhost` FUNCTION `FN_Sift3Similarity`(s1 text, s2 text, maxOffset int) RETURNS float<br>BEGIN<br><br><br><br>DECLARE distance float;<br><br>DECLARE maxLen int;<br><br>DECLARE returnVar float;<br><br><br><br>SET distance= FN_Sift3Distance(s1,s2,maxOffset);<br><br>SET maxLen = CASE WHEN LENGTH(s1) > LENGTH(s2) THEN<br><br>LENGTH(s1) <br><br>ELSE LENGTH(s2) <br><br>END;<br><br><br><br>SET returnVar = CASE WHEN maxLen = 0 THEN 1 <br><br>ELSE(1- (distance / maxLen)) * 100<br><br>END;<br><br>RETURN returnVar;<br><br>END<br><br><br>and the second one i found is :<br>DELIMITER $$<br><br>CREATE DEFINER=`root`@`localhost` FUNCTION `FN_Sift3Distance`(s1 text, s2 text, maxOffset int) RETURNS float<br>BEGIN<br><br>DECLARE s1LEN INT;<br><br>DECLARE s2LEN INT;<br><br>DECLARE currPos INT;<br><br>DECLARE matchCnt INT;<br><br>DECLARE wrkPos INT;<br><br>DECLARE s1Offset INT;<br><br>DECLARE s1Char CHAR;<br><br>DECLARE s1Pos INT;<br><br>DECLARE s1Dist INT;<br><br>DECLARE s2Offset INT;<br><br>DECLARE s2Char CHAR;<br><br>DECLARE s2Pos INT;<br><br>DECLARE s2Dist INT;<br><br>DECLARE returnVar FLOAT;<br><br><br><br>SET s1LEN = LENGTH(s1), s2LEN = LENGTH(s2), maxOffset = IFNULL(maxOffset,2);<br><br>SET s1Offset = 0, s2Offset = 0, matchCnt=0, currPos=0;<br><br><br><br>WHILE ( (currPos+s1Offset < s1LEN) AND (currPos+s2Offset < s2LEN) ) DO<br><br>SET wrkPos = currPos + 1;<br><br>IF ( SUBSTRING(s1,wrkPos+s1Offset,1) = SUBSTRING(s2,wrkPos+s2Offset,1) ) THEN<br><br>SET matchCnt = matchCnt + 1;<br><br>ELSE<br><br>SET s1Offset = 0;<br><br>SET s2Offset = 0;<br><br>SET s1Char = SUBSTRING(s1,wrkPos,1), s2Char=SUBSTRING(s2,wrkPos,1);<br><br>SET s1Pos = LOCATE(s2Char,s1,wrkPos)-1, s2Pos = LOCATE(s1Char,s2,wrkPos)-1;<br><br>SET s1Dist = s1Pos-currPos, s2Dist = s2Pos-currPos;<br><br>IF (s1Pos > 0 AND (s1Dist <= s2Dist OR s2Pos < 1) AND s1Dist < maxOffset) THEN<br><br>SET s1Offset = (s1Pos-wrkPos) + 1;<br><br>ELSEIF (s2Pos > 0 AND (s2Dist < s1Dist OR s1Pos < 1) AND s2Dist < maxOffset) THEN<br><br>SET s2Offset= (s2Pos-wrkPos) + 1;<br><br>END IF;<br><br>END IF;<br><br>SET currPos = currPos + 1;<br><br>END WHILE;<br><br><br><br>SET returnVAR = (s1LEN + s2LEN) / 2.0 - matchCnt;<br><br>RETURN returnVAR;<br><br><br><br>END</p>

Anonymous<p>Thank you! But you added some extra stuff to it. It would be nice if you could explain it to others.</p>

Siderite<p>A java version , thanks for the algorithm<br><br>public class SIFT3 extends StringDistance {<br> private final static byte CASE_BITWISE_MASK = (byte)0x20;<br> private final static float CASE_PUNISHMENT = 0.05f;<br> private static SIFT3 instance;<br> <br> private SIFT3(){ <br> }<br> <br> public final static SIFT3 getInstance(){<br> if(null==instance){<br> instance = new SIFT3();<br> } <br> return instance;<br> }<br> <br> @Override<br> public float computeDistance(String s1, String s2 , int maxOffset) {<br> if(null==s1){<br> return null==s2?0:s2.length(); <br> }<br> else if(null==s2){<br> return null==s1?0:s1.length();<br> }<br> <br> boolean isCaseDiff = false;<br> <br> int l1 = s1.length();<br> int l2 = s2.length();<br> <br> int c = 0;<br> int offset1 = 0;<br> int offset2 = 0;<br> float lcs = 0f;<br> <br> while((c + offset1 < l1) && (c + offset2 < l2)){<br> if ((s1.charAt(c + offset1)|CASE_BITWISE_MASK) == (s2.charAt(c + offset2)|CASE_BITWISE_MASK)){<br> lcs++;<br> if(!isCaseDiff && s1.charAt(c + offset1)!=s2.charAt(c + offset2)){<br> isCaseDiff=true;<br> }<br> }<br> else{<br> offset1 = 0;<br> offset2 = 0;<br> <br> for (int i = 0;i< maxOffset;i++){<br> if ((c + i < l1) && ((s1.charAt(c + i)|CASE_BITWISE_MASK) == (s2.charAt(c)|CASE_BITWISE_MASK))){<br> offset1 = i;<br> if(!isCaseDiff && s1.charAt(c + i)!=s2.charAt(c)){<br> isCaseDiff=true;<br> } <br> break;<br> }<br> <br> if ((c + i < l2) && ((s1.charAt(c)|CASE_BITWISE_MASK) == (s2.charAt(c + i)|CASE_BITWISE_MASK))){<br> offset2 = i;<br> if(!isCaseDiff && s1.charAt(c)!=s2.charAt(c+i)){<br> isCaseDiff=true;<br> } <br> break;<br> }<br> }<br> } <br> c++; <br> }<br><br> return (l1+l2)/2.0f - lcs + (isCaseDiff?CASE_PUNISHMENT:0);<br> }<br><br> @Override<br> public float computeSimilarity(String s1, String s2 , int maxOffset){<br> if(null==s1){<br> return null==s2?0:1f/s2.length(); <br> }<br> else if(null==s2){<br> return null==s1?0:1f/s1.length();<br> }<br> <br> return 1.0f-computeDistance(s1,s2,maxOffset) / (float)Math.max(s1.length(), s2.length());<br> }<br>}<br></p>

Anonymous<p>Thanks, Victor!<br>I don't have standard strings because I am lazy :) I tested it with randomly created strings - which I know don't really test anything - and with the data in different projects where I needed the algorithm.<br><br>Incidentally I am now researching string kernels and something called <a rel="nofollow">http://en.wikipedia.org/wiki/Ukkonen%27s_algorithm</a>. I don't know your specific requirements, though.</p>

Siderite<p>Gist with ruby implementation:<br><br><a href="https://gist.github.com/shaggyone/8645347" rel="nofollow noopener" title="https://gist.github.com/shaggyone/8645347">https://gist.github.com/shaggyone/8645347</a></p>

Виктор Загорский<p>Hello, thanks much for the algorithm. Switching from levenshtein distance to it caused my application to run 100 times faster.<br><br>I can provide ruby version of the code. <br><br><br>By the way, do you have any standard example strings pairs for testing purpose?</p>

Виктор Загорский<p>You are describing a similarity function, one that is computed from a distance function. For example the similarity formula I use when having the distance is 1-distance/max(l1,l2). You propose (l1 +l2-matchcount)/(l1+l2) which is 1-matchcount/(l1+l2) which is similar (pardon the pun).<br><br>A distance is a length, a similarity is a percentage and there should be some relationship between them.<br><br>I can quote (formula used with current variables) from <a rel="nofollow">http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Relation_to_other_problems</a>: "The edit distance when only insertion and deletion is allowed (no substitution), or when the cost of the substitution is the double of the cost of an insertion or deletion, is: l1+l2-2*LCS" which also inspired me to refactor Sift to use the ending formula as it is now.</p>

Siderite<p>sorry, this email will be better.<br>email : catodique@gmail.com (Carmelo Guarneri ).</p>

Carmelo Guarneri<p>hello there...<br>Looking for a very fast fuzzy compare function I stepped on yours.<br>I found it really great BUT I could not find things like clermond ferrand using the captcha string clermon feran.<br>After some research I found that it was because your formula was not taking into account the length of the two strings in the results...<br>This can be easily corrected just by dividing the result by the sum of the lentgh of the chains...<br>The exact formula would be :<br>(length(string1) +length(string2)-matchcount)/(length(string1) +length(string2))...<br>this will give you a number betwen 0 and 1 that will give 0 if the two string are exactly the same and one if they are 'totaly' different (not a char in common)...<br>This would be something like a probabilty distance creating a cloud arround the string...<br>you could use 1 minus the values if you want a fuzzy logic paramter. It would give you 1 if the two chains are exactly the same...<br>I thinkt this formula will bring a great improvement over you algorythm and that it is giving it more sense... Just try it and see...<br>If you need more explanation, you can join me on this email : catodique@hotmail.com (Carmelo Guarneri ).</p>

Unknown<p>Of course it's OK, buddy! Just tell me how it worked out in the end.</p>

Siderite<p>Hi Siderite - thank you for this algorithm. I hope you don't mind, but I have cited you and your algorithm in my Dissertation, and I have implemented it in MYSQL in a system I am writing (I hope that's OK?)<br><br>Here is the MYSQL implementation for your blog. Once again - thank you.<br><br>/***************************************** FN_Sift3Distance **************************************/<br><br>/*6000 is guessed as the upper limit size of a FONT LIST or MIME INFO - either way, its enough for a fingerprint*/<br><br>DROP FUNCTION IF EXISTS FN_Sift3Distance;<br><br>$$CREATE FUNCTION `FN_Sift3Distance`(s1 varchar(6000), s2 varchar(6000), maxOffset int) <br><br>RETURNS float<br><br><br><br>BEGIN<br><br> DECLARE s1LEN INT;<br><br> DECLARE s2LEN INT;<br><br> DECLARE currPos INT;<br><br> DECLARE matchCnt INT;<br><br> DECLARE wrkPos INT;<br><br> DECLARE s1Offset INT;<br><br> DECLARE s1Char CHAR;<br><br> DECLARE s1Pos INT;<br><br> DECLARE s1Dist INT;<br><br> DECLARE s2Offset INT;<br><br> DECLARE s2Char CHAR;<br><br> DECLARE s2Pos INT;<br><br> DECLARE s2Dist INT;<br><br> DECLARE returnVar FLOAT;<br><br><br><br> SET s1LEN = LENGTH(s1), s2LEN = LENGTH(s2), maxOffset = IFNULL(maxOffset,2);<br><br> SET s1Offset = 0, s2Offset = 0, matchCnt=0, currPos=0;<br><br><br><br> WHILE ( (currPos+s1Offset < s1LEN) AND (currPos+s2Offset < s2LEN) ) DO<br><br> SET wrkPos = currPos + 1;<br><br> IF ( SUBSTRING(s1,wrkPos+s1Offset,1) = SUBSTRING(s2,wrkPos+s2Offset,1) ) THEN<br><br> SET matchCnt = matchCnt + 1;<br><br> ELSE<br><br> SET s1Offset = 0;<br><br> SET s2Offset = 0;<br><br> SET s1Char = SUBSTRING(s1,wrkPos,1), s2Char=SUBSTRING(s2,wrkPos,1);<br><br> SET s1Pos = LOCATE(s2Char,s1,wrkPos)-1, s2Pos = LOCATE(s1Char,s2,wrkPos)-1;<br><br> SET s1Dist = s1Pos-currPos, s2Dist = s2Pos-currPos;<br><br> IF (s1Pos > 0 AND (s1Dist <= s2Dist OR s2Pos < 1) AND s1Dist < maxOffset) THEN<br><br> SET s1Offset = (s1Pos-wrkPos) + 1;<br><br> ELSEIF (s2Pos > 0 AND (s2Dist < s1Dist OR s1Pos < 1) AND s2Dist < maxOffset) THEN<br><br> SET s2Offset= (s2Pos-wrkPos) + 1;<br><br> END IF;<br><br> END IF;<br><br> SET currPos = currPos + 1;<br><br> END WHILE;<br><br><br><br>SET returnVAR = (s1LEN + s2LEN) / 2.0 - matchCnt;<br><br>RETURN returnVAR;<br><br><br><br>END$$<br><br><br><br><br><br><br><br>/***************************************** START FN_Sift3Similarity **************************************/<br><br>DROP FUNCTION IF EXISTS FN_Sift3Similarity;<br><br>$$CREATE FUNCTION `FN_Sift3Similarity`(s1 varchar(6000), s2 varchar(6000), maxOffset int) <br><br>RETURNS float<br><br><br><br>BEGIN<br><br><br><br>DECLARE distance float;<br><br>DECLARE maxLen int;<br><br>DECLARE returnVar float;<br><br><br><br>SET distance= FN_Sift3Distance(s1,s2,maxOffset);<br><br>SET maxLen = CASE WHEN LENGTH(s1) > LENGTH(s2) THEN<br><br> LENGTH(s1) <br><br> ELSE LENGTH(s2) <br><br> END;<br><br><br><br>SET returnVar = CASE WHEN maxLen = 0 THEN<br><br> 1 <br><br> ELSE<br><br> (1- (distance / maxLen)) * 100<br><br> END;<br><br>RETURN returnVar;<br><br>END$$<br><br>/***************************************** END FN_Sift3Similarity **************************************/<br><br><br></p>

darran thomas<p>Thank you for the bug report! I am updating the post.</p>

Siderite<p>Thanks you for this algorithm.<br><br>There is just an error in PHP code.<br>$s2{$s2 + $i} should be $s2{$c2 + $i}</p>

thibjl<p>Thanks, Fernando! I've updated the post with your code.</p>

Siderite<p>Here is a simple version of this script in Python: <a href="https://gist.github.com/3067867" rel="nofollow noopener" title="https://gist.github.com/3067867">https://gist.github.com/306...</a></p>

Fernando Jorge MotaHere is a simple version of this script in Python: https://gist.github.com/3067867

Fernando Jorge Mota<p>Thank you, Diogo! I will update the post. Please let me know how you used the algorithm and if it worked for you.</p>

Siderite<p>I converted it to PHP code:<br><br>function sift3Plus($s1, $s2, $maxOffset) {<br> $s1Length = strlen($s1); $s2Length = strlen($s2);<br> if (empty($s1)) return (empty($s2) ? 0 : $s2Length);<br> if (empty($s2)) return $s1Length;<br> $c1 = $c2 = $lcs = 0;<br><br> while (($c1 < $s1Length) && ($c2 < $s2Length)) {<br> if (($d = $s1{$c1}) == $s2{$c2}) $lcs++;<br> else {<br> for ($i = 1; $i < $maxOffset; $i++) {<br> if (($c1 + $i < $s1Length) && (($d = $s1{$c1 + $i}) == $s2{$c2})) {<br> $c1 += $i;<br> break;<br> }<br> if (($c2 + $i < $s2Length) && (($d = $s1{$c1}) == $s2{$s2 + $i})) {<br> $c2 += $i;<br> break;<br> }<br> }<br> }<br> $c1++;<br> $c2++;<br> }<br> return (($s1Length + $s2Length) / 2 - $lcs);<br>}<br><br><br>echo sift3Plus('nechtan', 'nectan', 4); // returns 1.5;</p>

Diogo Nechtan<p>:-P Perhaps. It was designed to find typos, not compare long texts and it performed well in (and was tuned by) tests on randomly generated strings. <br><br>You use it to find if strings are basically the same with small changes rather than a perfect distance score.<br><br>What sort of metric did you have in mind? Maybe a discussion on the desired functionality of the algorithm might help improve it.</p>

Siderite<p>Damn it is so fast. It is not very accurate though. Or maybe the interpretation of Accuracy is different :)</p>

Anonymous<p>I have a suggestion, you could cache the length of s1 and s2. I don't know what the performance is for the len function in Python.</p>

Siderite<p>What maxOffset did you use? Do you think there is a performance issue in the algorithm?</p>

Siderite<p>Thanks for your post!<br>I converted it to Python: <a href="http://pastebin.com/iDUN0G1G" rel="nofollow noopener" title="http://pastebin.com/iDUN0G1G">http://pastebin.com/iDUN0G1G</a><br><br>Sadly, it is just slightly faster than my Damerau Levenshtein implementation...</p>

Major Tal<p>The story of the algorithm is pretty funny. It started with removing the identical letters at the same index, then shifting the strings with an offset, then repeating the operation, until a number of operations has been completed. Hence the name of "sift", as the strings where moving back and forth.<br><br>Sift2 and Sift3 are natural evolutions of the algorithm, by optimizing the code, not the concept. Later on, someone wrote about it belonging to the class of LCS algorithms, so I changed the sense of a variable and renamed it to lcs, as someone might want to compute the LCS and there is a clear formula relating distance and lcs.<br><br>Now, I also remember (it was a long time ago after all) that one version of the algorithm used two pointers and no offset.<br><br>In other words, I didn't invent this based on some previous work or on any knowledge of string distance algorithms, it's something I did out of my own head. Also the "logic" of the algo came only later, when I tried to explain to myself why it worked. So it's all a little backwards :)<br><br>You may be right about possible optimizations, but right now I don't have the time to work on it. I am interested, though, as this is one of my soul children. So I will give a complete new analysis, maybe in another blog entry, when I can find a spare moment.<br><br>Again, thank you for your interest and please let me know about any other ideas that you have about Sift. After all, Sift4 is long overdue :)</p>

Siderite<p>It sure would, it sure would. But why would you want to match the Bs when they aren't on the same side of the matched As? If we imagine that A and B have been switched, a string distance algorithm should certainly impose a penalty for this. Also, if we consider the algorithm an LCS finder, the two strings have no LCS "AB", just the LCS "A" (or "B").</p>

Mikkel<p>What about AxxxB and xBAxxx ? wouldn't the incrementing of the A pointer prohibit the matching of B?</p>

Siderite<p>The two-pointer version has no need to do any such resetting to match A with A and then B with B in such a pair of strings. With >>A1234B<< and >>5678AB<<, c1 and c2 are first 0 and 4, respectively; both pointers are then incremented, to 1 and 5, and finally c1 is set to 5 as well. Cf. this "spypointish" output from a test run:<br><br>Sift3Plus comparing >>A1234B<<, >>5678AB<<:<br> c1 is 0, c2 is 0<br> >>A1234B<<[0]=A doesn't match >>5678AB<<[0]=5<br>-- >>A1234B<<[1]=1 doesn't match >>5678AB<<[0]=5<br>-- >>A1234B<<[0]=A doesn't match >>5678AB<<[1]=6<br>-- >>A1234B<<[2]=2 doesn't match >>5678AB<<[0]=5<br>-- >>A1234B<<[0]=A doesn't match >>5678AB<<[2]=7<br>-- >>A1234B<<[3]=3 doesn't match >>5678AB<<[0]=5<br>-- >>A1234B<<[0]=A doesn't match >>5678AB<<[3]=8<br>-- >>A1234B<<[4]=4 doesn't match >>5678AB<<[0]=5<br>-- >>A1234B<<[0]=A matches >>5678AB<<[4]=A<br> c1 is 0, c2 is 4<br> c1 is 1, c2 is 5<br> >>A1234B<<[1]=1 doesn't match >>5678AB<<[5]=B<br>-- >>A1234B<<[2]=2 doesn't match >>5678AB<<[5]=B<br>-- >>A1234B<<[3]=3 doesn't match >>5678AB<<[5]=B<br>-- >>A1234B<<[4]=4 doesn't match >>5678AB<<[5]=B<br>-- >>A1234B<<[5]=B matches >>5678AB<<[5]=B<br> c1 is 5, c2 is 5<br> LCS is AB<br><br>Here it's always a matter of incrementing the position pointers, never one of resetting them to previous values.</p>

Mikkel<p>Because the algorithm always looks at the first match for one of the letter at the current position. If a match is found, it moves the offset accordingly, up to maxOffset. If then it finds no match, it should start with the characters it skipped when it moved the offset. <br><br>For something like AXXXXB and XXXXAB, first it matches A, then if must reset the offset to continue with B.<br><br>But thinking of this I just realized the algorihtm has a small bug. If the offset is large, it might reach the end of the string. In that case it will not reset the offsets.</p>

Siderite<p>Thanks for replying!<br><br>Indeed it's a matter of semantics: Why would you want to reset, at any time, the position pointer to a previous position? I mean, once the algorithm has matched the character indexed 'c1' in the one string with the character indexed 'c2' in the other string, it seems weird, philosophically, to allow the algorithm to try, at some point, to re-match either c1 or c2 with yet another index -- which Sift3's resetting mechanism does. After all, any index of the one string should correspond at most to one index of the other one. This is why I consider a two-pointers version of the algorithm more straightforward.</p>

Mikkel<p>It is a matter of semantics rather than code.<br><br>The c pointer shows a general position from which offsets are computed. Assuming both strings are identical, then the c pointer just moves along, while the offsets are preserved. If they are not, offsets are changed to skip the differences, then c advances as before.<br><br>The algorithm changes a little when no match is found. In that case the pointer is advanced to a position where there is a match between any two letters.<br><br>You are correct that using two pointers would speed things up a little, but also you would find a problem when trying to reset the counter to a previous position.<br><br>It's a matter of personal choice, I think.</p>

Siderite<p>Hi Siderite!<br><br>Your 'Sift3' algortihm is just what I was looking for -- thank you very much for having made it! I immediately made a Java port of it for my project, where I was stuck with the traditionel and FAR too slow O(n^2) dynamic programming algorithm for string distance calculation. Sift3 was WAY faster and well suited to my kind of data, i.e. inaccurately OCR'ed text. However, studying Sift3 a bit deeper, I came to wonder why it makes use of just a single position pointer 'c' for both strings being compared, instead of two pointers "c1" and "c2", one for each string. The single-postition-pointer approach of Sift3 implies a fairly small limit (i.e., maxOffset) on the distance between equivalent indices in the strings. Why this limit? I wrote a two-position-pointers version of Sift3; 96,75% of the time it yields the same results as Sift3 with my data, and it is somewhat faster than Sift3, too. Here's the Java code (don't know how to make it look more readable in this forum; sorry!):<br><br>public static int sift3Plus(CharSequence s1, CharSequence s2, int maxOffset) {<br> final int s1Length = s1.length(), s2Length = s2.length();<br> if(s1 == null || s1Length == 0)<br> return((s2 == null || s2Length == 0) ? 0 : s2Length);<br> if(s2 == null || s2Length == 0)<br> return(s1Length);<br> int c1 = 0;<br> int c2 = 0;<br> int lcs = 0;<br> while((c1 < s1Length) && (c2 < s2Length)) {<br> if((d = s1.charAt(c1)) == s2.charAt(c2))<br> lcs++;<br> else {<br> for(int i = 1; i < maxOffset; i++) {<br> if((c1 + i < s1Length) && ((d = s1.charAt(c1+i)) == s2.charAt(c2))) {<br> c1 += i;<br> break;<br> }<br> if((c2 + i < s2Length) && ((d = s1.charAt(c1)) == s2.charAt(c2+i))) {<br> c2 += i;<br> break;<br> }<br> }<br> }<br> c1++;<br> c2++;<br> }<br> return((s1Length + s2Length)/2 - lcs);<br>}<br><br>Thanks again,<br>Mikkel</p>

Mikkel<p>Sorry, I did not test it much, cannot judge how good it is. (You left a message on my blog asking for feedback, so here it is.)</p>

Simon Hawkin<p>Thank you; an interesting piece of code. I expect to test/use it in near future and report on my experience.<br><br>Simon</p>

Simon<p>You are most welcome. I really appreciate you leaving this comment. I am glad I helped.</p>

Siderite<p>Hey, it's me again. This post is pretty old, but I figured I owed you a heads up. Quite some time ago I ported your SIFT3 over to ColdFusion script and then modified the heck out of it as an experiment. I came up with a version that I used to highlight difference in longer strings of text (1 or 2 paragraphs). With a maxoffset of anywhere from 10 to 50 it would recognize spelling differences as well as minor insertions/deletions. It would highlight the differences by inserting HTML tags into the strings for display in a web page as well as reporting the difference count and percentage. The function is nowhere as small and fast as SIFT3 was, but since I based my work off of SIFT3 I wanted to be fair and let you know. <br><br>If you want to see it, I have a small example of it along with the ColdFusion code posted on my site so I could share it with people. I credited your name and blog in the comments.<br><br><a href="http://www.bradwood.com/string_compare/" rel="nofollow noopener" title="http://www.bradwood.com/string_compare/">http://www.bradwood.com/str...</a><br><br><br>Thanks!<br><br>~Brad</p>

Brad WoodHey, it's me again. This post is pretty old, but I figured I owed you a heads up. Quite some time ago I ported your SIFT3 over to ColdFusion script and then modified the heck out of it as an experiment. I came up with a version that I used to highlight difference in longer strings of text (1 or 2 paragraphs). With a maxoffset of anywhere from 10 to 50 it would recognize spelling differences as well as minor insertions/deletions. It would highlight the differences by inserting HTML tags into the strings for display in a web page as well as reporting the difference count and percentage. The function is nowhere as small and fast as SIFT3 was, but since I based my work off of SIFT3 I wanted to be fair and let you know. <br><br>If you want to see it, I have a small example of it along with the ColdFusion code posted on my site so I could share it with people. I credited your name and blog in the comments.<br><br>http://www.bradwood.com/string_compare/<br><br><br>Thanks!<br><br>~Brad

Brad Wood<p>One minor suggestion...include maxOffset as an input parameter and make the method Static.</p>

David Larson<p>I have a few ideas. Already got a small algorithm that is twice as fast as Sift3, but terribly innacurate, partly based on your optimisation. Made it today.</p>

Siderite<p>I totally understand. It seems these days that I only learn new stuff for which I have a need. No more time to just go have some "fun". :-)<br><br>I've been toying with the idea of adding a MaxDistance param to short circuit the loop when it falls into range to maybe shave a few more milliseconds off.<br><br>Probably won't do much for the casual app but when your comparing 13,000 known terms to 300,000 possible misspellings each iteration means something!<br><br>Will post anything else that comes out of my testing.</p>

Todd Wolff<p>Ah, the good old days when I had the time to pursue my own ideas and research. Thank you for giving me feedback. For me it was a pleasure to create the algorithm and most of all to see it used.<br><br>I will try to test your improvement and also to integrate the other cool ideas from the others, maybe change version to Sift4. If only I had the time...</p>

Siderite<p>I have a store of possibly misspelled words with a count of around 300,000. I have a dictionary of around 1000 terms. Your algo gave me some great results but takes quite a bit of time because of the loooping. I modified the SQL UDF to be more efficient and thought that I would send you my results. (Yes, I know I'm way too anal about spacing ;-) )<br><br>CREATE FUNCTION [dbo].[Sift3Distance](@s1 nvarchar(3999), @s2 nvarchar(3999),@maxOffset int)<br>RETURNS FLOAT <br>AS <br>BEGIN<br><br> DECLARE @s1LEN INT,<br> @s2LEN INT<br><br> SELECT @s1LEN = LEN(ISNULL(@s1, '')),<br> @s2LEN = LEN(ISNULL(@s2, ''))<br><br> IF @s1LEN = 0<br> RETURN @s2LEN<br> ELSE IF @s2LEN = 0<br> RETURN @s1LEN<br><br> IF ISNULL(@maxOffset,0)=0 <br> SET @maxOffset=5<br> <br> DECLARE @currPos INT,<br> @matchCnt INT,<br> @wrkPos INT,<br><br> @s1Offset INT,<br> @s1Char VARCHAR,<br> @s1Pos INT,<br> @s1Dist INT,<br><br> @s2Offset INT, <br> @s2Char VARCHAR,<br> @s2Pos INT,<br> @s2Dist INT<br> <br> SELECT @s1Offset=0,<br> @s2Offset=0,<br> @matchCnt=0,<br> @currPos=0 <br> <br> <br> WHILE (@currPos+@s1Offset<@s1LEN AND @currPos+@s2Offset<@s2LEN)<br> BEGIN<br> SET @wrkPos = @currPos + 1<br> IF (SUBSTRING(@s1,@wrkPos+@s1Offset,1)=SUBSTRING(@s2,@wrkPos+@s2Offset,1))<br> SET @matchCnt=@matchCnt+1<br> ELSE<br> BEGIN<br> SET @s1Offset=0<br> SET @s2Offset=0<br><br> SELECT @s1Char = SUBSTRING(@s1,@wrkPos,1),<br> @s2Char = SUBSTRING(@s2,@wrkPos,1)<br><br> SELECT @s1Pos = CHARINDEX(@s2Char, @s1, @wrkPos) - 1,<br> @s2Pos = CHARINDEX(@s1Char, @s2, @wrkPos) - 1<br><br> SELECT @s1Dist = @s1Pos-@currPos,<br> @s2Dist = @s2Pos-@currPos<br><br> IF(@s1Pos > 0 AND (@s1Dist <= @s2Dist OR @s2Pos<1) AND @s1Dist < @MaxOffset)<br> SET @s1Offset = (@s1Pos-@wrkPos) + 1<br> ELSE IF(@s2Pos > 0 AND (@s2Dist < @s1Dist OR @s1Pos<1) AND @s2Dist < @MaxOffset)<br> SET @s2Offset = (@s2Pos-@wrkPos) + 1 <br> END <br> <br> SET @currPos=@currPos+1<br> END<br> <br> RETURN (@s1LEN+@s2LEN)/2.0-@matchCnt<br>END<br><br><br>I'm still trying to determine if this will give me the throughput that I'm looking for but I sure appreciate the hard work that you did!</p>

Todd Wolff<p>Thanks for the input. Your sugestions certainly seem reasonable. Alas, I lack the time to work on this algorithm at the moment.<br>If you do transform your ideas in code (and maybe some test data as well), please let me know, I will add your contribution to this post.</p>

Siderite<p>Dear,<br><br>I've downloaded and start goofing arround with your algo.<br>first off - great work, some things are best left of simple :-)<br><br>I'd like to propose 2 improvements that would not hit performance and may be of good use:<br><br>1. make maxOffset varing according to strings lengths. i've found that u best use lower maxoffset when u have short words, than in a sentence. - easy to implement, no cpu overload, and results seem better (didn't heavy test it though).<br><br>2. location priority - again simple implementation (i.e *log(pos)), as in some sentences the begining is more important than the end or vice versa<br><br>I'd be happy to discuss it with you if you think my ideas has any value to you.<br><br>best,<br>DaberElay.</p>

DaberElay<p>I am glad I could help. Thank you for your comment!</p>

Siderite<p>I was looking for a method to compare two digital ham radio messages to see if they were the same message. Since there can be some line noise, it has to be a little forgiving.<br><br>I tried Swift3 and at the default maxOffset of 5 it worked like crap in my test cases. Before giving up and moving on, I tried 20. Now it works like a champ.<br><br>I'm very impressed.</p>

John Davies<p>Sift 3 is better and faster.</p>

Siderite<p>witch one is faster? Sift2 or Sift3?<br><br>Tks</p>

Anonymous<p>Maybe it wasn't clear enough that this algorithm is aproximating the Levenshtein distance, it doesn't always get it right.<br><br>I tried to minimize the difference between the Levenshtein distance and the result of this function and this is what I came up with. However, the first priority was speed.<br><br>In order to improve on the accuracy, you should use Levenshtein for short words (let's say if they have less than 6 letters) or on the top similarity results from Sift. In this way you double check the results, but only on those strings similar enough to be worth it.<br><br>As for the int, it's a good catch, but I am returning float so I don't have to cast anything when computing similarity and to compare with Sift2, which returned a float. Dividing by 2.0 increased the difference from Levenshtein, too :)</p>

Siderite<p>One more thing: your function is typed to return float but the return statement is operating on ints, so you'll never send back anything but an int. I don't think edit distance has the concept of anything but ints, anyways, so I think the function ought to reflect that.</p>

Pawprint<p>Okay, I have tested this function and I can say that it's not giving me the results I expect. For example, I compared the name "Dimas" to "Jim" and "Tina" and in both cases I got a distance of 2 when I should have gotten 3. To make sure we're both on the same page here, this is why I think I should have gotten three:<br>Jim-> Change first letter, delete two letters at the end: 3<br>Tina-> Change first letter, change third letter, delete one letter at the end: 3.<br>Do you agree?</p>

Pawprint<p>Sorry about that. Published Sift3 in a bit of a hurry and I only copy pasted the method from an object.<br>I am glad I could help, I am no mathematician and after looking at some of the formulas you are talking about and not understanding anything, I started from scratch with my silly brain.<br>I am still wondering it worked :)</p>

Siderite<p>oh, wait, I looked at the code for Sift2 and I see that it's a private field initialized during construction. This is an excellent example of why I am a proponent of decorating such fields, either with _ (Microsoft's recommendation) or m_ (the older, MFC-style version). If it had been called _maxOffset I probably would have figured it out myself.<br>Anyways, thanks for the code. I'm trying to implement matching/duplicate elimination in-house and this will be my starting point. There's a lot of <i>talk</i> on the 'Net about edit distance and a lot of mathematical formulas that I probably would have understood 20 years ago in school, but very little <i>actual code</i>. Again, thanks.</p>

Pawprint<p>In the C# sample, was maxOffset supposed to be a parameter like in the T-SQL version? Because it's not declared in the function itself.</p>

Pawprint<p>Thank you for your comments. I didn't have time to test this properly so I might have missed something ;)<br>Can you give me some examples of where it didn't quite work?<br><br>Also, this algorithm is basically finding typos. maxOffset should be around 5, and it detects (or it should :-/) mispelled words or small 4 letter word insertions and so on.<br><br>What you need for getting around the "trick" with the word rearranging is an algorithm that searches and compares groups of two or three letters or even entire words. But that's beyond the scope of Sift3.<br><br>Again, thank you for posting comments of your experiences with Sift.</p>

Siderite<p>Sorry to be spamming you comments, but I wanted to say that it appears the behavior I reported in my second post was simply becase I had a maxOffset of 0. With a larger maxOffset I am able to insert several words into the middle or beginning of one string and it still produces a relativley close match.<br><br>Of course, I can still trick it by simply re-arranging the same peices of data. <br>For instance, if I was to compare the strings:<br>"I like spam. Let's go to the store."<br>and <br>"Let's go to the store. I like spam."<br><br>then they produce a pretty low comparison even though they are the same sentences, but rearranged.<br>To compare those would be much more complicated though.</p>

Brad Wood<p>Nevermind my previous post as my problem appears to have nothing to do with SQL server. I re-wrote it in ColdFusion script and experienced the exact same behavior. I am comparing a larger amount of text like a couple of paragraphs. <br>If I were to compare two sentences which were idential with the exception of an extra letter being present at the beginning of on of the sentences, then the two would compare as being very different. Furthermore if I was to omit or add a single letter at the end of a string, they would compare as being very similar. <br>That would obviously not be the case, since it was only a single-character difference in each scenario. Perhaps I shouldn't use this algorithm for larger strings such as sentences.</p>

Brad Wood<p>I like this. Using the SQL function, I have had a couple strange results right off the bat with a single back slash "\" making a really big difference. Is "\" a reserved character in SQL? This is SQL Server 2005.</p>

Brad Wood