Using Multi-Byte Character Sets in PHP (Unicode, UTF-8, etc)
Symfony 2 Crash Course
Nice n' Easy JQuery Image Rotator
Changing Mailman Python Scripts for Virtual Host Support
ENUMs, User Preferences, and the MySQL SET Datatype
A Simple ISAPI Filter for Authentication on IIS

Using PHP pspell Spell Check Functions with a Custom Dictionary

Wednesday, 2 January 08, 12:00 am
The pspell_* functions are a very useful feature of PHP, allowing you to scan text and highlight words which are potentially misspelt. Pspell implements the open source aspell spell-checking routines in PHP.

Basic Usage

Before using the functions, you need to open up the dictionary you're going to use by calling the pspell_new() function, specifying at least the language to use. You can also specify a second argument, if the language you plan to use has multiple spellings (such as British English vs US English):
$dictionary_link = pspell_new("en", "british");
The return value of this function can then be passed into other pspell functions so that they know which dictionary to check against. The following function will check the word stored in $word:
pspell_check($dictionary_link, $word)
If the function returns true, then the word matches a spelling in the specified dictionary; if false is returned, then there was no match, suggesting the word may be misspelt.

For an unrecognised word, you can get a list of suggestions from the spelling engine using the following function:
pspell_suggest($dictionary_link, $word)
The list is returned as an array of closely matching words.

Adding Custom Spellings

So far, so good. However, a useful feature of a spell checker is an ability to add new words to the dictionary, particularly when the text contains a lot of specialist jargon and acronyms. pspell does have support for custom dictionaries, and using them is fairly straightforward.

There are two ways to enable custom spellings. If you only require a custom word-list, you can simply use the pspell_new_personal() function in place of the pspell_new() call that opens the dictionary. It takes an additional first argument, which specifies the path to the custom dictionary:
$dictionary_link = pspell_new_personal("/var/dictionaries/custom_spellings", "en", "british");
You can then add new spellings to the custom dictionary like so:
pspell_add_to_personal($dictionary_link, $word)
To save the custom dictionary, call the following function:
If the spellings file specified when you called pspell_new_personal() doesn't exist, PHP will attempt to create it (clearly PHP will need the relevant permissions on this file/directory).

Storing Custom Suggestions for Misspellings

There is a more advanced way to enable custom spellings, which requires the creation of a pspell config before opening the dictionary. A config is created using the pspell_config_create() function, passing in the language and spelling dictionary to use:
$pspell_config = pspell_config_create("en","british")
Once you have the config, you specify the location of any custom word-list using pspell_config_personal():
pspell_config_personal($pspell_config, "/var/dictionaries/custom_spellings")
..and the location of any custom replacement file with pspell_config_repl():
pspell_config_repl($pspell_config, "/var/dictionaries/custom_replacements")
Once the config is set up, you open the dictionary with a call to pspell_new_config():
$dictionary_link = pspell_new_config($pspell_config)
The call to pspell_config_repl() function means that we can use our own custom suggestions, and we can add new suggestions like so:
pspell_store_replacement($dictionary_link, $misspelled, $correct )
Adding a new spelling is just as before:
pspell_add_to_personal($dictionary_link, $word)
Note that you need to call pspell_save_wordlist() in order to save any such custom spellings as well as the custom suggestions. As before, PHP will attempt to create either file if it doesn't already exist.


11:06 am, Tuesday, 15 September 09

can you please give an example along with text area to display how it works?
Leave Comment

/xkcd/ Wifi vs Cellular

About This Page