Happy New Year!
[squirrelmail.git] / plugins / squirrelspell / class / php_pspell.php
CommitLineData
85dcf0fb 1<?php
2/**
3 * PHP pspell spellcheck class functions
ae5dddc0 4 * @copyright 2006-2011 The SquirrelMail Project Team
85dcf0fb 5 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
6 * @version $Id$
7 * @package plugins
8 * @subpackage squirrelspell
9 */
10
11/**
12 * PHP Pspell class
13 * @package plugins
14 * @subpackage squirrelspell
15 */
16class 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 * Check block of text
81 * @return array
82 */
83 function check_text($sText) {
84 // resets missed words array
85 $this->missed_words = array();
86
87 $line = 0;
88 $start = 0;
89 $position = 0;
90 $word = '';
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.
95 $char = ' ';
96 } else {
97 $char = sq_substr($sText,$i,1,$this->charset);
98 }
99 // Current
100 switch($char) {
101 case ' ':
102 case '.':
103 case ';':
104 case "\t":
105 case "\r":
106 case "\n":
107 if (!empty($word)) {
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);
111 }
112 $this->missed_words[$word]['locations'][] = "$line:$start";
113 }
114 $word = '';
115 }
116 if ($char == "\n") {
117 $position = 0;
118 $line++;
119 } else {
120 $position++;
121 }
122 break;
123 default:
124 // a-zA-Z0-9' + 8bit chars (nbspace and other spaces excluded, depends on charset)
125 // add char to word
126 if(empty($word)) {
127 $start = $position; // squirrelspell adds one space to checked text
128 }
129 $position++;
130 $word.=$char;
131 }
132 }
133 return $this->missed_words;
134 }
135}