Spelling Checker API In ASP.NET Core

Hi, Coders. Today we are going to create an API from scratch. This API will give you information about two things. First is how to create an API in ASP.NET Core environment. And second is how to create an application for spelling match related features.

Step 1

Create a new project and name it “WordSuggestionAPI”. Keeping the same name as here will help you to understand how it's all happening.

Spelling checker API in ASP.NET Core

Step 2

Now Configure the Project Settings as suggested. You can use your customized setting in your own practice.

Spelling checker API in ASP.NET Core

Step 3

Now add a new folder from ‘Solution Explorer’ (In case it is not visible then go to View->SolutionExplorer) as follows. The name of the folder will be ‘Models’

And then add a new class file named ‘Word.cs’ as we did below.

Spelling checker API in ASP.NET Core

Step 4

Add a property into the model for word input.

Spelling checker API in ASP.NET Core

Step 5

Now download the DLL package from this link https://www.aspnetspell.com/. This will download a zip file containing several files in it. Now unzip the folders and extract ASPNETSPELL.dll and English (International).dic . This dic file is a dictionary file that contains the word we are going to match.

There are some other dictionaries available for different languages on the same website. If you need them then you can kindly download them.

Step 6

Include the DLL to the references.

Spelling checker API in ASP.NET Core

Step 7

Now add two folders ‘ASPNetSpellInclude’ and the other ‘dictionaries’ respectively.

And then add Dictionary from an existing file. Only add English (International).dic and leave others.

The folder structure we have followed here can be updated at your convenience.

Here is the list of dictionaries available. CLICK HERE 

Spelling checker API in ASP.NET Core

Step 8

Here I am sharing some code that you can add to your controller class. 

Add a new item to the application and go to API Controller. Name it as 'WordSuggestionController'. 

using System.Collections.Generic;
using ASPNetSpell;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using WordSuggestionAPI.Models;
using Microsoft.AspNetCore.Hosting;
namespace WordSuggestionAPI.Controllers {
    [Route("api/[controller]")]
    [ApiController]
    public class WordSuggestionController: ControllerBase {
        private IHostingEnvironment _environment;
        public WordSuggestionController(IHostingEnvironment environment) {
                environment = _environment;
            }
            [HttpGet]
        public List < Word > CheckList(string query) {
            string[] arrQuery = query.Split(' ');
            SpellChecker spellChecker = new SpellChecker();
            spellChecker.DictionaryPath = Path.Combine(_environment.WebRootPath, "/ASPNetSpellInclude/dictionaries");
            spellChecker.LoadDictionary("English (International)");
            bool rr;
            List < Word > result = new List < Word > ();
            for (int i = 0; i < arrQuery.Length; i++) {
                rr = spellChecker.SpellCheckWord(arrQuery[i]);
                if (!rr) {
                    result.Add(new Word {
                        InputWord = arrQuery[i]
                    });
                }
            }
            return result;
        }
    }
}

The constructor and the private hostingenvironment variable are used for accessing the application folder structure. If we want to use a file that is present in the application then we must do two things. First call a UseStaticFiles middleware and access the path through IHostingEnvironment interface which gives access to all webroot files. 

Step 9: Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace WordSuggestionAPI {
    public class Startup {
        public Startup(IConfiguration configuration) {
            Configuration = configuration;
        }
        public IConfiguration Configuration {
            get;
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services) {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            }
            app.UseStaticFiles();
            app.UseMvc();
        }
    }
}

In the above code, we have used the middleware. 

Step 10

Here the code has been completed and now you can run the application on localhost or on a proxy server.

To test this we have several ways. I have given the screenshots of the postman app which handles all HTTP requests. You can also use a request by some HTML pages using jquery ajax request. 

Now open fiddler or postman or send requests through HTML and jquery. And check the result in the postman.

Spelling checker API in ASP.NET Core

Here on the result, you get the Correct word that is available in Dictionary. However, with this DLL you can also get features of client-side javascript for wrongly spelled words. You can also customize the output as per your requirement. 

Now you can try and send me feedback. If anything is wrong kindly revert. 

Similar Articles