【发布时间】:2021-07-12 17:34:41
【问题描述】:
我正在尝试学习 Lucene 8,这是我第一次使用 Lucene。
我想要每个学期的 TF-IDF,以便获得我的 Lucene 目录中的前 10.000 个令牌。我已经尝试了很多方法,但我被卡住了,我不知道如何继续。这是我所做的一个例子:
private static void getTokensForField(IndexReader reader, String fieldName) throws IOException {
List<LeafReaderContext> list = reader.leaves();
Similarity similarity = new ClassicSimilarity();
int docnum = reader.numDocs();
for (LeafReaderContext lrc : list) {
Terms terms = lrc.reader().terms(fieldName);
if (terms != null) {
TermsEnum termsEnum = terms.iterator();
BytesRef term;
while ((term = termsEnum.next()) != null) {
double tf = termsEnum.totalTermFreq() / terms.size();
double idf =Math.log(docnum / termsEnum.docFreq());
// System.out.println(term.utf8ToString() + "\tTF: " + tf + "\tIDF: " + idf);
}
}
}
}
其实我正在研究这个话题,但是我找到的资源并不是很有用。
我也在互联网上搜索过,但所有内容都已弃用。
你有什么建议吗?
【问题讨论】: