2

最初我使用 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 中获取每个文档的词频?

4

1 回答 1

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();
}
于 2013-05-23T12:03:15.570 回答