【问题标题】:php spellcheck iteration optimizationphp拼写检查迭代优化
【发布时间】: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


【解决方案1】:

阅读 Norvig 的How to Write a Spelling Corrector。文章虽然使用了Python,但其他人已经用PHP实现了herehere

【讨论】:

  • 在它的底部有 2 个链接到 2 个 PHP 实现
  • 我设法获得了每个单词 0.0001-0.001 的执行时间,以 Norvig 为例,稍微调整一下并将初始化留给启动(将 python 脚本作为服务器运行,处理拼写问题并回答有更正的版本),非常感谢!
【解决方案2】:

您最好将您的字典实现为二叉树或其他更高效的数据结构。树将极大地减少查找时间。

【讨论】:

    猜你喜欢
    • 2011-04-10
    • 2010-11-21
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多