如何在Lucene 4.2中获取每个文档中的术语频率

时间:2013-05-23 02:42:59

标签: lucene

最初我使用Lucene 3.2来获得这样的代码的术语和术语频率:

for(int docNum=0; docNum < ir.numDocs(); docNum++) {
TermFreqVector tfv = ir.getTermFreqVector(docNum, "TERJEMAHAN");
if (tfv == null) {
    // ignore empty fields
    continue;
}
String terms[] = tfv.getTerms();
int termCount = terms.length;
int freqs[] = tfv.getTermFrequencies();

for(int t=0; t < termCount; t++) {
    int freqn = ir.docFreq(new Term("TERJEMAHAN", terms[t])); 
    }
}

如何在Lucene 4.2中获取每个文档的术语频率?

1 个答案:

答案 0 :(得分:3)

我设法使用这些行计算一个术语的频率:

Term term = ...;
IndexReader reader = ...;

DocsEnum docEnum = MultiFields.getTermDocsEnum(reader, MultiFields.getLiveDocs(reader), "contents", term.bytes());
int termFreq = 0;

int doc = DocsEnum.NO_MORE_DOCS;
while ((doc = docEnum.nextDoc()) != DocsEnum.NO_MORE_DOCS) {
    termFreq += docEnum.freq();
}