PHP 使用 pspell_check 从单词中获取单词
PHP get words from a word using pspell_check
我有一个 PHP 字符串,其中包含 英语 单词。我想从字符串中提取所有可能的单词,而不是 space 的 explode()
因为我只有一个单词。我的意思是从一个词中提取词。
例子:有单词"Whosebug"
,我需要提取stack, over, flow, overflow
全部
我正在使用 pspell_check()
进行拼写检查。我目前得到以下组合。
--> sta
--> stac
--> stack
and so on.
所以我找到了唯一匹配 stack
的单词,但我想找到以下单词。请注意,我不想说最后一句话,因为我已经这样做了。
--> stack
--> over
--> flow
我的代码:
$myword = "Whosebug";
$word_length = strlen($myword);
$myword_prediction = $myword[0].$myword[1];
//(initial condition as words detection starts after 3rd index)
for ($i=2; $i<$word_length; $i++) {
$myword_prediction .= $myword[$i];
if (pspell_check(pspell_new("en"), $myword_prediction))
{
$array[] = $myword_prediction;
}
}
var_dump($array);
如果你有这样的外循环怎么样?第一次从 $myword 的第一个字符开始。第二次从第二个字符开始,依此类推。
$myword = "Whosebug";
$word_length = strlen($myword);
$startLetter = 0;
while($startLetter < $word_length-2 ){
$myword_prediction = $myword[$startLetter] . $myword[$startLetter +1];
for ($i=$startLetter; $i<$word_length; $i++) {
$myword_prediction .= $myword[$i];
if (pspell_check(pspell_new("en"), $myword_prediction)) {
$array[] = $myword_prediction;
}
}
$startLetter ++;
}
好吧,您需要获取所有子字符串,并检查每一个:
function get_all_substrings($input){
$subs = array();
$length = strlen($input);
for($i=0; $i<$length; $i++){
for($j=$i; $j<$length; $j++){
$subs[] = substr($input, $i, $j);
}
}
return array_unique($subs);
}
$substrings = get_all_substrings("Whosebug");
$pspell_link = pspell_new("en");
$words = array_filter($substrings, function($word) use ($pspell_link) {
return pspell_check($pspell_link, $word);
});
var_dump($words);
我有一个 PHP 字符串,其中包含 英语 单词。我想从字符串中提取所有可能的单词,而不是 space 的 explode()
因为我只有一个单词。我的意思是从一个词中提取词。
例子:有单词"Whosebug"
,我需要提取stack, over, flow, overflow
全部
我正在使用 pspell_check()
进行拼写检查。我目前得到以下组合。
--> sta
--> stac
--> stack
and so on.
所以我找到了唯一匹配 stack
的单词,但我想找到以下单词。请注意,我不想说最后一句话,因为我已经这样做了。
--> stack
--> over
--> flow
我的代码:
$myword = "Whosebug";
$word_length = strlen($myword);
$myword_prediction = $myword[0].$myword[1];
//(initial condition as words detection starts after 3rd index)
for ($i=2; $i<$word_length; $i++) {
$myword_prediction .= $myword[$i];
if (pspell_check(pspell_new("en"), $myword_prediction))
{
$array[] = $myword_prediction;
}
}
var_dump($array);
如果你有这样的外循环怎么样?第一次从 $myword 的第一个字符开始。第二次从第二个字符开始,依此类推。
$myword = "Whosebug";
$word_length = strlen($myword);
$startLetter = 0;
while($startLetter < $word_length-2 ){
$myword_prediction = $myword[$startLetter] . $myword[$startLetter +1];
for ($i=$startLetter; $i<$word_length; $i++) {
$myword_prediction .= $myword[$i];
if (pspell_check(pspell_new("en"), $myword_prediction)) {
$array[] = $myword_prediction;
}
}
$startLetter ++;
}
好吧,您需要获取所有子字符串,并检查每一个:
function get_all_substrings($input){
$subs = array();
$length = strlen($input);
for($i=0; $i<$length; $i++){
for($j=$i; $j<$length; $j++){
$subs[] = substr($input, $i, $j);
}
}
return array_unique($subs);
}
$substrings = get_all_substrings("Whosebug");
$pspell_link = pspell_new("en");
$words = array_filter($substrings, function($word) use ($pspell_link) {
return pspell_check($pspell_link, $word);
});
var_dump($words);