From: thunk Date: 2010-12-15T18:25:29+09:00 Subject: Re: do your bit for my mental health - how to find the difference between two strings? Robert, I really do understand your message, and thank you. The details of the example did not make sense to me, among other things I'm not using Sinatra yet. I readily confess this. But he did say that he was looking to: find the difference between two strings? not just about, but EXACTLY. I would not be defensive it I had not been called on this in such a rude fashion. Really. Being ignored is a strong enough signal. Logically, it still seems that there is a "work-around" solution in that code somewhere because he could have his own overriding equal test on string. If they Look the same - I'd sure bet that they would pass this test whatever the deviation in the inner workings. ....and BTW this code DOES work EXACTLY as submitted in Ruby 1.9 just as it did for me 3 years ago in whatever version we were using back then. It seemed like it COULD be a useful piece of code to look at for a few folks in any event. You guys have been VERY helpful to me, I am also looking seriously to use Sinatra, this guy did really seem in a bit of distress.... and bla bla bla I did not expect to have somebody call me out and tell me the approach was totally off the mark, and make the completely UNFOUNDED (and untested) statement that the code does not work. It does, at least in my version 1.9 Mr Ryan, it sure seems to me, went well over the top, and also was just plain WRONG about the code if you should care to run it: Way too much sideways motion, I won't let myself be provoked into any more re-re-rebuttals they do no good and are just so much noise. class String def levenshtein( other, ins=2, del=2, sub=1) #ins, del, sub are weighted costs return nil if self.nil? return nil if other.nil? dm = [] #distance matrix #Initialize first row values dm[0] = (0..self.length).collect { | i | i * ins } fill = [0] * (self.length - 1) #initialize first column values for i in 1..other.length dm[i] = [i * del, fill.flatten] end #populate matrix for i in 1..other.length for j in 1..self.length #critical comparison dm[i][j] = [dm[i-1][j-1] + (self[j-1] == other[i-1] ? 0 : sub), dm[i] [j-1] + ins, dm[i-1][j] + del ].min end end #The last value in the matrix is the Levenshtein distance betw the strings dm[other.length][self.length] end end def ls( ar, threshold=3 )#Array must have at least 2 elements word1, word2, nRslt, lRslt = ar.first.to_s, ar[1].to_s, 999, false if ar.size == 2 nRslt = word1.levenshtein( word2 ) lRslt = nRslt <= threshold elsif ar.size > 2 range = 1..ar.size - 1 range.each do | n | word2 = ar[n] nRslt = word1.levenshtein( word2 ) puts "word2 = " + word2.to_s + ", ls value = " + nRslt.to_s if nRslt <= threshold lRslt = true break end end end puts "word1 = " + word1.to_s + ", and word2 = " + word2.to_s + "Result = " + nRslt.to_s + " Passed? " + lRslt.to_s lRslt end puts ls( ["Davis", "Bully"] ) puts ls( ["Bully", "Bully"] ) >ruby1.9 t.rb word1 = Davis, and word2 = Bully Result = 5 Passed? false false word1 = Bully, and word2 = Bully Result = 0 Passed? true true >Exit code: 0 Sincerely, George