phplibrary/core/spell.php
2016-07-14 16:29:26 +03:00

100 lines
No EOL
2.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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;
}
}
?>