forked from moodleou/moodle-qtype_pmatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmatchlib.php
447 lines (386 loc) · 14.7 KB
/
pmatchlib.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?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 the API for accessing pmatch expression interpreter and matcher.
*
* @package qtype_pmatch
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/question/type/pmatch/pmatch/interpreter.php');
require_once($CFG->dirroot . '/question/type/pmatch/spellinglib.php');
// The following is required because the xdebug library defaults to throwing a fatal error if
// there is more than 100 nested function calls.
if (extension_loaded('xdebug')) {
ini_set('xdebug.max_nesting_level', 1000);
}
/**
* Options that control the overall way the matching is done.
*/
class pmatch_options {
/** @var boolean */
public $ignorecase;
/** @var string of sentence divider characters. */
public $sentencedividers = '.?!';
/** @var string of word diveder characters. */
public $worddividers = " \f\n\r\t";
/** @var string of characters that will be converted to spaces before matching. */
public $converttospace = "";
/**
* @var array of words to recognise. These words may include sentence or
* word divider characters.
*/
public $extradictionarywords = array('e.g.', 'eg.', 'etc.', 'i.e.', 'ie.');
/**
* @var string language of string -
* current language of respondee or saved for this attempt step.
*/
public $lang;
/**
* @var array of strings with preg expressions to match words that can be replaced.
*/
public $wordstoreplace = array();
/**
* @var array of strings to replace words with.
*/
public $synonymtoreplacewith = array();
/** @var array of words from synonyms that are exempt from spell check. */
public $nospellcheckwords = array();
public function set_synonyms($synonyms) {
$toreplace = array();
$replacewith = array();
foreach ($synonyms as $synonym) {
$synonym->word = $this->unicode_normalisation($synonym->word);
$synonym->synonyms = $this->unicode_normalisation($synonym->synonyms);
$toreplaceitem = preg_quote($synonym->word, '~');
$toreplaceitem = preg_replace('~\\\\\*~u',
'('.$this->character_in_word_pattern().')*', $toreplaceitem);
// The ?<= and ?= ensures that the adjacent characters are not replaced also.
$toreplaceitem = '~(?<=^|\PL)'.$toreplaceitem.'(?=\PL|$)~';
if ($this->ignorecase) {
$toreplaceitem .= 'i';
}
$this->wordstoreplace[] = $toreplaceitem;
$this->synonymtoreplacewith[] = "{$synonym->word}|{$synonym->synonyms}";
$this->nospellcheckwords[] = $synonym->word;
$synonymsforthisword = explode('|', $synonym->synonyms);
foreach ($synonymsforthisword as $synonymforthisword) {
$this->nospellcheckwords[] = $synonymforthisword;
}
}
}
public function set_extra_dictionary_words($wordlist) {
$wordlist = $this->unicode_normalisation($wordlist);
$this->extradictionarywords = preg_split('~\s+~', $wordlist);
}
public function unicode_normalisation($unicodestring) {
global $COURSE;
static $errorlogged = false;
if (class_exists('Normalizer')) {
return Normalizer::normalize($unicodestring);
} else {
// Limit the errors added to the log to one per pagee load.
if (!$errorlogged) {
debugging(get_string('env_peclnormalisationmissing', 'qtype_pmatch'), DEBUG_NORMAL);
$errorlogged = true;
}
return $unicodestring;
}
}
public function words_to_ignore_patterns() {
$words = array_merge($this->extradictionarywords, $this->nospellcheckwords);
$wordpatterns = array(PMATCH_NUMBER);
foreach ($words as $word) {
if (trim($word) === '') {
continue;
}
$wordpattern = preg_quote($word, '~');
$wordpattern = preg_replace('~\\\\\*~u',
'('.$this->character_in_word_pattern().')*',
$wordpattern);
$wordpatterns[] = $wordpattern;
}
return $wordpatterns;
}
/**
* @return string fragment of preg_match pattern to match sentence separator.
*/
public function sentence_divider_pattern() {
return $this->pattern_to_match_any_of($this->sentencedividers);
}
/**
* @return string fragment of preg_match pattern to match sentence separator.
*/
public function word_has_sentence_divider_suffix($word) {
$sd = $this->sentence_divider_pattern();
return (1 === preg_match('~('.$sd.')$~u', $word));
}
/**
*
* Strip one and only one sentence divider from the end of a string.
* @param string $string
* @return string with sentence divider stripped off.
*/
public function strip_sentence_divider($string) {
$sd = $this->sentence_divider_pattern();
if ($this->word_has_sentence_divider_suffix($string)) {
$string = core_text::substr($string, 0, -1);
}
return $string;
}
public function word_divider_pattern() {
return $this->pattern_to_match_any_of($this->worddividers . $this->converttospace);
}
public function character_in_word_pattern() {
return PMATCH_CHARACTER.'|'.PMATCH_SPECIAL_CHARACTER;
}
public function pattern_options() {
if ($this->ignorecase) {
return 'i';
} else {
return '';
}
}
private function pattern_to_match_any_of($charsinstring) {
$pattern = '';
for ($i = 0; $i < core_text::strlen($charsinstring); $i++) {
$char = core_text::substr($charsinstring, $i, 1);
if ($pattern != '') {
$pattern .= '|';
}
$pattern .= preg_quote($char, '~');
if ($char === '.') {
// Full stop should only match if it is not a decimal point (followed by a digit).
$pattern .= '(?![0-9])';
}
}
return $pattern;
}
}
/**
* Represents a string that is ready for matching, and provides the method to
* match expressions against it.
*/
class pmatch_parsed_string {
/** @var pmatch_options */
protected $options;
/** @var array of words created by splitting $string by $options->worddividers */
public $words;
private $misspelledwords = null;
private $unrecognizedfragment = '';
/**
* Constructor. Parses string.
* @param string $string the string to match against.
* @param pmatch_options $options the options to use.
*/
public function __construct($string, pmatch_options $options = null) {
if (!is_null($options)) {
$this->options = $options;
} else {
$this->options = new pmatch_options();
}
$this->words = array();
$wordno = 0;
$cursor = 0;
$string = trim($string); // Trim off any extra whitespace.
$string = $this->options->unicode_normalisation($string);
$sd = $this->options->sentence_divider_pattern();
$wd = $this->options->word_divider_pattern();
$wtis = $this->options->words_to_ignore_patterns();
$po = $this->options->pattern_options();
while ($cursor < core_text::strlen($string)) {
$toprocess = core_text::substr($string, $cursor);
$matches = array();
// Using a named sub pattern to make sure to capture the sentence divider.
$endofword = "(((?'sd'{$sd})({$wd})*)|({$wd})+|$)";
foreach ($wtis as $wti) {
if (preg_match("~({$wti})$endofword~Au$po", $toprocess, $matches)) {
// We found a number or extra dictionary word.
break;
}
}
if (!count($matches)) {
if (!preg_match("~(.+?)$endofword~A$po", $toprocess, $matches)) {
// Ignore the rest of the string.
break;
}
}
$word = $matches[1];
if (isset($matches['sd'])) {
$word .= $matches['sd'];
}
$this->words[$wordno] = $word;
$wordno++;
$cursor = $cursor + core_text::strlen($matches[0]);
if ('' === $this->options->strip_sentence_divider($word)) {
$this->unrecognizedfragment = core_text::substr($string, 0, $cursor);
}
}
if (count($this->words) == 0) {
$this->words = array('');
}
}
/**
* @return boolean returns false if any word is misspelt.
*/
public function is_spelt_correctly() {
$this->misspelledwords = $this->spell_check($this->words);
return (count($this->misspelledwords) == 0);
}
public function is_parseable() {
if ($this->unrecognizedfragment === '') {
return true;
} else {
return false;
}
}
public function unparseable() {
return $this->unrecognizedfragment;
}
protected function spell_check() {
global $COURSE;
$spellchecker = qtype_pmatch_spell_checker::make($this->options->lang);
$endofpattern = '(' . $this->options->sentence_divider_pattern() . ')?$~A';
if ($this->options->ignorecase) {
$endofpattern .= 'i';
}
$words = array_unique($this->words);
foreach ($this->options->words_to_ignore_patterns() as $wordstoignorepattern) {
$words = (preg_grep('~'.$wordstoignorepattern.$endofpattern, $words, PREG_GREP_INVERT));
}
$misspelledwords = array();
foreach ($words as $word) {
$word = $this->options->strip_sentence_divider($word);
if (!$spellchecker->is_in_dictionary($word)) {
$misspelledwords[] = $word;
}
}
return $misspelledwords;
}
/**
* @return array all the distinct misspelt words.
*/
public function get_spelling_errors() {
return $this->misspelledwords;
}
/**
* @return integer no of words.
*/
public function get_word_count() {
return count($this->words);
}
/**
* @return pmatch_options the options that were used to construct this object.
*/
public function get_options() {
return $this->options;
}
/**
* @return array the words to try to match.
*/
public function get_words() {
return $this->words;
}
}
/**
* Represents a pmatch_expression.
*/
class pmatch_expression {
/** @var pmatch_interpreter_whole_expression */
protected $interpreter;
/** @var string the original expression passed to the constructor */
protected $originalexpression;
/** @var string */
protected $errormessage;
/** @var boolean */
protected $valid;
/**
* @param string $string the string to match against.
* @param pmatch_options $options the options to use.
*/
public function __construct($expression, $options = null) {
if (!is_null($options)) {
$this->options = $options;
} else {
$this->options = new pmatch_options();
}
$expression = $this->options->unicode_normalisation($expression);
$this->originalexpression = $expression;
$this->interpreter = new pmatch_interpreter_whole_expression($options);
list($matched, $endofmatch) = $this->interpreter->interpret($expression);
$this->errormessage = $this->interpreter->get_error_message();
if ($endofmatch == core_text::strlen($expression) && $matched && $this->errormessage == '') {
$this->valid = true;
} else {
$this->valid = false;
if ($this->errormessage == '') {
$this->errormessage = get_string('ie_unrecognisedexpression', 'qtype_pmatch');
}
}
}
/**
* Test a string with a given pmatch expression.
* @param pmatch_parsed_string $parsedstring the parsed string to match.
* @return boolean whether this string matches the expression.
*/
public function matches(pmatch_parsed_string $parsedstring) {
if (!$this->is_valid()) {
throw new coding_exception('Oops. You called matches for an expression that is not '.
'valid. You should call is_valid first. Interpreter error :'.
$this->get_parse_error());
return false;
}
$matcher = $this->interpreter->get_matcher($this->options);
return $matcher->match_whole_expression($parsedstring->get_words());
}
/**
* @return boolean returns false if the string passed to the constructor
* could not be parsed as a valid pmatch expression.
*/
public function is_valid() {
return $this->valid;
}
/**
* @return string description of the syntax error in the expression string
* if is_valid returned false. Otherwise returns an empty string.
*/
public function get_parse_error() {
return $this->errormessage;
}
/**
* @return pmatch_options the options that were used to construct this object.
*/
public function get_options() {
return $this->options;
}
/**
* @return string the expression, exactly as it was passed to the constructor.
*/
public function get_original_expression_string() {
return $this->originalexpression;
}
/**
* @return string a nicely formatted version of the expression.
*/
public function get_formatted_expression_string() {
if (!$this->is_valid()) {
throw new coding_exception('Oops. You called get_formatted_expression_string for an '.
'expression that is not valid. You should call is_valid first.');
return false;
}
return $this->interpreter->get_formatted_expression_string();
}
}