About

A fuzzy search library built specifically for unity. This library provides implementations of a wide array of fuzzy search methods so you can pick the one that’s right for your use case. This library can be used in both runtime applications and editor tools. It has a simple interface that makes it incredibly simple to work with. You can start using it within minutes, with the option to dive deep and create your own implementations.

Terms

  • Distance Metric: Is a function that computes the distance between two strings. You can measure these distances in many different ways. One example is the Levenshtein distance, which is the number of insertions, deletions, and substitutions required to transform one string into another. In this project the IDistanceMetric interface represents a computation of a metric. An object that implements IDistanceMetric represents one computation and is stateful. Make sure to reset the state before reusing.
  • Search Method: Is a strategy for sorting a set of strings in order of distance from a query string. The ISearchMethod interface encapsulates this strategy. All ISearchMethods are offline and require initializing with a set of strings before use. This allows optimizations and search indexing which speeds up search times. The simplest of these classes is LinearSearch which just runs an IDistanceMetric over every string in the set and sorts the results.
  • Search Provider: Is simply an interface with a search method. Mostly these are wrapper classes to make them compatible with EditorTime or Runtime. They also manage multithreading of the search. ISearchProvider will be synchronous and blocking, while anything implementing INonBlockingSearchProvider does its computation in a worker thread.

A Basic Setup

To start you’ll need to decide on an IDistanceMetric, ISearchMethod, and an ISearchProvider. This requires a technical overview, so for now I’ll choose for you! Get ready for this mouthful of code.

ISearchProvider provider = new EditorSearchProvider<LinearSearch<WagnerFischerDamerauLevenshtein>>();

This creates a search provider for the editor, which uses a LinearSearch with WagnerFischerDamerauLevenshtein as the distance metric. Our focus now is to get you up and running.

Next we need to make some settings.

SearchMethodSettings settings = new SearchMethodSettings() 
{
    MaxDistance = int.MaxValue, // Sets the maximum distance to be included in the search results
};

Then we need to provide our search provider with a list of options to relay to the search method. This can be any IEnumerable. If you want to tie data to your strings, you can store everything in a dictionary.

Dictionary<string, Weapon> data = new Dictionary<string, Weapon>();

And finally we need to initialize the search provider.

provider.Initialize(data, settings);

Now we can search whatever we want!

List<SearchResult> results = provider.Search("sword");

The SearchResult struct contains the string and the distance from the query string. To get back the original Weapon object we can query the dictionary.

Weapon weapon = data[results[0].Option];

And that’s it! The flow for INonBlockingSearchProvider is slightly different. Instead of returning a List we get nothing, but you instead register for a callback on the provider.

provider.OnResultsReceived += (results) => {
    // Do something with the results
};

And an example for using monobehaviours at runtime can be found in the Examples folder. The API is similar, but adapted for monobehaviours.

Contact

If you need any help feel free to reach out to predashgames@gmail.com