Spell checking a Sentence

From my previous post on the spell check object, you should now be able to list suggestions for a single word. How can we do this for a whole sentence?

If we have a string like “Hello, my name is Jack” we need to split it into separate words, remove the punctuation and then check each word in turn. We can then hi-light each incorrect word.

The problem is, that is boring and too much work. Surely an object whose sole purpose in life is to check spellings can handle a sentence? And the answer is yes! =)

The spell check object has ‘Auto-parse’ features, which are actually quite powerful. First, open MMF2 and insert a spell check object without changing any properties on the dialog box. Also, add an edit box, a button, and a list box. We wish to have the list box show all the incorrectly spelt words in the edit box when the button is clicked.

First, add an action to load a word-list at the start of frame. Now, we add the extra stuff.

When the button is clicked, first of all we want to clear the list. Next, call the “Autoparse -> Start” action, and put the text of the edit box as the text to parse.

What does this do then? Autoparse will take the text you put in, split it into individual words and then spell-check each word. If it comes across a word which is incorrect, it calls the event “On Incorrect Word” and pauses it so that you can do what you want. This might be showing a dialog with a list of spelling suggestions, or it might just be hi-lighting the text.

It is important to remember that the auto-parse is now paused, so it won’t continue until you call the “Resume Parse” action.

(Also note that you cannot run two auto-parses at the same time. Why? Because this would almost certainly cause bugs with your code so it was thought better to disallow it altogether.)

So for now we will just add the word to a list box. Add the “On Incorrect Word” event and add a line to the list box with the text from the expression “Autoparse -> Get Incorrect Word” (IncorrectWord$). Now also add the action “Resume Parse”. If you don’t do that, it won’t progress past the first spelling mistake!

You should get something that looks like this:

Now, you will notice that if you click the button twice in quick succession, an alert box will come up reminding you that you cannot start an autoparse when another one is already happening. To stop this happening, you can add the condition “Is Autoparse in progress?” and negate it:

Now, let’s try hi-lighting misspelt words instead. Remove the edit box and list box and insert a Rich Text box. We’ll still have it start an auto-parse when the button is clicked, this time with the text of the RTF object as the parameter.

Now on “On Incorrect Word”, it should do the following to the RTF object:

  1. Select the range of text that starts at “Get incorrect word start index” (IncorrectIndex) and carries on “Get incorrect word length” (IncorrectLen).
  2. Set the current range as bold

It should then resume the parse. You could make “On parse complete” make it unselect all the text.

This works but isn’t automatic. It is hard to get this working well in real-time because the RTF object is so slow. However, alternative objects like the Scintilla object offer hope. Anyway, that is something for you to do!

Now there are two other features I’d like to quickly mention.

The first is that if you change an incorrectly spelt word with the “Change last incorrect word” action, the object will keep track of it and you can use “Get corrected string” to return the whole string with the changes made in it. This helps you actually correct a string once you have spell checked it.

The next is that you can make autoparse automatically ignore incorrectly spelt words that don’t match certain conditions. For instance, if you didn’t want anything within quotes to be spell checked, to go the dialog box and on the “Auto-parse” tab make sure that “Ignore Keys” is selected. Then click “Add” and put a single quote in each box. Then try and spellcheck the string “xd xd ‘sd sd’ xd”. Notice that the the sd won’t turn bold – they have been ignored.

Likewise, you can make it only spell check certain things. For instance, if you only wanted things in brackets to be spell checked, then unselected “Spellcheck when neutral”, select “Spellcheck keys” and put a left bracket in the first box and a right bracket in the second box. Now if you try and spell check something, it will only be considered as possibly wrong if it is in brackets.

In fact, auto-parse is even more powerful than that and can be set up to spell check most programming languages. The reason for this is that when Spell check was developed, making HTML and other editors in MMF was very popular. That doesn’t seem to be the case any more, however.

Advertisements

One Response to Spell checking a Sentence

  1. Pingback: Spellcheck Object Guide « Jack's Personal Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: