2012-09-11 51 views
4

嗨我使用Drupal的6默認jQuery自動完成功能爲我的自定義文本字段之一。自動完成與拼寫檢查和textfield的複數檢查

現在我想添加拼寫檢查程序以及複數檢查以及。

例如

Word: Potatos 

Spellchecker suggestions (including plural check): Potato, Potatoes 

有什麼辦法來實現此功能,這樣當用戶型青枯病馬鈴薯。首先拼寫檢查腳本將運行,這將建議正確的替代方案,一旦用戶選擇適當的單詞,自動完成腳本將運行?

+0

cfk編輯器或tinymce想到 – 2012-09-11 06:28:08

+0

@Dagon是否可以將相同的功能添加到文本字段? –

回答

0

你需要做的是先安裝一個服務器端解決方案來執行你的拼寫檢查,你可以使用例如PSpell。如果您無法安裝PSpell,您可以使用Googlespell(只要確保查看使用條款)。還有一些客戶端實現可用(儘管不如服務器端解決方案可靠)。

這裏有很多插件可以幫助你進行拼寫檢查,但是對於你的任務ID,你可以選擇更爲輕量級的東西,比如jQuery spellchecker

啓動你的拼寫檢查與所需的選項,併爲您的文本框的事件監聽器,一個簡單的例子是:

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(); 
    }); 

那麼你將要添加的偵聽器的拼寫檢查事件replace.word

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

如果你想要做一些更高級的,你也可以嘗試重寫Drupals內置自動完成Drupal.jsAC事件.keyup(),看看事件check.success被解僱(N o拼寫錯誤的單詞)。如果沒有拼寫錯誤的單詞,您可以使用dukeals自動完成實現onkeyup事件。如果拼寫錯誤,對這些事件不做任何處理。

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

但是它更有意義,我用jQuery's AJAX功能在拼寫檢查replace.word事件來實現自己的自動完成 - 你也可以使用/自定義的jQuery插件自動完成的一個。

0

我已經使用Levenshein距離算法爲jQuery UI默認自動完成添加了拼寫檢查功能。檢查在GitHub上的代碼:

https://github.com/martintaleski/spellcheckAutocomplete

它包裹起來作爲一個jQuery插件。所有你需要做的是:

$('.your-selector').spellcheckAutocomplete(); 
0

我認爲解決方案應該是服務器端由ajax調用。

選項1 - 同音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 - sphinxsearch

其難以安裝,配置和學習,但支持多國語言,詞幹algoritms,高級查詢(布爾模式等),提高性能...

如果您想要更先進的系統全文搜索引擎與stemming支持將幫助你很多。看看http://sphinxsearch.com/docs/current.html#conf-morphology

隨着詞幹,當你搜索馬鈴薯,potatos和土豆配置morpohology = en的搜索引擎將返回相同的結果。