Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tapping into the OpenOffice Spellchecker

, 25 Jan 2008 CPOL
A simple and inexpensive way to get your hands on a working spellchecker
oospellcheck.zip
OOSpellCheck.suo
OOSpellCheck
bin
Release
OOSpellCheck.csproj.user
Properties
Settings.settings
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;

using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.linguistic2;

namespace OOSpellCheck
{
	public partial class MainForm : Form
	{
		private TextBox txtToCheck = new TextBox();
		private Button btnCheck = new Button();
		private XSpellChecker mxSpell = null;
		private Label msg = new Label();

		public MainForm()
		{
			Text = "OOSpellCheck";
			Size = new Size(400, 300);
			MinimumSize = new Size(300, 200);

			Controls.Add(txtToCheck);
			txtToCheck.Multiline = true;
			txtToCheck.HideSelection = false;
			txtToCheck.Width = 400;
			txtToCheck.Height = 220;
			txtToCheck.Anchor = AnchorStyles.Left | AnchorStyles.Top |
								AnchorStyles.Right | AnchorStyles.Bottom;
			#region Sample text
//			txtToCheck.Text = "There is a tide in the affairs of men,\r\n" +
//							  "Which, taken at the flood, leads on to fortune;\r\n" +
//							  "Omitted, all the voyage of their life\r\n" +
//							  "Is bound in shallows and in miseries.\r\n\r\n" +
//							  "American English: center theater color\r\n" +
//							  "British English: centre theatre colour";
			txtToCheck.Text = "Thire is a tide in the affuirs of men,\r\n" +
							  "Which, taken at the flosd, leads on to fortune;\r\n" +
							  "Owitted, all the voyage of their life\r\n" +
							  "Is bofnd in shalgows and in miseries.\r\n\r\n" +
							  "American English: center theater color\r\n" +
							  "British English: centre theatre colour";
			#endregion
			txtToCheck.Select(0, 0);

			Controls.Add(btnCheck);
			btnCheck.Location = new System.Drawing.Point(10, 230);
			btnCheck.Text = "Spelling";
			btnCheck.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
			btnCheck.Click += new EventHandler(btnCheck_Click);

			Controls.Add(msg);
			msg.AutoSize = true;
			msg.Location = new Point(200, 237);
			msg.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

			btnCheck.Enabled = LoadSpellChecker();
		}

		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;
		}

		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");
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 25 Jan 2008
Article Copyright 2008 by Gordon Tolley
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid