Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a Levenshtein distance function in a sqlite where clause?

I'm trying to implement a "Did you mean?" sort of function for a search.

I'm trying to do a query that uses the levenshtein function, which was written in ruby. I was wondering how I could use this function in a sqlite3 query. I was thinking it might be something like this:

@results = the_db.where('levenshtein(name, ?) <= 3', searchphrase)

But i'm not sure how to get it to work. Could someone help me out?

like image 582
areke Avatar asked Dec 18 '12 07:12

areke


People also ask

How do you use Levenshtein distance?

A General Example. Given two words, hello and hello, the Levenshtein distance is zero because the words are identical. For the two words helo and hello, it is obvious that there is a missing character "l". Thus to transform the word helo to hello all we need to do is insert that character.

What is Levenshtein distance in SQL?

The Levenshtein distance metric measures the difference between two strings. That is the minimum number of single-character edits that are required to change one string into another other. Single-character edits can be insertions, deletions, and substitutions.

What is the difference between edit distance and Levenshtein distance?

Different definitions of an edit distance use different sets of string operations. Levenshtein distance operations are the removal, insertion, or substitution of a character in the string. Being the most common metric, the term Levenshtein distance is often used interchangeably with edit distance.

Is Levenshtein distance NLP?

The Levenshtein distance used as a metric provides a boost to accuracy of an NLP model by verifying each named entity in the entry. The vector search solution does a good job, and finds the most similar entry as defined by the vectorization.


Video Answer


1 Answers

Try the editdist3 function:

The editdist3 algorithm is a function that computes the minimum edit distance (a.k.a. the Levenshtein distance) between two input strings.

  • http://www.sqlite.org/spellfix1.html
like image 129
nickaknudson Avatar answered Oct 08 '22 00:10

nickaknudson