我正在学习Lucene 8,这是我第一次使用Lucene。
我想有每个术语的TF-IDF,以获得前10.000令牌在我的Lucene目录.我已经尝试了很多方法,但我卡住了,我不知道如何进行.这是一个例子,我做了什么:
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);
}
}
}
}
其实我也在研究这个课题,但是我找到的资源并不是很有用。
我也在网上搜索过,但所有的东西都被否决了。
你有什么建议吗?
1条答案
按热度按时间kgsdhlau1#
据我所知,访问TF和IDF等统计信息的最简单方法是使用
Explanation
类。不过,我想澄清一下(如果我告诉你的是你已经知道的事情,请道歉):“术语频率”值用于文档 * 中的术语 * -因此,相同的术语在不同的文档中可能会产生不同的值。
我不太清楚这对于您的“* 获得Lucene目录中的前10.000个令牌 *"的愿望意味着什么。也许这意味着您需要计算每个文档中每个术语的TF,然后为该术语选择“最佳”值,以满足您的需要?
下面是一个构建
Explanation
的简单示例:因此,您可以在循环访问查询的命中数时调用此方法:
explanation.getDetails()
提供的信息基本上与使用Luke分析查询时看到的信息相同:作为文本: