归一化TF-IDF结果

时间:2012-07-01 11:03:56

标签: normalization normalize tf-idf

我想将我从这个给定代码中获得的tfidf结果标准化:

for (int docNum = 0; docNum < ir.numDocs(); docNum++) {
            TermFreqVector tfv = ir.getTermFreqVector(docNum, "contents");
            if (tfv == null) {
                // ignore empty fields
                continue;
            }
            String[] tterms = tfv.getTerms();
            int termCount = tterms.length;
            int[] freqs = tfv.getTermFrequencies();
            for (int t = 0; t < termCount; t++) {
                double idf = ir.numDocs() / ir.docFreq(new Term("contents", tterms[t]));
                System.out.println(" " + tterms[t] + " " + freqs[t]*Math.log(idf));
            }
        }

此代码的输出为:

area 0.0
areola 5.877735781779639
ari 3.9318256327243257
art 1.6094379124341003
artifici 1.0986122886681098
assign 2.1972245773362196
associ 3.295836866004329
assur 1.9459101490553132
averag 1.0986122886681098
avoid 0.6931471805599453
.
.
.

非常感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:5)

一种常见的方法是按文档大小进行标准化。即使用相对频率,而不是使用术语计数(或绝对频率)。

freqsum为频率数组的总和。然后使用

freqs[t]/(double)freqsum*Math.log(idf)

为避免这种混淆,我建议使用术语:

  • 术语计数表示“绝对频率”
  • 相对频率,用于文档中的比率

而不是含糊不清的术语“ term frequency ”。

我知道,从历史上看,如果你查看Salton,Yang,关于自动索引(1973)中术语值的说明,它们指的是绝对计数。余弦相似性将取消规模,所以无论如何它都无关紧要。像Lucene这样的现代系统将试图更好地控制文档的影响。