# Fuzzy searching

Hi, everyone,

Just wondering if someone had come up with a good way to do fuzzy
searches if you use MySQL as your database (we tried switching to
PostgreSQL, but that ended up adding even more problems). Ferret sounds
great, but reading through the discussion it looks like we need to solve
the problem of write conflicts. I just wrote a post in ruby-talk about
using KirbyBase maybe to solve this. Of course, I’m hoping someone has
come up with an even better solution!

Anyway, any advice would be appreciated.

Thanks!

Jen

Here’s a simple fuzzy-search algorithm you can plug in to whatever you
decide to use.
Most of this code was stolen from php: http://us2.php.net/similar_text
O(m*n), where m and n are lengths of the two target strings.
You may want to rewrite it to make it look more Rubyish.

# compute longest common subsequence btn strings s1 and s2

def lcs_length(s1, s2)

m = [s1.length,128].min
n = [s2.length,128].min
x = 1 + [m,n].max

lcs_tbl = []

# initialize 2-dimensional array to 0

(0…x).each { || lcs_tbl << Array.new(x,0) }

(1…m).each { |i|
(1…n).each { |j|
if (s1[i-1] == s2[j-1])
lcs_tbl[i][j] = lcs_tbl[i-1][j-1] + 1
elsif (lcs_tbl[i-1][j] >= lcs_tbl[i][j-1])
lcs_tbl[i][j] = lcs_tbl[i-1][j]
else
lcs_tbl[i][j] = lcs_tbl[i][j-1]
end
}
}

return lcs_tbl[m][n]
end

def get_lcs(s1, s2, verbose=false)

# ok, now replace all spaces and weird characters with ‘normal’

ascii-7
s1 = normalize_string(s1);
s2 = normalize_string(s2);

# check for pathological cases

return 0 if [s1.length, s2.length].max < 1

puts “comparing strings #{s1} and #{s2}” if verbose
lcs = lcs_length(s1,s2) # compute longest common subsequence

# average string length

ms = (s1.length + s2.length) / 2.0

(puts “ms: #{ms} lcs: #{lcs}”;puts) if verbose
return (lcs*100)/ms
end

# code stolen from one of the ruby MLs.

def normalize_string(foo)
foo = foo.downcase.strip
foo.gsub!(/[Ã?ÃÃ?Ã?Ã¢Ã¤Ã Ã£Ã¡Ã¤Ã¥ÄÄ?Ä?Ç?Ç?Ç¡Ç»ÈÈ?È§áºµáº·]/,‘a’)
foo.gsub!(/[Ã?Ã?Ã?Ã?Ã?Ã²Ã³Ã´ÃµÅÅÈ¯Ã¶á»Å?Ç?ÈÈÆ¡Ç«á»ÉµÃ¸á»?á»?á»?á»?È±È«È­á¹á¹á¹?á¹?á»á»?á»¡á»?á»£Ç­á»?Ç¿]/,‘o’)
foo.gsub!(/[á»³Ã½Å·á»¹È³áºÃ¿á»·áº?Æ´á»µ]/,‘y’)
foo.gsub!(/[Å?]/,‘oe’)
foo.gsub!(/[Ã?Ç¼Ç¢Ã¦]/,‘ae’)
foo.gsub!(/[Ã±Ç¹Å?]/,‘n’)
foo.gsub!(/[Ã?Ã§]/,‘c’)
foo.gsub!(/[Ã?]/,‘b’)
foo.gsub!(/[Å?]/,‘oe’)
foo.gsub!(/[Ä³]/,‘ij’)
foo.gsub!(/[\s’"\/?.=+&%]/,’’)
foo.gsub!(/
+/,’_’)
foo
end

# a little test

[
[“a”,“a”],[“ab”,“ab”],[“abc”,“abc”],
[“a”,“b”],[“ab”,“bb”],[“abc”,“abb”],
[“a”,“1”],[“ab”,"_b"],[“abcd”,“a cd”],
[“abcd”,“abcde”],[“abcd”,“abcdefghi”],[“abcde”,“abcdf”],
[“abcd”,“zbcd”],[“abcd”,“efghiabcd”],[“abcde”,“fabcd”],
[“abc”,“acb”],[“abcde”,“abzde”],[“abcde”,“abde”],
[“abc”,“ac”],[“abcde”,“ab1de”],[“abcde”,“ab de”]
].each { |e|
puts get_lcs(e[0],e[1],true)
puts
}

On 11/26/05, Lou V. removed_email_address@domain.invalid wrote:

Here’s a simple fuzzy-search algorithm you can plug in to whatever you decide to use.
Most of this code was stolen from php: http://us2.php.net/similar_text (in comments section).
O(m*n), where m and n are lengths of the two target strings.
You may want to rewrite it to make it look more Rubyish.

I believe that Jen was looking for a solution for doing the fuzzy
search on the MySQL server itself.

Sincerely,

Tom L.
http://AllTom.com/

MySQL does not have a fuzzy search capability, nor does the stored
procedure language have arrays, so fuzzy search embedded w/in
the MySQL database is not possible to do efficiently (although you
can fake arrays either with temp tables or gobs of string ops).
match outside of the db. On the surface this appears inefficient,
because, after all, the data has to be moved to the client each
time a search is processed. But considering that the search itself
is O(m*n), data transfer is not the overriding concern (unless you
have an unbearably slow transfer speed).
In fact, many fuzzy search algorithms have a much worse running-time
order [O(n^3)], so pulling the data off the server so that a more
efficient algorithm can be used may even be faster than doing the
computation centrally on the server.

I may try that algorithm Lou posted. I’m going to choose between that
and using Ferret to do this … David B. said that locking in
Ferret should be able to handle this without a server process except for
really high concurrency applications. Since I’m dealing with less than
10,000 records, I don’t think this will be a big issue.

Thanks!

Jen

Scott W. wrote:

Not sure how easy it would be to fix, or if it even needs fixing. If
http://www.butlerpress.com/sti.zip

What about explicitly using the ‘model :x’ statements?

–J

I ran across some odd behavior with multiple subclasses of
ActiveRecord. Sometimes find() and find_all() would find subclass
records and sometimes not, depending on whether a subclass had been
loaded or not. I can imagine why this happens, and how it might be
difficult to fix, but it was surprising and took me a while to figure
out.

Here’s a concrete example. Say I have three classes that inherit from
ActiveRecord, and I have one instance of each in my ‘animals’ table:
Dog < Mammal < Animal < ActiveRecord::Base

Run these command in order and you get:
Mammal.count() = 1
Dog.count() = 1
Mammal.count() = 2!

Here’s the SQL:
SHOW FIELDS FROM animals
SELECT COUNT() FROM animals WHERE ( (animals.`type` = ‘Mammal’ ) )
SHOW FIELDS FROM animals
SELECT COUNT(
) FROM animals WHERE ( (animals.`type` = ‘Dog’ ) )
SELECT COUNT(*) FROM animals WHERE ( (animals.`type` = ‘Mammal’ OR
animals.`type` = ‘Dog’ ) )

Sure looks like Mammal/Rails doesn’t know about Dog until I use Dog.
There’s a workaround:
Dog
Mammal.count() = 2
Dog.count() = 1
Mammal.count() = 2

Not sure how easy it would be to fix, or if it even needs fixing. If