Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Lucene(JAVA)的BM25实现中计算avgLengthPath_Java_Search_Lucene_Indexing - Fatal编程技术网

如何在Lucene(JAVA)的BM25实现中计算avgLengthPath

如何在Lucene(JAVA)的BM25实现中计算avgLengthPath,java,search,lucene,indexing,Java,Search,Lucene,Indexing,有人能给我解释一下如何在Lucene的实现中计算“avgLengthPath”变量吗。我的理解是,我必须在索引期间计算它。但仍不清楚如何做到这一点 该实例提供了: IndexSearcher searcher = new IndexSearcher("IndexPath"); //Load average length BM25Parameters.load(avgLengthPath); BM25BooleanQuery query = new BM25BooleanQuery("This

有人能给我解释一下如何在Lucene的实现中计算“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);
}
IndexSearcher searcher=newindexsearcher(“IndexPath”);
//荷载平均长度
bm25参数。负载(avgLengthPath);
BM25BooleanQuery query=新的BM25BooleanQuery(“这是我的查询”,
“搜索字段”,
新的StandardAnalyzer());
TopDocs top=searcher.search(查询,空,10);
ScoreDoc[]docs=top.scoreDocs;
//打印结果

对于(int i=0;i$,我已经解决了这个问题,我想与大家分享我的答案,以获得任何更正或评论

问题是如何计算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();
}
 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;
}
现在,看看如何创建这样的文件。我们可以看到上面的方法逐行读取文件,并将每两行发送到另一个名为
BM25Parameters.setAverageLength()
的方法。avgLengthPath文件的格式如下:

CONTENT 
459.2903f
ANCHOR
84.55523f
其中第一行是文件名,第二行是该字段的平均长度。 此外,第三行是另一个字段,第四行是该字段的平均长度

此类文件的问题在于,我们无法在默认位置从Lucene获取文档长度。为了克服这一问题,我对集合重新编制了索引,并将文档长度添加为Lucene要编制索引的字段之一

首先,我创建了一个方法,该方法接受一个文件并将文档长度作为字符串返回。我将其称为
getDocLength(file f)

在索引过程中调用此方法以添加文档长度字段,如下所示:

    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();
}
 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文件中。我将此方法称为
generateAvglengthpath()

public static void generateAvGlengthPath文件(字符串luceneIndexPath,字符串outputFilePath){
试一试{
Directory dir=FSDirectory.open(新文件(luceneIndexPath));
IndexReader=IndexReader.open(dir);
int-totalength=0;
//这里我们循环浏览索引中的所有文档
对于(int i=0;i
我刚刚发现这篇文章提供了在Lucene中运行BM25的详细信息。此外,从这篇文章来看,Lucene 4似乎对BM25有一些支持。