5

我正在尝试让 PHP 拼写检查应用程序正常工作,但是当我尝试使用 Enchant 扩展时,我无法让它检查单词的拼写错误。

网络服务器配置

  • PHP 版本 5.4.7
  • 视窗服务器 2008
  • IIS 7

在 php.ini 文件中,我启用了 Enchant 扩展。例如:

extension=php_enchant.dll

示例代码

    $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);

返回

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:

但是,这并不能告诉我“soong”这个词是否拼写正确!

4

3 回答 3

2

事实证明,让 Enchant 扩展在 Windows、IIS 和 PHP 5.4.7 中运行非常容易!

您需要做的就是创建一些文件夹,下载一些字典文件,它就可以很好地工作!

转到https://wiki.mozilla.org/L10n:Dictionaries并下载您要对其进行拼写检查的词典。

然后在你的 PHP 文件夹中创建这个目录结构:[PHP]\share\myspell\dicts

最后,将 *.aff 和 *.dic 文件(例如 en_US.aff 和 en_US.dic)放入 dicts 文件夹,然后就可以使用了!

现在上面的代码返回字典信息,加上拼写建议!

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
)

学分

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

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

于 2013-05-24T06:25:43.753 回答
2

我必须执行其他人在这里和其他地方建议的步骤组合。我在网上找不到一个地方将所有这些步骤都记录在同一个地方,所以我把它们写在这里。我正在使用从 xamp 安装的 Windows 7 和 php 5.5。这是我必须做的:

  1. 取消注释 php.ini 中的 extension=php_enchant.dll 行
  2. 将php安装目录添加到windows PATH。否则 php_enchant.dll 将无法找到 libenchant.dll
  3. 将 libenchant_ispell.dll 和 libenchant_myspell.dll 从 php 安装目录移动到 [php]/lib/enchant/ 。您必须创建这些文件夹。
  4. 将 en_US.aff 和 en_US.dic 添加到 [php]/share/myspell/dicts 。您还必须创建这些文件夹。这些文件可以在 C:\Program Files\Firefox\dictionaries 中以稍微不同的名称找到。但它们必须重命名为 en_US.aff 和 en_US.dic 否则它们将不起作用。

完成这些步骤并删除 Paul 代码中对 enchant_broker_set_dict_path() 的调用后,效果很好。

于 2015-03-17T15:35:30.647 回答
2

就我而言,它甚至没有列出那些后端!!!

您需要将 libenchant_ispell.dll 和 libenchant_myspell.dll 复制到“c:\PHP\lib\enchant”。

然后在下载字典并使用此 UNDOCUMENTED 功能后:

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

我终于成功了!

于 2013-11-24T01:07:44.567 回答