4

您好我正在为我的自定义文本字段之一使用 Drupal 的 6 个默认 jQuery 自动完成功能。

现在我想添加拼写检查以及复数检查

例如

Word: Potatos

Spellchecker suggestions (including plural check): Potato, Potatoes

有什么方法可以实现此功能,以便当用户键入Potatos时。首先,拼写检查脚本会运行,它会建议正确的替代方案,一旦用户选择了正确的单词,自动完成脚本就会运行吗?

4

3 回答 3

0

我认为解决方案应该是由ajax调用的服务器端。

选项 1 - soundex mysql

简单快捷

如果您使用 mysql 并且您的数据语言是英语 SOUNDEX 可能是您的选择。

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex

例子:

mysql> select SOUNDEX('potato'), SOUNDEX('potatos'), SOUNDEX('potatoes');
+-------------------+--------------------+---------------------+
| SOUNDEX('potato') | SOUNDEX('potatos') | SOUNDEX('potatoes') |
+-------------------+--------------------+---------------------+
| P300              | P320               | P320                |
+-------------------+--------------------+---------------------+

现在,在您的数据库中搜索基于 soundex 算法的相同或具有微小差异的单词。

选项 2 - 狮身人面像搜索

它更难安装、配置和学习,但支持多种语言、词干算法、高级查询(布尔模式等)、改进的性能......

如果您想要更高级的系统,支持词干提取的全文搜索引擎将对您有很大帮助。看看http://sphinxsearch.com/docs/current.html#conf-morphology

使用词干,当你搜索土豆时,土豆和土豆搜索引擎配置了 morpohology=en 将返回相同的结果。

于 2014-11-13T14:37:14.000 回答
0

您需要做的是首先安装用于执行拼写检查的服务器端解决方案,例如,您可以使用PSpell。如果您无法安装 PSpell,您可以使用 Googlespell(请务必查看使用条款)还有一些可用的客户端实现(尽管不如服务器端解决方案可靠)。

有很多插件可以帮助您进行拼写检查,但对于您的任务 Id,请选择更轻量级的东西,例如jQuery 拼写检查器

使用所需的选项启动您的拼写检查器,并为您的文本字段创建一个事件侦听器,一个简单的示例是:

var spellchecker = new $.SpellChecker('textarea', {
    lang: 'en',
    parser: 'text',
    webservice: {
       path: '../../webservices/php/SpellChecker.php',
       driver: 'pspell'
    },
    suggestBox: {
       position: 'above'
    }
});

$('textarea').keyup(function() {
        //you would probably want to add a timer looking to see if user is finished typing
        spellchecker.check();
    });

然后你会想要为 Spellchecker 事件添加一个监听器replace.word

spellchecker.on('replace.word', function() {
      //ie: submit the form
   });

如果你想做一些更高级的事情,你也可以尝试覆盖 Drupals 内置的自动完成Drupal.jsAC事件.keyup()并查看该事件check.success是否被触发(没有拼写错误的单词)。如果没有拼写错误的单词,则对 onkeyup 事件使用 drupal 自动完成实现。如果有拼写错误的单词,则对这些事件无能为力。

var misspelledWord = false;
spellchecker.on('check.success', function() { misspelledWord = false; });
spellchecker.on('check.fail', function() { misspelledWord = true; });

jQuery(document).ready(function(){
    Drupal.jsAC.prototype.onkeyup = function (input, e) {
      if(misspelledWord) {
          if (!e) {
          e = window.event;
        }
        switch (e.keyCode) {
          case 16: // shift
          case 17: // ctrl
          case 18: // alt
          case 20: // caps lock
          case 33: // page up
          case 34: // page down
          case 35: // end
          case 36: // home
          case 37: // left arrow
          case 38: // up arrow
          case 39: // right arrow
          case 40: // down arrow
            return true;

          case 9:  // tab
          case 13: // enter
          case 27: // esc
            this.hidePopup(e.keyCode);
            return true;

          default: // all other keys
            if (input.value.length > 0)
              this.populatePopup();
            else
              this.hidePopup(e.keyCode);
            return true;
        }
      }
    };
});

然而,通过在拼写检查器 replace.word 事件中使用 jQuery 的 AJAX 功能来实现您自己的自动完成对我来说更有意义 - 您也可以使用/自定义其中一个 jQuery 自动完成插件。

于 2013-03-07T06:57:07.033 回答
0

我使用 Levenshein 距离算法为 jQuery UI 默认自动完成添加了拼写检查功能。查看github上的代码:

https://github.com/martintaleski/spellcheckAutocomplete

它被包装成一个 jquery 插件。您需要做的就是:

$('.your-selector').spellcheckAutocomplete();
于 2014-08-24T19:40:55.843 回答