2012-08-09 38 views
0

任何人都可以向我解释如何计算Lucene的BM25实现中的'avgLengthPath'变量。我的理解是,我必须在索引期间计算它。但仍不清楚如何去做。如何计算Lucene(JAVA)的BM25实现中的avgLengthPath

提供的示例:

IndexSearcher searcher = new IndexSearcher("IndexPath"); 

//Load average length 
BM25Parameters.load(avgLengthPath); 
BM25BooleanQuery query = new BM25BooleanQuery("This is my Query", 
    "Search-Field", 
    new StandardAnalyzer()); 

TopDocs top = searcher.search(query, null, 10); 
ScoreDoc[] docs = top.scoreDocs; 

//Print results 
for (int i = 0; i $<$ top.scoreDocs.length; i++) { 
     System.out.println(docs[i].doc + ":"+docs[i].score); 
} 

表明,有一个方法或类以从加载的平均长度。

希望得到任何帮助......

感谢

回答

1

我已经解决了这个问题,我想和大家分享我的回答得到任何更正或意见..

的问题是如何计算avgLengthPath参数。当我查看采用此参数的方法时:load()可以看出它需要一个字符串,它是包含平均长度的文件的路径。所以avgLengthPath会是这样的:

/Users/admib/Study/avgLength 

load()方法如下:

public static void load(String path) throws NumberFormatException, 
     IOException { 
    BufferedReader in = new BufferedReader(new FileReader(path)); 
    String line; 
    while (null != (line = in.readLine())) { 
     String field = line; 
     Float avg = new Float(in.readLine()); 
     BM25Parameters.setAverageLength(field, avg); 
    } 
    in.close(); 
} 

现在恐怕看看如何创建这样的文件。我们可以看到上面的方法逐行读取文件并将每两行发送到另一个称为BM25Parameters.setAverageLength()的方法。该avgLengthPath文件的甲应该是这样的:

CONTENT 
459.2903f 
ANCHOR 
84.55523f 

当第一行是提起名字,第二行是这一领域的平均长度。 此外,第三行是另一个字段,第四行是该字段的平均长度。

这个文件的问题是,我们无法从默认位置获取Lucene的文档长度。为了克服这个问题,我重新索引了我的集合,并将文档长度添加为由Lucene索引的字段之一。

首先我创建了一个方法,它接受一个文件并将文档长度作为字符串返回。我把它叫做getDocLength(File f)

public static String getDocLength(File f) throws IOException { 
    FileInputStream stream = new FileInputStream(f); 
    try { 
     FileChannel fc = stream.getChannel(); 
     MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 

     String doc = Charset.defaultCharset().decode(bb).toString(); 
     int length = doc.length(); 
     return Integer.toString(length); 
    } finally { 
     stream.close(); 
    } 
} 

在索引过程中该方法被调用并把文件长度字段中添加如下:

protected Document getDocument(File f) throws Exception { 
    Document doc = new Document(); 
    String docLength = Integer.toString(io.getDocLength(f)); 
    doc.add(new Field("contents", new FileReader(f), Field.TermVector.YES)); 
    doc.add(new Field("docLength", i, Field.Store.YES, Field.Index.NOT_ANALYZED)); 
    doc.add(new Field("filename", f.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED)); 
    doc.add(new Field("fullpath", f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));   
    return doc; 
} 

最后,我在索引创建了一个方法,通过所有文档环路和计算平均文档长度,最后将结果保存到带有正确的合成文件的avgLengthPath文件中。我称这种方法generateAvgLengthPathFile()

public static void generateAvgLengthPathFile(String luceneIndexPath, String outputFilePath) { 
    try { 
     Directory dir = FSDirectory.open(new File(luceneIndexPath)); 
     IndexReader reader = IndexReader.open(dir); 
     int totalLength = 0; 
     //here we loop through all the docs in the index 
     for (int i = 0; i < reader.maxDoc(); i++) { 
      if (reader.isDeleted(i)) { 
       continue; 
      } 
      Document doc = reader.document(i); 
      totalLength += Integer.parseInt(doc.get("docLength")); 
     } 
     //calculate the avarage length 
     float avarageLength = totalLength * 1.0f/reader.maxDoc() * 1.0f; 
     //create the a String varibale with the correct formate 
     String avgLengthPathFile = "contents" + "\n" + avarageLength; 

     //finally, save the file 
     Writer output = null; 
     String text = "contents" + "\n" + avarageLength; 
     File file = new File(outputFilePath); 
     output = new BufferedWriter(new FileWriter(file)); 
     output.write(text); 
     output.close(); 

    } catch (Exception e) { 
System.err.println(e); 
    } 
} 
+0

我刚刚发现这个[链接](http://ipl.cs.aueb.gr/stougiannis/bm25_2.html),提供有关在Lucene的运行BM25好细节。另外,从这个[question](http://stackoverflow.com/questions/9675444/lucene-4-0-statistics),似乎Lucene 4对BM25有一些支持。 – user692704 2012-08-13 02:55:35