Source for file php_pspell.php

Documentation is available at php_pspell.php

  1. <?php
  2. /**
  3.  * PHP pspell spellcheck class functions
  4.  * @copyright &copy; 2006 The SquirrelMail Project Team
  5.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  6.  * @version $Id: php_pspell.php,v 1.1 2006/09/30 17:35:20 tokul Exp $
  7.  * @package plugins
  8.  * @subpackage squirrelspell
  9.  */
  10.  
  11. /**
  12.  * PHP Pspell class
  13.  * @package plugins
  14.  * @subpackage squirrelspell
  15.  */
  16. class php_pspell extends squirrelspell {
  17.     //
  18.         var $dict = 'en';
  19.     var $subdict = '';
  20.     var $jargon = '';
  21.     var $charset = 'utf-8';
  22.     var $mode = null;
  23.     var $userdic = array();
  24.  
  25.     /**
  26.      */
  27.     var $missed_words = array();
  28.  
  29.     /**
  30.      * Error buffer
  31.      * @var string 
  32.      */
  33.     var $error = '';
  34.     /**
  35.      */
  36.     var $dictionary_link = null;
  37.  
  38.     /**
  39.      * Constructor function
  40.      * @param array $aParams 
  41.      */
  42.     function php_pspell($aParams=array()) {
  43.         if (extension_loaded('pspell')) {
  44.             return $this->set_error('Pspell extension is not available');
  45.         }
  46.         //
  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]);
  52.         }
  53.         if (isset($aParams['charset'])) {
  54.             $this->charset = $aParams['charset'];
  55.         }
  56.         if (isset($aParams['userdic'])) {
  57.             $this->userdic = $aParams['userdic'];
  58.         }
  59.         if (isset($aParams['mode'])) {
  60.             $this->mode = $aParams['mode'];
  61.         else {
  62.             $this->mode = PSPELL_FAST;
  63.         }
  64.         // dict, subdict, jargon, charset, spellcheck_type
  65.         $this->dictionary_link = pspell_new($this->dict,$this->subdict,$this->jargon,$this->charset,$this->mode);
  66.     }
  67.  
  68.     // private functions
  69.         function check_word($sWord{
  70.         return pspell_check($this->dictionary_link,$sWord);
  71.     }
  72.  
  73.     function suggest($sWord{
  74.         return pspell_suggest($this->dictionary_link,$sWord);
  75.     }
  76.  
  77.     // public function
  78.  
  79.     
  80.     /**
  81.      * Check block of text
  82.      * @return array 
  83.      */
  84.     function check_text($sText{
  85.         // resets missed words array
  86.         $this->missed_words = array();
  87.  
  88.         $line 0;
  89.         $start 0;
  90.         $position 0;
  91.         $word '';
  92.         // parse text. sq_* functions are used in order to work with characters and not with bytes
  93.         for ($i 0$i <= sq_strlen($sText,$this->charset)$i++{
  94.             if ($i == sq_strlen($sText,$this->charset)) {
  95.                 // add space in order to check last $word.
  96.                 $char ' ';
  97.             else {
  98.                 $char sq_substr($sText,$i,1,$this->charset);
  99.             }
  100.             // Current
  101.             switch($char{
  102.             case ' ':
  103.             case '.':
  104.             case ';':
  105.             case "\t":
  106.             case "\r":
  107.             case "\n":
  108.                 if (!empty($word)) {
  109.                     if (isset($this->missed_words[$word]|| !$this->check_word($word)) {
  110.                         if (isset($this->missed_words[$word]['suggestions'])) {
  111.                             $this->missed_words[$word]['suggestions'$this->suggest($word);
  112.                         }
  113.                         $this->missed_words[$word]['locations']["$line:$start";
  114.                     }
  115.                     $word '';
  116.                 }
  117.                 if ($char == "\n"{
  118.                     $position 0;
  119.                     $line++;
  120.                 else {
  121.                     $position++;
  122.                 }
  123.                 break;
  124.             default:
  125.                 // a-zA-Z0-9' + 8bit chars (nbspace and other spaces excluded, depends on charset)
  126.                 // add char to word
  127.                 if(empty($word)) {
  128.                     $start $position// squirrelspell adds one space to checked text
  129.                 }
  130.                 $position++;
  131.                 $word.=$char;
  132.             }
  133.         }
  134.         return $this->missed_words;
  135.     }
  136. }

Documentation generated on Sat, 07 Oct 2006 16:13:02 +0300 by phpDocumentor 1.3.0RC6