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

Tapping into the OpenOffice Spellchecker

, 25 Jan 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A simple and inexpensive way to get your hands on a working spellchecker

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)

Share

About the Author

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

Comments and Discussions

 
GeneralProblem try Ajax Spell Checker ASP.NET PinmemberMember 26665682-Apr-08 3:47 
GeneralRe: (Assemblies not available) Pinmemberkazim bhai26-May-09 1:00 

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 | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 25 Jan 2008
Article Copyright 2008 by Gordon Tolley
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid