插件96:单词拼写提示

<?php // Plug-in 96: Suggest Spelling
/*
* 单词拼写提示
* 插件说明:
* 插件接受一个无法识别的单词,返回最接近它的单词。
* 若操作成功,则返回两个元素的数组,其中第一个元素表示返回单词的个数,第二个元素是一个单词数组。
* 若操作失败,则返回一个一元素的数组,元素的值为FALSE。
* 它需要以下参数:
* $word 一个单词
* $dictionary 字典文件的路径。
*/
// This is an executable example with additional code supplied
// To obtain just the plug-ins please click on the Download link

$word = 'spenr';
echo "Suggested spellings for '$word':<br /><ul>";

$results = PIPHP_SuggestSpelling($word, 'dictionary.txt');
if (!$results[0]) echo "No suggested spellings.";
else foreach ($results[1] as $spelling)
   echo "<li>$spelling</li>";

function PIPHP_SuggestSpelling($word, $dictionary)
{
   // Plug-in 96: Suggest Spelling
   //
   // This plug-in should be supplied with a misspelled word
   // using which it will search $dictionary for words it
   // believes the user may have meant to have typed in. Upon
   // success it returns a two element array, the first of
   // which is the number of possible words it suggests, and
   // the second is an array of words in order of likelihood.
   // On failure a single element array with the value FALSE
   // is returned. It requires these arguments:
   //
   //    $word:       A word to look up
   //    $dictionary: The location of a list of words separated
   //                 by non-word or space characters such as
   //                 \n or \r\n

   if (!strlen($word)) return array(FALSE);

   static $count, $words;

   if ($count++ == 0)
   {
      $dict = @file_get_contents($dictionary);
      if (!strlen($dict)) return array(FALSE);
      $words = explode("\r\n", $dict);
   }

   $possibles = array();
   $known     = array();
   $suggested = array();
   $wordlen   = strlen($word);
   $chars     = str_split('abcdefghijklmnopqrstuvwxyz');

   for($j = 0 ; $j < $wordlen ; ++$j)
   {
       //单词中去掉一个字母
      $possibles[] =    substr($word, 0, $j) .
                        substr($word, $j + 1);
      //对单词替换一个字母 
      foreach($chars as $letter)
         $possibles[] = substr($word, 0, $j) .
                        $letter .
                        substr($word, $j + 1);
   }

   for($j = 0; $j < $wordlen - 1 ; ++$j)
      //单词中两个字母交换位置
      $possibles[] =    substr($word, 0, $j) .
                        $word[$j + 1] .
                        $word[$j] .
                        substr($word, $j +2 );

   for($j = 0; $j < $wordlen + 1 ; ++$j)
      foreach($chars as $letter)
	     //单词中插入一个新的字母
         $possibles[] = substr($word, 0, $j).
                        $letter.
                        substr($word, $j);
   //所有以上四种情况的单词与字典中单词对比,如果字典中存在,返回到$known中。
   $known = array_intersect($possibles, $words);
   $known = array_count_values($known);
   arsort($known, SORT_NUMERIC);

   foreach ($known as $temp => $val)
      $suggested[] = $temp;

   return array(count($suggested), $suggested);
}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值