Did you mean feature using Nodejs

In this article, we will implement a very popular feature of the search engines did you mean using Nodejs.How often we misspell while searching something on the internet, well there can be many reasons for this raging from the Unknown spelling of  Goods (while shopping), Keyboard problem, Misspelled words or Unaccustomed international celebrity names.Different search engine reacts differently to this problem.

Let’s talk about very popular search engine Google, When you type something wrong in Google search bar it will show you the correct word with the message, Did you mean ‘##Something##’. Same with Amazon whenever you type the wrong spelling of the product then it will correct you by showing possible suggestions.

Here I will demonstrate this features on the small collection of words using Levenshtein distance. But in real life, you may have a situation where you have a Huge dataset to work on.In that case, you can do some workaround and divide data into small chunks and then you can apply this idea.Here we will find out the Levenshtein distance between the word which user is searching for with respect to each word in the collection.



1. What is Levenshtein Distance?

In very simple terms,

Levenshtein Distance is a similarity between two strings

This is distance is as basically a matric for measuring the amount of difference between two words. Now, this distance is calculated on the basis of Insertion, deletion, and substitution required to match the source string to target string. Levenshtein Distance is also known and Edit distance.


Let’s understand this with the help of a small example, Here we have two strings.

First String is shashank, where s1 = shashank.

Second String is shashanq, where s2= shashanq.

So the  Levenshtein Distance between s1 and s2 would 1.

i.e. LD(s1, s2) = 1.


Here we are comparing the shashank(s1) with shashanq(s2). To transform shashank to shashanq, we need only one substitution.

2. Creating a new Nodejs project

1. Let’s start off by creating a new Nodejs project by using ng init command.This command will create a new package.json file.

2. I found a very useful node module to calculate the Levenshtein Distance, run the below command to install the package for Levenshtein Distance.

Below is my package.json file for this application.


3. Collection of Words Example

For this application, I have created a small collection of words in the form of array shown below, Which we will include in our application.


4. Creating a NodeJs Server

Now we’ll start with a new project and see how to get the desired search output. But before that let’s take look at our project directory structure shown below.


did you mean using Nodejs project structure

1. Create a server.js in the root of the project, which will be our entry point for the project.Here we will define our route and we will start the server.

=> The appConfig() method will set the application configuration.

=> In includeRoutes() method we will execute the application route.

=> The appExecute() method will call the appConfig() as well as appExecute() and it will run the nodejs server.


5. did you mean using Nodejs and levenshtein-distance

Now the only remaining part left on the server side is implementing this feature. Below is the code that you need write inside the /getSuggestion post request.

=> Inside the /getSuggestion post request we will calculate and write the rest of the code.


6. Implementing FrontEnd using AngularJs

1. Let’s create index.html inside the /client folder and write down the below markup.


2. Now create a script.js inside the /js folder under the /client folder and write down the below code.

=> In the below code we have Angularjs service to call the nodejs endpoint /getSuggestion and in that HTTP request we will send the user entered text.

And rest is self-explanatory.


7. Conclusion

I won’t say this is the exact and the only method to implement this feature, but this can be helpful when you are working with a small collection of words. I hope this explains the little bit about the Levenshtein distance and how to use it to implement such feature.If you have any suggestion or anything that you want to ask, do let me know in below comment box.