This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
/
SpellCheck.php
118 lines (106 loc) · 3.38 KB
/
SpellCheck.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
<?php
/**
* Phergie
*
* PHP version 5
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.
* It is also available through the world-wide-web at this URL:
* http://phergie.org/license
*
* @category Phergie
* @package Phergie_Plugin_TerryChay
* @author Phergie Development Team <team@phergie.org>
* @copyright 2008-2012 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_TerryChay
*/
/**
* Handles requests for checking spelling of specified words and returning
* either confirmation of correctly spelled words or potential correct
* spellings for misspelled words.
*
* @category Phergie
* @package Phergie_Plugin_SpellCheck
* @author Phergie Development Team <team@phergie.org>
* @license http://phergie.org/license New BSD License
* @link http://pear.phergie.org/package/Phergie_Plugin_TerryChay
* @uses Phergie_Plugin_Command pear.phergie.org
* @uses extension pspell
*/
class Phergie_Plugin_SpellCheck extends Phergie_Plugin_Abstract
{
/**
* Spell check dictionary handler
*
* @var resource
*/
protected $pspell;
/**
* Limit on the number of potential correct spellings returned
*
* @var int
*/
protected $limit;
/**
* Check for dependencies.
*
* @return void
*/
public function onLoad()
{
if (!extension_loaded('pspell')) {
$this->fail('pspell php extension is required');
}
if (!$this->getConfig('spellcheck.lang')) {
$this->fail('Setting spellcheck.lang must be filled-in');
}
$this->plugins->getPlugin('Command');
set_error_handler(array($this, 'loadDictionaryError'));
$this->pspell = pspell_new($this->getConfig('spellcheck.lang'));
restore_error_handler();
$this->limit = $this->getConfig('spellcheck.limit', 5);
}
/**
* Intercepts and handles requests for spell checks.
*
* @param string $word the string to perform checks against
*
* @return void
*/
public function onCommandSpell($word)
{
$source = $this->event->getSource();
$target = $this->event->getNick();
$message = $target . ': The word "' . $word;
$message .= '" seems to be spelled correctly.';
if (!pspell_check($this->pspell, $word)) {
$suggestions = pspell_suggest($this->pspell, $word);
$message = $target;
$message .= ': I could not find any suggestions for "' . $word . '".';
if (!empty($suggestions)) {
$suggestions = array_splice($suggestions, 0, $this->limit);
$message = $target . ': Suggestions for "';
$message .= $word . '": ' . implode(', ', $suggestions) . '.';
}
}
$this->doPrivmsg($source, $message);
}
/**
* Handle any errors from loading dictionary
*
* @param integer $errno Error code
* @param string $errstr Error message
* @param string $errfile File that errored
* @param integer $errline Line where the error happened
*
* @return void
*/
protected function loadDictionaryError($errno, $errstr, $errfile, $errline)
{
$this->fail($errstr);
}
}