Home > Database > Mysql Tutorial > How to Implement a Levenshtein Distance Function in MySQL?

How to Implement a Levenshtein Distance Function in MySQL?

Mary-Kate Olsen
Release: 2024-12-27 15:05:10
Original
674 people have browsed it

How to Implement a Levenshtein Distance Function in MySQL?

Adding Levenshtein Function to MySQL

In MySQL, adding a custom function like the Levenshtein distance requires you to create a stored procedure or function. However, you can leverage the code provided in the referenced WordPress post by following these steps:

  1. Connect to MySQL: Establish a connection to your MySQL server using your preferred method (e.g., command line, MySQL Workbench).
  2. Create the Stored Function: Execute the following statement in your SQL editor:
DELIMITER $$
CREATE FUNCTION levenshtein(str1 VARCHAR(255), str2 VARCHAR(255))
  RETURNS INT
  DETERMINISTIC
BEGIN
  DECLARE str1len, str2len, i, j, cost, c, vtemp INT;
  DECLARE vmatrix VARCHAR(255);

  CALL max(length(str1), length(str2), str1len, str2len);

  SET vtemp = power(2, str2len) * power(2, str1len);
  SET vmatrix = LPAD('0', vtemp, '0');

  SET i = 1;
  WHILE i <= str1len DO
    SET t = SUBSTR(str1, i, 1);
    SET j = 1;
    WHILE j <= str2len DO
      SET s = SUBSTR(str2, j, 1);

      IF i = 1 THEN
        CALL min3(j, vmatrix, i * j, j + 1);
      END IF;
      IF j = 1 THEN
        CALL min3(i, vmatrix, i, i * str2len + j);
      END IF;

      SET cost = IF(t = s, 0, 1);
      CALL min3(vmatrix, vmatrix, cost + vmatrix, i * str2len + j);

      CALL min3(i, vmatrix, vmatrix, i * str2len + j + 1);
      CALL min3(j, vmatrix, vmatrix, (i + 1) * str2len + j);

      SET j = j + 1;
    END WHILE;
  SET i = i + 1;
END WHILE;

  RETURN vmatrix;
END
$$
DELIMITER ;
Copy after login
  1. Test the Function: You can now execute the following query to test the Levenshtein distance function:
SELECT levenshtein('abcde', 'abced');
Copy after login

Example:

The query returns 2, confirming that theLevenshtein distance between 'abcde' and 'abced' is 2.

Usage in PHP:

To use the Levenshtein function within PHP when connecting to MySQL, you can execute the following code:

$sql = 'SELECT levenshtein(?, ?) AS distance';
$stmt = $conn->prepare($sql);
$stmt->bind_param('ss', $str1, $str2);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
  $distance = $row['distance'];
}
Copy after login

The above is the detailed content of How to Implement a Levenshtein Distance Function in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template