• Stars
    star
    110
  • Rank 314,907 (Top 7 %)
  • Language
    C
  • License
    GNU Lesser Genera...
  • Created almost 13 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

General Levenshtein algorithm and k-bounded Levenshtein distance in linear time and constant space. Implementation in C as UDFs for MySQL🐬 and MariaDB🦭

MySQL/MariaDB UDF functions implemented in C

Features:

  • General Levenshtein algorithm
  • k-bounded Levenshtein distance algorithm (linear time, constant space).
    • Info: this is when you only care about the distance if it's smaller or equal than your given k (e.g. to test if the spelling difference between two words is of maximum 1). In this case, the algorithm runs faster while using less memory.
  • Levenshtein ratio
    • Info: this is syntactic sugar for levenshtein_ratio(s, t) = 1 - levenshtein(s, t) / max(s.length, t.length))
  • k-bounded Levenshtein ratio

Install

First you need to compile the library and tell MySQL/MariaDB about it:

# Unix; on Linux x64 you may need to add the -fPIC flag
gcc -o levenshtein.so -shared levenshtein.c `mysql_config --include`  

# macOS
gcc -bundle -o levenshtein.so levenshtein.c `mysql_config --include`

# all systems
cp levenshtein.so `mysql_config --plugindir` # You may need sudo

If you run into issues, please take a look at this workaround or this.

Then in a console, run:

CREATE FUNCTION levenshtein RETURNS INT SONAME 'levenshtein.so';
CREATE FUNCTION levenshtein_k RETURNS INT SONAME 'levenshtein.so';
CREATE FUNCTION levenshtein_ratio RETURNS REAL SONAME 'levenshtein.so';
CREATE FUNCTION levenshtein_k_ratio RETURNS REAL SONAME 'levenshtein.so';

That should be all 🐬 🦭 !

Note

Just in case the last SQL statements failed, consider that to create and use UDF functions, you need CREATE ROUTINE, EXECUTE privileges.

For MariaDB you have to grant additional privileges on mysql database. Here is an example of privileges allowing dropping, altering, creating and using functions:

GRANT INSERT, DELETE, DROP ROUTINE, CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON mysql.* TO 'user'@'%';

For more details on setting up UDFs in MySQL see: UDF Compiling and Installing

Run

For example:

select 3 = levenshtein('maneuver', 'manoeuvre');
select 3 = levenshtein_k('maneuver', 'manoeuvre', 5);  -- k=5, allow the distance to be up to 5, otherwise it's 6 or greater
select 2 = levenshtein_k('maneuver', 'manoeuvre', 1);  -- k=1, allow the distance to be up to 1, otherwise it's 2 or greater

select 0.6666666666666667 = levenshtein_ratio('maneuver', 'manoeuvre'); -- that is, 1 - 3/9
select 0.6666666666666667 = levenshtein_k_ratio('maneuver', 'manoeuvre', 5); -- same
select 0 = levenshtein_k_ratio('maneuver', 'manoeuvre', 1); -- 0 because the distance (3) is greater than the maximum k allowed (1)

Test (for Development)

Your contributions are very much welcome!

Please before making a pull request, run the unit tests file.

This should return 1 at the end:

mysql -uroot < unittest.sql  # Change your username & password as needed

More Repositories

1

mallet

My fork of MAchine Learning for LanguagE Toolkit
Java
11
star
2

CL-HMM

HMM Library for Common Lisp
Common Lisp
6
star
3

jeniatagger

Java port of the GENIA Tagger
Java
6
star
4

Scala-XMLPrettyPrinter

Improved XML/HTML Pretty Printer for Scala; 'cause beauty matters ✨
Scala
5
star
5

geniatagger

GENIA Tagger
C++
2
star
6

drae

Simple Firefox search add-on for DRAE (Spanish official Dictionary)
1
star
7

some-scripts

💻 A handful of utility scripts
Shell
1
star
8

drupal-module-CalDAV-Events

CalDAV reader & writer (drupal module)
PHP
1
star
9

stackexchange-stats-flair

🎨 Beautiful Stack Overflow & Exchange profile flair (stats)
TypeScript
1
star
10

juanmirocks

README & config files for my GitHub profile.
1
star
11

BLAHmuc

Website for the BLAHmuc hackthon
CSS
1
star
12

website

My #aboutme page
CSS
1
star
13

jmc.cl.utils

My utilities for Common Lisp
Common Lisp
1
star
14

AtDSI

My coding answers for the book (Chapter 9): Ace the Data Science Interview (AtDSI), 1st Edition
Python
1
star
15

fastchecks

🚥 Fast website monitoring - CLI & Python API
Python
1
star
16

snorkel2pubannotation

Python conversion tool from Snorkel framework format (TSV's) to PubAnnotation format (JSON)
Python
1
star
17

Anki_German_Support

German Support, add-on for Anki
Python
1
star
18

mkpdf

🖨️🎨 CLI, TypeScript API, & build tools plugins to "print" a website (URL/HTML/Markdown) into a PDF, using Google's Chrome & puppeteer
TypeScript
1
star
19

my-templates.g8

My template files for various project types
Scala
1
star
20

STRING-tagger-server

Dockerized REST server out of larsjuhljensen's (STRING) tagger
Python
1
star
21

clTMHMM

TMHMM-based TM Protein Topology Predictor in CL
Common Lisp
1
star
22

CtCI

My answers for the book: Cracking the Code Interview (CtCI), 6th Edition
Python
1
star