Skip to main content
added 520 characters in body
Source Link
D.W.
  • 165.5k
  • 21
  • 230
  • 489

One other trickIf you only need to find a single match: if you find a match at edit distance one, you can immediately stop without searching for any other matches -- that's the best one you'll ever find.

Why? The edit distance is a non-negative integer, so the only smaller edit distance possible is zero. However, you already checked that the word is not in the dictionary (that was your first bullet point), so edit distance is zero. That means that as soon as you find a distance-one match, you know you'll never find anything better.


Another trick: Suppose the best match you have found so far has edit distance $k$, and now you are comparing word w to word d. Then it suffices to use an "early-out" version of the edit distance computation: if the edit distance between w and d is $\ge k$, we don't care what its precise value is, as this is not a better match. This can be used to speed up the computation a little bit: rather than computing the entire matrix, we only compute the elements that are most $k$ away from the diagonal.

If you want all matches of the minimal edit distance, change $\ge k$ to $>k$.


As mentioned in my comment to your prior question, there's lots written on spelling correction and this problem. There are many data structures, algorithms, and tricks documented in the literature and online. I suggest you spend some quality time doing some research and familiarizing yourself with them.

One other trick: if you find a match at edit distance one, you can immediately stop without searching for any other matches -- that's the best one you'll ever find.

Why? The edit distance is a non-negative integer, so the only smaller edit distance possible is zero. However, you already checked that the word is not in the dictionary (that was your first bullet point), so edit distance is zero. That means that as soon as you find a distance-one match, you know you'll never find anything better.

As mentioned in my comment to your prior question, there's lots written on spelling correction and this problem. There are many data structures, algorithms, and tricks documented in the literature and online. I suggest you spend some quality time doing some research and familiarizing yourself with them.

If you only need to find a single match: if you find a match at edit distance one, you can immediately stop without searching for any other matches -- that's the best one you'll ever find.

Why? The edit distance is a non-negative integer, so the only smaller edit distance possible is zero. However, you already checked that the word is not in the dictionary (that was your first bullet point), so edit distance is zero. That means that as soon as you find a distance-one match, you know you'll never find anything better.


Another trick: Suppose the best match you have found so far has edit distance $k$, and now you are comparing word w to word d. Then it suffices to use an "early-out" version of the edit distance computation: if the edit distance between w and d is $\ge k$, we don't care what its precise value is, as this is not a better match. This can be used to speed up the computation a little bit: rather than computing the entire matrix, we only compute the elements that are most $k$ away from the diagonal.

If you want all matches of the minimal edit distance, change $\ge k$ to $>k$.


As mentioned in my comment to your prior question, there's lots written on spelling correction and this problem. There are many data structures, algorithms, and tricks documented in the literature and online. I suggest you spend some quality time doing some research and familiarizing yourself with them.

Source Link
D.W.
  • 165.5k
  • 21
  • 230
  • 489

One other trick: if you find a match at edit distance one, you can immediately stop without searching for any other matches -- that's the best one you'll ever find.

Why? The edit distance is a non-negative integer, so the only smaller edit distance possible is zero. However, you already checked that the word is not in the dictionary (that was your first bullet point), so edit distance is zero. That means that as soon as you find a distance-one match, you know you'll never find anything better.

As mentioned in my comment to your prior question, there's lots written on spelling correction and this problem. There are many data structures, algorithms, and tricks documented in the literature and online. I suggest you spend some quality time doing some research and familiarizing yourself with them.