从文本文件中提取有效单词 PHP

Extract valid words from a text file PHP

我创建了一个 PHP 代码,可以从文本文件中提取有效单词:

$pspell_link = pspell_new("en");
$handle = fopen("list.txt", "r");

if ($handle) {
            while (($line = fgets($handle)) !== false) {

                    $line = str_replace(' ', '', $line);
                    $line = preg_replace('/\s+/', '', $line);

                    if (pspell_check($pspell_link, $line)) 
                    {
                        echo $line."<br>";
                    }

            }
}

我们假设 list.txt 包含

啊啊 fghy 你好 你好

上面的代码只会打印:你好

我想要做的是打印 Hellothere 以及它包含两个有效词 Hello 和 there

(已编辑)

您可以尝试将常量 PSPELL_RUN_TOGETHER 作为选项传递:

$pspell_link = pspell_new( "en", Null, Null, Null, PSPELL_RUN_TOGETHER );

来自 PHP 文档:

The mode parameter is the mode in which spellchecker will work. There are several modes available:

PSPELL_FAST - Fast mode (least number of suggestions)

PSPELL_NORMAL - Normal mode (more suggestions)

PSPELL_BAD_SPELLERS - Slow mode (a lot of suggestions)

PSPELL_RUN_TOGETHER - Consider run-together words as legal compounds. That is, "thecat" will be a legal compound, although there should be a space between the two words. Changing this setting only affects the results returned by pspell_check(); pspell_suggest() will still return suggestions.

此外,替换 line 中的所有空格,您将类似“ghghfghyHelloHellothere”的字符串传递给 pspell_check()

尝试爆炸:

(...)
$words = explode( ' ', $line );
foreach($words as $word)
{
    if (pspell_check($pspell_link, $word)) 
    {
        echo "---> ".$word.PHP_EOL;
    }
}
(...)