jmcejuela/Levenshtein-MySQL-UDF — pull request #1 by popthestack (2013-02-01 20:47:36+00:00)
Title: Add levenshtein ratio function

Add a method to compute the ratio between two words. Ordering a result set by the ratio is often more accurate than by the levenshtein distance.

```SQL
SELECT levenshtein_ratio('Kellogg', 'Kellog');
-- 86
```

MySQL equivalent:
```SQL
SELECT ROUND((1 - levenshtein('Kellogg', 'Kellog') / GREATEST(LENGTH('Kellogg'), LENGTH('Kellog'))) * 100)
-- 86
```

Consider the following example:

```SQL
SELECT `name`, levenshtein(`name`, 'Kellog') AS lev, levenshtein_ratio(`name`, 'Kellog') AS lev_ratio
FROM manufacturers
WHERE levenshtein_ratio(`name`, 'Kellog') > 0
ORDER BY lev_ratio DESC;
```

Result:

Kellogg	1	86

Kelloggs	2	75

Kellogg's	3	67

Kelly's	3	57

Kallas	3	50

Kellogg, Co.	6	50

Kelsey's	5	38

Klik	4	33

Klass	4	33

Kalocsa	5	29

Ka Lei Eggs	8	27

Kowalski	6	25

Kissel's	6	25

Without the ratio, 'Kellogg, Co.' would be near the bottom of the result set, instead of closer to the top.

At the end of the day, the SQL required to get the ratio isn't that big of a deal, but having the UDF makes it easier.