【发布时间】:2011-05-31 15:32:54
【问题描述】:
最近开始着手一个可能需要(良好)扩展可能性的项目,我提出了以下问题:
不考虑 levensthein 算法(我正在使用/处理不同的变体),我遍历每个字典单词并计算字典单词与输入字符串中每个单词之间的 levensthein 距离。大致如下:
<?php
$input_words = array("this", "is", "a", "test");
foreach ($dictionary_words as $dictionary_word) {
foreach ($input_words as $input_word) {
$ld = levenshtein($input_word, $accepted_word);
if ($ld < $distances[$input_word] || $distances[$word] == NULL) {
$distances[$input_word] = $ld;
if ($ld == 0)
continue;
}
}
}
?>
我的问题是关于最佳实践:执行时间约为 1-2 秒。 我正在考虑运行一个“字典服务器”,它在启动时将字典单词加载到内存中,然后在收到请求时作为拼写检查的一部分进行迭代(如上所述)。这会减少执行时间还是迭代(for循环)的缓慢部分?如果是这样,我可以做些什么来正确优化?
Google 的“您的意思是:?”不需要几秒钟来检查相同的输入字符串;)
提前致谢,新年快乐。
【问题讨论】:
标签: php algorithm optimization iteration spell-checking