![](/img/trans.png)
[英]How to Calculate cosine similarity with tf-idf using Lucene and Java
[英]Do Lucene(java framework) by default calculates the tf-idf and cosine similarity of a document against the term?
我正在開發基於搜索引擎的應用程序,並且正在Lucene Java框架上工作,我對Lucene默認提供的評分功能感到困惑,即默認情況下評分功能是否實現了tf-idf和余弦相似性,還是我們必須做點什么還有嗎?
public class LuceneTester {
String indexDir = "C:\\Users\\hamda\\Documents\\NetBeansProjects\\luceneDemo\\Index";
String dataDir = "C:\\Users\\hamda\\Documents\\NetBeansProjects\\luceneDemo\\Data";
Indexer indexer;
Searcher searcher;
public static void main(String[] args) {
LuceneTester tester;
try {
tester = new LuceneTester();
tester.createIndex();
tester.search("DataGuides");
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private void createIndex() throws IOException{
indexer = new Indexer(indexDir);
int numIndexed;
long startTime = System.currentTimeMillis();
numIndexed = indexer.createIndex(dataDir, new TextFileFilter());
long endTime = System.currentTimeMillis();
indexer.close();
System.out.println(numIndexed+" File indexed, time taken: "
+(endTime-startTime)+" ms");
}
我在下面的搜索功能末尾得到文檔分數
private void search(String searchQuery) throws IOException, ParseException{
searcher = new Searcher(indexDir);
long startTime = System.currentTimeMillis();
TopDocs hits = searcher.search(searchQuery);
long endTime = System.currentTimeMillis();
System.out.println(hits.totalHits +
" documents found. Time :" + (endTime - startTime));
for(ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = searcher.getDocument(scoreDoc);
System.out.println(scoreDoc.score+" File: "
+ doc.get(LuceneConstants.FILE_PATH));
}
searcher.close();
}
}
我已經用Google搜索它,發現了這一點: 如何在Lucene中實現tf-idf和余弦相似度? 任何幫助將不勝感激:)
當我瀏覽http://lucene.apache.org/中的一些細節時,我發現默認情況下,lucene評分模型使用此類DefaultSimilarity http://lucene.apache.org/core/4_0_0/core/org/ apache / lucene / search / similarities / TFIDFSimilarity.html擴展了TFIDFSimilarity類, http: //lucene.apache.org/core/4_0_0/core/org/apache/lucene/search/similarities/TFIDFSimilarity.html因此,在該文檔中據指出,評分模型默認情況下實現tf-idf和余弦相似度。 我可能以任何方式錯了,所以您可以糾正我:)
從Lucene 6.0開始,默認的相似性實現是BM25Similarity ,它實現了BM25 。
如果要使用舊的標准相似性實現,請使用ClassicSimilarity 。
為了比較兩者,您可以簽出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.