Spell Checking WinRT Component (C#, VB, HTML5/JavaScript)

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

 

↑ Return to Top


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
 

 

↑ Return to Top


The Project


This document describes the project available for download at TechNet Gallery

http://gallery.technet.microsoft.com/Spell-Checking-WinRT-cb59a4d5

↑ Return to Top


 

Leave a Comment
  • Please add 4 and 5 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Ed Price - MSFT edited Revision 13. Comment: Tags, white space

  • Ed Price - MSFT edited Revision 11. Comment: Grammar

  • Ed Price - MSFT edited Revision 10. Comment: Grammar

  • Sachin  S edited Revision 8. Comment: Remove redundant top links

  • Sachin  S edited Revision 5. Comment: Reason behind this implementation

  • Sachin  S edited Revision 4. Comment: Added image

  • Sachin  S edited Revision 3. Comment: Finishing initial draft

  • Sachin  S edited Revision 1. Comment: Fixing initial draft

Page 1 of 1 (8 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
Page 1 of 2 (16 items) 12