forked from moodleou/moodle-qtype_pmatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspellinglib.php
333 lines (275 loc) · 10.2 KB
/
spellinglib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains an API for accessing spell-checking back-ends.
*
* @package qtype_pmatch
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir . '/adminlib.php');
/**
* Object that provides spell-checking, to make it easy to support different back-ends.
*
* Which back-end to use is controlled by get_config('qtype_pmatch', 'spellchecker').
* Create an instance of this class using
* $spellchecker = qtype_pmatch_spell_checker::make(); then test
* words using $spellchecker->is_in_dictionary($word);
*/
abstract class qtype_pmatch_spell_checker {
/**
* @var array lang code => qtype_pmatch_spell_checker, so we only load each dictionary once.
* We were experiencing incomprehensible errors if we loaded the same dictionaries
* repeatedly (it just died on enchant_broker_request_dict with no error message).
* Using this cache avoids that.
*/
protected static $checkers = array();
/**
* Spell-check a word.
* @param string $word the word to check.
* @return bool whether the word is in the dictionary.
*/
public abstract function is_in_dictionary($word);
/**
* Factory method create a new spell-checker object for a given language.
* @param string $lang the language code. If null, defaults to get_string('iso6391', 'langconfig').
* @return qtype_pmatch_spell_checker the requested object.
*/
public static function make($lang = null) {
$spellchecker = get_config('qtype_pmatch', 'spellchecker');
if ($lang === null) {
$lang = get_string('iso6391', 'langconfig');
}
if (isset(self::$checkers[$lang])) {
return self::$checkers[$lang];
}
$backends = self::get_known_backends();
if (!array_key_exists($spellchecker, $backends)) {
debugging('Unknown spell checker back end ' . $spellchecker);
return self::make_null_checker($lang);
}
$classname = $backends[$spellchecker];
if (!$classname::is_available()) {
debugging('Selected spell checker back end ' . $spellchecker . ' is not available.');
return self::make_null_checker($lang);
}
$checker = new $classname($lang);
if (!$checker->is_initialised()) {
debugging('Spell checker back end ' . $spellchecker .
' could not be initialised for language ' . $lang);
return self::make_null_checker($lang);
}
self::$checkers[$lang] = $checker;
return $checker;
}
/**
* Helper method used by {@link make()} when a real dictionary can't be found.
* @param string $lang a language code.
* @return qtype_pmatch_null_spell_checker
*/
protected static function make_null_checker($lang) {
self::$checkers[$lang] = new qtype_pmatch_null_spell_checker($lang);
return self::$checkers[$lang];
}
/**
* @return array a list of all the back-end library classes.
*/
public static function get_known_backends() {
return array(
'null' => 'qtype_pmatch_null_spell_checker',
'pspell' => 'qtype_pmatch_pspell_spell_checker',
'enchant' => 'qtype_pmatch_enchant_spell_checker',
);
}
/**
* @return array a list of the back-end library classes that might work on this server.
*/
public static function get_installed_backends() {
$backends = qtype_pmatch_spell_checker::get_known_backends();
$installedbackends = array();
foreach ($backends as $key => $classname) {
if ($classname::is_available()) {
$installedbackends[$key] = $classname;
}
}
return $installedbackends;
}
/**
* Just to document the expected constructor API.
* @param string $lang the language code.
*/
protected function __construct($lang) {
}
/**
* This method only exists to support pspell. Pspell cannot tell us if the
* required dictionary exists until we have tried to create it.
* @return boolean whether this class was initialised correctly.
*/
public function is_initialised() {
return true;
}
/**
* Subclasses must implement this.
* @return string translated name of this back-end, for use in the UI.
*/
public static function get_name() {
return '';
}
/**
* Subclasses must implement this.
* @return bool whether the necessary libraries are installed for this back-end to work.
*/
public static function is_available() {
return false;
}
}
/**
* Implements the {@core_spell_checker} by saying that that any string is a
* correctly spelled word. This can be used when there is no back-end installed.
*/
class qtype_pmatch_null_spell_checker extends qtype_pmatch_spell_checker {
public function is_in_dictionary($word) {
return true;
}
public static function get_name() {
return get_string('spellcheckernull', 'qtype_pmatch');
}
public static function is_available() {
return true;
}
}
/**
* Implements the {@core_spell_checker} API using pspell.
*/
class qtype_pmatch_pspell_spell_checker extends qtype_pmatch_spell_checker {
/** @var int the pspell link handle. */
protected $pspell;
public function __construct($lang) {
parent::__construct($lang);
$this->pspell = pspell_new($lang);
}
public function is_initialised() {
return (bool) $this->pspell;
}
public function is_in_dictionary($word) {
return pspell_check($this->pspell, $word);
}
public static function get_name() {
return get_string('spellcheckerpspell', 'qtype_pmatch');
}
public static function is_available() {
return function_exists('pspell_new');
}
}
/**
* Implements the {@core_spell_checker} API using enchant.
*/
class qtype_pmatch_enchant_spell_checker extends qtype_pmatch_spell_checker {
/** @var resource the enchant broker. */
protected static $broker = null;
/** @var resource the enchant dictionary. */
protected $dictionary = null;
public function __construct($lang) {
parent::__construct($lang);
$broker = self::get_broker();
if (!$broker) {
throw new coding_exception('Failed to create an enchant broker.');
}
$this->dictionary = enchant_broker_request_dict($broker, $lang);
}
public function __destruct() {
if ($this->dictionary) {
enchant_broker_free_dict($this->dictionary);
}
}
public function is_in_dictionary($word) {
return enchant_dict_check($this->dictionary, $word);
}
public static function get_name() {
return get_string('spellcheckerenchant', 'qtype_pmatch');
}
public static function is_available() {
if (!function_exists('enchant_broker_init')) {
return false;
}
return (bool) self::get_broker();
}
public function is_initialised() {
return (bool) $this->dictionary;
}
/**
* @return a broker.
*/
protected static function get_broker() {
if (self::$broker === null) {
self::$broker = enchant_broker_init();
}
return self::$broker;
}
}
/**
* Admin settings class for chosing a spell-checker back-end.
*
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_pmatch_admin_setting_spell_checker extends admin_setting_configselect {
public function load_choices() {
if (is_array($this->choices)) {
return true;
}
$this->choices = array();
$backends = qtype_pmatch_spell_checker::get_installed_backends();
foreach ($backends as $key => $classname) {
$this->choices[$key] = $classname::get_name();
}
return true;
}
}
/**
* Admin settings class for chosing a spell-checker back-end.
*
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_pmatch_admin_setting_environment_check extends admin_setting_heading {
public function output_html($data, $query = '') {
$results = array();
if (class_exists('Normalizer')) {
$results[] = get_string('env_peclnormalisationok', 'qtype_pmatch');
} else {
$results[] = get_string('env_peclnormalisationmissing', 'qtype_pmatch');
}
$spellchecker = qtype_pmatch_spell_checker::make();
$results[] = $spellchecker->get_name();
if (!$spellchecker instanceof qtype_pmatch_null_spell_checker) {
$stringmanager = get_string_manager();
foreach (get_string_manager()->get_list_of_translations() as $lang => $humanfriendlylang) {
$a = new stdClass();
$a->lang = $lang;
$a->humanfriendlylang = $humanfriendlylang;
$a->langforspellchecker = $stringmanager->get_string('iso6391', 'langconfig', null, $lang);
if (qtype_pmatch_spell_checker::make($a->langforspellchecker) instanceof qtype_pmatch_null_spell_checker) {
$results[] = get_string('env_dictmissing', 'qtype_pmatch', $a);
} else {
$results[] = get_string('env_dictok', 'qtype_pmatch', $a);
}
}
}
$this->description = implode("\n\n", $results);
return parent::output_html($data, $query);
}
}