基于搜索项对文档进行排名


Ranking document based on searched terms

如何实现这个

tf-idf(WORD) = occurrences(WORD,DOCUMENT)/number-of-words(DOCUMENT) * log10 (documents(ALL)/(1 + documents(WORD, ALL)))

进入我的PHP编码排名搜索结果?

可参考此处查看当前编码:

https://stackoverflow.com/a/8574651/1107551

我只理解你要求的一部分,但我想我可以帮助你的occurrences(WORD,DOCUMENT) / number-of-words(DOCUMENT)部分:

<?php
function rank($word, $document)
{
    // Swap newlines for spaces, you'll see why
    $document = str_replace("'n",' ',$document);
    // Remove special characters except '-' from the string
    for($i = 0; $i <= 127; $i++)
    {
        // Space is allowed, Hyphen is a legitimate part of some words. Also allow range for 0-9, A-Z, and a-z
        // Extended ASCII (128 - 255) is purposfully excluded from this since it isn't often used
        if($i != 32 && $i != 45 && !($i >= 48 && $i <=57) && !($i >= 65 && $i <= 90) && !($i >= 97 && $i <= 122))
            $document = str_replace(chr($i),'',$document);
    }
    // Split the document on spaces. This gives us individual words
    $tmpDoc = explode(' ',trim($document));
    // Get the number of elements with $word in them
    $occur = count(array_keys($tmpDoc,$word));
    // Get the total number of elements
    $numWords = count($tmpDoc);
    return $occur / $numWords;
}
?>

我相信有更有效的方法,但肯定也有更糟糕的方法。

注意:我没有测试PHP代码