略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: pspell_suggest

2024-12-21

pspell_suggest

(PHP 4 >= 4.0.2, PHP 5, PHP 7, PHP 8)

pspell_suggestSuggest spellings of a word

说明

pspell_suggest(PSpell\Dictionary $dictionary, string $word): array|false

pspell_suggest() returns an array of possible spellings for the given word.

参数

dictionary

An PSpell\Dictionary instance.

word

The tested word.

返回值

Returns an array of possible spellings.

更新日志

版本 说明
8.1.0 The dictionary parameter expects an PSpell\Dictionary instance now; previously, a 资源(resource) was expected.

范例

示例 #1 pspell_suggest() example

<?php
$pspell 
pspell_new("en");

if (!
pspell_check($pspell"testt")) {
    
$suggestions pspell_suggest($pspell"testt");

    foreach (
$suggestions as $suggestion) {
        echo 
"Possible spelling: $suggestion<br />";
    }
}
?>
add a noteadd a note

User Contributed Notes 1 note

up
9
webmaster at hostpure dot com
18 years ago
It seems if you are trying to do something similar to the Google 'Did you mean:' suggestions and are selecting the first word given by the pspell_suggest() function, then it will not work well with custom dictionaries and replacements. Take the following code for example:

<?php
    $pspell_config
= pspell_config_create("en");
   
pspell_config_personal($pspell_config, "/home/user/public_html/custom.pws");
   
pspell_config_repl($pspell_config, "/home/user/public_html/custom.repl");
   
$pspell_link = pspell_new_config($pspell_config);

   
$words = preg_split ("/\s+/", $query);
   
$ii = count($words);

    global
$spellchecked;
   
$spellchecked = "";
   
    for(
$i=0;$i<$ii;$i++){

        if (
pspell_check($pspell_link, $words[$i]))
        {
           
$spellchecked .= $words[$i]." ";
        }
        else
        {
           
$erroneous = "yes";
           
$suggestions = pspell_suggest($pspell_link, $words[$i]);
           
$spellchecked .= $suggestions[0]." ";
        }
    }
    if(
$erroneous == "yes")
    {
        echo
"Did you mean: <i>".$spellchecked."?";
    }
    else
    {
        echo
$spellchecked . " is a valid word/phrase";
    }
?>

This works fine most of the time, and gives suggestions to what you meant when inserting a spelling mistake with most inputs. However, if you specify a custom replacement and then search for the misspelt word that you specified, then if it is not the first returned suggestion it wont be used in the 'Did you mean' end result. What you need to do is open up the custom dictionary using fopen and fread, and then for each of the suggested words, check if they are in the dictionary. If the suggested word is in the custom dictionary then use it in the 'Did you mean' part, if not, discard it and try the next. Hope this helps anyone who comes across this problem with trying to get more accurate suggestions.

官方地址:https://www.php.net/manual/en/function.pspell-suggest.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3