news.

String similarity measures for cheese

We want to know how “close” words are to other words. A similarity measure is a function that gives a score to two words (or more generally, character strings). For example, we want the distance between “ppl” and “people” to be low, but the distance between “ppl” and “cheese” to be far. In an automated cheese shop, one would expect people to sometimes misspell these words. If you have a way to get the distance between these abbreviations and words in a dictionary, you could guess that someone meant “Red Leicester” when she wrote “Red Lester”, because the distance between them is very small. (Perceptive people might notice that this is a convoluted way of saying: “spelling correction”)

Levenshtein distance

One distance measure that is often used is the Levenshtein edit distance. It gives a cost of one for each character that differs between the two strings. The Levenshtein distance between “Tilsit” and “Tulsit” is 1. The distance between “Caerphilly” and “Carfilly” is 3, since two letters were dropped (or inserted depending on where you are coming from) and one letter had to change.

How would one compute the distance? One might think at first that you just compare the first letter of the first word with the first letter of the second word, then the second letters and so forth, each time incrementing a counter when the letters differ. Unfortunately that would say that the distance between “Czechoslovakian sheep’s milk cheese” and “Mud” is 35, while in actual fact it is only 34. It doesn’t take character insertions and deletions into account. To do this one has to somehow align the two strings:

c z e c h o s l o v a k i a n s h e e p s m i l k c h e e s e
m u d

Algorithm to compute the edit distance

An efficient way to calculate the correct distance is by using dynamic programming . Start with an empty table (using a somewhat smaller example):

e m m e n t a l
0
m
e
l
t

Then beginning in the top-left corner, fill in the table. The number in each cell represents the lowest distance one could get if the two strings were aligned such that the letter represented by the column and the letter represented by the row were aligned. In the above case, the red cell would contain the distance if we had aligned “em” and “mel” such that “m” and “l” aligned.

The first column (and row) then represents the case where the two words are aligned so that the first letter of the one word lines up with either the first letter of the second word, or the second letter of the second word or the third and so forth.

e m m e n t a l
0 1 2 3 4 5 6 7 8
m 1
e 2
l 3
t 4

Now we just need to fill in the rest. If the words have been aligned up to the previous character, the cells above, to the left, and above left to the current cell should be filled in already.

  • Moving from the left cell to the current cell represents inserting a letter into the top string.
  • Moving from the above cell to the current cell represents insertion of a character into the left word.
  • Moving from top left to the current cell represents changing the character represented by the row into the character represented by the column. (or visa versa)

Each of these operations has a cost of 1, except when the characters represented by the row and column are the same already. Then the cost of moving diagonally is 0. From these three choices we select the one that would give the lowest total cost (we are looking for the minimum edit distance, otherwise it can become arbitrarily large).

e m m e n t a l
0 1 2 3 4 5 6 7 8
m 1 1 1 2 3 4 5 6 7
e 2 1 2 2 2 3 4 5 6
l 3 2 2 3 3 3 4 5 5
t 4 3 3 3 4 4 3 4 5

The filled in table gives the minimum distance in the green lower right hand cell, since this represents the minimum cost when the two strings are aligning the so that the last characters of the two strings are taken into account. One possible way of aligning the words to get that distance is highlighted in yellow.

Damerau–Levenshtein distance

We might also want to assign a cost of 1 if two letters next to each other swop around. The normal Levenshtein distance in such a case would be 2. To add this case is simple, just check for it in addition to the three cases already checked for at each cell.

Going even further

In the same way we can add even more cases. We might want certain transitions to cost less or more. For example, the cost of going from “i” to “u” can be set to 0.8. When doing a diagonal transition when the row is “i” and the column is “u” we just check if “i changes to u” is in a list of non-standard transition-costs. If it is, add that cost to the total, in stead of the usual cost of 1. We can also have a different list for insertions. One would guess that inserting vowels (or equivalently deleting them in the other string) should be cheap. Therefore just add all the vowels to the deletion list with a cost of say 0.5.

Furthermore, some gansta might be very likely to spell “Ilchester” as “ilchesta”, “Venezuelan Beaver Cheese” as “Venezuelan Beava Cheese”, or “Perle de Champagne” as “Perle da Shampane”. We might therefore want to add “er to a” with cost 0.7, and “ch to sh” with cost 0.9. Finally, for the likely case that someone change “Roquefort” to “roc4t”, we add “q to c” with cost 0.9 and “for to 4” with cost 0.8.

R o q u e f o r t
0 1 2 3 4 5 6 7 8 9
r 1 0 0.5 1.5 2 2.5 3.5 4 5 6
o 2 1 0 1 1.5 2 3 3.5 4.5 5.5
c 3 2 1 0.9 1.4 1.9 2.9 3.4 4.4 5.4
4 4 3 2 1.9 1.9 2.4 2.9 3.4 2.7 3.7
t 5 4 3 2.9 2.9 2.9 3.4 3.9 3.7 2.7

 

Because one character in the first string can now map to two or more characters in the second string, we have to compare costs from more than 3 cells. For example, when we stand on the blue cell, we can move from either one of the usual 3 surrounding cells with costs 4.4, 4.4, or 5.4. Or we can recognise the “4 to for” in our list of transformations. The string “for” has a length of 3, so we look 3 characters back to find the total cost of 1.9 + 0.8 = 2.7. This is the lowest cost from these 4 possibilities, so we fill in the cell with that.

Results

We present here the confused cheese matrix:

This is not very useful. But it has a good name. (Note that we did not define transformations to be symmetrical, so the distance between “ppl” and “people” is 1.5, but from “people” to “ppl” is 3) Below we list the top 10 variants from a dictionary for “ppl”, “ilchesta”, “roc4t”, and “cthns”.

ppl ilchesta roc4t cthns
pupil 1 ilchester 0.7 rocket 1.5 athens 1.5
pp. 1 winchester 2.7 rock 2 this 2
papal 1 chester 2.7 root 2 thus 2
people 1.5 inches 3 rocks 2 tons 2
pool 1.5 orchestra 3 roast 2 chains 2
phil 1.5 chest 3 rocky 2 coins 2
pale 1.5 incest 3 rot 2 johns 2
pull 1.5 richest 3 recent 2.5 cans 2
papa 1.5 chests 3 robert 2.5 catkins 2
pope 1.5 manchester 3.2 roquefort 2.7 caithness 2.5

 

The next and final step is of course to implement a fully functional automated cheese shop, which is left as an exercise to the reader.

 

 

 

 

 

No comments yet.

Leave a comment

Leave a Reply

(required)