By: Jan Vanhove
Re-posted from: http://janhove.github.io/programming/2023/02/09/julia-levenshtein
In this second blog post about Julia, I’ll share
with you a Julia implementation of the Levenshtein algorithm.
The Levenshtein algorithm
The basic Levenshtein algorithm is used to count the minimum number of
insertions, deletions and substitutions that are needed to convert one
string into another. For instance, to convert English doubt into
French doute, you need at least two operations. You could replace the
b by a t and then replace the t by an e; or you could
delete the b and then insert the e. As this example shows, there
may be more than one way to convert one string into another using the minimum
number of required operations, but this minimum number itself is unique
for each pair of strings.
Implementation in Julia
I won’t cover the logic of the Levenshtein algorithm here.
The following is a straightforward Julia implementation of the pseudocode
found on Wikipedia, assuming a cost of 1 for all operations.
The function takes two inputs (a string a
that is to be converted
to a string b
) and outputs an array with the Levenshtein distances
between all substrings of a
on the one hand and all substrings of b
on the other hand. The entry in the bottom right corner of this array
is the Levenshtein distances between the full strings and this is output
separately as well.
Let’s compute the Levenshtein distance between
the German word Zyklus (‘cycle’) and its
Swedish counterpart cykel.
Note the use of ;
at the end of the line to suppress
the output.
This checks out: you do indeed need four operations
to transform Zyklus into cykel.
A vectorised function
But what if we wanted to apply our new
functions to several pairs of strings?
Let’s first define three Dutch-German word pairs:
We can run our levenshtein()
on these three
word pairs without introducing for-loops by simply
appending a dot to the function name:
However, since the levenshtein()
function outputs
two pieces of information (both the matrix with the
distances between the substrings as well as the final
Levenshtein distance), this vectorised call yields
a tuple of three subtuples, each subtuple containing
both a matrix and the corresponding final Levenshtein distance.
This is why the output above looks so messy.
If we wanted to obtain just the Levenshtein distances,
we could write a for-loop to extract them.
But I think an easier solution is to first write a wrapper
around the levenshtein()
function that outputs only
the final Levenshtein distance and use the vectorised version
of this wrapper instead:
Now use the vectorised version of lev_dist()
:
Nice!
Obtaining the operations
We now know that we need four operations to transform
Zyklus into cykel and five to transform zuster
into Schwester. But which are the operations that you need
for these transformations?
The function lev_alignment()
defined below outputs
one possible set of operations that would do the job.
(Unlike the minimum number of operations required to
transform one string into another, the set of operations needed
isn’t uniquely defined.)
I won’t cover the logic behind the algorithm as this is more
about learning Julia that the Levenshtein algorithm.
On the Julia side, note first how empty character vectors
can be initialised. Moreover, notice that the pushfirst!()
function is decorated with a !
(a ‘bang’). This communicates
to whoever is reading the code that this function changes
some of its input. For instance, pushfirst!(source, a[row])
means that the current character of a
(i.e., a[row]
)
is added to the front of the source
vector. That is,
this command changes the source
vector.
Finally, the source
, target
and operations
vectors
are all column vectors. In order to display them somewhat
nicely, I converted each of them to a single-row matrix
using reshape()
. Then, the three resulting rows are
concatenated vertically using vcat()
to show how the
two strings can be aligned and which operations are needed
to transform one into the other.
Let’s see how we can transform Zyklus into cykel:
So we substitute c for z,
insert an e and delete the u and s.
As I mentioned, this set of operations isn’t
uniquely defined. Indeed, we could have also
substituted c for z, e for l
and l for u and then deleted the s.
This also corresponds to a Levenshtein distance
of four operations.
Normalised Levenshtein distances
Above, we computed raw Levenshtein distances.
The problem with these is that longer string pairs
will tend to have larger raw Levenshtein distances
than shorter string pairs, even if they do seem
more similar. To correct for this, we can computed
normalised Levenshtein distances instead.
There are various ways to compute these;
one option is to divide the raw Levenshtein distance
by the length of the alignment:
(Behind the scenes, we run the Levenshtein algorithm
twice: once in lev_dist()
and again in lev_alignment()
.
This seems wasteful – unless the Julia compiler is able
to clean up the double work? I’m not sure.)
We obtain a normalised Levenshtein distance of
about 0.57 for Zyklus – cykel:
We can use a vectorised version of this function, too:
Of course, normalised Levenshtein distances are symmetric,
so we obtain the same result when running the following command: