短语查询扩展的最快方法


Fastest approach for phrase query expansion

我使用同义词列表来指导查询扩展过程。格式如下:

fu=foo
ba=bar
etc=etcetera
werd=word

我使用一个简单的二进制搜索算法来根据这个列表运行每个用户输入的单词。问题是,当使用短语时。

    quick brown fox=alphabet
    out of this world=space
    why hello there=hello

典型输入:why hello there, where can I get an out of this world hopper?

期望输出为:hello, where can I get an space hopper?

我不想在搜索中运行每个词对或三个词组,并且我想避免根据输入对同义词库列表进行线性搜索,因为这是低效的(尽管列表应该相当小,所以这是一个选项)。

因此,我正在寻找在短语上运行二进制搜索的方法,或者以这种方式构建同义词库以补偿短语。

我使用PHP。欢迎有任何建议

简单的方法是使用str_replace。但我不知道性能如何。

$list = array('out of this world' => 'space');
$str = 'why hello there, where can I get an out of this world hopper?';
foreach ($list as $old => $new) {
    $str = str_replace($old, $new, $str);
}

编辑:我经常注意到,使用内置函数比编写自己的函数更有效,因为内置函数已经编译,但你的优化算法需要解释,这是一个巨大的减速。

我的第一个想法是使用像这样的关联数组

$thesaurus = array(
   'alphabet'  => 'quick brown fox',
   'space'     => 'out of this world',
   'hello'     => 'why hello there'
);

这样你就可以使用内置的array_search函数,这将比你在PHP中编写的任何东西都要快(我认为)。

preg_replace_callback代替你现在做的任何事情。PCRE恰好在字符串搜索方面非常有效,因为这就是它的目的。

你只需要构建一个单独的备选列表,然后在回调中通过原始map/dictionary进行实际替换。

$phrases = array(...);
$rx = implode("|", array_keys($phrases));
$text = preg_replace("/'b($rx)'b/musie", '$phrases["'1"]', $text);

此处仅使用/e表达式,回调可能更有用。