100 lines
No EOL
2.7 KiB
PHP
100 lines
No EOL
2.7 KiB
PHP
<?php
|
|
|
|
require_once 'core/search/htmlhelper.php';
|
|
/**
|
|
* Ôóíêöèè äëÿ ïðîâåðêè îðôîãðàôèè
|
|
* rename Spell -> SpellHelper ??
|
|
*/
|
|
class Spell
|
|
{
|
|
private $pspell;
|
|
public $autocorrect = false;
|
|
public function __construct ($language, $encoding)
|
|
{
|
|
$this->pspell = pspell_new($language, '', '', $encoding, PSPELL_NORMAL);
|
|
}
|
|
|
|
function spellCheckWord ($word)
|
|
{
|
|
$word = $word[0];
|
|
|
|
// Ignore ALL CAPS
|
|
if (preg_match('/^[A-Z0-9]*$/', $word)) return $word;
|
|
|
|
// Return dictionary words
|
|
if (pspell_check($this->pspell, $word))
|
|
return $word;
|
|
|
|
// Auto-correct with the first suggestion, color green
|
|
if ($this->autocorrect && $suggestions = pspell_suggest($this->pspell, $word))
|
|
return '<span class="spell-suggest">' . current($suggestions) . '</span>';
|
|
|
|
// No suggestions, color red
|
|
return '<span class="spell-nosuggest">' . $word . '</span>';
|
|
}
|
|
|
|
function isGoodWord ($word)
|
|
{
|
|
// Ignore ALL CAPS
|
|
if (preg_match('/^[A-Z]*$/', $word)) return true;
|
|
if (preg_match('/^[0-9]*$/', $word)) return true;
|
|
|
|
// Return dictionary words
|
|
if (pspell_check($this->pspell, $word))
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
function suggest ($string)
|
|
{
|
|
return preg_replace_callback('/\b\w+\b/', array($this, 'spellCheckWord'), $string);
|
|
}
|
|
|
|
/**
|
|
* Âûçûâàåò ôóíêöèþ äëÿ êàæäîãî íàéäåííîãî ñëîâà
|
|
*/
|
|
function eachWord ($call, $string)
|
|
{
|
|
$begin = indexLeft ($string, 'ctype_alpha', 0);
|
|
while ($begin !== false) {
|
|
$end = indexLeft ($string, 'not_ctype_alpha', $begin);
|
|
call_user_func ($call, $string, $begin, $end);
|
|
$begin = indexLeft ($string, 'ctype_alpha', $end);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Ïðîâåðÿåò ñëîâî íà ñîîòâåòñòâèå ñî ñëîâàðåì
|
|
*
|
|
*/
|
|
function doWord (&$string, $begin, $end)
|
|
{
|
|
$word = substr($string, $begin, $end - $begin);
|
|
if (! $this->isGoodWord ($word)) {
|
|
$start = max(indexLeft ($string, 'not_ctype_alpha', $begin - 100), 0);
|
|
$offset = indexLeft ($string, 'not_ctype_alpha', $end + 100);
|
|
if (! $offset) {
|
|
$offset = strlen($string);
|
|
}
|
|
$length = $offset - $start;
|
|
throw new Exception ($this->suggest(substr($string, $start, $length)));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* ×àñòü òåêñòà ñîäåðæàùèé íåïðàâèëüíîå ñëîâî
|
|
*/
|
|
function getError ($string)
|
|
{
|
|
try {
|
|
self::eachWord (array ($this, 'doWord'), $string);
|
|
} catch (Exception $e) {
|
|
return $e->getMessage();
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
?>
|