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::LevenshteinXSandText::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::Dameraudefaults to a pure perl implementation, but if you've installedText::Levenshtein::Damerau::XSthen 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::PPis a pure perl implementation of the same interface.String::Similarityβ takes two strings and returns a value between0(entirely different) and1(identical). Apparently based on edit distance.Text::Diceβ calculates Dice's coefficienthttps://en.wikipedia.org/wiki/SΓΈrensenβDice_coefficientfor two strings. This formula was originally developed to measure the similarity of two different populations in ecological research.String::KeyboardDistanceandString::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 AND LICENSE π
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
- opensuse-leap 16.0: perl-Text-Levenshtein 0.15-bp160.1.11 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms