Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Enchant Spell Checking not working. Configuration in Windows?

Tags:

php

I am attempting to get a PHP spell checking app working, however when I try and use the Enchant extension, I can't get it to check a word for spelling errors.

Web server config

  • PHP version 5.4.7
  • Windows Server 2008
  • IIS 7

In the php.ini file I have enabled the Enchant extension. eg:

extension=php_enchant.dll

Sample code:

    $broker = enchant_broker_init();
    $tag = 'en_US';

    $bprovides = enchant_broker_describe($broker);
    echo "Current broker provides the following backend(s):\n";
    print_r($bprovides);

    $dicts = enchant_broker_list_dicts($broker);
    echo "Current broker provides the following dictionaries:\n";
    print_r($dicts);

    enchant_broker_set_dict_path($broker, ENCHANT_MYSPELL, 'C:\php5.4.7\lib\enchant\MySpell');

    if (enchant_broker_dict_exists($broker, $tag)) {
     $dict = enchant_broker_request_dict($broker, $tag);
     $word = 'soong';
     $isCorrectlySpelled = enchant_dict_check($dict, $word);

     if ($isCorrectlySpelled !== true) {
      $suggestions = enchant_dict_suggest($dict, $word);

      echo nl2br(print_r($suggestions, true));
     } else {
      echo 'The word is correctly spelt!';
     }
    }

    enchant_broker_free($broker);

Returns:

Current broker provides the following backend(s):
Array
(
    [0] => Array
        (
            [name] => ispell
            [desc] => Ispell Provider
            [file] => C:\php5.4.7\libenchant_ispell.dll
        )

    [1] => Array
        (
            [name] => myspell
            [desc] => Myspell Provider
            [file] => C:\php5.4.7\libenchant_myspell.dll
        )

)
Current broker provides the following dictionaries:

However, this doesn't tell me if the word "soong" is spelt correctly or not!

like image 857
Paul Avatar asked May 24 '13 06:05

Paul


2 Answers

It turns out that it is quite easy to get the Enchant extension working in Windows, IIS and PHP 5.4.7!

All you need to do is create some folders, download some dictionary files and it works brilliantly!

Go to https://wiki.mozilla.org/L10n:Dictionaries and download the dictionaries that you want to spell check against.

Then create this directory structure in your PHP folder: [PHP]\share\myspell\dicts

Finally, place the *.aff and the *.dic files (eg. en_US.aff and en_US.dic) into the dicts folder and then it works!

Now the code above returns the dictionary information, plus the spelling suggestions!

Current broker provides the following backend(s):
Array
(
    [0] => Array
        (
            [name] => ispell
            [desc] => Ispell Provider
            [file] => C:\php5.4.7\libenchant_ispell.dll
        )

    [1] => Array
        (
            [name] => myspell
            [desc] => Myspell Provider
            [file] => C:\php5.4.7\libenchant_myspell.dll
        )

)
Current broker provides the following dictionaries:
Array
(
    [0] => Array
        (
            [lang_tag] => en_GB
            [provider_name] => myspell
            [provider_desc] => Myspell Provider
            [provider_file] => C:\php5.4.7\libenchant_myspell.dll
        )

    [1] => Array
        (
            [lang_tag] => en_US
            [provider_name] => myspell
            [provider_desc] => Myspell Provider
            [provider_file] => C:\php5.4.7\libenchant_myspell.dll
        )

)
Array
(
    [0] => suing
    [1] => sung
    [2] => goons
    [3] => song
    [4] => soon
    [5] => soon g
)

Credits:

http://www.php.net/manual/en/enchant.examples.php#109925

http://my.opera.com/iwanluijks/blog/using-enchant-with-php-on-windows-part-1

like image 163
Paul Avatar answered Oct 11 '22 12:10

Paul


In my case it didn't even listed those backends!!!

You need to copy libenchant_ispell.dll and libenchant_myspell.dll to "c:\PHP\lib\enchant".

Then after downloading dictionaries and using this UNDOCUMENTED function:

enchant_broker_set_dict_path($broker, ENCHANT_MYSPELL, 'C:\PHP\enchant\MySpell');

I made it finally work!

like image 22
Martin Zvarík Avatar answered Oct 11 '22 12:10

Martin Zvarík