Search Extensions: Basic Levenshtein support

less than a minute read

Search Extensions : Basic Levenshtein support

As part of release 1.3 of NinjaNye.SearchExtensions, 2 new features have been introduced.

This post talks about Levenshtein Distance support including, what has been delivered and what is still to come.

Basic Levenstein Distance support

Using Search Extensions, you can now calculate the Levenshtein distance between a string property and any string value.

context.TestModels.LevenshteinDistanceOf(x => x.StringOne)

The comparison can also be made between two properties:

context.TestModels.LevenshteinDistanceOf(x => x.StringOne)
                  .ComparedTo(x => x.StringTwo);

The result

The result of the above is defined as IEnumerable<ILevenshteinDistance<T>>.

In order to return the Levenshtein distance for a particular record, a new interface has been created. This interface allows us to return the result of the comparison as well as the source item itself and is defined as follows:

public interface ILevenshteinDistance<out T>
    int Distance { get; }
    T Item { get; }

This interface means that you can begin to filter out results based on the Levenshtein Distance. For example if we wanted to retrieve records where the Levenshtein Distance from "test" is less than 5 we would write the following:

var result = data.LevenshteinDistanceOf(x => x.StringOne)
                 .Where(x => x.Distance < 5)
                 .Select(x => x.Item);

Future enhancements

Enhancements I'd like to make to the current Levenshtein support include the following:

  • Levenshtein Distance against multiple properties
  • Levenshtein Distance compared to multiple values.
  • A combination of the above

Levenshtein Distance against multiple properties

For example, it would be nice to be able to do something like the following:

context.TestModels.LevenshteinDistanceOf(x => x.StringOne, x => x.StringTwo)

Levenshtein Distance compared to multiple values.

This would extend both the string search and the property search

// Distance from  "test" and "another"
context.TestModels.LevenshteinDistanceOf(x => x.StringOne)
                  .ComparedTo("test", "another");

// Distance from `StringTwo` and `StringThree`
context.TestModels.LevenshteinDistanceOf(x => x.StringOne)
                  .ComparedTo(x => x.StringTwo, x => x.StringThree);

Combining multiple properties with multiple comparisons

context.TestModels.LevenshteinDistanceOf(x => x.StringOne, x => x.StringTwo)
                  .ComparedTo("test", "another");

Useful Links

If you think you would find this package useful, you can download it using the following command.

<p class="nuget-badge"><code>PM> Install-Package NinjaNye.SearchExtensions</code></p>

If you would like to get in touch about any of the above, please do so by adding a comment below, contact me on twitter (@ninjanye) or you can raise an issue on the GitHub project page

Bets says:June 5, 2016

Hi! Have you added the "Future enhancements" yet?

John says:June 23, 2016

Hi Betz,

Unfortunately not but if there is a need for such a feature, I'd be happy to prioritise it