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(); objTextBox.Text = word; objTextBox.SpellCheck.IsEnabled = true; var spellingErrors = objTextBox.GetSpellingError(0); if (spellingErrors != null) {
IEnumerable<string> spellSuggestions = spellingErrors.Suggestions; return new List<string>(spellSuggestions); }
else {
return null;
}
}
Following is the method GetSuggestionForSentence
:
[WebMethod]
public List<WordSuggestion> GetSuggestionForSentence(string sentence)
{
TextBox objTextBox = new TextBox(); objTextBox.SpellCheck.IsEnabled = true; var words = sentence.Split(new char[] { ' ', ',', ';', '.' }); List<WordSuggestion> suggestions = new List<WordSuggestion>();
foreach (var item in words) {
objTextBox.Text = item; var spellErrors = objTextBox.GetSpellingError(0); if (spellErrors != null) {
suggestions.Add(new WordSuggestion
{
Suggestions = new List<string>(spellErrors.Suggestions),
Word = item
}); }
}
return suggestions.Count > 0 ? suggestions : 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