【问题标题】:How to compute tf-idf from multiple text files in php?如何从 php 中的多个文本文件计算 tf-idf?
【发布时间】:2015-01-30 18:57:33
【问题描述】:

我正在成功地从一个数组计算 tf-idf。现在我希望 tf-idf 应该从多个文本文件中计算出来,因为我的目录中有多个文本文件。任何人都可以为多个文本文件修改此代码,以便首先读取目录中的所有文件,然后根据这些文件内容 tf-idf 计算。下面是我的代码,谢谢...

$collection = array(
    1 => 'this string is a short string but a good string',
    2 => 'this one isn\'t quite like the rest but is here',
    3 => 'this is a different short string that\' not as short'
);

$dictionary = array();
$docCount = array();

foreach($collection as $docID => $doc) {
    $terms = explode(' ', $doc);
    $docCount[$docID] = count($terms);

    foreach($terms as $term) {
        if(!isset($dictionary[$term])) {
            $dictionary[$term] = array('df' => 0, 'postings' => array());
        }
        if(!isset($dictionary[$term]['postings'][$docID])) {
            $dictionary[$term]['df']++;
            $dictionary[$term]['postings'][$docID] = array('tf' => 0);
        }

        $dictionary[$term]['postings'][$docID]['tf']++;
    }
}

$temp = ('docCount' => $docCount, 'dictionary' => $dictionary);

计算 tf-idf

$index = $temp;
$docCount = count($index['docCount']);
$entry = $index['dictionary'][$term];
foreach($entry['postings'] as  $docID => $postings) {
    echo "Document $docID and term $term give TFIDF: " .
        ($postings['tf'] * log($docCount / $entry['df'], 2));
    echo "\n";
}

【问题讨论】:

  • 我们不在codingserverviceoverflow上……请自行尝试,卡住时来

标签: php tf-idf


【解决方案1】:

看看这个答案:Reading all file contents from a directory - php

在那里您可以找到如何从目录中读取所有文件内容的信息。
有了这些信息,您应该能够自行修改代码,使其按预期工作。

【讨论】:

    猜你喜欢
    • 2020-05-11
    • 1970-01-01
    • 2012-04-23
    • 2023-03-04
    • 2015-04-17
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    相关资源
    最近更新 更多