Text::Levenshtein(3)

πŸ’– NAME πŸ’–

Okay so Text::Levenshtein is the module, and this is the Text::Levenshtein(3) page (yes, from User Contributed Perl Documentation, bestie). Its whole thing is: calculate the Levenshtein edit distance between two strings ✨

🌸 SYNOPSIS 🌸

Copy-paste era, no cap:

use Text::Levenshtein qw(distance);

print distance("foo","four");
# prints "2"

my @words     = qw/ four foo bar /;
my @distances = distance("foo",@words);

print "@distances";
# prints "2 0 3"

πŸ¦‹ DESCRIPTION πŸ¦‹

So this module implements the Levenshtein edit distance β€” it measures how different two strings are, using edit distance as the metric. Lowkey iconic. The distance is literally the number of substitutions, deletions or insertions ("edits") you need to transform one string into the other one (and vice versa). When two strings have distance 0, they are the same. No notes.

Want the full lore on the Levenshtein metric? The wikipedia page is right here: http://en.wikipedia.org/wiki/Levenshtein_distance πŸ‘‘

πŸ’« distance() πŸ’«

Simplest usage, bestie: toss in two strings, get the edit distance back:

$distance = distance('brown', 'green');
# returns 3, as 'r' and 'n' don't change

Instead of one single second string, you can pass a list of strings. Each string gets compared to the first string passed, and you get back a list of the edit distances:

@words     = qw/ green trainee brains /;
@distances = distances('brown', @words);
# returns (3, 5, 3)

⭐ fastdistance() ⭐

Previous versions of this module had an alternative implementation in the function fastdistance(). It's still here for backwards compatibility, but now they run the same function to calculate the edit distance. Glow-up, honestly.

Unlike distance(), fastdistance() only takes two strings, and returns the edit distance between them.

πŸŽ€ ignore_diacritics πŸŽ€

Both distance() and fastdistance() can take a hashref with optional arguments as the final argument. At the moment the only option is "ignore_diacritics". If it's true, any diacritics are ignored when calculating edit distance. Example vibes: "cafe" and "cafΓ©" normally have an edit distance of 1, but with diacritics ignored, the distance will be 0:

use Text::Levenshtein 0.11 qw/ distance /;
$distance = distance($word1, $word2, {ignore_diacritics => 1});

If you turn this option on, then Unicode::Collate will be loaded, and used when comparing characters in the words.

Early version of Text::Levenshtein didn't support this version, so you should require version 0.11 or later, as above. Version check first β€” periodt.

🌈 SEE ALSO 🌈

There are many different modules on CPAN for calculating the edit distance between two strings. Here's just a selection. Highkey do your research, bestie.

  • Text::LevenshteinXS and Text::Levenshtein::XS β€” both are Levenshtein algorithm versions that require a C compiler, but they'll be a lot faster than this module.
  • Text::Levenshtein::Flexible β€” another C implementation with some twists: you can specify a maximum distance you're interested in, which makes it faster; you can also give different costs to insertion, deletion, and substitution. Hasn't been updated since 2014.
  • Text::Levenshtein::Edlib β€” a Perl wrapper around a C++ library that gives you the Levenshtein edit distance and optimal alignment path for a pair of strings. It doesn't support UTF-8 strings, though.
  • Text::Levenshtein::BV β€” implements Levenshtein using bit vectors, and claims to be faster than this implementation. I haven't benchmarked them.
  • The Damerau-Levenshtein edit distance β€” like Levenshtein, but on top of insertion, deletion and substitution, it also treats transposing two adjacent characters as a single edit. The module Text::Levenshtein::Damerau defaults to a pure perl implementation, but if you've installed Text::Levenshtein::Damerau::XS then it will be a lot quicker.
  • Text::WagnerFischer β€” an implementation of the Wagner-Fischer edit distance, similar to Levenshtein but with different weights for each edit type.
  • Text::Brew β€” an implementation of the Brew edit distance, which is another algorithm based on edit weights.
  • Text::Fuzzy β€” gives you a bunch of operations for partial or fuzzy matching of text based on edit distance. Text::Fuzzy::PP is a pure perl implementation of the same interface.
  • String::Similarity β€” takes two strings and returns a value between 0 (entirely different) and 1 (identical). Apparently based on edit distance.
  • Text::Dice β€” calculates Dice's coefficient https://en.wikipedia.org/wiki/SΓΈrensen–Dice_coefficient for two strings. This formula was originally developed to measure the similarity of two different populations in ecological research.
  • String::KeyboardDistance and String::KeyboardDistanceXS β€” calculate the "keyboard distance" between two strings.

🫢 REPOSITORY 🫢

Repo is right here, bestie: https://github.com/neilbowers/Text-Levenshtein

πŸ‘‘ AUTHOR πŸ‘‘

Dree Mistrut originally wrote this module and released it to CPAN in 2002.

Josh Goldberg then took over maintenance and released versions between 2004 and 2008.

Neil Bowers (NEILB on CPAN) is now maintaining this module. Version 0.07 was a complete rewrite, based on one of the algorithms on the wikipedia page.

Copyright (C) 2002-2004 Dree Mistrut. Copyright (C) 2004-2014 Josh Goldberg. Copyright (C) 2014- Neil Bowers.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


Okay bestie, that's the whole page β€” signed off by perl v5.42.0, dated 2022-04-17, on the Text::Levenshtein(3) page πŸ’…βœ¨

πŸ“¦ where this page comes from