Products Technologies Demo Docs Blog Support Company

Building a Spell Checking Web API in ASP.NET Core

TX Spell .NET provides a fully-featured spell checking interface and integration when used with TX Text Control editors. But it can be used without a UI as a spell checking engine as well. This sample shows how to create a spell checking Web API in ASP.NET Core.

Building a Spell Checking Web API in ASP.NET Core

TX Spell .NET is a very powerful spell checking engine with a tight integration into the document editors from Text Control including ASP.NET, Angular and Windows Forms. It provides fully-featured UI elements including dialog boxes and context menus when connected to a Text Control instance.

Non-Visual Spell Engine

But the product also includes the non-visual component TXSpell that can be used for background processing. This sample shows how to use this class to create a spell checking Web API using ASP.NET Core.

Implementing the Web API Controller

The sample implements the Web API controller SpellController:

[Route("api/[controller]")]
[ApiController]
public class SpellController : ControllerBase
{

}

This controller implements 4 endpoints:

  • Check
  • CreateSuggestions
  • CreateSynonyms
  • DetectLanguageScopes

The following code shows the implementation of the Check method that accepts the Text to be checked and an optional language identifier:

[HttpGet]
[Route("Check")]
public List<IncorrectWordModel> Check(string text, string language = "en_US")
{
    if (text == null)
        return null;

    // create a new spell checking engine
    TXTextControl.Proofing.TXSpell spell = new TXTextControl.Proofing.TXSpell();
    spell.Create();

    TXTextControl.Proofing.OpenOfficeDictionary dict = 
        new OpenOfficeDictionary(@"Dictionaries\en_US.dic");
    spell.Dictionaries.Add(dict);

    spell.Check(text, new System.Globalization.CultureInfo(language));

    List<IncorrectWordModel> lIncorrectWords = new List<IncorrectWordModel>();

    var incorrectWords = spell.IncorrectWords;

    if (incorrectWords == null)
        return null;

    foreach (IncorrectWord word in incorrectWords)
    {
        lIncorrectWords.Add(new IncorrectWordModel()
        {
            Text = word.Text,
            Index = word.Index,
            IsDuplicate = word.IsDuplicate,
            Length = word.Length,
            Start = word.Start
        });
    }

    return lIncorrectWords;
}

In the method Check, a new instance of the spell checker is created and a dictionary is loaded and added to the available dictionaries using the Dictionaries.Add method.

The given Text is checked using the Check method and the results are returned as a list of IncorrectWords.

Calling from JavaScript

On the JavaScript side, the endpoint is called using a jQuery ajax call:

async function check(text, language = "en_US") {
    var serviceURL = "api/spell/check?text=" + text + "&language=" + language;
    return await callEndpoint(serviceURL);
}
async function callEndpoint(serviceURL) {
  return new Promise(resolve => {
      $.ajax({
          type: "GET",
          url: serviceURL,
          contentType: 'application/json',
          success: successFunc,
          error: errorFunc
      });

      function successFunc(data, status) {
          resolve(data);
      }

      function errorFunc(data) {
          throw "Request failed. Please try again later.";
      }
  });
  }

The spell checking functions are encapsulated in a private scope and exposed as properties in the TXSpell JavaScript object. This way, the check method can be called like this in the sample:

async function checkText() {
    // call the check method
    var results = await TXSpell.check(document.getElementById('textbox').textContent);

    // check console for details
    console.log(results);
    var consoleText = "";

    // loop through the results and return the text
    results.forEach(function (element) {
        consoleText += "<p>Misspelled: <strong>" + element.text + "</strong></p>";
    });

    document.getElementById('results').innerHTML = consoleText;
}

On a button click, the function checkText is called:

<input value="Check" type="button" onclick="checkText()" />

Demo Screenshots

The following screenshot shows the sample after the Check button has been clicked:

Spell Checking

Create Suggestions:

Spell Checking

Create Synonyms:

Spell Checking

Detect Languages:

Spell Checking

Download the sample project and test this on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.Proofing.IncorrectWord Class
  • TXTextControl.Proofing.OpenOfficeDictionary Class
  • TXTextControl.Proofing.TXSpell.Check Method
  • TXTextControl.Proofing.TXSpell Class
  • TXTextControl.Proofing.TXSpell.CreateSuggestions Method
  • TXTextControl.Proofing.TXSpell.CreateSynonyms Method

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • TX Spell .NET 7
  • Visual Studio 2019

Related Posts

ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format

Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…


ActiveXASP.NETWindows Forms

Expert Implementation Services for Legacy System Modernization

We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.


ActiveXASP.NETWindows Forms

Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5

TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…