5b7d861430f5f128a6a549cf30044782305b6c94
3 * PHP pspell spellcheck class functions
4 * @copyright 2006-2010 The SquirrelMail Project Team
5 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
8 * @subpackage squirrelspell
14 * @subpackage squirrelspell
16 class php_pspell
extends squirrelspell
{
21 var $charset = 'utf-8';
23 var $userdic = array();
27 var $missed_words = array();
36 var $dictionary_link = null;
39 * Constructor function
40 * @param array $aParams
42 function php_pspell($aParams=array()) {
43 if (! extension_loaded('pspell')) {
44 return $this->set_error('Pspell extension is not available');
47 if (isset($aParams['dictionary'])) {
48 $aDict = explode(',',$aParams['dictionary']);
49 if (isset($aDict[0])) $this->dict
= trim($aDict[0]);
50 if (isset($aDict[1])) $this->subdict
= trim($aDict[1]);
51 if (isset($aDict[2])) $this->jargon
= trim($aDict[2]);
53 if (isset($aParams['charset'])) {
54 $this->charset
= $aParams['charset'];
56 if (isset($aParams['userdic'])) {
57 $this->userdic
= $aParams['userdic'];
59 if (isset($aParams['mode'])) {
60 $this->mode
= $aParams['mode'];
62 $this->mode
= PSPELL_FAST
;
64 // dict, subdict, jargon, charset, spellcheck_type
65 $this->dictionary_link
= pspell_new($this->dict
,$this->subdict
,$this->jargon
,$this->charset
,$this->mode
);
69 function check_word($sWord) {
70 return pspell_check($this->dictionary_link
,$sWord);
73 function suggest($sWord) {
74 return pspell_suggest($this->dictionary_link
,$sWord);
83 function check_text($sText) {
84 // resets missed words array
85 $this->missed_words
= array();
91 // parse text. sq_* functions are used in order to work with characters and not with bytes
92 for ($i = 0; $i <= sq_strlen($sText,$this->charset
); $i++
) {
93 if ($i == sq_strlen($sText,$this->charset
)) {
94 // add space in order to check last $word.
97 $char = sq_substr($sText,$i,1,$this->charset
);
108 if (isset($this->missed_words
[$word]) ||
!$this->check_word($word)) {
109 if (! isset($this->missed_words
[$word]['suggestions'])) {
110 $this->missed_words
[$word]['suggestions'] = $this->suggest($word);
112 $this->missed_words
[$word]['locations'][] = "$line:$start";
124 // a-zA-Z0-9' + 8bit chars (nbspace and other spaces excluded, depends on charset)
127 $start = $position; // squirrelspell adds one space to checked text
133 return $this->missed_words
;