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

Tapping into the OpenOffice Spellchecker

By , 25 Jan 2008
 

Introduction

There are a few articles, including some from Microsoft, explaining how to use Microsoft Office’s spellchecker in other applications through automation and .NET. However, the Microsoft Office suite is expensive, so here is a method using the spellchecker in OpenOffice 2.3.0. Microsoft Office provides a dialog box, but OpenOffice doesn't, so we have to make our own. Not a problem. This example is written with Visual C# 2008 Express.

Using the Code

The finished program won't start unless OpenOffice is installed on the target machine. The LoadSpellChecker() function does what’s necessary to load the modules into memory. mxSpell is a class member of type XSpellChecker.

private bool LoadSpellChecker()
{
    try
    {
        XComponentContext context = uno.util.Bootstrap.bootstrap();
        XMultiComponentFactory factory = context.getServiceManager();
        XLinguServiceManager lsm = 
            (XLinguServiceManager)factory.createInstanceWithContext(
                "com.sun.star.linguistic2.LinguServiceManager",
                context);
        mxSpell = lsm.getSpellChecker();
    }
    catch(unoidl.com.sun.star.uno.Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return mxSpell != null;
} 

In the spellchecking function, the Regex breaks the text in the TextBox into words for checking, and each word is fed in turn into XSpellChecker.spell(). This returns null if there is no alternative spelling suggestion - we take that to mean a correctly spelt word. Otherwise it returns a list of possible alternatives.

private void btnCheck_Click(object sender, EventArgs e)
{
    int nCorrections = 0;
    PropertyValue[] aEmptyProps = new PropertyValue[0];
//  Locale aLocale = new Locale("en", "US", "");
    Locale aLocale = new Locale("en", "GB", "");

    Regex reg = new Regex("\\w+");
    MatchCollection words = reg.Matches(txtToCheck.Text);
    foreach(Match word in words)
    {
        txtToCheck.Select(word.Index, word.Length);
        XSpellAlternatives xAlt = mxSpell.spell(word.Value, aLocale, aEmptyProps);
        if(xAlt == null)
            continue;

        SpellCheckDlg dlg = new SpellCheckDlg();
        dlg.Word = word.Value;
        foreach(string str in xAlt.getAlternatives())
            dlg.ListBox.Items.Add(str);

        if(dlg.ListBox.Items.Count > 0)
        {
            dlg.EnableButton = true;
            dlg.ListBox.SelectedIndex = 0;
        }

        switch(dlg.ShowDialog())
        {
            case DialogResult.Yes:
                txtToCheck.SelectedText = dlg.Word;
                nCorrections++;
                break;

            case DialogResult.No:
                break;

            case DialogResult.Cancel:
                goto Finish;
        }
    }
    Finish: 

    msg.Text = string.Format("{0} error{1} corrected.", nCorrections,
                    nCorrections == 1 ? "" : "s");
} 

These alternatives are loaded into the ListBox in SpellCheckDlg, so that the user can decide what to do: replace the word, ignore the correction or quit the spellchecker - according to what is returned by the dialog. nCorrections keeps a count of corrections made, and this is subsequently displayed in the Label msg.

Points of Interest

It is not necessary to use the OpenOffice SDK in order to compile, but if you want to read the DevelopersGuide.pdf, you'll probably have to download the SDK. You'll need references in Visual Studio to the following DLLs:

  • C:\Program Files\OpenOffice.org 2.3\program\cli_uno.dll
  • C:\Program Files\OpenOffice.org 2.3\program\assembly\cli_basetypes.dll
  • C:\Program Files\OpenOffice.org 2.3\program\assembly\cli_cppuhelper.dll
  • C:\Program Files\OpenOffice.org 2.3\program\assembly\cli_types.dll

Of course, OpenOffice may be installed at some other place on your computer.

It is quite easy and interesting to change the language of the spellcheck, by changing the Locale which is fed into spell().

History

  • 25th January, 2008: Initial post

License

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

About the Author

Gordon Tolley
Instructor/Trainer
Hong Kong Hong Kong
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralOpen Office Spell Checker for .NET - spell checking without Open Office and the Open Office SDKmemberThomas Maierhofer9 Sep '09 - 8:31 
I've written an wrapper for the Open Office spell check library Hunspell: NHunspell .NET spell Checker. It is LGPL and MPL licensed and can be used for free in commercial and non commercial applications. You can use the Open Office dictionaries, but check the licenses! Hyphenation is also included Thesaurus will follow soon.
 

GeneralRe: Open Office Spell Checker for .NET - spell checking without Open Office and the Open Office SDK Pinmemberjimmiea2 Oct '09 - 3:18 
This is great, thank you
 
Is there any way to get noticed when the Thesaurus implementation is ready?
GeneralRe: Open Office Spell Checker for .NET - spell checking without Open Office and the Open Office SDK PinmemberThomas Maierhofer10 Nov '09 - 5:32 
The Thesaurus implementation is ready (NHunspell 0.9.2). Just download the latest binaries. There is also a sample project available. Regards Thomas
 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 25 Jan 2008
Article Copyright 2008 by Gordon Tolley
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid