Spell Checking WinRT Component (C#, VB, HTML5/JavaScript)
This article provides spell checking functionality for Windows Store Apps, which is missing out-of-box for C#-VB/XAML, HTML/JavaScript apps.
This was implemented in response to MSDN forum questions where developers are often looking for spell check solutions in C# or JavaScripts apps. Writing a wrapper over the spell checker factory is easy. However, it could be a little tricky for C# developers. Also, in some way, it completes this Code Sample Request.
http://gallery.technet.microsoft.com/Spell-Checking-WinRT-cb59a4d5
Introduction
*This implements WINRT component wrapper over C++ SpellChecker library which can be used for basic spelling checks and providing suggestions in Window Store App irrespective of App Language. *
It also shows how to ...
1. Use WinRT Component
2. Create WinRT Component Wrapper over Win32 libraries
3. Use C++/CX class as a wrapper to expose Win32 apis
Description
It implements the component wrapper over Win32 SpellChecker library. A wrapper that it can be easily used by caller of Windows Store App irrespective of Application Language and without worrying about C++ and COM intricacies.
Spell checking client sample (C++)
Also it needs to have spell checker provider installed. See below link for more details
Spell checking provider sample
Internals
The functionality to check spellings is exposed by **ISpellCheckerFactory**and **ISpellChecker**interface.
ISpellCheckerFactory interface
ISpellCheckerFactory : public IUnknown
{
public:
virtual /* [propget] */ HRESULT STDMETHODCALLTYPE get_SupportedLanguages(
/* [retval][out] */ __RPC__deref_out_opt IEnumString **value) = 0;
virtual HRESULT STDMETHODCALLTYPE IsSupported(
/* [in] */ __RPC__in LPCWSTR languageTag,
/* [retval][out] */ __RPC__out BOOL *value) = 0;
virtual HRESULT STDMETHODCALLTYPE CreateSpellChecker(
/* [in] */ __RPC__in LPCWSTR languageTag,
/* [retval][out] */ __RPC__deref_out_opt ISpellChecker **value) = 0;
};
To start using the functionality, we initialize the COM infrastructure and create a instance of ISpellCheckerFactory as shown below inside Win RT Component.
ISpellCheckerFactory
HRESULT hr = CoCreateInstance(__uuidof(SpellCheckerFactory), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(spellCheckerFactory));
if (FAILED(hr))
{
//"Unable to create Spell Checker Factory"
}
After this we can create a spell checker using CreateSpellChecker method which will return ISpellChecker interface.
ISpellChecker
ISpellChecker* spellChecker = nullptr;
hr = spellCheckerFactory->CreateSpellChecker(languageTag, &spellChecker);
if (SUCCEEDED(hr))
{
// Spell checker interface created
}
Finally we can call any of the available methods on ISpellChecker methods. For example call to Check will look like this
Check
IEnumSpellingError* enumSpellingError = nullptr;
HRESULT hr = ReadText(buffer, ARRAYSIZE(text), text); // See project for more details of this function
if (SUCCEEDED(hr))
{
hr = spellChecker->Check(text, &enumSpellingError);
}
After work is done, we Release the ISpellCheckerFactory and ISpellChecker interface instances.
Consuming WinRT Spell Checker Component in Window Store App
1. Copy SpellCheckerWrapperComponent project to your solution
2. Add SpellCheckerWrapperComponent as Project Reference to the Project you would like to use the Spell Checker functions.
3. Use SpellChecker class inside namespace SpellCheckerWinRTComponent present in test project SpellChecker or simplly call the functions inside the WinRT component.
To get all available supported languages.
C#
SpellCheckerWrapperComponent.SpellChecker spellChecker = new SpellCheckerWrapperComponent.SpellChecker();
IList<string> lstAllAvailablleLangs = spellChecker.GetSupportedLanguages();
Visual Basic
Dim spellChecker As SpellCheckerWrapperComponent.SpellChecker = New SpellCheckerWrapperComponent.SpellChecker()
Dim lstAvailablleLangs As IList(Of String) = spellChecker.GetSupportedLanguages()
To check spellings and get suggestions
C#
SpellCheckerWrapperComponent.SpellChecker spellChecker = new SpellCheckerWrapperComponent.SpellChecker();
SpellCheckerWrapperComponent.SpellingCheckResults spellingCheckResults = spellChecker.CheckWordSpelling(word, languageTag/*"en-US"*/);
Visual Basic
Dim spellChecker As SpellCheckerWrapperComponent.SpellChecker = new SpellCheckerWrapperComponent.SpellChecker();
Dim language As String = "en-US"
Dim word As String = "testin"
Dim spellingCheckResults As SpellCheckerWrapperComponent.SpellingCheckResults = spellChecker.CheckWordSpelling(word, language)
Dim isSpellingCorrect As String = spellingCheckResults.IsCorrect
Dim lstSuggestions As IList(Of String) = spellingCheckResults.Suggestions
Dim replacementIfAny As String = spellingCheckResults.ReplacementIfAny
The Project
This document describes the project available for download at TechNet Gallery
http://gallery.technet.microsoft.com/Spell-Checking-WinRT-cb59a4d5