Adding a Spell Checker to your Application

From Director Online Wiki
Jump to: navigation, search

Preamble

Whether you are designing an online chat or adding a text editor to your application, you might have added a spell checker to the 'would be nice to have' section of your feature list. After doing some preliminary research into it you might come to the conclusion that adding a spell checker doesn't seem possible in Director because of a few issues:

  • The only paid Xtra currently available, WordWrangler, is no longer being maintained.
  • Integrating a spell checker feature seems like a lot of work on the surface without an Xtra.

There is, however, a simple solution - one that is open source and easy to integrate, thus it's free and actively maintained. This solution utilizes the open source spell checker, HunSpell. But how can it be integrated with Director? Easy. Via Valentin's Shell Xtra we can communicate with the HunSpell console application.

Brief Description

This article will walk the reader through integrating HunSpell into a 'check as you type' spell checking system using a text member, Valentin's Shell Xtra, and the Buddy Menu Xtra for the word list context menu.

The system will check words, as they're typed, against the dictionary and highlight any misspelled words. Right-clicking on a highlighted word will open a context menu containing spelling suggestions and an option to allow the user to add the highlighted word to their own personal word list.

Requirements

The Physical Setup

Note: You should have both the Shell Xtra and Buddy Menu in the Xtras folder of Director.

Now, let's get started:

  1. Create a new folder named Spell Checker Tutorial.
  2. Unzip the contents from the HunSpell package into the new folder.
  3. There will be a new folder named hunspell-1.2.8-win32. Rename this folder to spellcheck. This folder contains all the HunSpell files, including the dictionary files.
  4. Open the newly renamed spellcheck folder and create a new folder named dicts, then within that another folder named en_US.
  5. Move the files, en_US.aff and en_US.dic, into the \dicts\en_US\ folder.
  6. Go back to the dicts folder and create a text file named personal.dic. Open the file and type the number zero 0 in the first line and then save the file.
  7. Launch Director.

The Stage Layout

  1. Select the
    Error creating thumbnail: Unable to save thumbnail to destination
    text member from the classic Tools and create a good sized text member on the stage so we can type a lot of text in it.
  2. Set the text member's framing to Scrolling from the Property Inspector. Also check the Editable option.
  3. Name the text member input.
  4. Create a behavior script and name it spellcheck script, then copy/paste the code below into the script.
-- Spellcheck Script
-- ©2009 Josh Chunick. All rights reserved.
-- Use however you like, but keep these comments
-- intact. This code is provided "as is" without
-- any expressed or implied warranties.
property sp, mem
 
on beginSprite me
  sp = sprite(me.spriteNum)
  mem = sp.member
  shell_setCurrentDir(the moviePath & "spellcheck\")
end
 
on keyDown me
  if _key.key = SPACE then
    me.spellCheckString()
  end if
  pass
end
 
on mouseUp me
  me.spellCheckString()
end
 
on rightMouseDown me
  hlR = mem.char[sp.pointToChar(_mouse.mouseLoc)].hyperlinkRange
  if hlR <> [0, 0] then
    -- it's a link so get the data
    strWord = mem.char[hlR[1]..hlR[2]]
    str = mem.char[hlR[1]..hlR[2]].hyperlink
    dataList = me.createList(str, ",")
    dataList.addAt(1, "Add word")
    dataList.addAt(2, "")
    selectedItem = bmMenu(dataList,0)
    case selectedItem of
      "": nothing
      "Add word":
        fObj = xtra("fileIO").new()
        fObj.openFile(the moviePath & "spellcheck\dicts\personal.dic", 0)
        strTemp = fObj.readFile()
        strWordCount = string(value(strTemp.line[1]) + 1)
        put strWordCount into line 1 of strTemp
        put strWord into line (strTemp.line.count + 1) of strTemp
        fObj.delete()
        fObj.createFile(the moviePath & "spellcheck\dicts\personal.dic")
        fObj.openFile(the moviePath & "spellcheck\dicts\personal.dic", 0)
        len = strTemp.line.count
        repeat with i = 1 to len
          fObj.writeString(strTemp.line[i])
         if i < len then fObj.writeReturn(#windows)
        end repeat
        fObj.closeFile()
        fObj = 0
        me.spellCheckString()
      otherwise
        mem.char[hlR[1]..hlR[2]].hyperlink = ""
        mem.char[hlR[1]..hlR[2]] = selectedItem
    end case
    mem.char[hlR[1]..hlR[2]].hyperlinkState = #normal
 
  end if
end
 
--------------------------------------
-- PRIVATE FUNCTIONS
--------------------------------------
on spellCheckString me
  str = shell_cmd("echo " & mem.text & " | hunspell -d en_US,personal 2>&1", RETURN, 0, 0)
  delete line 1 of str
  delete the last line of str
  delete the last line of str
  lnCnt = str.line.count
  repeat with i = 1 to lnCnt
    ln = str.line[i]
    if ln starts "&" or ln starts "#" then
      fchar = value(ln.word[4]) + 1
      lchar = fchar + ln.word[2].char.count - 1
      mem.char[fchar..lchar].hyperlink = ln.word[5..ln.word.count]
    else
      if mem.word[i].hyperlinkRange <> [0, 0] then
        mem.word[i].hyperlink = ""
      end if
    end if
  end repeat
end
 
on createList me, str, delim
  tmpDelim = the itemDelimiter
  the itemDelimiter = delim
  lst = []
  cnt = str.item.count
  repeat with i = 1 to cnt
    lst.append(str.item[i].word[1..str.item[i].word.count])
  end repeat
  the itemDelimiter = tmpDelim
  return lst
end
  • Attach the behavior script to the input text sprite on the stage.
  • Press the 'Play' button to run the project and start typing. Try spelling a word incorrectly and right-click on the highlighted word to get the context menu.

The Code Explained

Other Resources