Click here to Skip to main content
Click here to Skip to main content

Spell Checker Web Service - Using a WPF TextBox

, 1 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip demonstrates the way we can use WPF Textbox to check for spelling errors and using it via web service.

Introduction

Spelling mistakes are common. Fixing them is somewhat a pain and fixing it automatically (or at least getting suggestions is even more) pain. This tip aims to provide a way to do spell checking. I have seen numerous posts online where many (or almost all) ask to create a Word Interop object and provide spell checks. It was essential for the server to be having Word installed on it. I always found it rather cumbersome to do this task. I always thought that there must be a simpler way to do it. One day, I needed a web service to check out the spelling errors. The requirement for a web service was as it was going to be checked on cross platform.

Background

I began to dive into the framework itself to get some hints. After looking up MSDN, I found that WPF TextBox can check spellings. Hmm... Interesting, I thought. But is it possible to incorporate a textbox in a web service? Haven't done that before but that didn't let me down. I tried incorporating the TextBox. I had to add reference to a couple of assemblies before I could do that. I added the necessary assembly references and it was built successfully! I had a strange feeling that it's like silence before the storm! And yes! I was right. Though it built successfully, it had a runtime error which stated that WPF Component cannot be created on a MTA (Multi Threaded Application) based application! Before losing all hopes, I thought I must give it a try once more by searching if there is any way to convert the webservice into a STA (Single Threaded Application). The answer was yes! The online MSDN Magazine has a great article regarding the same! Wow I was really happy to see that article which of course was of great help. In case you want to have a look at it, check it out here.

Now that I had my code being shifted to STA, I kept my fingers crossed and pressed the heavenly buttons Ctrl + F5... Building... Build Succeeded... and a page which prompted me to execute the method I want... Click on the method, wow now I am getting a prompt to enter the text (let's hope everything is fine now...) and Invoke! Wow, this worked! This is really working!

The Solution

The web service currently has two methods. The first one is called GetSuggestionForWord which takes in a string with just a single word to get the suggestion for the word. The second one is called GetSuggestionForSentence which takes in a sentence and provides suggestion per word basis.

Following is the method GetSuggestionForWord

[WebMethod]
public List<string> GetSuggestionForWord(string word)
{ 
  TextBox objTextBox = new TextBox(); //Creating an instance of the WPF Textbox.
  objTextBox.Text = word; //Setting the text property of it to the word which came.
  objTextBox.SpellCheck.IsEnabled = true; //Changing the spellcheck to enable.
  var spellingErrors = objTextBox.GetSpellingError(0); //Get the spelling error if any at index 0.
  if (spellingErrors != null) //If spelling errors is not null we have an error.
  {
    IEnumerable<string> spellSuggestions = spellingErrors.Suggestions; //Getting all the suggestions
    return new List<string>(spellSuggestions); //Returning a list of string which has the suggestions.
  }
  else //else we return null.
  {
    return null;
  }
}

Following is the method GetSuggestionForSentence:

[WebMethod]
public List<WordSuggestion> GetSuggestionForSentence(string sentence)
{
  TextBox objTextBox = new TextBox(); //Creating an instance of WPF Textbox.
  objTextBox.SpellCheck.IsEnabled = true; //Enabling spellcheck.
  var words = sentence.Split(new char[] { ' ', ',', ';', '.' }); //splitting the words from the sentences.
  //Creating a generic list of WordSuggestion class.
  List<WordSuggestion> suggestions = new List<WordSuggestion>();
  foreach (var item in words) //iterating over the words
  { 
    objTextBox.Text = item; //setting the text property of the textbox to the current word.
    var spellErrors = objTextBox.GetSpellingError(0); //getting the spelling error if any.
    if (spellErrors != null) //if we have a spelling mistake
    {
       suggestions.Add(new WordSuggestion
                           { 
                              Suggestions = new List<string>(spellErrors.Suggestions),
                              Word = item
                           }); //adding the current word and suggestion.
    }
  }
  return suggestions.Count > 0 ? suggestions : null; //If the suggestions count is greater than one, return list else return null.
}

Points of Interest

It was really interesting to find that we have to make the service a STA. I wonder if that affects the scalability in some or other manner. I am also concerned about creation of such instances on the web server. How does it scale? Is the biggest question in my mind right now. This is my first article on CodeProject. I would be happy if given feedback regarding the same. Positive / Negative, welcome!

History

  • Version 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pankaj Nikam
Software Developer
India India
Currently a .NET Developer. Passionate about Microsoft .NET Technologies.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
QuestionSpellChecker Pingroupnik kamble11-Feb-14 19:59 
AnswerRe: SpellChecker PinmemberPankaj Nikam11-Feb-14 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141015.1 | Last Updated 1 May 2012
Article Copyright 2012 by Pankaj Nikam
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid