# Fuzzy Searching a Database

**URL:** <https://rubytalk.org/t/fuzzy-searching-a-database/22994>\
**Category:** ruby-talk\
**Created:** [4 December 2005 18:37 UTC](https://rubytalk.org/t/fuzzy-searching-a-database/22994 "2005-12-04T18:37:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![TomRossi71](https://avatars.discourse-cdn.com/v4/letter/t/d6d6ee/32.png) [@TomRossi71](https://rubytalk.org/u/TomRossi71)\
**Post date:** [4 December 2005 18:37 UTC](https://rubytalk.org/t/fuzzy-searching-a-database/22994/1 "2005-12-04T18:37:33Z")

</div>

How would you recommend implementing something like a fuzzy search with  
Ruby and a SQL database. For example, I want to search a field in a  
table for anything that is similar to "Frank Smithe". I would want an  
array of matches with some sort of score telling me how good of a match  
the item received ("frank smithe" 100%, "frank smith" 90%).

Am I making sense? Is there a third party app I would need?

Thanks,  
Tom

---

<div class="post-metadata">

**Author:** ![Rich\_Morin1](https://avatars.discourse-cdn.com/v4/letter/r/ce7236/32.png) [@Rich\_Morin1](https://rubytalk.org/u/Rich_Morin1)\
**Post date:** [4 December 2005 18:58 UTC](https://rubytalk.org/t/fuzzy-searching-a-database/22994/2 "2005-12-04T18:58:53Z")

</div>

Soundex is a phonetic algorithm for indexing names by their sound when  
&nbsp;&nbsp;pronounced in English. The basic aim is for names with the same  
&nbsp;&nbsp;pronunciation to be encoded to the same string so that matching can occur  
&nbsp;&nbsp;despite minor differences in spelling. Soundex is the most widely known of  
&nbsp;&nbsp;all phonetic algorithms and is often used (incorrectly) as a synonym for  
&nbsp;&nbsp;"phonetic algorithm".

&nbsp;&nbsp;&nbsp;&nbsp;-- [Soundex - Wikipedia](http://en.wikipedia.org/wiki/Soundex)

If you process your database's names into a column of Soundex encodings,  
you should be able to search this for an encoded target word. You could  
then post-process the results to get the "score" for each one. Note that  
you may want to look around for alternative encodings (eg, Metaphone).

-r

> **···**
>
> At 3:37 AM +0900 12/5/05, TomRossi7 wrote:
> 
> > How would you recommend implementing something like a fuzzy search with  
> > Ruby and a SQL database. For example, I want to search a field in a  
> > table for anything that is similar to "Frank Smithe". I would want an  
> > array of matches with some sort of score telling me how good of a match  
> > the item received ("frank smithe" 100%, "frank smith" 90%).
> 
> --  
> email: rdm@cfcl.com; phone: +1 650-873-7841  
> [http://www.cfcl.com](http://www.cfcl.com) - Canta Forda Computer Laboratory  
> [http://www.cfcl.com/Meta](http://www.cfcl.com/Meta) - The FreeBSD Browser, Meta Project, etc.

---

<div class="post-metadata">

**Author:** ![Hampton](https://avatars.discourse-cdn.com/v4/letter/h/8edcca/32.png) [@Hampton](https://rubytalk.org/u/Hampton)\
**Post date:** [4 December 2005 21:17 UTC](https://rubytalk.org/t/fuzzy-searching-a-database/22994/3 "2005-12-04T21:17:32Z")

</div>

Just make Kit screen all of the queries. Get her a blackberry and email  
them to her.

---

<div class="post-metadata">

**Author:** ![Lou\_Vanek](https://avatars.discourse-cdn.com/v4/letter/l/c77e96/32.png) [@Lou\_Vanek](https://rubytalk.org/u/Lou_Vanek)\
**Post date:** [4 December 2005 21:21 UTC](https://rubytalk.org/t/fuzzy-searching-a-database/22994/4 "2005-12-04T21:21:47Z")

</div>

this was discussed on both this ML and the Rails ML about a week ago. The  
threads have the word 'fuzzy' in the subject.

TomRossi7 wrote:

> **···**
>
> > How would you recommend implementing something like a fuzzy search with  
> > Ruby and a SQL database. For example, I want to search a field in a  
> > table for anything that is similar to "Frank Smithe". I would want an  
> > array of matches with some sort of score telling me how good of a match  
> > the item received ("frank smithe" 100%, "frank smith" 90%).
> > 
> > Am I making sense? Is there a third party app I would need?
> > 
> > Thanks,  
> > Tom

---

<div class="post-metadata">

**Author:** ![Paul7](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Paul7](https://rubytalk.org/u/Paul7)\
**Post date:** [4 December 2005 23:27 UTC](https://rubytalk.org/t/fuzzy-searching-a-database/22994/5 "2005-12-04T23:27:33Z")

</div>

see fulltext searching at mysql.com.

from [http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html](http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html)  
.....  
The following example is more complex. The query returns the relevance  
values and it also sorts the rows in order of decreasing relevance. To  
achieve this result, you should specify MATCH() twice: once in the  
SELECT list and once in the WHERE clause. This causes no additional  
overhead, because the MySQL optimizer notices that the two MATCH()  
calls are identical and invokes the full-text search code only once.

> SELECT id, body, MATCH (title,body) AGAINST

&nbsp;&nbsp;&nbsp;&nbsp;-\> ('Security implications of running MySQL as root') AS score  
&nbsp;&nbsp;&nbsp;&nbsp;-\> FROM articles WHERE MATCH (title,body) AGAINST  
&nbsp;&nbsp;&nbsp;&nbsp;-\> ('Security implications of running MySQL as root');

> **···**
>
> +----+-------------------------------------  
> +-----------------+
> 
> > id | body |
> 
> score |  
> +----+-------------------------------------  
> +-----------------+
> 
> > 4 | 1. Never run mysqld as root. 2. ... |
> 
> 1.5219271183014 |
> 
> > 6 | When configured properly, MySQL ... | 1.3114095926285 |
> 
> +----+-------------------------------------  
> +-----------------+  
> 2 rows in set (0.00 sec)  
> .....
> 
> [http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html](http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html)

---

<div class="post-metadata">

**Author:** ![Stephen\_Waits1](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@Stephen\_Waits1](https://rubytalk.org/u/Stephen_Waits1)\
**Post date:** [4 December 2005 21:34 UTC](https://rubytalk.org/t/fuzzy-searching-a-database/22994/6 "2005-12-04T21:34:52Z")

</div>

Also worth mentioning is that many database systems have Soundex support built-in.

--Steve

> **···**
>
> On Dec 4, 2005, at 10:58 AM, Rich Morin wrote:
> 
> > If you process your database's names into a column of Soundex encodings,  
> > you should be able to search this for an encoded target word.

---

<div class="post-metadata">

**Author:** ![Tom\_Reilly](https://avatars.discourse-cdn.com/v4/letter/t/9de0a6/32.png) [@Tom\_Reilly](https://rubytalk.org/u/Tom_Reilly)\
**Post date:** [4 December 2005 21:36 UTC](https://rubytalk.org/t/fuzzy-searching-a-database/22994/7 "2005-12-04T21:36:06Z")

</div>

Lou Vanek wrote:

> this was discussed on both this ML and the Rails ML about a week ago. The  
> threads have the word 'fuzzy' in the subject.
> 
> TomRossi7 wrote:
> 
> > How would you recommend implementing something like a fuzzy search with  
> > Ruby and a SQL database. For example, I want to search a field in a  
> > table for anything that is similar to "Frank Smithe". I would want an  
> > array of matches with some sort of score telling me how good of a match  
> > the item received ("frank smithe" 100%, "frank smith" 90%).
> > 
> > Am I making sense? Is there a third party app I would need?
> > 
> > Thanks,  
> > Tom

I needed to do something similar with my office database. I used the Levenshtein distance. The routine was also  
quite useful in finding name differences between my office data base and the billing company's.

> **···**
>
> #------------------------------------------------------  
> # Determine Levenshtein distance of two strings
> 
> def Ld(s,t)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;#s string #1  
> &nbsp;&nbsp;&nbsp;&nbsp;#t string #2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;n = s.size  
> &nbsp;&nbsp;&nbsp;&nbsp;m = t.size  
> &nbsp;&nbsp;&nbsp;&nbsp;a = Array.new
> 
> &nbsp;&nbsp;&nbsp;&nbsp;if n != 0 && m != 0  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#2 create array  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = Array.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rz = Array.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.upto(m) {|x| r.push(0)}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.upto(n) {|x|a.push(r.dup)}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp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i- 1][j] +1,a[i][j - 1] + 1,a[i - 1][j - 1] + cost].min  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[n][m]  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> end

---

<div class="post-metadata">

**Author:** ![Ben\_Stroud](https://avatars.discourse-cdn.com/v4/letter/b/6f9a4e/32.png) [@Ben\_Stroud](https://rubytalk.org/u/Ben_Stroud)\
**Post date:** [5 December 2005 02:10 UTC](https://rubytalk.org/t/fuzzy-searching-a-database/22994/8 "2005-12-05T02:10:04Z")

</div>

/How would you recommend implementing something like a fuzzy search with  
Ruby and a SQL database. For example, I want to search a field in a  
table for anything that is similar to "Frank Smithe". I would want an  
array of matches with some sort of score telling me how good of a match  
the item received ("frank smithe" 100%, "frank smith" 90%)./

Less direct, but maybe useful...  
The pyLucene project is the apache java lucene library implemented in C and SWIG'd to python (i think).  
You might be able to create ruby bindings for the full text search C library used there.

[http://pylucene.osafoundation.org/](http://pylucene.osafoundation.org/)

[http://lucene.apache.org/java/docs](http://lucene.apache.org/java/docs)

---

<div class="post-metadata">

**Author:** ![Horacio\_Sanson1](https://avatars.discourse-cdn.com/v4/letter/h/e480ec/32.png) [@Horacio\_Sanson1](https://rubytalk.org/u/Horacio_Sanson1)\
**Post date:** [5 December 2005 04:48 UTC](https://rubytalk.org/t/fuzzy-searching-a-database/22994/9 "2005-12-05T04:48:55Z")

</div>

check [http://ferret.davebalmain.com/api/files/TUTORIAL.html](http://ferret.davebalmain.com/api/files/TUTORIAL.html)

Ferret is a Lucene port for Ruby.

Monday 05 December 2005 11:10、Ben Stroud さんは書きました:

> **···**
>
> > /How would you recommend implementing something like a fuzzy search with  
> > Ruby and a SQL database. For example, I want to search a field in a  
> > table for anything that is similar to "Frank Smithe". I would want an  
> > array of matches with some sort of score telling me how good of a match  
> > the item received ("frank smithe" 100%, "frank smith" 90%)./
> > 
> > Less direct, but maybe useful...  
> > The pyLucene project is the apache java lucene library implemented in C  
> > and SWIG'd to python (i think).  
> > You might be able to create ruby bindings for the full text search C  
> > library used there.
> > 
> > [http://pylucene.osafoundation.org/](http://pylucene.osafoundation.org/)
> > 
> > [Apache Lucene - Apache Lucene Core](http://lucene.apache.org/java/docs)
