我正在尝试使用 HunSpellChecker 类拼写检查字符串(请参阅https://web.archive.org/web/20130311163032/http://www.phpkode.com/source/s/php-spell-checker/php- spell-checker/HunSpellChecker.class.php ) 和 hunspell 拼写引擎。相关函数复制到这里:
public function checkSpelling ($text, $locale, $suggestions = true) {
$text = trim($text);
if ($this->textIsHtml == true) {
$text = strtr($text, "\n", ' ');
} elseif ($text == "") {
$this->spellingWarnings[] = array(self::SPELLING_WARNING__TEXT_EMPTY=>"Text empty");
return false;
}
$descspec = array(
0=>array('pipe', 'r'),
1=>array('pipe', 'w'),
2=>array('pipe', 'w')
);
$pipes = array();
$cmd = $this->hunspellPath;
$cmd .= ($this->textIsHtml) ? " -H ":"";
$cmd .= " -d ".dirname(__FILE__)."/dictionaries/hunspell/".$locale;
$process = proc_open($cmd, $descspec, $pipes);
if (!is_resource($process)) {
$this->spellingError[] = array(self::SPELLING_ERROR__INTERNAL_ERROR=>"Hunspell process could not be created.");
return false;
}
fwrite($pipes[0], $text);
fclose($pipes[0]);
$out = '';
while (!feof($pipes[1])) {
$out .= fread($pipes[1], 4096);
}
fclose($pipes[1]);
// check for errors
$err = '';
while (!feof($pipes[2])) {
$err .= fread($pipes[2], 4096);
}
if ($err != '') {
$this->spellingError[] = array(self::SPELLING_ERROR__INTERNAL_ERROR=>"Spell checking error: ".$err);
fclose($pipes[2]);
return false;
}
fclose($pipes[2]);
proc_close($process);
if (strlen($out) === 0) {
$this->spellingError[] = array(self::SPELLING_WARNING__EMPTY_RESULT=>"Empty result");
return false;
}
return $this->parseHunspellOutput(explode("\n", $out), $locale, $suggestions);
}
它适用于 ASCII 字符串,但我必须检查不同语言的字符串,这些字符串具有重音字符(necessário、segrança 等)或非拉丁字母(希腊语、阿拉伯语等)。
在这些情况下的问题是非 ASCII 单词被错误地分段,并且发送到 Hunspell 的“拼写错误”的单词实际上是一个子字符串而不是完整的单词(necess,segran)。
我试图跟踪问题发生的位置,并且我假设它必须在上面链接的类的第 072 行中,当字符串被转换为资源时(或之后的某个地方)。第 072 行包含:
fwrite($pipes[0], $text);
该课程没有评论,所以我不确定那里发生了什么。
有没有人处理过类似的问题,或者有人可以提供任何帮助吗?
该类包含在文件示例/HunspellBased.php 中(从http://titirit.users.phpclasses.org/package/5597-PHP-Check-spelling-of-text-and-get-fix-suggestions.html下载的包)。我尝试使用 Enchant,但我根本无法让它发挥作用。
谢谢!干杯,曼努埃尔