Created
November 4, 2012 21:24
-
-
Save linuxscout/4013847 to your computer and use it in GitHub Desktop.
Spellchecker HunSpell driver class for jQuery Spellchecker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Spellchecker HunSpell driver class | |
* !! Hunspell is required !! | |
* | |
* @package jQuery Spellchecker (https://github.com/badsyntax/jquery-spellchecker) | |
* @author Taha Zerrouki | |
* @copyright (c) Taha Zerrouki | |
* @license https://github.com/badsyntax/jquery-spellchecker/blob/master/LICENSE-MIT | |
*/ | |
class SpellChecker_Driver_HunSpell extends Spellchecker_Driver | |
{ | |
protected $_default_config = array( | |
'dictionary' => 'pspell/dictionary', | |
'lang' => 'en' | |
); | |
private $encoding = "en_US.utf-8"; | |
private $raw; | |
private $hunspellVersion; | |
// Identify a 'miss'. See [man hunspell] | |
private $matcher = "/^(?P<type>&)\s(?P<original>\w+)\s(?P<count>\d+)\s(?P<offset>\d+):\s(?P<misses>.*+)$/u"; | |
private $response = array(); | |
public function __construct($config = array()) | |
{ | |
parent::__construct($config); | |
} | |
public function get_suggestions() | |
{ | |
$word = $_POST['word']; | |
$this->checkHunspell($word,$this->_config['lang']); | |
$response = $this->response; | |
$suggestions = array(); | |
if (isset($response[$word])) | |
{ | |
$suggestions = $response[$word]['misses']; | |
} | |
$this->send_data(NULL, $suggestions); | |
} | |
public function get_incorrect_words() | |
{ | |
$language = $this->_config['lang']; | |
$input = join(" ", (array)$_POST['text']); | |
$command = "LANG={$this->encoding}; echo '{$input}' | hunspell -d {$language} -l "; | |
$this->raw = shell_exec($command); | |
if($this->raw != null) | |
{ | |
$incorrectWords = explode("\n", $this->raw); | |
//return $words; | |
array_pop($incorrectWords); | |
$response[]=$incorrectWords; | |
} | |
else $response[]=array(); | |
$this->send_data('success', $response); | |
} | |
public function add_to_dictionary() | |
{ | |
} | |
public function checkHunspell($input, $language = null){ | |
if(($language == null) or ($input=="")) return ; | |
$command = "LANG={$this->encoding}; echo '{$input}' | hunspell -d {$language} "; | |
$this->raw = shell_exec($command); | |
$this->parse(); | |
} | |
private function parse(){ | |
if($this->raw == null) throw new Exception("Can't parse: no response."); | |
// Split the response into lines. | |
$lines = explode("\n", $this->raw); | |
// First item is the version # | |
$this->hunspellVersion = $lines[0]; | |
unset($lines[0]); | |
foreach($lines as $line){ | |
preg_match($this->matcher, $line, $matches); | |
if(count($matches) == 0) continue; | |
if($matches['type'] == "&") { | |
$this->response[$matches['original']] = array( | |
"count" => $matches['count'], | |
"offset" => $matches['offset'], | |
"misses" => explode(", ", $matches['misses']) | |
); | |
} | |
} | |
} | |
public function get() | |
{ | |
return $this->response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment